6#ifndef STOCHTREE_CONTAINER_H_
7#define STOCHTREE_CONTAINER_H_
9#include <stochtree/data.h>
10#include <stochtree/ensemble.h>
11#include <stochtree/log.h>
12#include <nlohmann/json.hpp>
13#include <stochtree/tree.h>
141 void PredictLeafIndicesInplace(Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>>&
covariates,
142 Eigen::Map<Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>>&
output,
145 inline TreeEnsemble* GetEnsemble(
int i) {
return forests_[
i].get(); }
146 inline int32_t NumSamples() {
return num_samples_; }
147 inline int32_t NumTrees() {
return num_trees_; }
152 inline double AverageMaxDepth() {
155 for (
int i = 0;
i < num_samples_;
i++) {
156 for (
int j = 0;
j < num_trees_;
j++) {
157 numerator +=
static_cast<double>(forests_[
i]->TreeMaxDepth(
j));
163 inline int32_t OutputDimension() {
return output_dimension_; }
165 inline bool IsLeafConstant() {
return is_leaf_constant_; }
167 inline bool IsExponentiated() {
return is_exponentiated_; }
172 inline void IncrementSampleCount() { num_samples_++; }
174 void SaveToJsonFile(std::string
filename) {
180 void LoadFromJsonFile(std::string
filename) {
187 std::string DumpJsonString() {
189 return model_json.dump();
192 void LoadFromJsonString(std::string&
json_string) {
202 output_dimension_ = 0;
203 is_leaf_constant_ = 0;
204 initialized_ =
false;
215 std::vector<std::unique_ptr<TreeEnsemble>> forests_;
218 int output_dimension_;
219 bool is_exponentiated_{
false};
220 bool is_leaf_constant_;
221 bool initialized_{
false};
230 const std::unique_ptr<ForestContainer>&
src,
232 if (
src ==
nullptr &&
dst ==
nullptr)
return;
233 if (
src ==
nullptr ||
dst ==
nullptr) {
234 Log::Fatal(
"Cannot merge samples: %s forest present in one chain but not the other",
name);
236 for (
int i = 0;
i <
src->NumSamples();
i++) {
237 dst->AddSample(*
src->GetEnsemble(
i));
Container of TreeEnsemble forest objects. This is the primary (in-memory) storage interface for multi...
Definition container.h:24
ForestContainer(int num_samples, int num_trees, int output_dimension=1, bool is_leaf_constant=true, bool is_exponentiated=false)
Construct a new ForestContainer object.
std::vector< double > Predict(ForestDataset &dataset)
Predict from every forest in the container on every observation in the provided dataset....
void MultiplyForest(int forest_index, double constant_multiple)
Multiply every leaf of every tree of a specified forest by a constant value.
Definition container.h:70
void InitializeRoot(std::vector< double > &leaf_vector)
Initialize a "root" forest of multivariate trees as the first element of the container,...
void DeleteSample(int sample_num)
Remove a forest from a container of forest samples and delete the corresponding object,...
void MergeForests(int inbound_forest_index, int outbound_forest_index)
Combine two forests into a single forest by merging their trees.
Definition container.h:52
std::vector< double > PredictRaw(ForestDataset &dataset, bool row_major=true)
Predict from every forest in the container on every observation in the provided dataset....
void append_from_json(const nlohmann::json &forest_container_json)
Append to a forest container from JSON, requires that the ensemble already contains a nonzero number ...
void CopyFromPreviousSample(int new_sample_id, int previous_sample_id)
Copy the forest stored at previous_sample_id to the forest stored at new_sample_id.
void from_json(const nlohmann::json &forest_container_json)
Load from JSON.
void InitializeRoot(double leaf_value)
Initialize a "root" forest of univariate trees as the first element of the container,...
nlohmann::json to_json()
Save to JSON.
void AddSamples(int num_samples)
Pre-allocate space for num_samples additional forests in the container.
ForestContainer(int num_trees, int output_dimension=1, bool is_leaf_constant=true, bool is_exponentiated=false)
Construct a new ForestContainer object.
void AddToForest(int forest_index, double constant_value)
Add a constant value to every leaf of every tree of a specified forest.
Definition container.h:61
void AddSample(TreeEnsemble &forest)
Add a new forest to the container by copying forest.
API for loading and accessing data used to sample tree ensembles The covariates / bases / weights use...
Definition data.h:272
Class storing a "forest," or an ensemble of decision trees.
Definition ensemble.h:31
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
void AppendForestContainerSamples(std::unique_ptr< ForestContainer > &dst, const std::unique_ptr< ForestContainer > &src, const char *name)
Append every retained forest sample from src onto the end of dst (deep copy). Used by BARTSamples::Me...
Definition container.h:229