From 1ad3ec435395aaf2ac0a89610b919c5291fe2b18 Mon Sep 17 00:00:00 2001 From: jgabry Date: Wed, 18 Nov 2026 14:47:09 -0700 Subject: [PATCH] Require posterior 1.8.0 and remove the Pareto weights clamp --- DESCRIPTION | 2 +- NEWS.md | 2 +- R/args.R | 4 ---- tests/testthat/test-fit-init.R | 38 ---------------------------------- 4 files changed, 2 insertions(+), 44 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c73a30d97..d26d36c53 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -54,7 +54,7 @@ Imports: data.table (>= 1.18.0), jsonlite (>= 1.8.7), methods, - posterior (>= 1.5.0), + posterior (>= 1.8.0), processx (>= 3.5.0), R6 (>= 2.4.0), rlang (>= 1.0.0), diff --git a/NEWS.md b/NEWS.md index 51bdaf5f9..9a8c9c1c0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -155,7 +155,7 @@ value. `init = fit` now includes tuple parameters. `write_stan_json()` gains a option also turns it on when another package such as brms calls CmdStanR for you. The feature is still experimental and may change in the future. (#1138) * Reading MCMC and generated quantities draws from CmdStan CSV files now uses -less memory. (#1041, #1307) +less memory. CmdStanR now requires posterior 1.8.0 or later. (#1041, #1307) * `as_cmdstan_fit()` now reads draws from the CSV files when they're first requested (like fits from `$sample()` do), so looking at a few variables of a large fit takes much less memory. (#1294) diff --git a/R/args.R b/R/args.R index 98a097e4b..b17e29cbc 100644 --- a/R/args.R +++ b/R/args.R @@ -1610,10 +1610,6 @@ process_init_approx <- function( return_k = FALSE, verbose = FALSE ) - # posterior <= 1.7.1 can return weights of about -2e-16 when many weights - # underflow to 0. Remove this once the minimum posterior version includes - # stan-dev/posterior#468. - weights <- pmax(weights, 0) } if (num_candidates < nrow(draws_df)) { # Collapse duplicate candidates to one row, summing their weights so a vector diff --git a/tests/testthat/test-fit-init.R b/tests/testthat/test-fit-init.R index 8e85e9553..7d3993814 100644 --- a/tests/testthat/test-fit-init.R +++ b/tests/testthat/test-fit-init.R @@ -252,44 +252,6 @@ test_that("Pathfinder fit initializations use the intended weights", { expect_equal(pareto_smooth_calls, 2L) }) -test_that("Pathfinder init weights are not negative after Pareto smoothing", { - # Most log weights are far below the maximum, so exp() underflows to 0 for - # them. posterior::pareto_smooth() can then return weights of about -2e-16 - # (posterior <= 1.7.1), which resample_draws() rejects. The mock gives those - # weights whatever the installed posterior version. Remove this test with - # the clamp once the minimum posterior version includes - # stan-dev/posterior#468. - lp <- c(-1000 - seq_len(350), -seq(0, 60, length.out = 50)) - pathfinder_fit <- structure( - list( - draws = function(format) { - posterior::as_draws_df(data.frame( - theta = seq_along(lp), - lp__ = lp, - lp_approx__ = 0 - )) - }, - metadata = function() { - list( - num_paths = 10, - psis_resample = FALSE, - calculate_lp = TRUE - ) - }, - return_codes = function() 0 - ), - class = "CmdStanPathfinder" - ) - local_mocked_bindings( - pareto_smooth = function(x, ...) x - 1e-16, - .package = "posterior" - ) - local_mocked_bindings(process_init = function(init, ...) init) - - inits <- process_init.CmdStanPathfinder(pathfinder_fit, 4) - expect_equal(posterior::ndraws(inits), 4) -}) - test_that("Pathfinder init candidates are distinct target parameter vectors", { draws <- posterior::as_draws_df(data.frame( theta = c(1, 1, 2, 3),