ppforest2 v0.1.3
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
GroupPartition.hpp
Go to the documentation of this file.
1#pragma once
2
3
4#include "utils/Types.hpp"
5#include "utils/Invariant.hpp"
6
7#include <map>
8#include <optional>
9#include <set>
10#include <vector>
11#include <Eigen/Dense>
12
13namespace ppforest2::stats {
41 using Group = types::GroupId;
42 using GroupSet = std::set<types::GroupId>;
43 using GroupMap = std::map<types::GroupId, types::GroupId>;
44 using GroupInvMap = std::map<types::GroupId, GroupSet>;
45 using GroupVector = types::GroupIdVector;
46
47 public:
54
63
69 GroupPartition(int start, int end);
70
82 GroupPartition bisect(int mid) const;
83
85 int group_start(Group const& group) const;
87 int group_end(Group const& group) const;
89 int group_size(Group const& group) const;
90
98 Group first_group() const {
99 invariant(!groups.empty(), "GroupPartition::first_group: partition is empty");
100 return *groups.begin();
101 }
102
111 int total_size() const;
112
124 template<typename Derived> auto group(Eigen::MatrixBase<Derived> const& x, Group const& group) const {
125 std::vector<int> indices;
126
127 auto const& subs = this->subgroups.at(group);
128
129 for (auto const& g : subs) {
130 for (int i = group_start(g); i <= group_end(g); ++i) {
131 invariant(i >= 0 && i < x.rows(), "GroupPartition::group: index out of bounds");
132 indices.push_back(i);
133 }
134 }
135
136 return x(indices, Eigen::all);
137 }
138
148 Eigen::VectorXi group_indices(Group const& group) const {
149 std::vector<int> indices;
150
151 auto const& subs = this->subgroups.at(group);
152
153 for (auto const& g : subs) {
154 for (int i = group_start(g); i <= group_end(g); ++i) {
155 indices.push_back(i);
156 }
157 }
158
159 return Eigen::VectorXi::Map(indices.data(), static_cast<Eigen::Index>(indices.size()));
160 }
161
168 template<typename Derived> auto data(Eigen::MatrixBase<Derived> const& x) const {
169 std::vector<int> indices;
170
171 for (auto const& kv : blocks) {
172 auto const& g = kv.first;
173 for (int i = group_start(g); i <= group_end(g); ++i) {
174 indices.push_back(i);
175 }
176 }
177
178 return x(indices, Eigen::all);
179 }
180
187
194 GroupPartition subset(GroupSet const& groups) const;
195
196 using SplitSizes = std::map<types::GroupId, int>;
197
212 std::pair<GroupPartition, GroupPartition> split(SplitSizes const& left_sizes) const;
213
220 GroupPartition remap(GroupMap const& mapping) const;
221
228
230 GroupSet const groups;
232 GroupMap const supergroups;
234 GroupInvMap const subgroups;
235
236 private:
237 struct Block {
238 int start = 0;
239 int end = 0;
240 int size = 0;
241 std::optional<types::GroupId> next = std::nullopt;
242 std::optional<types::GroupId> prev = std::nullopt;
243 };
244
245 using BlockMap = std::map<types::GroupId, Block>;
246 BlockMap const blocks;
247
248 static BlockMap init_blocks(GroupVector const& y);
249 static GroupMap init_supergroups(GroupSet const& groups);
250
251 explicit GroupPartition(BlockMap const& blocks);
252
253 GroupPartition(BlockMap const& blocks, GroupSet const& groups);
254
255 GroupPartition(BlockMap const& blocks, GroupMap const& supergroups);
256
257 GroupPartition(BlockMap const& blocks, GroupSet const& groups, GroupMap const& supergroups);
258 };
259}
void invariant(bool condition, char const *message)
Runtime assertion that throws on failure.
GroupPartition remap(GroupMap const &mapping) const
Merge groups according to a mapping.
GroupPartition(types::GroupIdVector const &y)
Construct from a sorted response vector.
GroupMap const supergroups
Maps each group to its supergroup (identity if no merge).
Definition GroupPartition.hpp:232
GroupPartition bisect(int mid) const
Bisect a single-group partition at row index mid into two groups.
Group first_group() const
Smallest group label in the partition.
Definition GroupPartition.hpp:98
types::FeatureMatrix wgss(types::FeatureMatrix const &x) const
Within-group sum of squares matrix (p × p).
types::FeatureVector mean(types::FeatureMatrix const &x) const
Overall mean of all grouped rows (p).
GroupSet const groups
Set of all group labels in this partition.
Definition GroupPartition.hpp:230
types::FeatureMatrix bgss(types::FeatureMatrix const &x) const
Between-group sum of squares matrix (p × p).
auto data(Eigen::MatrixBase< Derived > const &x) const
Extract all rows across all groups.
Definition GroupPartition.hpp:168
GroupPartition(int start, int end)
Construct a single-group partition covering rows [start, end].
int group_end(Group const &group) const
Last row index (inclusive) of the block for group.
int total_size() const
Total number of observations across all groups in the partition.
GroupPartition subset(GroupSet const &groups) const
Create a partition containing only the given groups.
GroupPartition(types::OutcomeVector const &y)
Construct from a float-typed response vector.
std::map< types::GroupId, int > SplitSizes
Definition GroupPartition.hpp:196
int group_start(Group const &group) const
First row index of the block for group.
Eigen::VectorXi group_indices(Group const &group) const
Row indices of a group (or supergroup) as an owned Eigen vector.
Definition GroupPartition.hpp:148
int group_size(Group const &group) const
Number of observations in group.
GroupPartition collapse() const
Collapse all groups into a single supergroup.
std::pair< GroupPartition, GroupPartition > split(SplitSizes const &left_sizes) const
Split each group's block into left and right children.
auto group(Eigen::MatrixBase< Derived > const &x, Group const &group) const
Extract rows belonging to a group (or supergroup).
Definition GroupPartition.hpp:124
GroupInvMap const subgroups
Maps each group to its set of subgroups.
Definition GroupPartition.hpp:234
Statistical infrastructure for training and evaluation.
Definition ConfusionMatrix.hpp:11
Eigen::Matrix< Feature, Eigen::Dynamic, Eigen::Dynamic > FeatureMatrix
Dynamic-size matrix of feature values.
Definition Types.hpp:43
Eigen::Matrix< Outcome, Eigen::Dynamic, 1 > OutcomeVector
Dynamic-size column vector of predictions.
Definition Types.hpp:52
Eigen::Matrix< GroupId, Eigen::Dynamic, 1 > GroupIdVector
Dynamic-size column vector of internal group labels.
Definition Types.hpp:49
Eigen::Matrix< Feature, Eigen::Dynamic, 1 > FeatureVector
Dynamic-size column vector of feature values.
Definition Types.hpp:46
int GroupId
Scalar type for internal group labels (integer). Used as map keys, set elements, and partition indice...
Definition Types.hpp:37