Skip to content

Read no warmup draws from fixed_param CSV files - #1343

Merged
jgabry merged 4 commits into
v1.0from
fixed-param-warmup
Nov 17, 2026
Merged

jgabry merged 4 commits into
v1.0from
fixed-param-warmup

Conversation

@avehtari

Copy link
Copy Markdown
Member

Submission Checklist

  • Run unit tests
  • [ x Declare copyright holder and agree to license (see below)

Summary

The CmdStan fixed_param sampler writes no warmup draws, even when save_warmup = true. read_cmdstan_csv() did not handle this case. For a CSV file with algorithm = fixed_param and save_warmup = true, it took the first ceiling(iter_warmup / thin) rows as warmup draws. The post-warmup draws then did not include these rows. If the file had no more rows than this number, the post-warmup draws were NULL. as_cmdstan_fit() gave the same wrong draws.

cmdstanr sets save_warmup = FALSE for fixed_param, so fits run by cmdstanr do not have this problem. CmdStan accepts fixed_param with save_warmup=1. With CmdStan 2.40.0, num_warmup=10 num_samples=5 save_warmup=1 algorithm=fixed_param writes 5 rows. The CSV header has save_warmup = true and algorithm = fixed_param. In the Stan services code, stan::services::sample::fixed_param() has no warmup argument and calls generate_transitions() only for the sampling iterations.

For a model without parameters and the default algorithm, the CSV header has algorithm = hmc. CmdStan 2.40.0 then writes the warmup rows: a cmdstanr run with save_warmup = TRUE, 20 warmup and 10 sampling iterations gave 30 rows. This PR does not change that case.

Changes

  • read_cmdstan_csv() reads no warmup draws when algorithm = fixed_param, and sets metadata$save_warmup to 0. Thus $draws(inc_warmup = TRUE) on a fit from as_cmdstan_fit() stops with an error, as it does for a fixed_param fit that cmdstanr ran.
  • The documentation of read_cmdstan_csv() states that it reads no warmup draws for fixed_param.

Tests

The new test in tests/testthat/test-csv.R runs fixed_param with cmdstanr and changes save_warmup = false to save_warmup = true in copies of the CSV files. Then it compares the output of read_cmdstan_csv() and as_cmdstan_fit() for these copies with the original fit.

I ran the tests at commit 9db49d7e on Triton (Linux) with R 4.5.3, CmdStan 2.40.0-rc0 (it reports version 2.39.0) and testthat 3.3.2:

file expectations failed skipped
test-csv.R 521 0 2 (WSL only)
test-fit-mcmc.R 144 0 0
test-model-sample.R 99 0 1

I also ran test-csv.R with R/csv.R from v1.0 and the new test, on my Linux workstation with R 4.6.2 and CmdStan 2.40.0. Only the new test fails:

  • metadata$save_warmup is 1.
  • The warmup draws are not NULL.
  • The post-warmup draws are NULL in read_cmdstan_csv() (the test has 30 warmup iterations and 20 rows).
  • $draws() on the fit from as_cmdstan_fit() gives an error.

I did not edit NEWS.md. Please add an item if you want one.

Not changed

  • The error message of $draws(inc_warmup = TRUE) says "Please rerun the model with save_warmup = TRUE". For fixed_param, a rerun with save_warmup = TRUE gives no warmup draws. This PR does not change the message.
  • check_csv_metadata_matches() still compares iter_warmup when save_warmup = 1. Two fixed_param CSV files with save_warmup = true and a different num_warmup (iter_warmup in the metadata) have the same number of rows. But they give the error "do not match in the number of output samples". Only CSV files that cmdstanr did not write have this case, and I did not change it.

Copyright and Licensing

Please list the copyright holder for the work you are submitting
(this will be you or your assignee, such as a university or company):
Aalto University

By submitting this pull request, the copyright holder is agreeing to
license the submitted work under the following licenses:

This PR was assisted by Claude

@avehtari avehtari added the bug Something isn't working label Nov 17, 2026
@avehtari
avehtari changed the base branch from master to v1.0 November 17, 2026 12:02
@codecov-commenter

codecov-commenter commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (a3a8e50) to head (283409f).
⚠️ Report is 3 commits behind head on v1.0.

Additional details and impacted files
@@            Coverage Diff             @@
##             v1.0    #1343      +/-   ##
==========================================
- Coverage   94.00%   93.16%   -0.85%     
==========================================
  Files          19       19              
  Lines        9232     7346    -1886     
==========================================
- Hits         8679     6844    -1835     
+ Misses        553      502      -51     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jgabry
jgabry merged commit 84cb88e into v1.0 Nov 17, 2026
18 of 19 checks passed
@jgabry
jgabry deleted the fixed-param-warmup branch November 17, 2026 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants