Algorithm Search⌘K

Modelcore

Bayesian linear regression

Also called conjugate normal linear model.

Linear regression with a normal or normal-inverse-gamma prior on the coefficients and variance. The posterior and predictive laws are in closed form.

Bayesian linear regression is the linear model y=Xβ+εy = X\beta + \varepsilon with normal errors, together with a prior on the coefficients β\beta and, if it is unknown, on the error variance σ2\sigma^2. With a normal prior on β\beta and known σ2\sigma^2, or a normal-inverse-gamma prior on (β,σ2)(\beta, \sigma^2), the prior is conjugate: the posterior is in the same family, and the posterior predictive distribution of a new response is normal or Student t, all in closed form. With the prior N(0,τ2I)N(0, \tau^2 I) and known σ\sigma the posterior mean is the ridge regression estimate with λ=σ2/τ2\lambda = \sigma^2/\tau^2, and with the prior proportional to 1/σ21/\sigma^2 the credible intervals have the numbers of the least squares t intervals.

The machine

The data are nn responses y=(y1,…,yn)⊤y = (y_1, \ldots, y_n)^\top and an n×pn \times p design matrix XX of fixed covariates, with rank pp unless the prior is proper. Given the parameters,

y∣β,σ2∼N(Xβ, σ2In),β∈Rp, σ2>0.y \mid \beta, \sigma^2 \sim N(X\beta,\ \sigma^2 I_n), \qquad \beta \in \mathbb{R}^p,\ \sigma^2 > 0.

So f(y∣β,σ2)=(2πσ2)−n/2exp⁡(−∥y−Xβ∥2/(2σ2))f(y \mid \beta, \sigma^2) = (2\pi\sigma^2)^{-n/2}\exp(-\lVert y - X\beta\rVert^2/(2\sigma^2)). The model is the same as for ordinary least squares; what is new is a prior, and three standard choices give closed forms.

  1. Known variance, normal prior. σ2\sigma^2 is known and β∼N(m0,V0)\beta \sim N(m_0, V_0) with V0V_0 positive definite.
  2. Normal-inverse-gamma prior. β∣σ2∼N(m0,σ2V0)\beta \mid \sigma^2 \sim N(m_0, \sigma^2 V_0) and σ2∼\sigma^2 \sim inverse gamma with shape a0>0a_0 > 0 and scale b0>0b_0 > 0, so the prior density of σ2\sigma^2 is proportional to (σ2)−a0−1e−b0/σ2(\sigma^2)^{-a_0 - 1} e^{-b_0/\sigma^2}. The prior variance of β\beta scales with σ2\sigma^2, which is what makes it conjugate.
  3. The reference prior. π(β,σ2)∝1/σ2\pi(\beta, \sigma^2) \propto 1/\sigma^2 on Rp×(0,∞)\mathbb{R}^p \times (0, \infty), an improper prior. The posterior is proper when XX has rank pp, n>pn > p and the residuals are not all 0.

The conditions are those of the linear model: the mean is linear in β\beta, the errors are independent with equal variance and normal, and XX is treated as fixed (or the analysis is conditional on it). A new response at covariate row x∗x_* is y∗=x∗⊤β+ε∗y_* = x_*^\top\beta + \varepsilon_* with ε∗∼N(0,σ2)\varepsilon_* \sim N(0, \sigma^2) independent of yy given the parameters.

What it counts

The parameters are β\beta and σ2\sigma^2. The model gives, in closed form:

  • the posterior law π(dβ,dσ2∣y)\pi(d\beta, d\sigma^2 \mid y), its mean, covariance and marginals, and so credible intervals for each coefficient and for each mean response x∗⊤βx_*^\top\beta;
  • the posterior predictive law of a new response y∗y_* at x∗x_*, which adds the noise ε∗\varepsilon_* to the uncertainty in x∗⊤βx_*^\top\beta;
  • the marginal likelihood m(y)=∫f(y∣β,σ2) π(dβ,dσ2)m(y) = \int f(y \mid \beta, \sigma^2)\, \pi(d\beta, d\sigma^2), used to compare designs, to choose prior scales and to compare models.

The arithmetic cost is that of least squares: forming X⊤XX^\top X and X⊤yX^\top y takes O(np2)O(np^2) operations, one Cholesky factorization of the p×pp \times p posterior precision matrix takes O(p3)O(p^3), and each predictive mean and variance then takes O(p2)O(p^2). No step needs an explicit matrix inverse.

Relation to other models

Ridge regression and Tikhonov regularization. Under prior 1 with m0=0m_0 = 0 and V0=τ2IV_0 = \tau^2 I, the posterior mean and mode are

mn=(X⊤X+λI)−1X⊤y,λ=σ2/τ2,m_n = (X^\top X + \lambda I)^{-1}X^\top y, \qquad \lambda = \sigma^2/\tau^2,

the ridge estimate, which is Tikhonov regularization with an identity operator. The penalty λ∥β∥2\lambda\lVert\beta\rVert^2 is −2σ2-2\sigma^2 times the log prior, up to a constant. The Bayesian model adds a covariance to the point estimate. A prior with V0=τ2IV_0 = \tau^2 I shrinks the intercept as well; to leave it unshrunk, give it a flat prior or center yy and the columns of XX.

Gaussian processes. If β∼N(0,τ2I)\beta \sim N(0, \tau^2 I), the function g(x)=ϕ(x)⊤βg(x) = \phi(x)^\top\beta of a feature vector ϕ(x)\phi(x) is a Gaussian process with covariance k(x,x′)=τ2ϕ(x)⊤ϕ(x′)k(x, x') = \tau^2\phi(x)^\top\phi(x'). The predictive law of this model is the Gaussian process regression predictive law with that kernel, by the Woodbury identity. The first form costs O(np2+p3)O(np^2 + p^3) and the second O(n3)O(n^3), so the form with the smaller of nn and pp is cheaper.

The Kalman filter. Under prior 1 the posterior after y1,…,yty_1, \ldots, y_t is the prior for yt+1y_{t+1}, and the update for one row is a rank-one correction of VV and mm. This is the Kalman filter for a state β\beta that does not change in time, observed through yt=xt⊤β+εty_t = x_t^\top\beta + \varepsilon_t; it is also recursive least squares.

Hierarchical and mixed models. Lindley and Smith (1972) put normal priors on β\beta in stages, so that the prior mean of β\beta has its own normal prior. A linear mixed model has the same structure for a subset of the coefficients, the random effects, whose prior variance is estimated. Choosing τ2\tau^2 and σ2\sigma^2 by maximizing m(y)m(y) is empirical Bayes, called the evidence approximation in machine learning.

Generalized linear models. For a binary or count response the normal likelihood is replaced and conjugacy is lost; the posterior then needs a Laplace approximation, Markov chain Monte Carlo or variational methods.

Known results in the model

Known variance and normal prior

Under prior 1, completing the square in β\beta in log⁡f(y∣β)+log⁡π(β)\log f(y \mid \beta) + \log \pi(\beta) gives the posterior

β∣y∼N(mn,Vn),Vn=(V0−1+X⊤X/σ2)−1,mn=Vn(V0−1m0+X⊤y/σ2).\beta \mid y \sim N(m_n, V_n), \qquad V_n = \big(V_0^{-1} + X^\top X/\sigma^2\big)^{-1}, \qquad m_n = V_n\big(V_0^{-1}m_0 + X^\top y/\sigma^2\big).

Precisions add, and the posterior mean is a precision-weighted combination of the prior mean and, when XX has rank pp, the least squares estimate β^=(X⊤X)−1X⊤y\hat\beta = (X^\top X)^{-1}X^\top y, since X⊤y=X⊤Xβ^X^\top y = X^\top X\hat\beta. The predictive law of y∗y_* at x∗x_* is

y∗∣y∼N(x∗⊤mn, σ2+x∗⊤Vnx∗),y_* \mid y \sim N\big(x_*^\top m_n,\ \sigma^2 + x_*^\top V_n x_*\big),

where σ2\sigma^2 is the noise and x∗⊤Vnx∗x_*^\top V_n x_* is the uncertainty about the mean. The marginal law of the data is y∼N(Xm0,σ2I+XV0X⊤)y \sim N(Xm_0, \sigma^2 I + XV_0X^\top), and its density at the observed yy is the marginal likelihood.

A hand example. Take x=(0,1,2,3,4)x = (0, 1, 2, 3, 4), y=(1.1,1.9,3.2,3.8,5.1)y = (1.1, 1.9, 3.2, 3.8, 5.1), the model yi=β0+β1xi+εiy_i = \beta_0 + \beta_1 x_i + \varepsilon_i with σ=0.5\sigma = 0.5, and the prior N(0,22I)N(0, 2^2 I), so λ=0.25/4=0.0625\lambda = 0.25/4 = 0.0625. Here X⊤X=(5101030)X^\top X = \begin{pmatrix} 5 & 10 \\ 10 & 30 \end{pmatrix} and X⊤y=(15.1,40.1)⊤X^\top y = (15.1, 40.1)^\top. The least squares estimate is (1.04,0.99)(1.04, 0.99). The posterior mean is (1.01442,0.99645)(1.01442, 0.99645), equal to (X⊤X+0.0625I)−1X⊤y(X^\top X + 0.0625 I)^{-1}X^\top y; both coefficients move a little toward 0. The posterior standard deviations are 0.37947 and 0.15572, with correlation −0.8106-0.8106. At x∗=5x_* = 5 the predictive law has mean 5.99668 and standard deviation 0.72197, so the 95% predictive interval is [4.5816,7.4117][4.5816, 7.4117]. A one-coefficient version, yi=βxi+εiy_i = \beta x_i + \varepsilon_i with the same data, σ=0.5\sigma = 0.5 and prior N(0,4)N(0, 4), has posterior mean 1.3338877 and variance 0.0083160 in closed form; a direct numerical integration of the unnormalized posterior on 200000 equal steps of [−5,5][-5, 5] gives the same values to 7 digits.

Unknown variance and normal-inverse-gamma prior

Under prior 2 the posterior is again normal-inverse-gamma:

Vn=(V0−1+X⊤X)−1,mn=Vn(V0−1m0+X⊤y),an=a0+n2,bn=b0+12(y⊤y+m0⊤V0−1m0−mn⊤Vn−1mn).V_n = (V_0^{-1} + X^\top X)^{-1}, \quad m_n = V_n(V_0^{-1}m_0 + X^\top y), \quad a_n = a_0 + \tfrac n2, \quad b_n = b_0 + \tfrac12\big(y^\top y + m_0^\top V_0^{-1}m_0 - m_n^\top V_n^{-1}m_n\big).

Integrating out σ2\sigma^2 gives the marginal posterior of β\beta as a multivariate Student t with 2an2a_n degrees of freedom, location mnm_n and scale matrix (bn/an)Vn(b_n/a_n)V_n. The predictive law of y∗y_* is Student t with 2an2a_n degrees of freedom, location x∗⊤mnx_*^\top m_n and squared scale (bn/an)(1+x∗⊤Vnx∗)(b_n/a_n)(1 + x_*^\top V_n x_*). Its tails are heavier than those of the normal predictive law because σ2\sigma^2 is uncertain. The posterior mean of σ2\sigma^2 is bn/(an−1)b_n/(a_n - 1) when an>1a_n > 1. The marginal likelihood is

m(y)=(2π)−n/2 det⁡(Vn)1/2det⁡(V0)1/2 b0a0bnan Γ(an)Γ(a0).m(y) = (2\pi)^{-n/2}\,\frac{\det(V_n)^{1/2}}{\det(V_0)^{1/2}}\,\frac{b_0^{a_0}}{b_n^{a_n}}\,\frac{\Gamma(a_n)}{\Gamma(a_0)}.

The same example. With m0=0m_0 = 0, V0=4IV_0 = 4I, a0=b0=1a_0 = b_0 = 1: mn=(0.94835,1.01211)m_n = (0.94835, 1.01211), an=3.5a_n = 3.5, bn=1.30204b_n = 1.30204 and E[σ2∣y]=0.52081E[\sigma^2 \mid y] = 0.52081. The least squares residual variance is only s2=0.035667s^2 = 0.035667 with n−p=3n - p = 3 degrees of freedom; the prior with b0/a0=1b_0/a_0 = 1 carries the weight of about 2a0=22a_0 = 2 observations with squared error 1, and it pulls σ2\sigma^2 up. The 95% interval for the slope from the t7t_7 marginal is [0.5812,1.4430][0.5812, 1.4430], and the 95% predictive interval at x∗=5x_* = 5, from t7t_7 with location 6.00893 and scale 0.87236, is [3.9461,8.0717][3.9461, 8.0717]. For a one-coefficient check, yi=βxi+εiy_i = \beta x_i + \varepsilon_i with β∣σ2∼N(0,4σ2)\beta \mid \sigma^2 \sim N(0, 4\sigma^2) and σ2∼\sigma^2 \sim inverse gamma(1,1)(1, 1), the t density of β\beta agrees with a numerical integration of the joint posterior (2001 points in β\beta by 1801 points in log⁡σ2\log\sigma^2) to about 1 part in 10610^6 at 5 points.

The prior proportional to 1/sigma^2

Prior 3 is the limit of prior 2 as V0−1→0V_0^{-1} \to 0, a0→−p/2a_0 \to -p/2 and b0→0b_0 \to 0. With s2=∥y−Xβ^∥2/(n−p)s^2 = \lVert y - X\hat\beta\rVert^2/(n - p),

β∣y∼tn−p(β^, s2(X⊤X)−1),y∗∣y∼tn−p(x∗⊤β^, s2(1+x∗⊤(X⊤X)−1x∗)),\beta \mid y \sim t_{n-p}\big(\hat\beta,\ s^2(X^\top X)^{-1}\big), \qquad y_* \mid y \sim t_{n-p}\big(x_*^\top\hat\beta,\ s^2(1 + x_*^\top(X^\top X)^{-1}x_*)\big),

and (n−p)s2/σ2(n - p)s^2/\sigma^2 given yy is chi-square with n−pn - p degrees of freedom. The posterior mean of β\beta is the least squares estimate, and the equal-tailed 95% credible interval for βj\beta_j is β^j±t(n−p,0.975) s((X⊤X)−1)jj\hat\beta_j \pm t_{(n-p, 0.975)}\, s\sqrt{((X^\top X)^{-1})_{jj}}. In the example it is [0.79994,1.18006][0.79994, 1.18006] for the slope, with β^1=0.99\hat\beta_1 = 0.99, s2=0.035667s^2 = 0.035667 and t(3,0.975)=3.18245t_{(3, 0.975)} = 3.18245.

Frequentist properties of the same numbers

The statements above are posterior probabilities given yy. Statements about repeated samples at a fixed β\beta are different and are listed separately.

  • Under prior 3 the credible intervals for βj\beta_j and the predictive intervals have the same numbers as the classical t confidence and prediction intervals, so their coverage at every fixed (β,σ2)(\beta, \sigma^2) is exactly 95% when the model holds. This is an exact agreement for this prior and model only.
  • Under prior 1 with m0=0m_0 = 0 and V0=τ2IV_0 = \tau^2 I, the posterior mean has expectation (X⊤X+λI)−1X⊤Xβ(X^\top X + \lambda I)^{-1}X^\top X\beta, so it is biased toward 0 for every β≠0\beta \ne 0, and covariance σ2(X⊤X+λI)−1X⊤X(X⊤X+λI)−1\sigma^2(X^\top X + \lambda I)^{-1}X^\top X(X^\top X + \lambda I)^{-1}, which is smaller than the least squares covariance σ2(X⊤X)−1\sigma^2(X^\top X)^{-1}. Least squares is the best linear unbiased estimator by the Gauss-Markov theorem; the posterior mean is not unbiased, so the theorem does not rank them.
  • The credible intervals under a proper prior have no exact coverage guarantee at a fixed β\beta. As n→∞n \to \infty with pp fixed and X⊤X/nX^\top X/n converging to a positive definite matrix, the prior's effect vanishes and they become asymptotic confidence intervals by the Bernstein-von Mises theorem.

Computation and checks

  • Compute mnm_n by solving (V0−1+X⊤X/σ2) mn=V0−1m0+X⊤y/σ2(V_0^{-1} + X^\top X/\sigma^2)\, m_n = V_0^{-1}m_0 + X^\top y/\sigma^2 with the Cholesky factor of the left matrix, and get x∗⊤Vnx∗x_*^\top V_n x_* as the squared norm of the solution of one triangular system. When X⊤XX^\top X is ill conditioned, the QR decomposition of X/σX/\sigma stacked on a square root of V0−1V_0^{-1} is more accurate than forming X⊤XX^\top X.
  • The posterior under prior 3 exists only if XX has full column rank and n>pn > p; with p>np > n a proper prior on β\beta is required, as for ridge regression.
  • The marginal likelihood under an improper prior is defined only up to a constant, so it cannot compare models; the Bayesian information criterion is its large-sample approximation for proper priors.

References

  1. D. V. Lindley and A. F. M. Smith, Bayes estimates for the linear model, Journal of the Royal Statistical Society B 34(1), 1972, pages 1 to 41, with discussion (the normal linear model with normal priors in stages, and the posterior mean as a weighted combination of least squares and prior means)
  2. Andrew Gelman, John B. Carlin, Hal S. Stern, David B. Dunson, Aki Vehtari and Donald B. Rubin, Bayesian Data Analysis, 3rd edition, CRC Press, 2013, Chapters 2 and 3 (conjugate normal models, the normal-inverse-chi-square prior and the t marginal) and Chapter 14 (the regression model under the prior proportional to 1/sigma^2, with its t posterior and predictive laws)
  3. Christopher M. Bishop, Pattern Recognition and Machine Learning, Springer, 2006, Section 3.3 (Bayesian linear regression with known noise, the parameter posterior and the predictive distribution) and Section 3.5 (the evidence approximation)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/PQS5CE/text.yaml