25#ifndef STOCHTREE_PARTITION_TRACKER_H_
26#define STOCHTREE_PARTITION_TRACKER_H_
28#include <stochtree/data.h>
29#include <stochtree/ensemble.h>
30#include <stochtree/log.h>
31#include <stochtree/openmp_utils.h>
32#include <stochtree/tree.h>
39class SampleNodeMapper;
40class SamplePredMapper;
41class UnsortedNodeSampleTracker;
42class SortedNodeSampleTracker;
43class FeaturePresortRootContainer;
59 void AssignAllSamplesToRoot();
61 void AssignAllSamplesToConstantPrediction(
double value);
74 void SyncPredictions();
82 std::vector<data_size_t>::iterator UnsortedNodeBeginIterator(
int tree_id,
int node_id);
83 std::vector<data_size_t>::iterator UnsortedNodeEndIterator(
int tree_id,
int node_id);
84 std::vector<data_size_t>::iterator SortedNodeBeginIterator(
int node_id,
int feature_id);
85 std::vector<data_size_t>::iterator SortedNodeEndIterator(
int node_id,
int feature_id);
86 SamplePredMapper* GetSamplePredMapper() {
return sample_pred_mapper_.get(); }
87 SampleNodeMapper* GetSampleNodeMapper() {
return sample_node_mapper_.get(); }
88 UnsortedNodeSampleTracker* GetUnsortedNodeSampleTracker() {
return unsorted_node_sample_tracker_.get(); }
89 SortedNodeSampleTracker* GetSortedNodeSampleTracker() {
return sorted_node_sample_tracker_.get(); }
90 double* GetSumPredictions() {
return sum_predictions_.data(); }
91 int GetNumObservations() {
return num_observations_; }
92 int GetNumTrees() {
return num_trees_; }
93 int GetNumFeatures() {
return num_features_; }
94 bool Initialized() {
return initialized_; }
98 std::vector<double> sum_predictions_;
100 std::unique_ptr<SamplePredMapper> sample_pred_mapper_;
102 std::unique_ptr<SampleNodeMapper> sample_node_mapper_;
106 std::unique_ptr<UnsortedNodeSampleTracker> unsorted_node_sample_tracker_;
110 std::unique_ptr<FeaturePresortRootContainer> presort_container_;
111 std::unique_ptr<SortedNodeSampleTracker> sorted_node_sample_tracker_;
112 std::vector<FeatureType> feature_types_;
114 int num_observations_;
116 bool initialized_{
false};
120 void UpdateSampleTrackersInternal(TreeEnsemble& forest, Eigen::MatrixXd&
covariates, Eigen::MatrixXd&
basis);
121 void UpdateSampleTrackersInternal(TreeEnsemble& forest, Eigen::MatrixXd&
covariates);
122 void UpdateSampleTrackersResidualInternalBasis(TreeEnsemble& forest, ForestDataset&
dataset, ColumnVector& residual,
bool is_mean_model);
123 void UpdateSampleTrackersResidualInternalNoBasis(TreeEnsemble& forest, ForestDataset&
dataset, ColumnVector& residual,
bool is_mean_model);
133 tree_preds_.resize(num_trees_);
134 for (
int j = 0;
j < num_trees_;
j++) {
135 tree_preds_[
j].resize(num_observations_);
151 inline int NumTrees() {
return num_trees_; }
153 inline int NumObservations() {
return num_observations_; }
155 inline void AssignAllSamplesToConstantPrediction(
int tree_id,
double value) {
162 std::vector<std::vector<double>> tree_preds_;
174 tree_observation_indices_.resize(num_trees_);
175 for (
int j = 0;
j < num_trees_;
j++) {
176 tree_observation_indices_[
j].resize(num_observations_);
181 num_trees_ =
other.NumTrees();
182 num_observations_ =
other.NumObservations();
184 tree_observation_indices_.resize(num_trees_);
185 for (
int j = 0;
j < num_trees_;
j++) {
186 tree_observation_indices_[
j].resize(num_observations_);
187 for (
int i = 0;
i < num_observations_;
i++) {
188 tree_observation_indices_[
j][
i] =
other.GetNodeId(
i,
j);
196 for (
int i = 0;
i < num_observations_;
i++) {
220 inline int NumTrees() {
return num_trees_; }
222 inline int NumObservations() {
return num_observations_; }
224 inline void AssignAllSamplesToRoot(
int tree_id) {
226 tree_observation_indices_[
tree_id][
i] = 0;
231 std::vector<std::vector<int>> tree_observation_indices_;
297 std::vector<data_size_t> node_begin_;
298 std::vector<data_size_t> node_length_;
299 std::vector<int32_t> parent_nodes_;
300 std::vector<int32_t> left_nodes_;
301 std::vector<int32_t> right_nodes_;
302 int num_nodes_, num_deleted_nodes_;
303 std::vector<int> deleted_nodes_;
307 void ConvertLeafParentToLeaf(
int node_id);
380 std::vector<data_size_t>::iterator NodeBeginIterator(
int tree_id,
int node_id) {
386 std::vector<data_size_t>::iterator NodeEndIterator(
int tree_id,
int node_id) {
424 std::vector<int>
const&
leaves =
tree->GetLeaves();
426 for (
int i = 0;
i <
leaves.size();
i++) {
440 std::vector<std::unique_ptr<FeatureUnsortedPartition>> feature_partitions_;
448 node_end_ = node_begin_ + node_size_;
453 void SetSorted() { presorted_ =
true; }
455 bool IsSorted() {
return presorted_; }
493 void ArgsortRoot(Eigen::MatrixXd&
covariates) {
497 if (feature_sort_indices_.size() != num_obs) {
498 feature_sort_indices_.resize(num_obs, 0);
500 std::iota(feature_sort_indices_.begin(), feature_sort_indices_.end(), 0);
506 std::stable_sort(feature_sort_indices_.begin(), feature_sort_indices_.end(),
comp_op);
510 std::vector<data_size_t> feature_sort_indices_;
519 feature_presort_.resize(num_features_);
520 for (
int i = 0;
i < num_features_;
i++) {
530 std::vector<std::unique_ptr<FeaturePresortRoot>> feature_presort_;
555 node_offset_sizes_.emplace_back(
node_offset, num_obs_);
598 std::vector<NodeOffsetSize> node_offset_sizes_;
609 feature_partitions_.resize(num_features_);
611 for (
int i = 0;
i < num_features_;
i++) {
619 StochTree::ParallelFor(0, num_features_, num_threads, [&](
int i) {
626 StochTree::ParallelFor(0, num_features_, num_threads, [&](
int i) {
633 StochTree::ParallelFor(0, num_features_, num_threads, [&](
int i) {
679 std::vector<std::unique_ptr<FeaturePresortPartition>> feature_partitions_;
Internal wrapper around Eigen::VectorXd interface for univariate floating point data....
Definition data.h:193
Data structure that tracks pre-sorted feature values through a tree's split lifecycle.
Definition partition_tracker.h:544
void SplitFeature(Eigen::MatrixXd &covariates, int32_t node_id, int32_t feature_index, TreeSplit &split)
Split numeric / ordered categorical feature and update sort indices.
data_size_t NodeEnd(int32_t node_id)
End position of node indexed by node_id.
Definition partition_tracker.h:573
std::vector< data_size_t > NodeIndices(int node_id)
Data indices for a given node.
std::vector< data_size_t > feature_sort_indices_
Feature sort indices.
Definition partition_tracker.h:591
data_size_t NodeSize(int32_t node_id)
Size (in observations) of node indexed by node_id.
Definition partition_tracker.h:576
FeatureType GetFeatureType()
Feature type.
Definition partition_tracker.h:585
data_size_t SortIndex(data_size_t j)
Feature sort index j.
Definition partition_tracker.h:582
data_size_t NodeBegin(int32_t node_id)
Start position of node indexed by node_id.
Definition partition_tracker.h:570
void UpdateObservationMapping(int node_id, int tree_id, SampleNodeMapper *sample_node_mapper)
Update SampleNodeMapper for all the observations in node_id.
void SplitFeatureNumeric(Eigen::MatrixXd &covariates, int32_t node_id, int32_t feature_index, double split_value)
Split numeric / ordered categorical feature and update sort indices.
void SplitFeatureCategorical(Eigen::MatrixXd &covariates, int32_t node_id, int32_t feature_index, std::vector< std::uint32_t > const &category_list)
Split unordered categorical feature and update sort indices.
Container class for FeaturePresortRoot objects stored for every feature in a dataset.
Definition partition_tracker.h:515
Data structure for presorting a feature by its values.
Definition partition_tracker.h:482
Mapping nodes to the indices they contain.
Definition partition_tracker.h:237
void PartitionNode(Eigen::MatrixXd &covariates, int node_id, int left_node_id, int right_node_id, int feature_split, double split_value)
Partition a node based on a new split rule.
void PartitionNode(Eigen::MatrixXd &covariates, int node_id, int left_node_id, int right_node_id, int feature_split, TreeSplit &split)
Partition a node based on a new split rule.
int RightNode(int node_id)
Right child of node_id.
int Parent(int node_id)
Parent node_id.
data_size_t NodeEnd(int node_id)
One past the last index of data points contained in node_id.
std::vector< data_size_t > indices_
Data indices.
Definition partition_tracker.h:287
void PartitionNode(Eigen::MatrixXd &covariates, int node_id, int left_node_id, int right_node_id, int feature_split, std::vector< std::uint32_t > const &category_list)
Partition a node based on a new split rule.
bool RightNodeIsLeaf(int node_id)
Whether node_id's right child is a leaf.
void UpdateObservationMapping(int node_id, int tree_id, SampleNodeMapper *sample_node_mapper)
Update SampleNodeMapper for all the observations in node_id.
data_size_t NodeSize(int node_id)
Number of data points contained in node_id.
std::vector< data_size_t > NodeIndices(int node_id)
Data indices for a given node.
void ReconstituteFromTree(Tree &tree, ForestDataset &dataset)
Reconstitute a tree partition tracker from root based on a tree.
bool IsLeaf(int node_id)
Whether node_id is a leaf.
void PruneNodeToLeaf(int node_id)
Convert a (currently split) node to a leaf.
bool LeftNodeIsLeaf(int node_id)
Whether node_id's left child is a leaf.
int LeftNode(int node_id)
Left child of node_id.
bool IsValidNode(int node_id)
Whether node_id is a valid node.
data_size_t NodeBegin(int node_id)
First index of data points contained in node_id.
API for loading and accessing data used to sample tree ensembles The covariates / bases / weights use...
Definition data.h:272
"Superclass" wrapper around tracking data structures for forest sampling algorithms
Definition partition_tracker.h:46
ForestTracker(Eigen::MatrixXd &covariates, std::vector< FeatureType > &feature_types, int num_trees, int num_observations)
Construct a new ForestTracker object.
Tracking cutpoints available at a given node.
Definition partition_tracker.h:445
Class storing sample-node map for each tree in an ensemble.
Definition partition_tracker.h:168
Class storing sample-prediction map for each tree in an ensemble.
Definition partition_tracker.h:127
Data structure for tracking observations through a tree partition with each feature pre-sorted.
Definition partition_tracker.h:605
void PartitionNode(Eigen::MatrixXd &covariates, int node_id, int feature_split, TreeSplit &split, int num_threads=-1)
Partition a node based on a new split rule.
Definition partition_tracker.h:618
data_size_t NodeSize(int node_id, int feature_index)
One past the last index of data points contained in node_id.
Definition partition_tracker.h:649
void UpdateObservationMapping(int node_id, int tree_id, SampleNodeMapper *sample_node_mapper, int feature_index=0)
Update SampleNodeMapper for all the observations in node_id.
Definition partition_tracker.h:674
void PartitionNode(Eigen::MatrixXd &covariates, int node_id, int feature_split, double split_value, int num_threads=-1)
Partition a node based on a new split rule.
Definition partition_tracker.h:625
std::vector< data_size_t > NodeIndices(int node_id, int feature_index)
Data indices for a given node.
Definition partition_tracker.h:666
data_size_t SortIndex(data_size_t j, int feature_index)
Feature sort index j for feature_index.
Definition partition_tracker.h:671
data_size_t NodeBegin(int node_id, int feature_index)
First index of data points contained in node_id.
Definition partition_tracker.h:639
void PartitionNode(Eigen::MatrixXd &covariates, int node_id, int feature_split, std::vector< std::uint32_t > const &category_list, int num_threads=-1)
Partition a node based on a new split rule.
Definition partition_tracker.h:632
data_size_t NodeEnd(int node_id, int feature_index)
One past the last index of data points contained in node_id.
Definition partition_tracker.h:644
Class storing a "forest," or an ensemble of decision trees.
Definition ensemble.h:31
Representation of arbitrary tree split rules, including numeric split rules (X[,i] <= c) and categori...
Definition tree.h:956
Decision tree data structure.
Definition tree.h:66
Mapping nodes to the indices they contain.
Definition partition_tracker.h:311
void PartitionTreeNode(Eigen::MatrixXd &covariates, int tree_id, int node_id, int left_node_id, int right_node_id, int feature_split, std::vector< std::uint32_t > const &category_list)
Partition a node based on a new split rule.
Definition partition_tracker.h:335
void UpdateObservationMapping(Tree *tree, int tree_id, SampleNodeMapper *sample_node_mapper)
Update SampleNodeMapper for all the observations in tree.
Definition partition_tracker.h:423
int RightNode(int tree_id, int node_id)
Right child of node_id.
Definition partition_tracker.h:408
bool IsLeaf(int tree_id, int node_id)
Whether node_id is a leaf.
Definition partition_tracker.h:351
bool RightNodeIsLeaf(int tree_id, int node_id)
Whether node_id's right child is a leaf.
Definition partition_tracker.h:366
bool IsValidNode(int tree_id, int node_id)
Whether node_id is a valid node.
Definition partition_tracker.h:356
void UpdateObservationMapping(int node_id, int tree_id, SampleNodeMapper *sample_node_mapper)
Update SampleNodeMapper for all the observations in node_id.
Definition partition_tracker.h:418
data_size_t NodeBegin(int tree_id, int node_id)
First index of data points contained in node_id.
Definition partition_tracker.h:371
int NumTrees()
Number of trees.
Definition partition_tracker.h:433
int LeftNode(int tree_id, int node_id)
Left child of node_id.
Definition partition_tracker.h:403
void ResetTreeToRoot(int tree_id, data_size_t n)
Convert a tree to root.
Definition partition_tracker.h:340
void PartitionTreeNode(Eigen::MatrixXd &covariates, int tree_id, int node_id, int left_node_id, int right_node_id, int feature_split, TreeSplit &split)
Partition a node based on a new split rule.
Definition partition_tracker.h:325
data_size_t NodeSize(int tree_id, int node_id)
One past the last index of data points contained in node_id.
Definition partition_tracker.h:393
bool LeftNodeIsLeaf(int tree_id, int node_id)
Whether node_id's left child is a leaf.
Definition partition_tracker.h:361
int Parent(int tree_id, int node_id)
Parent node_id.
Definition partition_tracker.h:398
void ReconstituteFromForest(TreeEnsemble &forest, ForestDataset &dataset)
Reconstruct the node sample tracker based on the splits in a forest.
data_size_t NodeEnd(int tree_id, int node_id)
One past the last index of data points contained in node_id.
Definition partition_tracker.h:376
void PartitionTreeNode(Eigen::MatrixXd &covariates, int tree_id, int node_id, int left_node_id, int right_node_id, int feature_split, double split_value)
Partition a node based on a new split rule.
Definition partition_tracker.h:330
FeatureUnsortedPartition * GetFeaturePartition(int i)
Return a pointer to the feature partition tracking tree i.
Definition partition_tracker.h:436
std::vector< data_size_t > TreeNodeIndices(int tree_id, int node_id)
Data indices for a given node.
Definition partition_tracker.h:413
void PruneTreeNodeToLeaf(int tree_id, int node_id)
Convert a (currently split) node to a leaf.
Definition partition_tracker.h:346
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