StochTree 0.5.0.9000
Loading...
Searching...
No Matches
normal_sampler.h
1
2#ifndef STOCHTREE_NORMAL_SAMPLER_H_
3#define STOCHTREE_NORMAL_SAMPLER_H_
4
5#include <Eigen/Dense>
6#include <stochtree/distributions.h>
7#include <stochtree/log.h>
8#include <random>
9#include <vector>
10
11namespace StochTree {
12
14 public:
15 UnivariateNormalSampler() { std_normal_dist_ = standard_normal(); }
17 double Sample(double mean, double variance, std::mt19937& gen) {
18 return mean + std::sqrt(variance) * std_normal_dist_(gen);
19 }
20
21 private:
23 standard_normal std_normal_dist_;
24};
25
27 public:
28 MultivariateNormalSampler() { std_normal_dist_ = standard_normal(); }
30 std::vector<double> Sample(Eigen::VectorXd& mean, Eigen::MatrixXd& covariance, std::mt19937& gen) {
31 // Dimension extraction and checks
32 int mean_cols = mean.size();
33 int cov_rows = covariance.rows();
34 int cov_cols = covariance.cols();
36
37 // Variance cholesky decomposition
38 Eigen::LLT<Eigen::MatrixXd> decomposition(covariance);
39 Eigen::MatrixXd covariance_chol = decomposition.matrixL();
40
41 // Sample a vector of standard normal random variables
42 Eigen::VectorXd std_norm_vec(cov_rows);
43 for (int i = 0; i < cov_rows; i++) {
44 std_norm_vec(i) = std_normal_dist_(gen);
45 }
46
47 // Compute and return the sampled value
49 std::vector<double> result(cov_rows);
50 for (int i = 0; i < cov_rows; i++) {
52 }
53 return result;
54 }
55 Eigen::VectorXd SampleEigen(Eigen::VectorXd& mean, Eigen::MatrixXd& covariance, std::mt19937& gen) {
56 // Dimension extraction and checks
57 int mean_cols = mean.size();
58 int cov_rows = covariance.rows();
59 int cov_cols = covariance.cols();
61
62 // Variance cholesky decomposition
63 Eigen::LLT<Eigen::MatrixXd> decomposition(covariance);
64 Eigen::MatrixXd covariance_chol = decomposition.matrixL();
65
66 // Sample a vector of standard normal random variables
67 Eigen::VectorXd std_norm_vec(cov_rows);
68 for (int i = 0; i < cov_rows; i++) {
69 std_norm_vec(i) = std_normal_dist_(gen);
70 }
71
72 // Compute and return the sampled value
74 }
75
76 private:
78 standard_normal std_normal_dist_;
79};
80
81} // namespace StochTree
82
83#endif // STOCHTREE_NORMAL_SAMPLER_H_
Definition normal_sampler.h:26
Definition normal_sampler.h:13
Definition distributions.h:56
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