StochTree 0.5.0.9000
Loading...
Searching...
No Matches
category_tracker.h
1
25#ifndef STOCHTREE_CATEGORY_TRACKER_H_
26#define STOCHTREE_CATEGORY_TRACKER_H_
27
28#include <Eigen/Dense>
29#include <stochtree/log.h>
30#include <stochtree/meta.h>
31
32#include <map>
33#include <numeric>
34#include <vector>
35
36namespace StochTree {
37
42 public:
43 SampleCategoryMapper(std::vector<int>& group_indices) {
44 num_observations_ = group_indices.size();
45 observation_indices_ = group_indices;
46 }
47
49 num_observations_ = num_observations;
50 observation_indices_.resize(num_observations_);
51 for (int i = 0; i < num_observations_; i++) {
52 observation_indices_[i] = group_indices[i];
53 }
54 }
55
57 num_observations_ = other.NumObservations();
58 observation_indices_.resize(num_observations_);
59 for (int i = 0; i < num_observations_; i++) {
60 observation_indices_[i] = other.GetCategoryId(i);
61 }
62 }
63
64 inline data_size_t GetCategoryId(data_size_t sample_id) {
65 CHECK_LT(sample_id, num_observations_);
66 return observation_indices_[sample_id];
67 }
68
69 inline void SetCategoryId(data_size_t sample_id, int category_id) {
70 CHECK_LT(sample_id, num_observations_);
71 observation_indices_[sample_id] = category_id;
72 }
73
74 inline int NumObservations() { return num_observations_; }
75
76 private:
77 std::vector<int> observation_indices_;
78 data_size_t num_observations_;
79};
80
84class CategorySampleTracker {
85 public:
86 CategorySampleTracker(const std::vector<int>& group_indices) {
87 int n = group_indices.size();
88 indices_ = std::vector<data_size_t>(n);
89 std::iota(indices_.begin(), indices_.end(), 0);
90
91 auto comp_op = [&](size_t const& l, size_t const& r) { return std::less<data_size_t>{}(group_indices[l], group_indices[r]); };
92 std::stable_sort(indices_.begin(), indices_.end(), comp_op);
93
94 category_count_ = 0;
95 int observation_count = 0;
96 for (int i = 0; i < n; i++) {
97 bool start_cond = i == 0;
98 bool end_cond = i == n - 1;
99 bool new_group_cond{false};
100 if (i > 0) new_group_cond = group_indices[indices_[i]] != group_indices[indices_[i - 1]];
101 if (start_cond || new_group_cond) {
102 category_id_map_.insert({group_indices[indices_[i]], category_count_});
103 unique_category_ids_.push_back(group_indices[indices_[i]]);
104 node_index_vector_.emplace_back();
105 if (i == 0) {
106 category_begin_.push_back(i);
107 } else {
108 category_begin_.push_back(i);
109 category_length_.push_back(observation_count);
110 }
112 category_count_++;
113 } else if (end_cond) {
114 category_length_.push_back(observation_count + 1);
115 } else {
117 }
118 // Add the index to the category's node index vector in either case
119 node_index_vector_[category_count_ - 1].emplace_back(indices_[i]);
120 }
121 }
122
123 CategorySampleTracker(int* group_indices, int num_observations) {
124 int n = num_observations;
125 indices_ = std::vector<data_size_t>(n);
126 std::iota(indices_.begin(), indices_.end(), 0);
127
128 auto comp_op = [&](size_t const& l, size_t const& r) { return std::less<data_size_t>{}(group_indices[l], group_indices[r]); };
129 std::stable_sort(indices_.begin(), indices_.end(), comp_op);
130
131 category_count_ = 0;
132 int observation_count = 0;
133 for (int i = 0; i < n; i++) {
134 bool start_cond = i == 0;
135 bool end_cond = i == n - 1;
136 bool new_group_cond{false};
137 if (i > 0) new_group_cond = group_indices[indices_[i]] != group_indices[indices_[i - 1]];
138 if (start_cond || new_group_cond) {
139 category_id_map_.insert({group_indices[indices_[i]], category_count_});
140 unique_category_ids_.push_back(group_indices[indices_[i]]);
141 node_index_vector_.emplace_back();
142 if (i == 0) {
143 category_begin_.push_back(i);
144 } else {
145 category_begin_.push_back(i);
146 category_length_.push_back(observation_count);
147 }
149 category_count_++;
150 } else if (end_cond) {
151 category_length_.push_back(observation_count + 1);
152 } else {
154 }
155 // Add the index to the category's node index vector in either case
156 node_index_vector_[category_count_ - 1].emplace_back(indices_[i]);
157 }
158 }
159
161 inline int CategoryNumber(int category_id) {
162 return category_id_map_[category_id];
163 }
164
166 inline data_size_t CategoryBegin(int category_id) { return category_begin_[category_id_map_[category_id]]; }
167
169 inline data_size_t CategoryEnd(int category_id) {
170 int id = category_id_map_[category_id];
171 return category_begin_[id] + category_length_[id];
172 }
173
175 inline data_size_t CategorySize(int category_id) {
176 return category_length_[category_id_map_[category_id]];
177 }
178
180 inline data_size_t NumCategories() { return category_count_; }
181
183 std::vector<data_size_t> indices_;
184
186 std::vector<data_size_t>& NodeIndices(int category_id) {
187 int id = category_id_map_[category_id];
188 return node_index_vector_[id];
189 }
190
192 std::vector<data_size_t>& NodeIndicesInternalIndex(int internal_category_id) {
193 return node_index_vector_[internal_category_id];
194 }
195
197 std::map<int, int>& GetLabelMap() { return category_id_map_; }
198
199 std::vector<int>& GetUniqueGroupIds() { return unique_category_ids_; }
200
201 private:
202 // Vectors tracking indices in each node
203 std::vector<data_size_t> category_begin_;
204 std::vector<data_size_t> category_length_;
205 std::map<int, int> category_id_map_;
206 std::vector<int> unique_category_ids_;
207 std::vector<std::vector<data_size_t>> node_index_vector_;
208 int category_count_;
209};
210
211} // namespace StochTree
212
213#endif // STOCHTREE_CATEGORY_TRACKER_H_
Class storing sample-node map for each tree in an ensemble TODO: Add run-time checks for categories w...
Definition category_tracker.h:41
static void GFRSampleOneIter(TreeEnsemble &active_forest, ForestTracker &tracker, ForestContainer &forests, LeafModel &leaf_model, ForestDataset &dataset, ColumnVector &residual, TreePrior &tree_prior, std::mt19937 &gen, std::vector< double > &variable_weights, std::vector< int > &sweep_update_indices, double global_variance, std::vector< FeatureType > &feature_types, int cutpoint_grid_size, bool keep_forest, bool pre_initialized, bool backfitting, int num_features_subsample, int num_threads, LeafSuffStatConstructorArgs &... leaf_suff_stat_args)
Definition tree_sampler.h:816
A collection of random number generation utilities.
Definition bart.h:15