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
24f4873 to
e32b237
Compare
|
I know we do the same thing in |
|
|
|
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 |
|
I think that's a good call (and the perfect example of something I'm glad came up before 2.0) |
|
@atarutin is the change we discussed something you're comfortable adding to this PR? Otherwise happy to do it separately. |
|
Oh @WardBrian - I just noticed #260. Not sure if that alters the desire to change the naming? |
Interesting, the issue doesn't state anything about why one name would be preferred over the other, and it couldn't have anticipated the additional methods using the same |
you mean propagating |
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