-
Notifications
You must be signed in to change notification settings - Fork 26
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
Add Laplace approximation as algorithm #16
Open
bbbales2
wants to merge
8
commits into
stan-dev:master
Choose a base branch
from
bbbales2:feature/hessians_optimize_cmdstan
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cfd727b
Added proposal for adding Hessian of log density in optimizing output…
bbbales2 327710b
Fixed some sign stuff, specified where normal approximation comes from
bbbales2 2e29c3e
Updated design doc to reflect prototype implementation and pull reque…
bbbales2 39eaa82
Changed laplace_diag_shift to laplace_add_diag and added more info ab…
bbbales2 eb41985
Merge remote-tracking branch 'origin/master' into feature/hessians_op…
bbbales2 cf45304
Rewrote Laplace approximation design doc as new algorithm + fixed the…
bbbales2 914a47f
Minor clarifications (design-doc #16)
bbbales2 98b5395
Minor updates and rename of laplace approximation algorithm (design-d…
bbbales2 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 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 |
---|---|---|
|
@@ -13,7 +13,16 @@ When computing a MAP estimate, the Hessian of the log density can be used to con | |
|
||
I have a Stan model that is too slow to practically sample all the time. Because optimization seem to give reasonable results, it would be nice to have the normal approximation to the posterior to give some sense of the uncertainty in the problem as well. | ||
|
||
An approximate posterior covariance comes from computing the inverse of the Hessian of the negative log density. | ||
It is standard to compute a normal approximation to the posterior covariance comes from the inverse of the Hessian of the negative log density. | ||
|
||
If the MAP estimate is ```u```, and the Hessian of the unnormalized log density at u is ```H```, then a posterior draw on the constrained scale is: | ||
|
||
``` | ||
unconstrained_sample = multivariate_normal_rng(mean = u, cov = -inverse(H)) | ||
constrained_sample = constrain(unconstrained_sample) | ||
``` | ||
|
||
We can output unnormalized log densities of the actual model and the approximate model to compute importance sampling diagnostics and estimates. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can output unnormalized log densities in the unconstrained space of the true posterior and the approximate posterior |
||
|
||
Rstan already supports this via the 'hessian' argument to 'optimizing'. | ||
|
||
|
@@ -24,7 +33,7 @@ This adds two arguments to the cmdstan interface. | |
|
||
```laplace_draws``` - The number of draws to take from the posterior approximation. By default, this is zero, and no laplace approximation is done | ||
|
||
```laplace_diag_shift``` - A value to add to the diagonal of the hessian approximation to fix small non-singularities (defaulting to zero) | ||
```laplace_add_diag``` - A value to add to the diagonal of the hessian approximation to fix small non-singularities (defaulting to zero) | ||
|
||
The output is printed after the optimimum. | ||
|
||
|
@@ -33,9 +42,9 @@ A model can be called by: | |
./model optimize laplace_draws=100 data file=data.dat | ||
``` | ||
|
||
or with the diagonal shift: | ||
or with the diagonal: | ||
``` | ||
./model optimize laplace_draws=100 laplace_diag_shift=1e-10 data file=data.dat | ||
./model optimize laplace_draws=100 laplace_add_diag=1e-10 data file=data.dat | ||
``` | ||
|
||
Optimizing output currently looks like: | ||
|
@@ -56,22 +65,26 @@ The new output would look like: | |
lp__,b.1,b.2 | ||
3427.64,7.66366,5.33466 | ||
# Draws from Laplace approximation: | ||
b.1,b.2 | ||
7.66364,5.33463 | ||
7.66367,5.33462 | ||
lp__, log_p, log_g, b.1,b.2 | ||
0, -1, -2, 7.66364,5.33463 | ||
0, -2, -3, 7.66367,5.33462 | ||
``` | ||
|
||
The lp__, log_p, log_g formatting is intended to mirror the advi output. | ||
|
||
# Reference-level explanation | ||
[reference-level-explanation]: #reference-level-explanation | ||
|
||
As far as computing the Hessian, because the higher order autodiff doesn't work with the ODE solvers 1D integrator and such, I think we should compute the Hessian with finite differences, and we use the sample finite difference implementation that the test framework does (https://github.com/stan-dev/math/blob/develop/stan/math/prim/functor/finite_diff_hessian_auto.hpp) | ||
|
||
Ben Goodrich points out it would be better to get this Hessian using finite differences of first order gradients. He is correct, but the fully finite differenced hessian is what is implemented in Stan math currently and so that is what I'm rolling with. | ||
|
||
# Drawbacks | ||
[drawbacks]: #drawbacks | ||
|
||
Providing draws instead of the Laplace approximation itself is rather inefficient, but it is the easiest thing to code. | ||
|
||
We also have to deal with possible singular Hessians. This is why I also added the laplace_diag_shift to overcome these. They'll probably be quite common, especially with the Hessians computed with finite differences. | ||
We also have to deal with possible singular Hessians. This is why I also added the laplace_add_diag to overcome these. They'll probably be quite common, especially with the Hessians computed with finite differences. | ||
|
||
# Rationale and alternatives | ||
[rationale-and-alternatives]: #rationale-and-alternatives | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove "comes" (I would remove it myself, but I don't have edit rights. It would be useful to have edit rights for design-docs repo)