Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

MCMC with INLA: a parameter outside the latent Gaussian model

inla() needs the model to be latent Gaussian: given the hyperparameters θ\theta, every effect enters the linear predictor linearly with a Gaussian prior. Many models are latent Gaussian only conditionally on a few extra parameters ϕ\phi, such as a distance-decay length, a change point or a spatial-lag coefficient.

Gómez-Rubio & Rue (2018) combine the two: run MCMC over ϕ\phi (here jointly with θ\theta), and let INLA integrate out the latent field. For fixed ϕ\phi the LGM gives the Laplace approximation of log⁡π(y∣θ,ϕ)\log\pi(y\mid\theta,\phi), with exact gradients. So lgm_laplace_factor can put it in a NumPyro model, and NUTS samples π(ϕ,θ∣y)\pi(\phi,\theta\mid y) while the latent field never appears in the chain.

This notebook fits a Poisson model with a non-linear distance effect:

yi∼Poisson(eηi),ηi=β0+β1e−di/ϕ+ug(i),ug∼N(0,τ−1).y_i \sim \mathrm{Poisson}(e^{\eta_i}),\qquad \eta_i = \beta_0 + \beta_1 e^{-d_i/\phi} + u_{g(i)},\qquad u_g \sim \mathcal N(0, \tau^{-1}).

Given the decay length ϕ\phi, the covariate zi=e−di/ϕz_i = e^{-d_i/\phi} is fixed and the model is an LGM. Two routes, which should agree:

  1. INLA over a grid of ϕ\phi, weighting each fit by its marginal likelihood (Bayesian model averaging, BMA).

  2. NUTS over (ϕ,θ)(\phi, \theta), with the Laplace evidence as a factor (lgm_laplace_factor).

Data

600 counts in 20 groups. Each observation sits at a distance di∈[0,6]d_i \in [0, 6] from a source whose effect decays with length ϕ=2\phi = 2.

Route 1: INLA on a grid of ϕ\phi

Each fit returns log⁡π~(y∣ϕ)\log\tilde\pi(y\mid\phi). With a log-normal prior on ϕ\phi, the posterior on the grid is proportional to π~(y∣ϕ) π(ϕ)\tilde\pi(y\mid\phi)\,\pi(\phi), and averaging the fits with those weights gives every other marginal.

33 INLA fits in 73.4 s
E[phi | y] by INLA + BMA: 2.784

Route 2: NUTS over (ϕ,θ)(\phi, \theta) with the Laplace evidence

lgm_laplace_factor adds log⁡π~(y∣θ,ϕ)\log\tilde\pi(y\mid\theta,\phi) to the model, with the latent field integrated out of it. It holds no hyperprior, so τ\tau is a site of the model under whatever prior we like; here it is the default of IID, so that the two routes fit the same model. The projector is rebuilt inside the model, because its zz column depends on ϕ\phi; its pattern does not, so gaussx reuses one symbolic Cholesky.

NUTS in 27.9 s

                mean       std    median      5.0%     95.0%     n_eff     r_hat
     g.tau      6.98      2.32      6.73      3.62     10.59    827.09      1.00
       phi      2.77      0.64      2.66      1.82      3.73    426.19      1.00

Number of divergences: 0

The chain holds τ\tau itself, beside ϕ\phi.

E[phi | y] by NUTS:       2.774
E[tau | y] by NUTS:       6.981
E[tau | y] by INLA + BMA: 7.046

The two routes agree

Left: the posterior of ϕ\phi, from NUTS (histogram) and from the INLA grid (curve, normalised as a density in ϕ\phi). Right: the slope β1\beta_1 of the distance effect from the BMA over the grid. Each fit’s Gaussian marginal is weighted by π(ϕ∣y)\pi(\phi\mid y). A longer decay with a larger slope fits these data almost as well, so the two posteriors lean the same way, and the mixture over ϕ\phi makes β1\beta_1’s marginal skewed where any single fit’s is Gaussian.

<Figure size 1000x350 with 2 Axes>

When to use which

  • The grid is cheap and deterministic for one or two extra parameters, and it gives the marginal likelihood of each.

  • NUTS scales to several extra parameters, where a grid cannot, and samples ϕ\phi jointly with θ\theta. Each step runs a Laplace fit, so a chain costs about as many fits as it takes leapfrog steps.

For latent effects under NUTS, condition the prior at a thinned set of draws (model.prior.condition_nongauss(theta, model.likelihood, data_at(phi))) and mix the Gaussian marginals, as the grid route does with its weights.

Reference. Gómez-Rubio, V. & Rue, H. (2018). Markov chain Monte Carlo with the integrated nested Laplace approximation. Statistics and Computing 28, 1033–1051.