StochTree 0.5.0.9000
Loading...
Searching...
No Matches
leaf_model.h
1
5#ifndef STOCHTREE_LEAF_MODEL_H_
6#define STOCHTREE_LEAF_MODEL_H_
7
8#include <Eigen/Dense>
9#include <stochtree/cutpoint_candidates.h>
10#include <stochtree/data.h>
11#include <stochtree/gamma_sampler.h>
12#include <stochtree/ig_sampler.h>
13#include <stochtree/log.h>
14#include <stochtree/meta.h>
15#include <stochtree/normal_sampler.h>
16#include <stochtree/openmp_utils.h>
17#include <stochtree/partition_tracker.h>
18#include <stochtree/prior.h>
19#include <stochtree/tree.h>
20
21#include <random>
22#include <variant>
23
24namespace StochTree {
25
353 kConstantLeafGaussian,
354 kUnivariateRegressionLeafGaussian,
355 kMultivariateRegressionLeafGaussian,
356 kLogLinearVariance,
357 kCloglogOrdinal
358};
359
362 public:
363 data_size_t n;
364 double sum_w;
365 double sum_yw;
370 n = 0;
371 sum_w = 0.0;
372 sum_yw = 0.0;
373 }
384 n += 1;
385 if (dataset.HasVarWeights()) {
386 sum_w += 1 / dataset.VarWeightValue(row_idx);
387 sum_yw += outcome(row_idx, 0) / dataset.VarWeightValue(row_idx);
388 } else {
389 sum_w += 1.0;
390 sum_yw += outcome(row_idx, 0);
391 }
392 }
397 n = 0;
398 sum_w = 0.0;
399 sum_yw = 0.0;
400 }
407 n += suff_stat.n;
408 sum_w += suff_stat.sum_w;
409 sum_yw += suff_stat.sum_yw;
410 }
418 n = lhs.n + rhs.n;
419 sum_w = lhs.sum_w + rhs.sum_w;
420 sum_yw = lhs.sum_yw + rhs.sum_yw;
421 }
429 n = lhs.n - rhs.n;
430 sum_w = lhs.sum_w - rhs.sum_w;
431 sum_yw = lhs.sum_yw - rhs.sum_yw;
432 }
439 return n > threshold;
440 }
453 return n;
454 }
455};
456
527
530 public:
531 data_size_t n;
532 double sum_xxw;
533 double sum_yxw;
538 n = 0;
539 sum_xxw = 0.0;
540 sum_yxw = 0.0;
541 }
552 n += 1;
553 if (dataset.HasVarWeights()) {
554 sum_xxw += dataset.BasisValue(row_idx, 0) * dataset.BasisValue(row_idx, 0) / dataset.VarWeightValue(row_idx);
555 sum_yxw += outcome(row_idx, 0) * dataset.BasisValue(row_idx, 0) / dataset.VarWeightValue(row_idx);
556 } else {
557 sum_xxw += dataset.BasisValue(row_idx, 0) * dataset.BasisValue(row_idx, 0);
558 sum_yxw += outcome(row_idx, 0) * dataset.BasisValue(row_idx, 0);
559 }
560 }
565 n = 0;
566 sum_xxw = 0.0;
567 sum_yxw = 0.0;
568 }
575 n += suff_stat.n;
576 sum_xxw += suff_stat.sum_xxw;
577 sum_yxw += suff_stat.sum_yxw;
578 }
586 n = lhs.n + rhs.n;
587 sum_xxw = lhs.sum_xxw + rhs.sum_xxw;
588 sum_yxw = lhs.sum_yxw + rhs.sum_yxw;
589 }
597 n = lhs.n - rhs.n;
598 sum_xxw = lhs.sum_xxw - rhs.sum_xxw;
599 sum_yxw = lhs.sum_yxw - rhs.sum_yxw;
600 }
607 return n > threshold;
608 }
621 return n;
622 }
623};
624
682
685 public:
686 data_size_t n;
687 int p;
688 Eigen::MatrixXd XtWX;
689 Eigen::MatrixXd ytWX;
696 n = 0;
697 XtWX = Eigen::MatrixXd::Zero(basis_dim, basis_dim);
698 ytWX = Eigen::MatrixXd::Zero(1, basis_dim);
699 p = basis_dim;
700 }
711 n += 1;
712 if (dataset.HasVarWeights()) {
713 for (int i = 0; i < p; i++) {
714 ytWX(0, i) += outcome(row_idx, 0) * dataset.BasisValue(row_idx, i) / dataset.VarWeightValue(row_idx);
715 for (int j = 0; j < p; j++) {
716 XtWX(i, j) += dataset.BasisValue(row_idx, i) * dataset.BasisValue(row_idx, j) / dataset.VarWeightValue(row_idx);
717 }
718 }
719 } else {
720 for (int i = 0; i < p; i++) {
721 ytWX(0, i) += outcome(row_idx, 0) * dataset.BasisValue(row_idx, i);
722 for (int j = 0; j < p; j++) {
723 XtWX(i, j) += dataset.BasisValue(row_idx, i) * dataset.BasisValue(row_idx, j);
724 }
725 }
726 }
727 }
732 n = 0;
733 for (int i = 0; i < p; i++) {
734 ytWX(0, i) = 0.0;
735 for (int j = 0; j < p; j++) {
736 XtWX(i, j) = 0.0;
737 }
738 }
739 }
746 n += suff_stat.n;
747 XtWX += suff_stat.XtWX;
748 ytWX += suff_stat.ytWX;
749 }
757 n = lhs.n + rhs.n;
758 XtWX = lhs.XtWX + rhs.XtWX;
759 ytWX = lhs.ytWX + rhs.ytWX;
760 }
768 n = lhs.n - rhs.n;
769 XtWX = lhs.XtWX - rhs.XtWX;
770 ytWX = lhs.ytWX - rhs.ytWX;
771 }
778 return n > threshold;
779 }
792 return n;
793 }
794};
795
798 public:
805 Sigma_0_ = Sigma_0;
806 multivariate_normal_sampler_ = MultivariateNormalSampler();
807 }
850 void SetEnsembleRootPredictedValue(ForestDataset& dataset, TreeEnsemble* ensemble, double root_pred_value);
851 void SetScale(Eigen::MatrixXd& Sigma_0) { Sigma_0_ = Sigma_0; }
852 inline bool RequiresBasis() { return true; }
853
854 private:
855 Eigen::MatrixXd Sigma_0_;
856 MultivariateNormalSampler multivariate_normal_sampler_;
857};
858
861 public:
862 data_size_t n;
863 double weighted_sum_ei;
865 n = 0;
866 weighted_sum_ei = 0.0;
867 }
878 n += 1;
879 weighted_sum_ei += std::exp(std::log(outcome(row_idx) * outcome(row_idx)) - tracker.GetSamplePrediction(row_idx) + tracker.GetTreeSamplePrediction(row_idx, tree_idx));
880 }
885 n = 0;
886 weighted_sum_ei = 0.0;
887 }
894 n += suff_stat.n;
895 weighted_sum_ei += suff_stat.weighted_sum_ei;
896 }
904 n = lhs.n + rhs.n;
905 weighted_sum_ei = lhs.weighted_sum_ei + rhs.weighted_sum_ei;
906 }
914 n = lhs.n - rhs.n;
915 weighted_sum_ei = lhs.weighted_sum_ei - rhs.weighted_sum_ei;
916 }
923 return n > threshold;
924 }
937 return n;
938 }
939};
940
943 public:
944 LogLinearVarianceLeafModel(double a, double b) {
945 a_ = a;
946 b_ = b;
947 gamma_sampler_ = GammaSampler();
948 }
965 double SuffStatLogMarginalLikelihood(LogLinearVarianceSuffStat& suff_stat, double global_variance);
992 void SetEnsembleRootPredictedValue(ForestDataset& dataset, TreeEnsemble* ensemble, double root_pred_value);
993 void SetPriorShape(double a) { a_ = a; }
994 void SetPriorRate(double b) { b_ = b; }
995 inline bool RequiresBasis() { return false; }
996
997 private:
998 double a_;
999 double b_;
1000 GammaSampler gamma_sampler_;
1001};
1002
1005 public:
1006 data_size_t n;
1007 double sum_Y_less_K;
1008 double other_sum;
1009
1014 n = 0;
1015 sum_Y_less_K = 0.0;
1016 other_sum = 0.0;
1017 }
1018
1029 n += 1;
1030
1031 // Get ordinal outcome value for this observation
1032 unsigned int y = static_cast<unsigned int>(outcome(row_idx));
1033
1034 // Get auxiliary data from tracker (assuming types: 0=latents Z, 1=forest predictions, 2=cutpoints gamma, 3=cumsum exp of gamma)
1035 double Z = dataset.GetAuxiliaryDataValue(0, row_idx); // latent variables Z
1036 double lambda_minus = dataset.GetAuxiliaryDataValue(1, row_idx); // forest predictions excluding current tree
1037
1038 // Get cutpoints gamma and cumulative sum of exp(gamma)
1039 const std::vector<double>& gamma = dataset.GetAuxiliaryDataVectorConst(2); // cutpoints gamma
1040 const std::vector<double>& seg = dataset.GetAuxiliaryDataVectorConst(3); // cumsum exp of gamma
1041
1042 int K = gamma.size() + 1; // Number of ordinal categories
1043
1044 if (y == K - 1) {
1045 other_sum += std::exp(lambda_minus) * seg[y]; // checked and it's correct
1046 } else {
1047 sum_Y_less_K += 1.0;
1048 other_sum += std::exp(lambda_minus) * (Z * std::exp(gamma[y]) + seg[y]); // checked and it's correct
1049 }
1050 }
1051
1056 n = 0;
1057 sum_Y_less_K = 0.0;
1058 other_sum = 0.0;
1059 }
1060
1067 n += suff_stat.n;
1068 sum_Y_less_K += suff_stat.sum_Y_less_K;
1069 other_sum += suff_stat.other_sum;
1070 }
1071
1079 n = lhs.n + rhs.n;
1080 sum_Y_less_K = lhs.sum_Y_less_K + rhs.sum_Y_less_K;
1081 other_sum = lhs.other_sum + rhs.other_sum;
1082 }
1083
1091 n = lhs.n - rhs.n;
1092 sum_Y_less_K = lhs.sum_Y_less_K - rhs.sum_Y_less_K;
1093 other_sum = lhs.other_sum - rhs.other_sum;
1094 }
1095
1102 return n > threshold;
1103 }
1104
1111 return n >= threshold;
1112 }
1113
1118 return n;
1119 }
1120};
1121
1124 public:
1132 CloglogOrdinalLeafModel(double a, double b) {
1133 a_ = a;
1134 b_ = b;
1135 gamma_sampler_ = GammaSampler();
1136 }
1138
1143
1148
1153
1158
1163
1169 inline bool RequiresBasis() { return false; }
1170
1171 private:
1172 double a_;
1173 double b_;
1174 GammaSampler gamma_sampler_;
1175};
1176
1189
1203
1204template <typename SuffStatType, typename... SuffStatConstructorArgs>
1205static inline SuffStatVariant createSuffStat(SuffStatConstructorArgs... leaf_suff_stat_args) {
1207}
1208
1209template <typename LeafModelType, typename... LeafModelConstructorArgs>
1210static inline LeafModelVariant createLeafModel(LeafModelConstructorArgs... leaf_model_args) {
1211 return LeafModelType(leaf_model_args...);
1212}
1213
1220static inline SuffStatVariant suffStatFactory(ModelType model_type, int basis_dim = 0) {
1221 if (model_type == kConstantLeafGaussian) {
1223 } else if (model_type == kUnivariateRegressionLeafGaussian) {
1225 } else if (model_type == kMultivariateRegressionLeafGaussian) {
1227 } else if (model_type == kLogLinearVariance) {
1229 } else {
1231 }
1232}
1233
1243static inline LeafModelVariant leafModelFactory(ModelType model_type, double tau, Eigen::MatrixXd& Sigma0, double a, double b) {
1244 if (model_type == kConstantLeafGaussian) {
1246 } else if (model_type == kUnivariateRegressionLeafGaussian) {
1248 } else if (model_type == kMultivariateRegressionLeafGaussian) {
1250 } else if (model_type == kLogLinearVariance) {
1252 } else {
1254 }
1255}
1256
1257template <typename SuffStatType, typename... SuffStatConstructorArgs>
1258static inline void AccumulateSuffStatProposed(
1260 ColumnVector& residual, double global_variance, TreeSplit& split, int tree_num, int leaf_num, int split_feature, int num_threads,
1262 // Determine the position of the node's indices in the forest tracking data structure
1263 int node_begin_index = tracker.UnsortedNodeBegin(tree_num, leaf_num);
1264 int node_end_index = tracker.UnsortedNodeEnd(tree_num, leaf_num);
1265
1266 // Extract pointer to the feature partition for tree_num
1267 UnsortedNodeSampleTracker* unsorted_node_sample_tracker = tracker.GetUnsortedNodeSampleTracker();
1268 FeatureUnsortedPartition* feature_partition = unsorted_node_sample_tracker->GetFeaturePartition(tree_num);
1269
1270 // Determine the number of threads to use
1271 int chunk_size = (node_end_index - node_begin_index) / num_threads;
1272 if (chunk_size < 100) {
1273 num_threads = 1;
1275 }
1276
1277 if (num_threads > 1) {
1278 // Split the work into num_threads chunks
1279 std::vector<std::pair<int, int>> thread_ranges(num_threads);
1280 std::vector<SuffStatType> thread_suff_stats_node;
1281 std::vector<SuffStatType> thread_suff_stats_left;
1282 std::vector<SuffStatType> thread_suff_stats_right;
1283 for (int i = 0; i < num_threads; i++) {
1284 thread_ranges[i] = std::make_pair(node_begin_index + i * chunk_size,
1285 node_begin_index + (i + 1) * chunk_size);
1286 thread_suff_stats_node.emplace_back(suff_stat_args...);
1287 thread_suff_stats_left.emplace_back(suff_stat_args...);
1288 thread_suff_stats_right.emplace_back(suff_stat_args...);
1289 }
1290
1291 // Accumulate sufficient statistics
1292 StochTree::ParallelFor(0, num_threads, num_threads, [&](int i) {
1293 int start_idx = thread_ranges[i].first;
1294 int end_idx = thread_ranges[i].second;
1295 for (int idx = start_idx; idx < end_idx; idx++) {
1296 int obs_num = feature_partition->indices_[idx];
1297 double feature_value = dataset.CovariateValue(obs_num, split_feature);
1298 thread_suff_stats_node[i].IncrementSuffStat(dataset, residual.GetData(), tracker, obs_num, tree_num);
1299 if (split.SplitTrue(feature_value)) {
1300 thread_suff_stats_left[i].IncrementSuffStat(dataset, residual.GetData(), tracker, obs_num, tree_num);
1301 } else {
1302 thread_suff_stats_right[i].IncrementSuffStat(dataset, residual.GetData(), tracker, obs_num, tree_num);
1303 }
1304 }
1305 });
1306
1307 // Combine the thread-local sufficient statistics
1308 for (int i = 0; i < num_threads; i++) {
1309 node_suff_stat.AddSuffStatInplace(thread_suff_stats_node[i]);
1310 left_suff_stat.AddSuffStatInplace(thread_suff_stats_left[i]);
1311 right_suff_stat.AddSuffStatInplace(thread_suff_stats_right[i]);
1312 }
1313 } else {
1314 for (int idx = node_begin_index; idx < node_end_index; idx++) {
1315 int obs_num = feature_partition->indices_[idx];
1316 double feature_value = dataset.CovariateValue(obs_num, split_feature);
1317 node_suff_stat.IncrementSuffStat(dataset, residual.GetData(), tracker, obs_num, tree_num);
1318 if (split.SplitTrue(feature_value)) {
1319 left_suff_stat.IncrementSuffStat(dataset, residual.GetData(), tracker, obs_num, tree_num);
1320 } else {
1321 right_suff_stat.IncrementSuffStat(dataset, residual.GetData(), tracker, obs_num, tree_num);
1322 }
1323 }
1324 }
1325}
1326
1327template <typename SuffStatType>
1328static inline void AccumulateSuffStatExisting(SuffStatType& node_suff_stat, SuffStatType& left_suff_stat, SuffStatType& right_suff_stat, ForestDataset& dataset, ForestTracker& tracker,
1329 ColumnVector& residual, double global_variance, int tree_num, int split_node_id, int left_node_id, int right_node_id) {
1330 // Acquire iterators
1331 auto left_node_begin_iter = tracker.UnsortedNodeBeginIterator(tree_num, left_node_id);
1332 auto left_node_end_iter = tracker.UnsortedNodeEndIterator(tree_num, left_node_id);
1333 auto right_node_begin_iter = tracker.UnsortedNodeBeginIterator(tree_num, right_node_id);
1334 auto right_node_end_iter = tracker.UnsortedNodeEndIterator(tree_num, right_node_id);
1335
1336 // Accumulate sufficient statistics for the left and split nodes
1337 for (auto i = left_node_begin_iter; i != left_node_end_iter; i++) {
1338 auto idx = *i;
1339 left_suff_stat.IncrementSuffStat(dataset, residual.GetData(), tracker, idx, tree_num);
1340 node_suff_stat.IncrementSuffStat(dataset, residual.GetData(), tracker, idx, tree_num);
1341 }
1342
1343 // Accumulate sufficient statistics for the right and split nodes
1344 for (auto i = right_node_begin_iter; i != right_node_end_iter; i++) {
1345 auto idx = *i;
1346 right_suff_stat.IncrementSuffStat(dataset, residual.GetData(), tracker, idx, tree_num);
1347 node_suff_stat.IncrementSuffStat(dataset, residual.GetData(), tracker, idx, tree_num);
1348 }
1349}
1350
1351template <typename SuffStatType, bool sorted>
1352static inline void AccumulateSingleNodeSuffStat(SuffStatType& node_suff_stat, ForestDataset& dataset, ForestTracker& tracker, ColumnVector& residual, int tree_num, int node_id) {
1353 // Acquire iterators
1354 std::vector<data_size_t>::iterator node_begin_iter;
1355 std::vector<data_size_t>::iterator node_end_iter;
1356 if (sorted) {
1357 // Default to the first feature if we're using the presort tracker
1358 node_begin_iter = tracker.SortedNodeBeginIterator(node_id, 0);
1359 node_end_iter = tracker.SortedNodeEndIterator(node_id, 0);
1360 } else {
1361 node_begin_iter = tracker.UnsortedNodeBeginIterator(tree_num, node_id);
1362 node_end_iter = tracker.UnsortedNodeEndIterator(tree_num, node_id);
1363 }
1364
1365 // Accumulate sufficient statistics
1366 for (auto i = node_begin_iter; i != node_end_iter; i++) {
1367 auto idx = *i;
1368 node_suff_stat.IncrementSuffStat(dataset, residual.GetData(), tracker, idx, tree_num);
1369 }
1370}
1371
1372template <typename SuffStatType>
1373static inline void AccumulateCutpointBinSuffStat(SuffStatType& left_suff_stat, ForestTracker& tracker, CutpointGridContainer& cutpoint_grid_container,
1374 ForestDataset& dataset, ColumnVector& residual, double global_variance, int tree_num, int node_id,
1375 int feature_num, int cutpoint_num) {
1376 // Acquire iterators
1377 auto node_begin_iter = tracker.SortedNodeBeginIterator(node_id, feature_num);
1378 auto node_end_iter = tracker.SortedNodeEndIterator(node_id, feature_num);
1379
1380 // Determine node start point
1381 data_size_t node_begin = tracker.SortedNodeBegin(node_id, feature_num);
1382
1383 // Determine cutpoint bin start and end points
1387
1388 // Cutpoint specific iterators
1389 // TODO: fix the hack of having to subtract off node_begin, probably by cleaning up the CutpointGridContainer interface
1392
1393 // Accumulate sufficient statistics
1394 for (auto i = cutpoint_begin_iter; i != cutpoint_end_iter; i++) {
1395 auto idx = *i;
1396 left_suff_stat.IncrementSuffStat(dataset, residual.GetData(), tracker, idx, tree_num);
1397 }
1398}
1399
// end of leaf_model_group
1401
1402} // namespace StochTree
1403
1404#endif // STOCHTREE_LEAF_MODEL_H_
Marginal likelihood and posterior computation for complementary log-log ordinal BART model.
Definition leaf_model.h:1123
CloglogOrdinalLeafModel(double a, double b)
Construct a new CloglogOrdinalLeafModel object.
Definition leaf_model.h:1132
double SplitLogMarginalLikelihood(CloglogOrdinalSuffStat &left_stat, CloglogOrdinalSuffStat &right_stat, double global_variance)
Log marginal likelihood for a proposed split, evaluated only for observations that fall into the node...
double SuffStatLogMarginalLikelihood(CloglogOrdinalSuffStat &suff_stat, double global_variance)
Helper function to compute log marginal likelihood from sufficient statistics.
double PosteriorParameterShape(CloglogOrdinalSuffStat &suff_stat, double global_variance)
Posterior shape parameter for leaf node log-gamma distribution.
double PosteriorParameterRate(CloglogOrdinalSuffStat &suff_stat, double global_variance)
Posterior rate parameter for leaf node log-gamma distribution.
void SampleLeafParameters(ForestDataset &dataset, ForestTracker &tracker, ColumnVector &residual, Tree *tree, int tree_num, double global_variance, std::mt19937 &gen)
Draw new parameters for every leaf node in tree, using a Gibbs update that conditions on the data,...
double NoSplitLogMarginalLikelihood(CloglogOrdinalSuffStat &suff_stat, double global_variance)
Log marginal likelihood of a node, evaluated only for observations that fall into the node being spli...
Sufficient statistic and associated operations for complementary log-log ordinal BART model.
Definition leaf_model.h:1004
bool SampleGreaterThan(data_size_t threshold)
Check whether accumulated sample size, n, is greater than some threshold.
Definition leaf_model.h:1101
void AddSuffStat(CloglogOrdinalSuffStat &lhs, CloglogOrdinalSuffStat &rhs)
Set the value of each sufficient statistic to the sum of the values provided by lhs and rhs
Definition leaf_model.h:1078
CloglogOrdinalSuffStat()
Construct a new CloglogOrdinalSuffStat object, setting all sufficient statistics to zero.
Definition leaf_model.h:1013
void SubtractSuffStat(CloglogOrdinalSuffStat &lhs, CloglogOrdinalSuffStat &rhs)
Set the value of each sufficient statistic to the difference between the values provided by lhs and t...
Definition leaf_model.h:1090
void ResetSuffStat()
Reset all of the sufficient statistics to zero.
Definition leaf_model.h:1055
void AddSuffStatInplace(CloglogOrdinalSuffStat &suff_stat)
Increment the value of each sufficient statistic by the values provided by suff_stat
Definition leaf_model.h:1066
data_size_t SampleSize()
Return the sample size accumulated by a sufficient stat object.
Definition leaf_model.h:1117
bool SampleGreaterThanEqual(data_size_t threshold)
Check whether accumulated sample size, n, is greater than or equal to some threshold.
Definition leaf_model.h:1110
void IncrementSuffStat(ForestDataset &dataset, Eigen::VectorXd &outcome, ForestTracker &tracker, data_size_t row_idx, int tree_idx)
Accumulate data from observation row_idx into the sufficient statistics.
Definition leaf_model.h:1028
Internal wrapper around Eigen::VectorXd interface for univariate floating point data....
Definition data.h:193
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
Definition gamma_sampler.h:10
Marginal likelihood and posterior computation for gaussian homoskedastic constant leaf outcome model.
Definition leaf_model.h:458
double SplitLogMarginalLikelihood(GaussianConstantSuffStat &left_stat, GaussianConstantSuffStat &right_stat, double global_variance)
Log marginal likelihood for a proposed split, evaluated only for observations that fall into the node...
double PosteriorParameterVariance(GaussianConstantSuffStat &suff_stat, double global_variance)
Leaf node posterior variance.
void SampleLeafParameters(ForestDataset &dataset, ForestTracker &tracker, ColumnVector &residual, Tree *tree, int tree_num, double global_variance, std::mt19937 &gen)
Draw new parameters for every leaf node in tree, using a Gibbs update that conditions on the data,...
double NoSplitLogMarginalLikelihood(GaussianConstantSuffStat &suff_stat, double global_variance)
Log marginal likelihood of a node, evaluated only for observations that fall into the node being spli...
bool RequiresBasis()
Whether this model requires a basis vector for posterior inference and prediction.
Definition leaf_model.h:521
GaussianConstantLeafModel(double tau)
Construct a new GaussianConstantLeafModel object.
Definition leaf_model.h:465
double PosteriorParameterMean(GaussianConstantSuffStat &suff_stat, double global_variance)
Leaf node posterior mean.
void SetScale(double tau)
Set a new value for the leaf node scale parameter.
Definition leaf_model.h:517
Sufficient statistic and associated operations for gaussian homoskedastic constant leaf outcome model...
Definition leaf_model.h:361
data_size_t SampleSize()
Return the sample size accumulated by a sufficient stat object.
Definition leaf_model.h:452
GaussianConstantSuffStat()
Construct a new GaussianConstantSuffStat object, setting all sufficient statistics to zero.
Definition leaf_model.h:369
void SubtractSuffStat(GaussianConstantSuffStat &lhs, GaussianConstantSuffStat &rhs)
Set the value of each sufficient statistic to the difference between the values provided by lhs and t...
Definition leaf_model.h:428
void AddSuffStat(GaussianConstantSuffStat &lhs, GaussianConstantSuffStat &rhs)
Set the value of each sufficient statistic to the sum of the values provided by lhs and rhs
Definition leaf_model.h:417
void AddSuffStatInplace(GaussianConstantSuffStat &suff_stat)
Increment the value of each sufficient statistic by the values provided by suff_stat
Definition leaf_model.h:406
bool SampleGreaterThanEqual(data_size_t threshold)
Check whether accumulated sample size, n, is greater than or equal to some threshold.
Definition leaf_model.h:446
bool SampleGreaterThan(data_size_t threshold)
Check whether accumulated sample size, n, is greater than some threshold.
Definition leaf_model.h:438
void IncrementSuffStat(ForestDataset &dataset, Eigen::VectorXd &outcome, ForestTracker &tracker, data_size_t row_idx, int tree_idx)
Accumulate data from observation row_idx into the sufficient statistics.
Definition leaf_model.h:383
void ResetSuffStat()
Reset all of the sufficient statistics to zero.
Definition leaf_model.h:396
Marginal likelihood and posterior computation for gaussian homoskedastic constant leaf outcome model.
Definition leaf_model.h:797
void SampleLeafParameters(ForestDataset &dataset, ForestTracker &tracker, ColumnVector &residual, Tree *tree, int tree_num, double global_variance, std::mt19937 &gen)
Draw new parameters for every leaf node in tree, using a Gibbs update that conditions on the data,...
Eigen::MatrixXd PosteriorParameterVariance(GaussianMultivariateRegressionSuffStat &suff_stat, double global_variance)
Leaf node posterior variance.
double SplitLogMarginalLikelihood(GaussianMultivariateRegressionSuffStat &left_stat, GaussianMultivariateRegressionSuffStat &right_stat, double global_variance)
Log marginal likelihood for a proposed split, evaluated only for observations that fall into the node...
GaussianMultivariateRegressionLeafModel(Eigen::MatrixXd &Sigma_0)
Construct a new GaussianMultivariateRegressionLeafModel object.
Definition leaf_model.h:804
Eigen::VectorXd PosteriorParameterMean(GaussianMultivariateRegressionSuffStat &suff_stat, double global_variance)
Leaf node posterior mean.
double NoSplitLogMarginalLikelihood(GaussianMultivariateRegressionSuffStat &suff_stat, double global_variance)
Log marginal likelihood of a node, evaluated only for observations that fall into the node being spli...
Sufficient statistic and associated operations for gaussian homoskedastic constant leaf outcome model...
Definition leaf_model.h:684
void AddSuffStatInplace(GaussianMultivariateRegressionSuffStat &suff_stat)
Increment the value of each sufficient statistic by the values provided by suff_stat
Definition leaf_model.h:745
bool SampleGreaterThan(data_size_t threshold)
Check whether accumulated sample size, n, is greater than some threshold.
Definition leaf_model.h:777
data_size_t SampleSize()
Return the sample size accumulated by a sufficient stat object.
Definition leaf_model.h:791
void AddSuffStat(GaussianMultivariateRegressionSuffStat &lhs, GaussianMultivariateRegressionSuffStat &rhs)
Set the value of each sufficient statistic to the sum of the values provided by lhs and rhs
Definition leaf_model.h:756
void SubtractSuffStat(GaussianMultivariateRegressionSuffStat &lhs, GaussianMultivariateRegressionSuffStat &rhs)
Set the value of each sufficient statistic to the difference between the values provided by lhs and t...
Definition leaf_model.h:767
bool SampleGreaterThanEqual(data_size_t threshold)
Check whether accumulated sample size, n, is greater than or equal to some threshold.
Definition leaf_model.h:785
void ResetSuffStat()
Reset all of the sufficient statistics to zero.
Definition leaf_model.h:731
GaussianMultivariateRegressionSuffStat(int basis_dim)
Construct a new GaussianMultivariateRegressionSuffStat object.
Definition leaf_model.h:695
void IncrementSuffStat(ForestDataset &dataset, Eigen::VectorXd &outcome, ForestTracker &tracker, data_size_t row_idx, int tree_idx)
Accumulate data from observation row_idx into the sufficient statistics.
Definition leaf_model.h:710
Marginal likelihood and posterior computation for gaussian homoskedastic constant leaf outcome model.
Definition leaf_model.h:626
double SplitLogMarginalLikelihood(GaussianUnivariateRegressionSuffStat &left_stat, GaussianUnivariateRegressionSuffStat &right_stat, double global_variance)
Log marginal likelihood for a proposed split, evaluated only for observations that fall into the node...
void SampleLeafParameters(ForestDataset &dataset, ForestTracker &tracker, ColumnVector &residual, Tree *tree, int tree_num, double global_variance, std::mt19937 &gen)
Draw new parameters for every leaf node in tree, using a Gibbs update that conditions on the data,...
double PosteriorParameterVariance(GaussianUnivariateRegressionSuffStat &suff_stat, double global_variance)
Leaf node posterior variance.
double PosteriorParameterMean(GaussianUnivariateRegressionSuffStat &suff_stat, double global_variance)
Leaf node posterior mean.
double NoSplitLogMarginalLikelihood(GaussianUnivariateRegressionSuffStat &suff_stat, double global_variance)
Log marginal likelihood of a node, evaluated only for observations that fall into the node being spli...
Sufficient statistic and associated operations for gaussian homoskedastic constant leaf outcome model...
Definition leaf_model.h:529
GaussianUnivariateRegressionSuffStat()
Construct a new GaussianUnivariateRegressionSuffStat object, setting all sufficient statistics to zer...
Definition leaf_model.h:537
void SubtractSuffStat(GaussianUnivariateRegressionSuffStat &lhs, GaussianUnivariateRegressionSuffStat &rhs)
Set the value of each sufficient statistic to the difference between the values provided by lhs and t...
Definition leaf_model.h:596
bool SampleGreaterThan(data_size_t threshold)
Check whether accumulated sample size, n, is greater than some threshold.
Definition leaf_model.h:606
bool SampleGreaterThanEqual(data_size_t threshold)
Check whether accumulated sample size, n, is greater than or equal to some threshold.
Definition leaf_model.h:614
data_size_t SampleSize()
Return the sample size accumulated by a sufficient stat object.
Definition leaf_model.h:620
void ResetSuffStat()
Reset all of the sufficient statistics to zero.
Definition leaf_model.h:564
void AddSuffStat(GaussianUnivariateRegressionSuffStat &lhs, GaussianUnivariateRegressionSuffStat &rhs)
Set the value of each sufficient statistic to the sum of the values provided by lhs and rhs
Definition leaf_model.h:585
void AddSuffStatInplace(GaussianUnivariateRegressionSuffStat &suff_stat)
Increment the value of each sufficient statistic by the values provided by suff_stat
Definition leaf_model.h:574
void IncrementSuffStat(ForestDataset &dataset, Eigen::VectorXd &outcome, ForestTracker &tracker, data_size_t row_idx, int tree_idx)
Accumulate data from observation row_idx into the sufficient statistics.
Definition leaf_model.h:551
Marginal likelihood and posterior computation for heteroskedastic log-linear variance model.
Definition leaf_model.h:942
double NoSplitLogMarginalLikelihood(LogLinearVarianceSuffStat &suff_stat, double global_variance)
Log marginal likelihood of a node, evaluated only for observations that fall into the node being spli...
double PosteriorParameterShape(LogLinearVarianceSuffStat &suff_stat, double global_variance)
Leaf node posterior shape parameter.
void SampleLeafParameters(ForestDataset &dataset, ForestTracker &tracker, ColumnVector &residual, Tree *tree, int tree_num, double global_variance, std::mt19937 &gen)
Draw new parameters for every leaf node in tree, using a Gibbs update that conditions on the data,...
double SplitLogMarginalLikelihood(LogLinearVarianceSuffStat &left_stat, LogLinearVarianceSuffStat &right_stat, double global_variance)
Log marginal likelihood for a proposed split, evaluated only for observations that fall into the node...
double PosteriorParameterScale(LogLinearVarianceSuffStat &suff_stat, double global_variance)
Leaf node posterior scale parameter.
Sufficient statistic and associated operations for heteroskedastic log-linear variance model.
Definition leaf_model.h:860
bool SampleGreaterThanEqual(data_size_t threshold)
Check whether accumulated sample size, n, is greater than or equal to some threshold.
Definition leaf_model.h:930
void SubtractSuffStat(LogLinearVarianceSuffStat &lhs, LogLinearVarianceSuffStat &rhs)
Set the value of each sufficient statistic to the difference between the values provided by lhs and t...
Definition leaf_model.h:913
void ResetSuffStat()
Reset all of the sufficient statistics to zero.
Definition leaf_model.h:884
data_size_t SampleSize()
Return the sample size accumulated by a sufficient stat object.
Definition leaf_model.h:936
void IncrementSuffStat(ForestDataset &dataset, Eigen::VectorXd &outcome, ForestTracker &tracker, data_size_t row_idx, int tree_idx)
Accumulate data from observation row_idx into the sufficient statistics.
Definition leaf_model.h:877
void AddSuffStatInplace(LogLinearVarianceSuffStat &suff_stat)
Increment the value of each sufficient statistic by the values provided by suff_stat
Definition leaf_model.h:893
bool SampleGreaterThan(data_size_t threshold)
Check whether accumulated sample size, n, is greater than some threshold.
Definition leaf_model.h:922
void AddSuffStat(LogLinearVarianceSuffStat &lhs, LogLinearVarianceSuffStat &rhs)
Set the value of each sufficient statistic to the sum of the values provided by lhs and rhs
Definition leaf_model.h:903
Definition normal_sampler.h:26
Class storing a "forest," or an ensemble of decision trees.
Definition ensemble.h:31
Decision tree data structure.
Definition tree.h:66
Definition normal_sampler.h:13
static SuffStatVariant suffStatFactory(ModelType model_type, int basis_dim=0)
Factory function that creates a new SuffStat object for the specified model type.
Definition leaf_model.h:1220
std::variant< GaussianConstantSuffStat, GaussianUnivariateRegressionSuffStat, GaussianMultivariateRegressionSuffStat, LogLinearVarianceSuffStat, CloglogOrdinalSuffStat > SuffStatVariant
Unifying layer for disparate sufficient statistic class types.
Definition leaf_model.h:1188
ModelType
Leaf models for the forest sampler:
Definition leaf_model.h:352
static LeafModelVariant leafModelFactory(ModelType model_type, double tau, Eigen::MatrixXd &Sigma0, double a, double b)
Factory function that creates a new LeafModel object for the specified model type.
Definition leaf_model.h:1243
std::variant< GaussianConstantLeafModel, GaussianUnivariateRegressionLeafModel, GaussianMultivariateRegressionLeafModel, LogLinearVarianceLeafModel, CloglogOrdinalLeafModel > LeafModelVariant
Unifying layer for disparate leaf model class types.
Definition leaf_model.h:1202
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