Skip to content

Commit 54ec3ad

Browse files
committed
add laplace section to vignette
1 parent 101810e commit 54ec3ad

1 file changed

Lines changed: 51 additions & 16 deletions

File tree

vignettes/cmdstanr.Rmd

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ stanfit <- rstan::read_stan_csv(fit$output_files())
326326

327327
CmdStanR also supports running Stan's optimization algorithms and its algorithms
328328
for variational approximation of full Bayesian inference. These are run via the
329-
`$optimize()` and `$variational()` methods, which are called in a similar way to
330-
the `$sample()` method demonstrated above.
329+
`$optimize()`, `$laplace()`, and `$variational()` methods, which are called in a
330+
similar way to the `$sample()` method demonstrated above.
331331

332332
### Optimization
333333

@@ -347,34 +347,69 @@ mcmc_hist(fit$draws("theta")) +
347347
vline_at(fit_mle$mle("theta"), size = 1.5)
348348
```
349349

350+
For optimization, by default the mode is calculated without the Jacobian
351+
adjustment for constrained variables, which shifts the mode due to the change of
352+
variables. To include the Jacobian adjustment and obtain a maximum a posteriori
353+
(MAP) estimate set `jacobian=TRUE`. See the
354+
[Maximum Likelihood Estimation](https://mc-stan.org/docs/cmdstan-guide/maximum-likelihood-estimation.html)
355+
section of the CmdStan User's Guide for more details.
356+
357+
```{r optimize-map}
358+
fit_map <- mod$optimize(data = data_list, jacobian = TRUE, seed = 123)
359+
```
360+
361+
### Laplace Approximation
362+
363+
The [`$laplace()`](https://mc-stan.org/cmdstanr/reference/model-method-laplace.html)
364+
produces a sample from a normal approximation centered at the mode of a
365+
distribution in the unconstrained space. If the mode is a MAP estimate, the
366+
samples provide an estimate of the mean and standard deviation of the posterior
367+
distribution. If the mode is the MLE, the sample provides an estimate of the
368+
standard error of the likelihood. Whether the mode is the MAP or MLE depends on
369+
the value of the `jacobian` argument when running optimization. See the
370+
[Laplace Sampling](https://mc-stan.org/docs/cmdstan-guide/laplace-sampling.html)
371+
chapter of the CmdStan User's Guide for more details.
372+
373+
Here we pass in the `fit_map` object from above as the `mode` argument. If
374+
`mode` is omitted then optimization will be run internally before taking draws
375+
from the normal approximation.
376+
377+
```{r laplace}
378+
fit_laplace <- mod$laplace(mode = fit_map, draws = 4000, data = data_list, seed = 123)
379+
fit_laplace$summary("theta")
380+
mcmc_hist(fit_laplace$draws("theta"), binwidth = 0.025)
381+
```
382+
350383
### Variational Bayes
351384

352385
We can run Stan's experimental variational Bayes algorithm (ADVI) using the
353386
[`$variational()`](https://mc-stan.org/cmdstanr/reference/model-method-variational.html)
354387
method.
355388

356389
```{r variational}
357-
fit_vb <- mod$variational(data = data_list, seed = 123, output_samples = 4000)
390+
fit_vb <- mod$variational(data = data_list, draws = 4000, seed = 123)
358391
fit_vb$summary("theta")
359392
```
360393

361-
The `$draws()` method can be used to access the approximate posterior draws.
362-
Let's extract the draws, make the same plot we made after MCMC, and compare the
363-
two. In this trivial example the distributions look quite similar, although
364-
the variational approximation slightly underestimates the posterior
365-
standard deviation.
366-
367-
```{r plot-variational-1, message = FALSE, fig.cap="Posterior from MCMC"}
368-
mcmc_hist(fit$draws("theta"), binwidth = 0.025)
369-
```
370-
```{r plot-variational-2, message = FALSE, fig.cap="Posterior from variational"}
371-
mcmc_hist(fit_vb$draws("theta"), binwidth = 0.025)
394+
Let's extract the draws, make the same plot we made after MCMC and Laplace
395+
approximation, and compare them all. In this simple example the distributions
396+
are quite similar, but this will not always be the case.
397+
398+
```{r plot-compare, message = FALSE, fig.width = 8, fig.cap="Comparing draws from the different algorithms"}
399+
bayesplot_grid(
400+
mcmc_hist(fit$draws("theta"), binwidth = 0.025),
401+
mcmc_hist(fit_laplace$draws("theta"), binwidth = 0.025),
402+
mcmc_hist(fit_vb$draws("theta"), binwidth = 0.025),
403+
xlim = c(0, 1),
404+
subtitles = c("MCMC", "Laplace", "Variational")
405+
)
372406
```
373407

374-
For more details on the `$optimize()` and `$variational()` methods, follow
375-
these links to their documentation pages.
408+
For more details on the `$optimize()`, `$laplace()` and `$variational()`
409+
methods, follow these links to their documentation pages.
376410

377411
* [`$optimize()`](https://mc-stan.org/cmdstanr/reference/model-method-optimize.html)
412+
* [`$laplace()`](https://mc-stan.org/cmdstanr/reference/model-method-laplace.html)
378413
* [`$variational()`](https://mc-stan.org/cmdstanr/reference/model-method-variational.html)
379414

380415

0 commit comments

Comments
 (0)