diff --git a/DESCRIPTION b/DESCRIPTION index dc1afb7b6..8e609128c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,6 +10,7 @@ Authors@R: person("Jingyu", "He", role = "aut"), person("Pedro", "Lima", role = "ctb"), person("Entejar", "Alam", role = "ctb"), + person("David", "Wachsmuth", role = "ctb"), person("stochtree", "contributors", role = c("cph")), person("Eigen", "contributors", role = c("cph"), comment = "C++ source uses the Eigen library for matrix operations, see inst/COPYRIGHTS"), person("xgboost", "contributors", role = c("cph"), comment = "C++ tree code and related operations include or are inspired by code from the xgboost library, see inst/COPYRIGHTS"), diff --git a/NEWS.md b/NEWS.md index fdcf9de3d..7c63cf1a6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,18 @@ # stochtree 0.4.6.9000 +## Bug Fixes + +* Fix the covariate range used to propose MCMC split points, which was computed incorrectly when all of a node's values for a feature were negative or when the node's first observation held its largest value, causing grow proposals to be rejected or drawn from the wrong range [#425](https://github.com/StochasticTree/stochtree/pull/425) +* Fix the move-type and node-selection probabilities in the MCMC grow / prune Metropolis-Hastings acceptance ratios, so that the tree sampler targets the correct posterior over trees [#426](https://github.com/StochasticTree/stochtree/pull/426) +* Fix ordering of missing (`NaN`) covariate values when presorting features for the grow-from-root (GFR) sampler, which now sorts `NaN`s last, consistent with how numeric splits route them [#427](https://github.com/StochasticTree/stochtree/pull/427) +* Fix the conditional posterior of the random effects variance components, which was missing factors of one half in its inverse gamma shape and scale [#428](https://github.com/StochasticTree/stochtree/pull/428) + +Fixes in [#425](https://github.com/StochasticTree/stochtree/pull/425), [#426](https://github.com/StochasticTree/stochtree/pull/426), and [#428](https://github.com/StochasticTree/stochtree/pull/428) change sampler output, as does [#427](https://github.com/StochasticTree/stochtree/pull/427) for covariates with missing values, so results for a given random seed will differ from previous versions. + ## Documentation and Other Maintenance * Add absolute tolerance to several python floating point comparison tests [#430](https://github.com/StochasticTree/stochtree/pull/430). +* Add exact-posterior tests of the MCMC tree sampler in R and Python, which check that a long chain on a small problem reproduces the analytically enumerated posterior over trees [#431](https://github.com/StochasticTree/stochtree/pull/431) # stochtree 0.4.5 diff --git a/man/stochtree-package.Rd b/man/stochtree-package.Rd index 415733696..9c285fff0 100644 --- a/man/stochtree-package.Rd +++ b/man/stochtree-package.Rd @@ -32,6 +32,7 @@ Other contributors: \itemize{ \item Pedro Lima [contributor] \item Entejar Alam [contributor] + \item David Wachsmuth [contributor] \item stochtree contributors [copyright holder] \item Eigen contributors (C++ source uses the Eigen library for matrix operations, see inst/COPYRIGHTS) [copyright holder] \item xgboost contributors (C++ tree code and related operations include or are inspired by code from the xgboost library, see inst/COPYRIGHTS) [copyright holder] diff --git a/test/R/testthat/test-mcmc-exact-posterior.R b/test/R/testthat/test-mcmc-exact-posterior.R new file mode 100644 index 000000000..605971ec0 --- /dev/null +++ b/test/R/testthat/test-mcmc-exact-posterior.R @@ -0,0 +1,109 @@ +# Check that the MCMC tree sampler targets the exact posterior on a tiny problem. +# +# One tree, one covariate taking values 1, 2, 3, 4, outcomes all zero, alpha = 0.6, +# beta = 2, min_samples_leaf = 1, and global and leaf variances fixed at 1. With so +# few observations every tree can be enumerated, so the posterior over the number +# of leaves is known exactly, and a long chain must reproduce it. +# +# The sampler's target over trees is +# +# p(T | y) ∝ prod_internal [ pg(d) / (width of the node's covariate range) ] +# * prod_leaves [ (1 - pg(d)) * (1 + n_leaf)^(-1/2) ], pg(d) = alpha (1+d)^-beta +# +# where 1/width is the uniform cutpoint density integrated over a unit gap between +# adjacent integers, and (1 + n)^(-1/2) is a leaf's marginal likelihood for zero +# outcomes, up to a constant shared by every tree. +# +# The covariate is fed in ascending and descending order: the posterior is the same, +# but the order changes which observation each node scans first when computing its +# split range, which is how the GH #425 extrema bug shows up. + +exact_x_values <- c(1, 2, 3, 4) +exact_alpha <- 0.6 +exact_beta <- 2 + +# Posterior probability of 1..4 leaves, by enumerating every tree +exact_leaf_distribution <- function() { + pg <- function(depth) exact_alpha * (1 + depth)^(-exact_beta) + n <- length(exact_x_values) + # Unnormalized posterior mass by number of leaves (element k = k leaves) + # for the subtree on points lo..hi + node <- function(lo, hi, depth) { + out <- numeric(n) + out[1] <- (1 - pg(depth)) * (1 + hi - lo + 1)^(-0.5) + width <- exact_x_values[hi] - exact_x_values[lo] + if (hi > lo) { + for (cut in lo:(hi - 1)) { + weight <- pg(depth) * + (exact_x_values[cut + 1] - exact_x_values[cut]) / + width + left <- node(lo, cut, depth + 1) + right <- node(cut + 1, hi, depth + 1) + for (kl in which(left > 0)) { + for (kr in which(right > 0)) { + out[kl + kr] <- out[kl + kr] + weight * left[kl] * right[kr] + } + } + } + } + out + } + mass <- node(1, n, 0) + mass / sum(mass) +} + +test_that("Exact leaf-count posterior enumeration", { + # Hand-checkable anchor for the enumeration itself + expect_equal( + exact_leaf_distribution(), + c(0.50183, 0.42176, 0.07196, 0.00446), + tolerance = 1e-5 + ) +}) + +test_that("MCMC sampler matches the exact posterior", { + skip_on_cran() + + exact <- exact_leaf_distribution() + for (descending in c(FALSE, TRUE)) { + x <- if (descending) rev(exact_x_values) else exact_x_values + model <- bart( + X_train = matrix(x, ncol = 1), + y_train = rep(0, length(x)), + num_gfr = 0, + num_burnin = 5000, + num_mcmc = 100000, + general_params = list( + standardize = FALSE, + sample_sigma2_global = FALSE, + sigma2_global_init = 1, + random_seed = 20260907 + ), + mean_forest_params = list( + num_trees = 1, + alpha = exact_alpha, + beta = exact_beta, + min_samples_leaf = 1, + sample_sigma2_leaf = FALSE, + sigma2_leaf_init = 1 + ) + ) + forests <- model$mean_forests + leaves <- sapply( + seq_len(forests$num_samples()) - 1, + forests$num_forest_leaves + ) + observed <- sapply(seq_along(exact), function(k) mean(leaves == k)) + + # With 100k draws the Monte Carlo standard error of each share is under 0.2 + # percentage points; the pre-fix sampler missed by 8-10 points (or never split). + expect_lt( + max(abs(observed - exact)), + 0.01, + label = sprintf( + "max leaf-share error (%s order)", + if (descending) "descending" else "ascending" + ) + ) + } +}) diff --git a/test/python/test_mcmc_exact_posterior.py b/test/python/test_mcmc_exact_posterior.py new file mode 100644 index 000000000..f31e6e39c --- /dev/null +++ b/test/python/test_mcmc_exact_posterior.py @@ -0,0 +1,91 @@ +"""Check that the MCMC tree sampler targets the exact posterior on a tiny problem. + +One tree, one covariate taking values 1, 2, 3, 4, outcomes all zero, alpha=0.6, +beta=2, min_samples_leaf=1, and global and leaf variances fixed at 1. With so few +observations every tree can be enumerated, so the posterior over the number of +leaves is known exactly, and a long chain must reproduce it. + +The sampler's target over trees is + + p(T | y) ∝ prod_internal [ pg(d) / (width of the node's covariate range) ] + * prod_leaves [ (1 - pg(d)) * (1 + n_leaf)^(-1/2) ], pg(d) = alpha (1+d)^-beta + +where 1/width is the uniform cutpoint density integrated over a unit gap between +adjacent integers, and (1 + n)^(-1/2) is a leaf's marginal likelihood for zero +outcomes, up to a constant shared by every tree. + +The covariate is fed in ascending and descending order: the posterior is the same, +but the order changes which observation each node scans first when computing its +split range, which is how the GH #425 extrema bug shows up. +""" + +import numpy as np +import pytest + +from stochtree import BARTModel + +X_VALUES = np.array([1.0, 2.0, 3.0, 4.0]) +ALPHA, BETA = 0.6, 2.0 + + +def exact_leaf_distribution(): + """Posterior probability of 1..4 leaves, by enumerating every tree.""" + + def pg(depth): + return ALPHA * (1 + depth) ** -BETA + + def node(lo, hi, depth): + # {num_leaves: unnormalized posterior mass} for the subtree on points lo..hi-1 + out = {1: (1 - pg(depth)) * (1 + hi - lo) ** -0.5} + width = X_VALUES[hi - 1] - X_VALUES[lo] + for cut in range(lo + 1, hi): + weight = pg(depth) * (X_VALUES[cut] - X_VALUES[cut - 1]) / width + for kl, ml in node(lo, cut, depth + 1).items(): + for kr, mr in node(cut, hi, depth + 1).items(): + out[kl + kr] = out.get(kl + kr, 0.0) + weight * ml * mr + return out + + mass = node(0, len(X_VALUES), 0) + total = sum(mass.values()) + return np.array([mass.get(k, 0.0) / total for k in range(1, len(X_VALUES) + 1)]) + + +def test_exact_leaf_distribution(): + # Hand-checkable anchor for the enumeration itself + np.testing.assert_allclose( + exact_leaf_distribution(), [0.50183, 0.42176, 0.07196, 0.00446], atol=1e-5 + ) + + +@pytest.mark.parametrize("descending", [False, True], ids=["ascending", "descending"]) +def test_mcmc_matches_exact_posterior(descending): + X = (X_VALUES[::-1] if descending else X_VALUES).reshape(-1, 1) + model = BARTModel() + model.sample( + X_train=X, + y_train=np.zeros(len(X_VALUES)), + num_gfr=0, + num_burnin=5000, + num_mcmc=100000, + general_params={ + "standardize": False, + "sample_sigma2_global": False, + "sigma2_init": 1.0, + "random_seed": 20260907, + }, + mean_forest_params={ + "num_trees": 1, + "alpha": ALPHA, + "beta": BETA, + "min_samples_leaf": 1, + "sample_sigma2_leaf": False, + "sigma2_leaf_init": 1.0, + }, + ) + forests = model.forest_container_mean + leaves = np.array([forests.num_forest_leaves(s) for s in range(forests.num_samples())]) + observed = np.array([np.mean(leaves == k) for k in range(1, len(X_VALUES) + 1)]) + + # With 100k draws the Monte Carlo standard error of each share is under 0.2 + # percentage points; the pre-fix sampler missed by 8-10 points (or never split). + np.testing.assert_allclose(observed, exact_leaf_distribution(), atol=0.01)