Repository navigation
Fix GFR treatment leaf scale and weighted tau_0 / adaptive coding in bcf() - #12
Merged
Merged
Conversation
…g fixes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With treatment_effect_forest_params$sample_sigma2_leaf = TRUE, each GFR iteration drew a new treatment-forest leaf scale but then handed the prognostic scale to the prognostic forest's config, so the treatment forest kept its initial scale for every GFR sweep. Update the treatment config with the treatment draw, as the MCMC loop already does. Fits that do not sample the treatment leaf scale, or that run no GFR iterations, are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The C++ leaf statistics read observation_weights as variance multipliers v_i (sum_w += 1 / v_i), but bcf() sampled tau_0 and the adaptive-coding parameters in R from unweighted sums, so with weights the two halves of the Gibbs sampler conditioned on different likelihoods and tau_0 drifted far from the effect while the weighted forest offset it. Use precisions 1 / v_i in Z'W r, Z'W Z and the s_tt / s_ty sums, through two helpers the count path now shares (bcf_tau_0_posterior, bcf_coding_suff_stats). Without weights, and for probit, the unweighted arithmetic is kept and draws are bitwise unchanged. The count tests' weighted Gaussian reference is no longer compared bitwise, since its draws change on purpose. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…bugs Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes two upstream bugs in
R/bcf.Rfound while adding count outcomes. No C++ changes. NASH fits (Gaussian, noobservation_weights, no adaptive coding, treatment leaf scale not sampled) go through none of the changed paths. The testgaussian_interceptbelow checks this bitwise.Base: current
cma-rfx(84f5ccb). #8, #9 and #10 were already merged into it, so this branch is on top of the count code and the count PRs need no rebase. The count path's weightedtau_0/ adaptive-coding sums now go through the same helpers, with the same arithmetic.Bug A: GFR updated the wrong forest's leaf scale
With
treatment_effect_forest_params = list(sample_sigma2_leaf = TRUE), each GFR iteration drew a treatment-forest leaf scale. It then calledforest_model_config_mu$update_leaf_model_scale(current_leaf_scale_mu), so the treatment forest kept its initial scale for every GFR sweep. The MCMC loop was already correct. Fix: updateforest_model_config_tauwithcurrent_leaf_scale_tau.Test:
createForestModelConfig()is traced to record everyupdate_leaf_model_scale()call. Withnum_gfr = 4,keep_gfr = TRUEand 3 MCMC draws, the treatment config receives exactlyc(gfr draws, last gfr draw (chain reset), mcmc draws)ofsigma2_leaf_tau_samples, and holds each value afterwards. The prognostic config gets the same forsigma2_leaf_mu_samples. On the pre-fix build this test fails.Bug B: Gaussian
tau_0and adaptive coding ignoredobservation_weightsThe C++ leaf statistics read
observation_weightsas variance multipliers v_i (sum_w += 1/VarWeightValue). The R-sidetau_0update and theb_0/b_1update used unweightedZ'r,Z'Z,s_tt,s_ty. The two halves of the Gibbs sampler therefore conditioned on different likelihoods.Fix: use precisions w_i = 1/v_i:
Z'W r,Z'W Z, and weighteds_tt/s_ty. The posterior isN(Σ Z'Wr/σ², Σ)withΣ = (Z'WZ/σ² + I/τ²)⁻¹. The adaptive-coding prior (+ 2σ²) is unchanged; only the data terms are weighted. The sums live in two small internal helpers,bcf_tau_0_posterior()andbcf_coding_suff_stats(), shared by the GFR and MCMC loops and the count path.current_precisionisNULLwithout weights or for probit, which keeps the old unweighted expressions.Bitwise evidence
fixtures/make-bcf-fixes-reference.Rcaptured seeded draws on 84f5ccb before either fix (commit 1).test-bcf-fixes.Rcompares current draws against them.expect_identicalon y_hat, mu_hat, tau_hat train and test, sigma², both leaf scales, tau_0, b_0, b_1, rfx, sigma²(x)):sample_intercept = TRUE(default)adaptive_coding = TRUE(no intercept)num_gfr = 0) with the treatment leaf scale sampledbcf-reference.rds) still matches bitwise, except forgaussian_dose_weighted. That fit carriesobservation_weights, so its draws change on purpose and it is now excluded from the bitwise comparison.Recovery evidence
Unit checks:
bcf_tau_0_posterior()matches the closed form for p = 1, and for p = 2 it matches the Bayesian WLS solved as OLS on rows scaled by1/sqrt(v σ²)with prior pseudo-rows.bcf_coding_suff_stats()matches direct weighted sums.NULLpath bitwise.Wiring check: both helpers are traced during a weighted adaptive-coding fit. On every GFR and MCMC iteration, the precision passed is exactly
1/observation_weights. The conditional mean used fortau_0equals the exact WLS conditional mean given that iteration's forests,Σ b r/v / (Σ b²/v + σ²/τ²). Unweighted Gaussian and weighted probit fits passNULL.End-to-end study (scratch script, not committed): n = 800, 20% of rows with 400× noise variance passed as weights, constant effect 1, 10 seeds, 10 GFR + 100 burn-in + 200 draws.
tau_0error, no adaptive coding (max / RMSE)Before the fix, the weighted forest offset the wandering
tau_0, so the ATE looked fine without adaptive coding. With adaptive coding it did not. With adaptive coding,tau_0alone is not identified (onlytau_0 · (b_1 − b_0)is), so the table reports the ATE there.A lighter committed version (n = 500, one seed) asserts
|mean tau_0 − 1| < 1. The value is 0.44 after the fix and 38.7 before. It also asserts the ATE is within 0.25.Test suite
NOT_CRAN=true, installed into a scratch library:FAIL 0 | WARN 13 | SKIP 2 | PASS 1841FAIL 0 | WARN 13 | SKIP 2 | PASS 1900Per file, the only changes are:
test-bcf-fixes.R(60 expectations)test-bcf-count.R: 167 → 166, from dropping thegaussian_dose_weightedcomparisonKnown and not fixed
These are also listed in
NASH-FORK.mdunder "Known upstream bugs":sample_intercept = TRUEomitstau_0·b(z)from the latent step's truncated-normal mean and from the residual handed to the forests. Fixing it changes probit draws, so it is deliberately left for a separate decision.tau_0and adaptive coding still ignoreobservation_weights. The probit latent step draws z with unit variance whatever the weights.tau_0and adaptive-coding updates ignore a variance forest's per-row variances.observation_weightsmake Gaussianbcf()return NaN draws, because of the C++ variance-multiplier convention. The input checks still describe all-zero weights as "prior sampling mode", and the roxygen text describes weights as precisions. Both are pre-existing.🤖 Generated with Claude Code