Skip to content

Report the Monte Carlo SE of the mean from the draws themselves - #116

Merged
choxos merged 1 commit into
mainfrom
cmdstanr-mcse-of-the-mean
Sep 23, 2026
Merged

choxos merged 1 commit into
mainfrom
cmdstanr-mcse-of-the-mean

Conversation

@choxos

@choxos choxos commented Sep 23, 2026 •

Copy link
Copy Markdown
Owner

What was wrong

With the cmdstanr engine, fit$summary$se_mean was computed as sd / sqrt(posterior::ess_bulk()). Bulk ESS is computed on rank-normalized draws, so it describes the autocorrelation of the ranks rather than of the draws whose mean is reported. The two differ whenever the draws are skewed, as an exponentiated effect such as a risk or rate ratio is.

For four stationary chains of exp(Z), with Z an AR(1) process with autocorrelation 0.75, the exact Monte Carlo SE of the mean and the old column compare as follows:

Draws per chain Old se_mean posterior::mcse_mean() Exact
2,000 0.0693 0.0606 0.0561
100,000 0.00903 0.00795 0.00795

The old value is about 13.8% too large in the limit, and more draws do not remove it.

What changes

  • se_mean comes from posterior::mcse_mean(), the mean-specific MCSE. The bulk and tail ESS columns are unchanged.
  • NEWS fix entry under "Fitting, diagnostics and model comparison"; the same line shipped in v0.1.0.
  • Nothing in the package prints or uses this column, so no reported estimate or interval changes; it reaches a user reading fit$summary directly. The rstan engine is unaffected.

Checks

  • The cmdstanr end-to-end test in test-engine.R now asserts se_mean against posterior::mcse_mean() for mu_index and rr_index. It fails against main and passes here.
  • Pure-R suite: 0 failures, 0 errors, 91 skips, 2,829 passes. lintr: no lints in the changed files.

Independent of #115; either can merge first. Version stays 0.1.0.9000.

Summary by CodeRabbit

  • Bug Fixes
    • CmdStanR fit summaries now calculate the standard error of the mean using Monte Carlo standard error, improving consistency with posterior draws. Other summary statistics are unchanged.

With the cmdstanr engine `fit$summary$se_mean` was the posterior SD over
the square root of `posterior::ess_bulk()`. Bulk ESS is computed on
rank-normalized draws, so it measures autocorrelation of the ranks, not of
the draws whose mean is being reported, and the two differ whenever the
draws are skewed. For a lognormal AR(1) chain with autocorrelation 0.75, as
an exponentiated effect such as a risk ratio behaves, the old value is
13.8% too large, and more draws do not remove it.

`se_mean` now comes from `posterior::mcse_mean()`, which uses the ESS of the
draws themselves. The bulk and tail ESS columns are unchanged. Nothing in
the package prints or uses this column; it reaches a user who reads
`fit$summary` directly. The rstan engine is unaffected.

Checks: the cmdstanr end-to-end test now asserts `se_mean` against
`posterior::mcse_mean()` for `mu_index` and `rr_index`; it fails against
main and passes here. Pure-R suite: 0 failures, 0 errors, 91 skips, 2,829
passes. Version stays 0.1.0.9000.
Copilot AI lite review requested due to automatic review settings September 23, 2026 02:42
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: choxos/mlumr/.coderabbit.yaml

Review profile: CHILL

Plan: Essentials

Run ID: 19da1b25-2691-4930-a38a-648e746897ca

📥 Commits

Reviewing files that changed from the base of the PR and between 977eeb4 and b38dba2.

📒 Files selected for processing (3)
  • NEWS.md
  • R/backend_cmdstanr.R
  • tests/testthat/test-engine.R

Included review availability: 4 reviews are currently available. Your included PR review attempts over the past 7 days set your current allowance at 5 reviews per hour.


📝 Walkthrough

Walkthrough

The CmdStanR summary now calculates se_mean with posterior::mcse_mean(). An end-to-end test checks the values for mu_index and rr_index, and the change is recorded in NEWS.md.

Changes

CmdStanR se_mean calculation

Layer / File(s) Summary
Calculate and verify se_mean
R/backend_cmdstanr.R, tests/testthat/test-engine.R, NEWS.md
The CmdStanR backend uses posterior::mcse_mean() instead of posterior SD divided by the square root of bulk ESS. The end-to-end test checks the results for mu_index and rr_index. NEWS.md records the change.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to b38db

The CmdStanR summary change is documented and covered by the targeted end-to-end assertions. No actionable merge risk remains after normal checks.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: reporting the mean’s Monte Carlo standard error from the draws themselves.
Description check ✅ Passed The description explains the problem, the change, its user-facing impact, and verification results. It does not use the template’s Type of Change or Checklist sections, and it does not list verificati…
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

@choxos
choxos merged commit 965dfc5 into main Sep 23, 2026
6 checks passed
@choxos
choxos deleted the cmdstanr-mcse-of-the-mean branch September 23, 2026 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants