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.

Bayesian Linear Regression with Precision Parameterization

This notebook demonstrates gaussx.MultivariateNormalPrecision in a NumPyro model for Bayesian linear regression. The precision parameterization is natural when:

  • The prior is specified via a precision matrix (e.g. ridge regularization, Gaussian Markov random fields)
  • You want to perform conjugate updates by adding precision matrices directly
  • The posterior precision has structure you want to preserve (e.g. banded, sparse, Kronecker)

What you’ll learn:

  1. Bayesian linear regression with a precision-parameterized prior
  2. Analytic conjugate posterior in precision form
  3. Using MultivariateNormalPrecision in a NumPyro model with MCMC
  4. Comparing MCMC posterior against the analytic solution
  5. Posterior predictive with uncertainty quantification

Setup

Background

In Bayesian linear regression with known noise variance σ2\sigma^2:

y=Xw+ϵ,ϵ∼N(0,σ2I)y = X w + \epsilon, \quad \epsilon \sim \mathcal{N}(0, \sigma^2 I)

With a Gaussian prior w∼N(0,Λ0−1)w \sim \mathcal{N}(0, \Lambda_0^{-1}) specified via precision Λ0\Lambda_0, the posterior is:

w∣y∼N(μpost,Λpost−1)w \mid y \sim \mathcal{N}(\mu_{\text{post}}, \Lambda_{\text{post}}^{-1})

where:

Λpost=Λ0+1σ2X⊤X\Lambda_{\text{post}} = \Lambda_0 + \tfrac{1}{\sigma^2} X^\top X
μpost=Λpost−1(1σ2X⊤y)\mu_{\text{post}} = \Lambda_{\text{post}}^{-1} \bigl(\tfrac{1}{\sigma^2} X^\top y\bigr)

The posterior precision is the sum of prior precision and data precision -- this additivity is why precision parameterization is natural for conjugate updates.

The precision form arises naturally in Gaussian Markov random fields (GMRFs) where the precision matrix is sparse (typically banded or block-tridiagonal), encoding conditional independence structure. In spatial statistics, the SPDE approach (Lindgren et al., 2011) constructs a GMRF approximation to a Matern GP, where the precision is a sparse matrix defined by the finite element mesh. gaussx’s MultivariateNormalPrecision can work with structured precision operators (banded, block-diagonal, Kronecker) for efficient inference in these models.

Generate data

We create a polynomial regression problem with known true weights.

Design matrix shape: (50, 6)
True weights: [ 0.5 -1.2  0.3  0.8 -0.4  0.1]

Analytic posterior (conjugate)

We compute the exact posterior using the conjugate precision-form update.

Posterior precision (diagonal): [  201.           254.39197138   549.16875     1462.54311184
  4294.27361813 13305.72205558]
Posterior mean: [ 0.59125703 -1.0474102   0.17119962  0.55799098 -0.36153802  0.16429194]
True weights:   [ 0.5 -1.2  0.3  0.8 -0.4  0.1]

NumPyro model with precision prior

We define the model using gaussx.MultivariateNormalPrecision for the prior on weights. This directly encodes the ridge regularization as a precision matrix.

Run NUTS

NumPyro’s NUTS sampler works directly with MultivariateNormalPrecision — the log-prob and its gradients are computed via gaussx structural dispatch.


                mean       std    median      5.0%     95.0%     n_eff     r_hat
      w[0]      0.59      0.17      0.59      0.28      0.85    272.51      1.01
      w[1]     -1.04      0.26     -1.04     -1.46     -0.63    217.18      1.03
      w[2]      0.18      0.30      0.17     -0.36      0.63    230.31      1.01
      w[3]      0.55      0.31      0.56      0.04      1.02    195.42      1.03
      w[4]     -0.37      0.09     -0.36     -0.52     -0.23    238.59      1.01
      w[5]      0.16      0.08      0.16      0.04      0.29    204.08      1.03

Number of divergences: 0
MCMC weight samples shape: (1000, 6)

Compare MCMC vs analytic posterior

The MCMC samples should match the analytic conjugate posterior. We compare marginal distributions for each weight.

<Figure size 1400x700 with 6 Axes>

Posterior predictive

We compute the predictive distribution by pushing MCMC weight samples through the model.

MCMC pred mean shape: (200,)
Analytic pred mean shape: (200,)

Plot predictions

The MCMC posterior predictive should closely match the analytic solution. Both include observation noise in the uncertainty bands.

<Figure size 1200x500 with 1 Axes>

Precision additivity

The key advantage of precision parameterization: posterior precision = prior precision + data precision. Let’s verify this with gaussx.

Each observation adds information (in the Fisher sense) to the posterior. The posterior precision Λpost=Λ0+Λdata\Lambda_{\text{post}} = \Lambda_0 + \Lambda_{\text{data}} directly reflects this: the total information is the sum of prior information and data information. This additivity is the defining property of the natural parameters of the exponential family.

Prior entropy: 8.513631199228037
Posterior entropy: -7.486669349647968
Entropy decreased: True
log p(mu_post | posterior): 10.486669349647968

Summary

  • gaussx.MultivariateNormalPrecision naturally encodes ridge regularization and conjugate Bayesian updates.
  • It plugs directly into NumPyro models — NUTS computes gradients through the precision-parameterized log-prob.
  • For linear-Gaussian models, the MCMC posterior matches the analytic conjugate solution, validating the implementation.
  • Precision additivity (Λpost=Λ0+Λdata\Lambda_{\text{post}} = \Lambda_0 + \Lambda_{\text{data}}) makes sequential updates trivial — just add precision operators.
  • The same pattern extends to structured precisions (banded, Kronecker, block-diagonal) for scalable models like GMRFs.

References

  • Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. (Section 3.3 on Bayesian linear regression)
  • Lindgren, F., Rue, H., & Lindstrom, J. (2011). An explicit link between Gaussian fields and Gaussian Markov random fields: the stochastic partial differential equation approach. JRSS-B, 73(4), 423--498.
  • Rue, H. & Held, L. (2005). Gaussian Markov Random Fields: Theory and Applications. Chapman & Hall/CRC.