From 8f528a950547f089b100a8abe06432debda045d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1chym=20Barv=C3=ADnek?= Date: Sat, 17 Oct 2026 20:13:04 +0200 Subject: [PATCH] Adapt the metric when the warmup is too short for the adaptation windows When init_buffer + window + term_buffer exceeds num_warmup, set_window_params reduced the stages to 15%/75%/10% but returned without restart(), so adapt_next_window_ kept the constructor's value (0 + 0 - 1, i.e. UINT_MAX). The adaptation window never ended and the diag_e / dense_e metric was never updated, although the warning says the reduced windows are used. Call restart() in that branch as in the other. --- src/stan/mcmc/windowed_adaptation.hpp | 1 + src/test/unit/mcmc/var_adaptation_test.cpp | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/stan/mcmc/windowed_adaptation.hpp b/src/stan/mcmc/windowed_adaptation.hpp index 36bf1d1d986..2093138cf4f 100644 --- a/src/stan/mcmc/windowed_adaptation.hpp +++ b/src/stan/mcmc/windowed_adaptation.hpp @@ -67,6 +67,7 @@ class windowed_adaptation : public base_adaptation { logger.info(term_buffer_msg); logger.info(""); + restart(); return; } diff --git a/src/test/unit/mcmc/var_adaptation_test.cpp b/src/test/unit/mcmc/var_adaptation_test.cpp index 708f5b2e7f8..130597afa83 100644 --- a/src/test/unit/mcmc/var_adaptation_test.cpp +++ b/src/test/unit/mcmc/var_adaptation_test.cpp @@ -25,3 +25,25 @@ TEST(McmcVarAdaptation, learn_variance) { EXPECT_EQ(0, logger.call_count()); } + +TEST(McmcVarAdaptation, learn_variance_reduced_windows) { + stan::test::unit::instrumented_logger logger; + + const int n = 10; + Eigen::VectorXd q = Eigen::VectorXd::Zero(n); + Eigen::VectorXd var(Eigen::VectorXd::Zero(n)); + + // init_buffer 75, term_buffer 50 and window 25 do not fit into 100 warmup + // iterations and are reduced to 15 / 10 / 75 + stan::mcmc::var_adaptation adapter(n); + adapter.set_window_params(100, 75, 50, 25, logger); + + for (int i = 0; i < 100; ++i) { + adapter.learn_variance(var, q); + } + + // Estimated once, from the 75 draws of the reduced window + for (int i = 0; i < n; ++i) { + EXPECT_DOUBLE_EQ(1e-3 * 5.0 / (75 + 5.0), var(i)); + } +}