Repository navigation
Conversation
CmdStan's laplace method writes the Hessian of the log density at the mode, on the unconstrained scale, to its diagnostic file as JSON. laplace_sample(save_diagnostics=True) now passes diagnostic_file= in the output argument group. CmdStanLaplace records the path as diagnostic_file, moves it in save_output_files, accepts it in from_files, and parses the matrix in the hessian property, which is None when the file was not saved. Closes stan-dev#868
atarutin
force-pushed
the
laplace-save-diagnostics
branch
from
October 9, 2026 18:16
24f4873 to
e32b237
Compare
atarutin
marked this pull request as ready for review
October 9, 2026 19:06
Collaborator
|
I know we do the same thing in |
Member
|
|
Collaborator
|
Oh yeah in that case, seems like the perfect time to get it all under diagnostics? We can deprecate/remove the latent_dynamics arg as part of the 2.0 release |
Member
|
I think that's a good call (and the perfect example of something I'm glad came up before 2.0) |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Submission Checklist
Summary
Closes #868.
CmdStan's
laplacemethod can write a diagnostic file holding the Hessian of the log density at the mode, on the unconstrained scale. In CmdStan 2.40.0 the file is JSON with the keyslp_mode,gradientandHessian. This PR implements the API agreed in the issue:CmdStanModel.laplace_sample(..., save_diagnostics: bool = False). WhenTrue,diagnostic_file=<output_dir>/<model>-<time>_diagnostic.jsonis added to theoutputargument group. It reuses the existingCmdStanArgs.save_latent_dynamics→RunSet.diagnostic_files→compose_command(diagnostic_file=...)path thatsampleuses.RunSetnow gives the diagnostic file a.jsonextension when the method is Laplace, and.csvotherwise, as before.CmdStanLaplace.diagnostic_file: str | Noneholds the raw path, likeCmdStanMCMC.diagnostic_files. It is included insave_output_files, accepted byCmdStanLaplace.from_files(diagnostic_file=...), and shown inrepr.CmdStanLaplace.hessian -> np.ndarray | Noneparses theHessiankey on first access. It isNonewhen the diagnostic file was not saved.Test:
test_laplace_hessianfitstest/data/linear_regression.stanwithjacobian=Falseandsig_figs=18. It asserts that the Hessian has shape (3, 3), one row per unconstrained parameter (alpha, beta, log sigma), that it is symmetric and negative definite, and that it matches the analytic Hessian of the normal log likelihood evaluated at the mode CmdStan used (rtol=1e-6, atol=1e-6). In five runs with different seeds the largest absolute difference was about 1e-8. The test also checks thatsave_output_filesmoves the diagnostic file and thatfrom_filesreads it back.test_laplace_outputsnow also asserts thatdiagnostic_fileandhessianareNoneby default.Points for review:
save_diagnostics, as agreed in the issue.samplecurrently calls the equivalent argumentsave_latent_dynamics, a name that describes HMC output. Internally this PR passessave_latent_dynamics=save_diagnosticstoCmdStanArgs. I can rename anything here if you prefer different names.lp_modeandgradientstay in the file, whichdiagnostic_filepoints to.from_output_filesdoes not discover the diagnostic file on its own. The config JSON'soutputsection is parsed with onlyfileandsig_figsas fields. I left discovery out to keep this PR small.What I ran. All runs were in a Linux container with Python 3.12.14 and CmdStan 2.40.0, on
developat edf9fe3:isort --check-only,black --check,flake8,pylint --rcfile=.pylintrc, andmypyoncmdstanpy test: all clean.pytest test/test_laplace.py test/test_laplace_output_files.py test/test_runset.py test/test_cmdstan_args.py: all pass.test_args_badpasses only as a non-root user; see below.pytest test, excludingtest_install_cmdstan.py,test_cxx_installation.py,test_model.pyandtest_compilation.py: 408 passed, 3 skipped, 4 failed. The 4 failures (test_args_bad,test_bernoulli_bad,test_save_csv,test_validate_dir) come from running as root, where read-only directories are writable. They fail the same way on unmodifieddevelopas root, and pass as a non-root user.developwithout the change,test_laplace_hessianand the extendedtest_laplace_outputsfail.AI disclosure, per the Stan AI Contribution Policy: this change was prepared with Claude Code (Anthropic).
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):
Aleksandr Tarutin
By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:
🤖 Generated with Claude Code