Extract log probability (target) — fit-method-lp • cmdstanr
Description: The $lp() method extracts lp__, the total log probability (target) accumulated in the model block of the Stan program. For variational inference the log density of the variational approximation to the posterior is also available via the $lp_approx() method. See the Log Probability Increment vs. Sampling Statement section of the Stan Reference Manual for details on when normalizing constants are dropped from log probability calculations.

# Extract log probability (target)

Source: `R/fit.R`
`fit-method-lp.Rd`

The `$lp()` method extracts `lp__`, the total log probability (`target`) accumulated in the model block of the Stan program. For variational inference the log density of the variational approximation to the posterior is also available via the `$lp_approx()` method.

See the Log Probability Increment vs. Sampling Statement section of the Stan Reference Manual for details on when normalizing constants are dropped from log probability calculations.

    lp()

    lp_approx()

## Value

A numeric vector with length equal to the number of (post-warmup) draws for MCMC and variational inference, and length equal to `1` for optimization.

## Details

`lp__` is the unnormalized log density on Stan's unconstrained space. This will in general be different than the unnormalized model log density evaluated at a posterior draw (which is on the constrained space). `lp__` is intended to diagnose sampling efficiency and evaluate approximations.

`lp_approx__` is the log density of the variational approximation to `lp__` (also on the unconstrained space). It is exposed in the variational method for performing the checks described in Yao et al. (2018) and implemented in the loo package.

## References

Yao, Y., Vehtari, A., Simpson, D., and Gelman, A. (2018). Yes, but did it work?: Evaluating variational inference. Proceedings of the 35th International Conference on Machine Learning, PMLR 80:5581–5590.

## See also

`CmdStanMCMC`, `CmdStanMLE`, `CmdStanVB`

## Examples

    # \dontrun{
    fit_mcmc <- cmdstanr_example("logistic")
    head(fit_mcmc$lp())
    #> [1] -66.8230 -66.2198 -66.0794 -65.9100 -66.1692 -66.3888

    fit_mle <- cmdstanr_example("logistic", method = "optimize")
    fit_mle$lp()
    #> [1] -63.9218

    fit_vb <- cmdstanr_example("logistic", method = "variational")
    plot(fit_vb$lp(), fit_vb$lp_approx())

    # }