Skip to content

Commit 3bd4c04

Browse files
authored
Merge pull request #357 from StochasticTree/cloglog-r-hotfix
Hotfix for cloglog prediction and initialization bugs
2 parents ac2c64d + 392638d commit 3bd4c04

6 files changed

Lines changed: 31 additions & 8 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*.csv
44
*.DS_Store
55
lib/
6-
build/
6+
build*/
77
.vscode/
88
xcode/
99
*.json

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
## Bug Fixes
1111

12+
* Fixed several initialization, sampling and prediction bugs for cloglog [#357](https://github.com/StochasticTree/stochtree/pull/357)
1213
* Fixed num_threads pass-through bug in R BART [#339](https://github.com/StochasticTree/stochtree/pull/339)
1314
* Fixed probit offset bug in R and Python [#337](https://github.com/StochasticTree/stochtree/pull/337)
1415
* Standardized multi-chain BCF handling of internal propensity models [#334](https://github.com/StochasticTree/stochtree/pull/334)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
## Bug Fixes
99

10+
* Fixed several initialization, sampling and prediction bugs for cloglog [#357](https://github.com/StochasticTree/stochtree/pull/357)
1011
* Fixed num_threads pass-through bug in R BART [#339](https://github.com/StochasticTree/stochtree/pull/339)
1112
* Fixed probit offset bug in R and Python [#337](https://github.com/StochasticTree/stochtree/pull/337)
1213
* Standardized multi-chain BCF handling of internal propensity models [#334](https://github.com/StochasticTree/stochtree/pull/334)

R/bart.R

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2867,8 +2867,12 @@ predict.bartmodel <- function(
28672867
mean_forest_probabilities[, j, ] <- (1 -
28682868
exp(
28692869
-exp(
2870-
mean_forest_predictions +
2871-
cloglog_cutpoint_samples[j, ]
2870+
sweep(
2871+
mean_forest_predictions,
2872+
2,
2873+
cloglog_cutpoint_samples[j, ],
2874+
"+"
2875+
)
28722876
)
28732877
))
28742878
} else if (j == cloglog_num_categories) {
@@ -2881,15 +2885,23 @@ predict.bartmodel <- function(
28812885
} else {
28822886
mean_forest_probabilities[, j, ] <- (exp(
28832887
-exp(
2884-
mean_forest_predictions +
2885-
cloglog_cutpoint_samples[j - 1, ]
2888+
sweep(
2889+
mean_forest_predictions,
2890+
2,
2891+
cloglog_cutpoint_samples[j, ],
2892+
"+"
2893+
)
28862894
)
28872895
) *
28882896
(1 -
28892897
exp(
28902898
-exp(
2891-
mean_forest_predictions +
2892-
cloglog_cutpoint_samples[j, ]
2899+
sweep(
2900+
mean_forest_predictions,
2901+
2,
2902+
cloglog_cutpoint_samples[j, ],
2903+
"+"
2904+
)
28932905
)
28942906
)))
28952907
}

src/forest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ void initialize_forest_model_active_forest_cpp(cpp11::external_pointer<StochTree
849849
int n = data->NumObservations();
850850
std::vector<double> initial_preds(n, init_val);
851851
data->AddVarianceWeights(initial_preds.data(), n);
852-
} else if (model_type == StochTree::ModelType::kLogLinearVariance) {
852+
} else if (model_type == StochTree::ModelType::kCloglogOrdinal) {
853853
leaf_init_val = init_val / static_cast<double>(num_trees);
854854
active_forest->SetLeafValue(leaf_init_val);
855855
UpdateResidualEntireForest(*tracker, *data, *residual, active_forest.get(), false, std::minus<double>());

stochtree/bart.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,9 @@ def sample(
16991699
residual_train,
17001700
True,
17011701
)
1702+
# Undo residual update done by reconstitute_from_forest for the cloglog model
1703+
if link_is_cloglog:
1704+
residual_train.update_data(resid_train[:, 0])
17021705
# Reset leaf scale
17031706
if sample_sigma2_leaf:
17041707
leaf_scale_double = self.leaf_scale_samples[
@@ -1754,6 +1757,9 @@ def sample(
17541757
residual_train,
17551758
True,
17561759
)
1760+
# Undo residual update done by reconstitute_from_forest for the cloglog model
1761+
if link_is_cloglog:
1762+
residual_train.update_data(resid_train[:, 0])
17571763
# Reset leaf scale
17581764
if sample_sigma2_leaf and previous_leaf_var_samples is not None:
17591765
leaf_scale_double = previous_leaf_var_samples[
@@ -1818,6 +1824,9 @@ def sample(
18181824
residual_train,
18191825
True,
18201826
)
1827+
# Undo residual update done by reconstitute_from_forest for the cloglog model
1828+
if link_is_cloglog:
1829+
residual_train.update_data(resid_train[:, 0])
18211830
# Reset mean forest leaf scale
18221831
if sample_sigma2_leaf and previous_leaf_var_samples is not None:
18231832
current_leaf_scale[0, 0] = sigma2_leaf

0 commit comments

Comments
 (0)