-
-
Notifications
You must be signed in to change notification settings - Fork 118
update manuals for Laplace approximation, optimization allows Jacobian adjustment #606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
64d5373
Merge branch 'master' of https://github.com/stan-dev/docs
mitzimorris dd3cfab
Merge branch 'master' of https://github.com/stan-dev/docs
mitzimorris b1e9bfe
Merge branch 'master' of https://github.com/stan-dev/docs
mitzimorris b8bfdc8
Merge branch 'master' of https://github.com/stan-dev/docs
mitzimorris 5c75386
checkpointing
mitzimorris 8728831
fix stale links, cleanup
mitzimorris 6ded81c
laplace algorithm for ref manual
mitzimorris cf46e48
changes per code review
mitzimorris 560a49a
changes per code review
mitzimorris 1e923e4
changes per code review
mitzimorris 2e3bdff
cleanup
mitzimorris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Laplace sampling | ||
|
||
The `laplace` method produces a sample from a normal approximation | ||
centered at the mode of a distribution in the unconstrained space. | ||
If the mode is a maximum a posteriori (MAP) estimate, | ||
the samples provide an estimate of the mean and standard deviation | ||
of the posterior distribution. | ||
If the mode is a maximum likelihood estimate (MLE), | ||
the sample provides an estimate of the standard error of the likelihood. | ||
In general, the posterior mode in the unconstrained space doesn't correspond | ||
to the mean (nor mode) in the constrained space, and thus the sample is needed | ||
to infer the mean as well as the standard deviation. | ||
(See [this case study](https://avehtari.github.io/casestudies/Jacobian/jacobian.html) | ||
for a visual illustration.) | ||
|
||
This is computationally inexpensive compared to exact Bayesian inference with MCMC. | ||
The goodness of this estimate depends on both the estimate of the mode | ||
and how much the true posterior in the unconstrained space resembles a Gaussian. | ||
|
||
|
||
## Configuration | ||
|
||
This method takes 2 arguments: | ||
|
||
- `jacobian` - Whether or not the | ||
[Jacobian adjustment](https://mc-stan.org/docs/stan-users-guide/changes-of-variables.html#changes-of-variables) | ||
should be included in the gradient. The default value is 1 (include adjustment). | ||
(Note: in optimization, the default value is `0`, for historical reasons.) | ||
|
||
- `mode` - Input file of parameters values on the constrained scale. | ||
When Stan's `optimize` method is used to estimate the modal values, | ||
the value of boolean argument `jacobian` should be 0 if `optimize` was | ||
run with default settings, i.e., the input is the MLE estimate; | ||
if `optimize` was run with argument `jacobian=1`, then the `laplace` | ||
method default setting, `jacobian=1`, should be used. | ||
|
||
|
||
|
||
## CSV output | ||
|
||
The output file consists of the following pieces of information: | ||
|
||
- The full set of configuration options available for the `log_prob` method is | ||
reported at the beginning of the output file as CSV comments. | ||
|
||
- Output columns for all model parameters on the constrained scale, | ||
followed by columns `log_p` and `log_q`, | ||
the unnormalized log density and the unnormalized density of the | ||
Laplace approximation, respectively. | ||
These can be used for diagnostics and importance sampling. | ||
|
||
## Example | ||
|
||
To get an approximate estimate of the mode and standard deviation of the | ||
example Bernoulli model given the example dataset: | ||
|
||
- find the MAP estimate by running optimization with argument `jacobian=1` | ||
|
||
- run the Laplace estimator using the MAP estimate as the `mode` argument. | ||
|
||
Because the default output file name from all methods is `output.csv`, | ||
a more informative name is used for the output of optimization. | ||
We run the commands from the CmdStan home directory. | ||
This results in a sample with mean 2.7 and standard deviation 0.12. | ||
In comparison, running the NUTS-HMC sampler results in mean 2.6 and standard deviation 0.12. | ||
|
||
|
||
``` | ||
./examples/bernoulli/bernoulli optimize jacobian=1 \ | ||
data file=examples/bernoulli/bernoulli.data.json \ | ||
output file=bernoulli_optimize_lbfgs.csv random seed=1234 | ||
|
||
|
||
./examples/bernoulli/bernoulli laplace mode=bernoulli_optimize_lbfgs.csv \ | ||
data file=examples/bernoulli/bernoulli.data.json random seed=1234 | ||
``` | ||
|
||
The header and first few data rows of the output sample are shown below. | ||
|
||
``` | ||
# stan_version_major = 2 | ||
# stan_version_minor = 31 | ||
# stan_version_patch = 0 | ||
# model = bernoulli_model | ||
# start_datetime = 2022-12-20 01:01:14 UTC | ||
# method = laplace | ||
# laplace | ||
# mode = bernoulli_lbfgs.csv | ||
# jacobian = 1 (Default) | ||
# draws = 1000 (Default) | ||
# id = 1 (Default) | ||
# data | ||
# file = examples/bernoulli/bernoulli.data.json | ||
# init = 2 (Default) | ||
# random | ||
# seed = 875960551 (Default) | ||
# output | ||
# file = output.csv (Default) | ||
# diagnostic_file = (Default) | ||
# refresh = 100 (Default) | ||
# sig_figs = -1 (Default) | ||
# profile_file = profile.csv (Default) | ||
# num_threads = 1 (Default) | ||
# stanc_version = stanc3 v2.31.0-7-g20444266 | ||
# stancflags = | ||
theta,log_p,log_q | ||
0.0498545,-9.4562,-2.33997 | ||
0.182898,-6.9144,-0.0117349 | ||
0.376428,-7.18171,-0.746034 | ||
... | ||
``` | ||
|
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
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
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.