Repository navigation
Make observation_weights precision weights, as documented - #17
Merged
Merged
Conversation
bart() and bcf() documented observation_weights as y_i ~ N(mu, sigma^2 / w_i) but handed w unchanged to the C++ datasets, whose variance-weight slot is a variance multiplier wherever it is read. A larger weight therefore gave a row less influence, and a constant weight c divided sigma^2 by c. Upstream stochtree (737f397) has the same mismatch. The R layer now converts once (1 / w) and uses that for every forest and random-effects dataset, for count outcomes (1 / (w omega)) and for BCF's R-side precision. Gaussian and probit random effects now receive the weights too. The weighted PosteriorShape counts only rows with a finite variance weight, so a zero weight removes a row from the likelihood. Unweighted fits are bitwise unchanged; weighted reference fixtures pass 1 / w and match to rounding. Co-Authored-By: Claude <noreply@anthropic.com>
dwachsmuth
marked this pull request as ready for review
November 13, 2026 08:05
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.
Summary
bart()andbcf()documentobservation_weightsas precision weights (y_i ~ N(mu(X_i), sigma^2 / w_i): a larger weight gives a row more influence). They did the opposite. The R layer handedwunchanged tocreateForestDataset(variance_weights = ), and the C++ layer reads that slot as a variance multiplier everywhere it is used: leaf sufficient statistics (sum_w += 1 / VarWeightValue), random effects (cwiseInverse()), and the global variance's posterior scale (r^2 / weights(i)). It is the same slot a variance forest scales byexp(s(x))and a count outcome sets to1 / omega.This PR keeps the C++ convention and makes the R layer pass
1 / w.Found on 12 Nov 2026 while adding noise-aware row weights to nash for NASH6 (nash_plus#100). A toy fit at
ff3eb820: X carries no signal, half the rows sit at y = 1 and half at y = 0.Upstream: stochtree has the same mismatch at 737f397:
R/bart.Rpassesvariance_weights = observation_weights, and its leaf model divides by the weight. Nothing has been reported upstream; that is left to David.Base:
main(f805419), since that is where the fix has to live. nash currently pinsff3eb820. The nash draft PR that moves its pin to this branch states what that requires.Changes
bart()andbcf()computeobservation_variance_weights <- 1 / wonce, after validation. That value is used for:1 / (w omega)after the Pólya-Gamma step;witself);bcf_count.Ronly validates. No other R function takesobservation_weights.NULL, so only the forest was weighted. Count outcomes already passed theirs.1 / 0 = Inf, an infinite variance multiplier: the row drops out of the leaf and random-effects statistics and the residual sum of squares. The weightedGlobalHomoskedasticVarianceModel::PosteriorShapenow counts only rows with a finite variance weight, so a zero-weight row is absent from the likelihood altogether. The documented "all weights zero = prior sampling" mode therefore samples the priors withnum_gfr = 0; the existing validation (num_gfr > 0refused) is unchanged. Under the old code a zero weight became a zero variance multiplier, which divides by zero. The outcome is still standardized, and default priors calibrated, over all rows without weights. The docs now say so.R/bart.R,R/bcf.Rand their Rd files; NEWS, NASH-FORK.md; version 0.4.6.9013.Tests
New tests in
tests/testthat/test-observation-weights.R. Each fails under the old behaviour:cmultiplies sigma^2 byc(ratio between 3.2 and 5 forc = 4);ff3eb820;Fixtures and tests that passed weights in the old meaning now pass
1 / w:make-outcome-reference.R,make-negbin-reference.R,make-bcf-fixes-reference.Randmake-bcf-reference.R;test-bcf-fixes.R, which passed variancesvas weights in two tests.For the bitwise reference fits,
1 / (1 / w)differs fromwin the last bit, so the four weighted reference entries now match to about 1e-15 and are compared withtolerance = 1e-10:gaussian_weighted;root_weighted(NB);probit_weighted_intercept_adaptive;gaussian_dose_weighted, which was already skipped.Every unweighted reference entry is still compared bitwise and passes.
Full suite (macOS arm64, R 4.6, installed package,
NOT_CRAN=true): 209 tests, 2159 expectations, 0 failures, 2 skipped (the suite's own slow-test skips).Identity: seeded unweighted fits at
ff3eb820and at this commit are bitwise identical in every draw. That covers BART, BART with a random intercept, probit BART, importance retention, and BCF with a random intercept: train and test predictions, sigma^2, leaf scales, random effects, and posterior predictive draws. The saved JSON differs only instochtree_version.Not verified
Draft: do not merge before the nash side is reviewed.
🤖 Generated with Claude Code