42 using GroupSet = std::set<types::GroupId>;
43 using GroupMap = std::map<types::GroupId, types::GroupId>;
44 using GroupInvMap = std::map<types::GroupId, GroupSet>;
99 invariant(!
groups.empty(),
"GroupPartition::first_group: partition is empty");
124 template<
typename Derived>
auto group(Eigen::MatrixBase<Derived>
const& x, Group
const&
group)
const {
125 std::vector<int> indices;
129 for (
auto const& g : subs) {
131 invariant(i >= 0 && i < x.rows(),
"GroupPartition::group: index out of bounds");
132 indices.push_back(i);
136 return x(indices, Eigen::all);
149 std::vector<int> indices;
153 for (
auto const& g : subs) {
155 indices.push_back(i);
159 return Eigen::VectorXi::Map(indices.data(),
static_cast<Eigen::Index
>(indices.size()));
168 template<
typename Derived>
auto data(Eigen::MatrixBase<Derived>
const& x)
const {
169 std::vector<int> indices;
171 for (
auto const& kv : blocks) {
172 auto const& g = kv.first;
174 indices.push_back(i);
178 return x(indices, Eigen::all);
241 std::optional<types::GroupId> next = std::nullopt;
242 std::optional<types::GroupId> prev = std::nullopt;
245 using BlockMap = std::map<types::GroupId, Block>;
246 BlockMap
const blocks;
248 static BlockMap init_blocks(GroupVector
const& y);
249 static GroupMap init_supergroups(GroupSet
const&
groups);
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