56 bool standardize_outcome =
true;
59 int cutpoint_grid_size = 100;
60 std::vector<FeatureType> feature_types;
65 bool keep_burnin =
false;
66 bool adaptive_coding =
false;
67 double b_0_init = 0.0;
68 double b_1_init = 1.0;
71 double a_sigma2_global = 0.0;
72 double b_sigma2_global = 0.0;
73 double sigma2_global_init = 1.0;
74 bool sample_sigma2_global =
true;
77 int num_trees_mu = 200;
78 double alpha_mu = 0.95;
80 int min_samples_leaf_mu = 5;
81 int max_depth_mu = -1;
82 bool leaf_constant_mu =
true;
84 bool exponentiated_leaf_mu =
false;
85 int num_features_subsample_mu = 0;
86 double a_sigma2_mu = 3.0;
87 double b_sigma2_mu = -1.0;
88 double sigma2_mu_init = -1.0;
89 std::vector<double> var_weights_mu;
90 bool sample_sigma2_leaf_mu =
false;
91 std::vector<int> sweep_update_indices_mu;
94 int num_trees_tau = 50;
95 double alpha_tau = 0.95;
96 double beta_tau = 2.0;
97 int min_samples_leaf_tau = 5;
98 int max_depth_tau = -1;
99 bool leaf_constant_tau =
false;
100 int leaf_dim_tau = 1;
101 bool exponentiated_leaf_tau =
false;
102 int num_features_subsample_tau = 0;
103 double a_sigma2_tau = 3.0;
104 double b_sigma2_tau = -1.0;
105 double sigma2_tau_init = -1.0;
106 std::vector<double> var_weights_tau;
107 std::vector<double> sigma2_leaf_tau_matrix;
108 bool sample_sigma2_leaf_tau =
false;
109 std::vector<int> sweep_update_indices_tau;
110 MeanLeafModelType tau_leaf_model_type = MeanLeafModelType::GaussianUnivariateRegression;
111 bool sample_tau_0 =
true;
112 double tau_0_prior_var_scalar = -1.0;
113 std::vector<double> tau_0_prior_var_multivariate;
116 int num_trees_variance = 0;
117 double leaf_prior_calibration_param = 1.5;
118 double shape_variance_forest = -1.0;
119 double scale_variance_forest = -1.0;
120 double variance_forest_leaf_init = -1.0;
121 double alpha_variance = 0.5;
122 double beta_variance = 2.0;
123 int min_samples_leaf_variance = 5;
124 int max_depth_variance = -1;
125 bool leaf_constant_variance =
true;
126 int leaf_dim_variance = 1;
127 bool exponentiated_leaf_variance =
true;
128 int num_features_subsample_variance = 0;
129 std::vector<double> var_weights_variance;
130 std::vector<int> sweep_update_indices_variance;
133 bool has_random_effects =
false;
134 BCFRFXModelSpec rfx_model_spec = BCFRFXModelSpec::Custom;
135 std::vector<double> rfx_working_parameter_mean_prior;
136 std::vector<double> rfx_group_parameter_mean_prior;
137 std::vector<double> rfx_working_parameter_cov_prior;
138 std::vector<double> rfx_group_parameter_cov_prior;
139 double rfx_variance_prior_shape = 1.0;
140 double rfx_variance_prior_scale = 1.0;
147 std::vector<double> y_hat_train;
150 std::vector<double> mu_forest_predictions_train;
153 std::vector<double> tau_forest_predictions_train;
156 std::vector<double> variance_forest_predictions_train;
159 std::vector<double> y_hat_test;
162 std::vector<double> mu_forest_predictions_test;
165 std::vector<double> tau_forest_predictions_test;
168 std::vector<double> variance_forest_predictions_test;
171 std::vector<double> global_error_variance_samples;
174 std::vector<double> leaf_scale_mu_samples;
175 std::vector<double> leaf_scale_tau_samples;
178 std::unique_ptr<ForestContainer> mu_forests;
181 std::unique_ptr<ForestContainer> tau_forests;
184 std::unique_ptr<ForestContainer> variance_forests;
187 std::vector<double> rfx_predictions_train;
190 std::vector<double> rfx_predictions_test;
193 std::vector<double> tau_0_samples;
196 std::vector<double> b0_samples;
197 std::vector<double> b1_samples;
200 std::unique_ptr<RandomEffectsContainer> rfx_container;
201 std::unique_ptr<LabelMapper> rfx_label_mapper;
207 int treatment_dim = 0;
220 void AppendToJson(nlohmann::json&
obj)
const {
222 nlohmann::json
forests = nlohmann::json::object();
224 if (mu_forests !=
nullptr) {
225 forests.emplace(
"prognostic_forest", mu_forests->to_json());
228 if (tau_forests !=
nullptr) {
229 forests.emplace(
"treatment_forest", tau_forests->to_json());
232 if (variance_forests !=
nullptr) {
233 forests.emplace(
"variance_forest", variance_forests->to_json());
239 nlohmann::json
parameters = nlohmann::json::object();
240 if (!global_error_variance_samples.empty()) {
241 parameters.emplace(
"sigma2_global_samples", global_error_variance_samples);
243 if (!leaf_scale_mu_samples.empty()) {
244 parameters.emplace(
"sigma2_leaf_mu_samples", leaf_scale_mu_samples);
246 if (!leaf_scale_tau_samples.empty()) {
247 parameters.emplace(
"sigma2_leaf_tau_samples", leaf_scale_tau_samples);
249 if (!b0_samples.empty()) {
252 if (!b1_samples.empty()) {
255 if (!tau_0_samples.empty()) {
256 parameters.emplace(
"tau_0_samples", tau_0_samples);
257 obj.emplace(
"tau_0_dim", treatment_dim);
263 obj.emplace(
"outcome_mean", y_bar);
264 obj.emplace(
"outcome_scale", y_std);
265 obj.emplace(
"num_samples", num_samples);
266 obj.emplace(
"treatment_dim", treatment_dim);
269 nlohmann::json
rfx = nlohmann::json::object();
270 if (rfx_container !=
nullptr && rfx_label_mapper !=
nullptr) {
271 rfx.emplace(
"random_effect_container_0", rfx_container->to_json());
272 rfx.emplace(
"random_effect_label_mapper_0", rfx_label_mapper->to_json());
273 rfx.emplace(
"random_effect_groupids_0", rfx_label_mapper->Keys());
276 obj[
"random_effects"] =
rfx;
282 void FromJson(
const nlohmann::json&
obj) {
284 if (
obj.contains(
"forests")) {
285 const nlohmann::json&
forests =
obj.at(
"forests");
286 if (
forests.contains(
"prognostic_forest")) {
287 mu_forests = std::make_unique<ForestContainer>(0, 0,
false,
false);
288 mu_forests->from_json(
forests.at(
"prognostic_forest"));
290 if (
forests.contains(
"treatment_forest")) {
291 tau_forests = std::make_unique<ForestContainer>(0, 0,
false,
false);
292 tau_forests->from_json(
forests.at(
"treatment_forest"));
294 if (
forests.contains(
"variance_forest")) {
295 variance_forests = std::make_unique<ForestContainer>(0, 0,
false,
false);
296 variance_forests->from_json(
forests.at(
"variance_forest"));
300 if (
obj.contains(
"parameters")) {
302 if (
parameters.contains(
"sigma2_global_samples")) {
303 global_error_variance_samples =
parameters.at(
"sigma2_global_samples").get<std::vector<double>>();
305 if (
parameters.contains(
"sigma2_leaf_mu_samples")) {
306 leaf_scale_mu_samples =
parameters.at(
"sigma2_leaf_mu_samples").get<std::vector<double>>();
308 if (
parameters.contains(
"sigma2_leaf_tau_samples")) {
309 leaf_scale_tau_samples =
parameters.at(
"sigma2_leaf_tau_samples").get<std::vector<double>>();
312 b0_samples =
parameters.at(
"b0_samples").get<std::vector<double>>();
315 b1_samples =
parameters.at(
"b1_samples").get<std::vector<double>>();
318 tau_0_samples =
parameters.at(
"tau_0_samples").get<std::vector<double>>();
322 if (
obj.contains(
"num_random_effects") &&
obj.at(
"num_random_effects").get<
int>() > 0) {
323 rfx_container = std::make_unique<RandomEffectsContainer>();
324 rfx_label_mapper = std::make_unique<LabelMapper>();
325 rfx_container->from_json(
obj.at(
"random_effects").at(
"random_effect_container_0"));
326 rfx_label_mapper->from_json(
obj.at(
"random_effects").at(
"random_effect_label_mapper_0"));
329 if (
obj.contains(
"outcome_mean")) y_bar =
obj.at(
"outcome_mean").get<
double>();
330 if (
obj.contains(
"outcome_scale")) y_std =
obj.at(
"outcome_scale").get<
double>();
331 if (
obj.contains(
"num_samples")) num_samples =
obj.at(
"num_samples").get<
int>();
332 if (
obj.contains(
"treatment_dim")) treatment_dim =
obj.at(
"treatment_dim").get<
int>();
341 if (y_bar !=
other.y_bar || y_std !=
other.y_std) {
342 Log::Fatal(
"Cannot merge BCFSamples with different outcome standardization");
344 if (rfx_container !=
nullptr &&
other.rfx_container !=
nullptr) {
345 if (rfx_container->NumComponents() !=
other.rfx_container->NumComponents() ||
346 rfx_container->NumGroups() !=
other.rfx_container->NumGroups()) {
347 Log::Fatal(
"Cannot merge BARTSamples with different random effects structure");
349 if (rfx_label_mapper->Keys() !=
other.rfx_label_mapper->Keys()) {
350 Log::Fatal(
"Cannot merge BARTSamples with different random effects label mapping");
352 if (rfx_label_mapper->Map() !=
other.rfx_label_mapper->Map()) {
353 Log::Fatal(
"Cannot merge BARTSamples with different random effects label mapping");
356 if (treatment_dim !=
other.treatment_dim) {
357 Log::Fatal(
"Cannot merge BCFSamples with different treatment_dim");
366 auto append = [](std::vector<double>&
dst,
const std::vector<double>&
src,
const std::string&
name =
"") {
367 if ((!
dst.empty() &&
src.empty()) || (
dst.empty() && !
src.empty())) {
368 Log::Fatal(
"Cannot merge BARTSamples objects: %s samples present in one chain but not the other",
name.c_str());
372 append(global_error_variance_samples,
other.global_error_variance_samples,
"global error variance");
373 append(leaf_scale_mu_samples,
other.leaf_scale_mu_samples,
"leaf scale mu");
374 append(leaf_scale_tau_samples,
other.leaf_scale_tau_samples,
"leaf scale tau");
375 append(tau_0_samples,
other.tau_0_samples,
"tau_0");
378 num_samples +=
other.num_samples;
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