# Bayesian linear regression

Bayesian linear regression is a type of conditional modeling in which the mean of one variable is described by a linear combination of other variables, with the goal of obtaining the posterior probability of the regression coefficients (as well as other parameters describing the distribution of the regressand) and ultimately allowing the out-of-sample prediction of the regressand (often labelled $y$) conditional on observed values of the regressors (usually $X$). The simplest and most widely used version of this model is the normal linear model, in which $y$ given $X$ is distributed Gaussian. In this model, and under a particular choice of prior probabilities for the parameters—so-called conjugate priors—the posterior can be found analytically. With more arbitrarily chosen priors, the posteriors generally have to be approximated.

## Model setup

Consider a standard linear regression problem, in which for $i=1,\ldots ,n$ we specify the mean of the conditional distribution of $y_{i}$ given a $k\times 1$ predictor vector $\mathbf {x} _{i}$: $y_{i}=\mathbf {x} _{i}^{\mathsf {T}}{\boldsymbol {\beta }}+\varepsilon _{i},$
where ${\boldsymbol {\beta }}$ is a $k\times 1$ vector, and the $\varepsilon _{i}$ are independent and identically normally distributed random variables: $\varepsilon _{i}\sim N(0,\sigma ^{2}).$
This corresponds to the following likelihood function:
$\rho (\mathbf {y} \mid \mathbf {X} ,{\boldsymbol {\beta }},\sigma ^{2})\propto (\sigma ^{2})^{-n/2}\exp \left(-{\frac {1}{2\sigma ^{2}}}(\mathbf {y} -\mathbf {X} {\boldsymbol {\beta }})^{\mathsf {T}}(\mathbf {y} -\mathbf {X} {\boldsymbol {\beta }})\right).$
The ordinary least squares solution is used to estimate the coefficient vector using the Moore–Penrose pseudoinverse: ${\hat {\boldsymbol {\beta }}}=(\mathbf {X} ^{\mathsf {T}}\mathbf {X} )^{-1}\mathbf {X} ^{\mathsf {T}}\mathbf {y}$
where $\mathbf {X}$ is the $n\times k$ design matrix, each row of which is a predictor vector $\mathbf {x} _{i}^{\mathsf {T}}$; and $\mathbf {y}$ is the column $n$-vector $[y_{1}\;\cdots \;y_{n}]^{\mathsf {T}}$.
This is a frequentist approach, and it assumes that there are enough measurements to say something meaningful about ${\boldsymbol {\beta }}$. In the Bayesian approach, the data are supplemented with additional information in the form of a prior probability distribution. The prior belief about the parameters is combined with the data's likelihood function according to Bayes theorem to yield the posterior belief about the parameters ${\boldsymbol {\beta }}$ and $\sigma$. The prior can take different functional forms depending on the domain and the information that is available a priori.
Since the data comprise both $\mathbf {y}$ and $\mathbf {X}$, the focus only on the distribution of $\mathbf {y}$ conditional on $\mathbf {X}$ needs justification. In fact, a "full" Bayesian analysis would require a joint likelihood $\rho (\mathbf {y} ,\mathbf {X} \mid {\boldsymbol {\beta }},\sigma ^{2},\gamma )$ along with a prior $\rho (\beta ,\sigma ^{2},\gamma )$, where $\gamma$ symbolizes the parameters of the distribution for $\mathbf {X}$. Only under the assumption of (weak) exogeneity can the joint likelihood be factored into $\rho (\mathbf {y} \mid {\boldsymbol {\mathbf {X} }},\beta ,\sigma ^{2})\rho (\mathbf {X} \mid \gamma )$. The latter part is usually ignored under the assumption of disjoint parameter sets. More so, under classic assumptions $\mathbf {X}$ are considered chosen (for example, in a designed experiment) and therefore has a known probability without parameters.

## With conjugate priors
### Conjugate prior distribution

For an arbitrary prior distribution, there may be no analytical solution for the posterior distribution. In this section, we will consider a so-called conjugate prior for which the posterior distribution can be derived analytically.
A prior $\rho ({\boldsymbol {\beta }},\sigma ^{2})$ is conjugate to this likelihood function if it has the same functional form with respect to ${\boldsymbol {\beta }}$ and $\sigma$. Since the log-likelihood is quadratic in ${\boldsymbol {\beta }}$, the log-likelihood is re-written such that the likelihood becomes normal in $({\boldsymbol {\beta }}-{\hat {\boldsymbol {\beta }}})$. Write
${\begin{aligned}(\mathbf {y} -\mathbf {X} {\boldsymbol {\beta }})^{\mathsf {T}}(\mathbf {y} -\mathbf {X} {\boldsymbol {\beta }})&=[(\mathbf {y} -\mathbf {X} {\hat {\boldsymbol {\beta }}})+(\mathbf {X} {\hat {\boldsymbol {\beta }}}-\mathbf {X} {\boldsymbol {\beta }})]^{\mathsf {T}}[(\mathbf {y} -\mathbf {X} {\hat {\boldsymbol {\beta }}})+(\mathbf {X} {\hat {\boldsymbol {\beta }}}-\mathbf {X} {\boldsymbol {\beta }})]\\&=(\mathbf {y} -\mathbf {X} {\hat {\boldsymbol {\beta }}})^{\mathsf {T}}(\mathbf {y} -\mathbf {X} {\hat {\boldsymbol {\beta }}})+({\boldsymbol {\beta }}-{\hat {\boldsymbol {\beta }}})^{\mathsf {T}}(\mathbf {X} ^{\mathsf {T}}\mathbf {X} )({\boldsymbol {\beta }}-{\hat {\boldsymbol {\beta }}})+\underbrace {2(\mathbf {X} {\hat {\boldsymbol {\beta }}}-\mathbf {X} {\boldsymbol {\beta }})^{\mathsf {T}}(\mathbf {y} -\mathbf {X} {\hat {\boldsymbol {\beta }}})} _{=\ 0}\\&=(\mathbf {y} -\mathbf {X} {\hat {\boldsymbol {\beta }}})^{\mathsf {T}}(\mathbf {y} -\mathbf {X} {\hat {\boldsymbol {\beta }}})+({\boldsymbol {\beta }}-{\hat {\boldsymbol {\beta }}})^{\mathsf {T}}(\mathbf {X} ^{\mathsf {T}}\mathbf {X} )({\boldsymbol {\beta }}-{\hat {\boldsymbol {\beta }}})\,.\end{aligned}}$
The likelihood is now re-written as $\rho (\mathbf {y} |\mathbf {X} ,{\boldsymbol {\beta }},\sigma ^{2})\propto (\sigma ^{2})^{-{\frac {v}{2}}}\exp \left(-{\frac {vs^{2}}{2{\sigma }^{2}}}\right)(\sigma ^{2})^{-{\frac {n-v}{2}}}\exp \left(-{\frac {1}{2{\sigma }^{2}}}({\boldsymbol {\beta }}-{\hat {\boldsymbol {\beta }}})^{\mathsf {T}}(\mathbf {X} ^{\mathsf {T}}\mathbf {X} )({\boldsymbol {\beta }}-{\hat {\boldsymbol {\beta }}})\right),$ where $vs^{2}=(\mathbf {y} -\mathbf {X} {\hat {\boldsymbol {\beta }}})^{\mathsf {T}}(\mathbf {y} -\mathbf {X} {\hat {\boldsymbol {\beta }}})\quad {\text{ and }}\quad v=n-k,$ where $k$ is the number of regression coefficients.
This suggests a form for the prior: $\rho ({\boldsymbol {\beta }},\sigma ^{2})=\rho (\sigma ^{2})\rho ({\boldsymbol {\beta }}\mid \sigma ^{2}),$ where $\rho (\sigma ^{2})$ is an inverse-gamma distribution $\rho (\sigma ^{2})\propto (\sigma ^{2})^{-{\frac {v_{0}}{2}}-1}\exp \left(-{\frac {v_{0}s_{0}^{2}}{2\sigma ^{2}}}\right).$
In the notation introduced in the inverse-gamma distribution article, this is the density of an ${\text{Inv-Gamma}}(a_{0},b_{0})$ distribution with $a_{0}={\tfrac {v_{0}}{2}}$ and $b_{0}={\tfrac {1}{2}}v_{0}s_{0}^{2}$ with $v_{0}$ and $s_{0}^{2}$ as the prior values of $v$ and $s^{2}$, respectively. Equivalently, it can also be described as a scaled inverse chi-squared distribution, ${\text{Scale-inv-}}\chi ^{2}(v_{0},s_{0}^{2}).$
Further the conditional prior density $\rho ({\boldsymbol {\beta }}|\sigma ^{2})$ is a normal distribution,
$\rho ({\boldsymbol {\beta }}\mid \sigma ^{2})\propto (\sigma ^{2})^{-k/2}\exp \left(-{\frac {1}{2\sigma ^{2}}}({\boldsymbol {\beta }}-{\boldsymbol {\mu }}_{0})^{\mathsf {T}}\mathbf {\Lambda } _{0}({\boldsymbol {\beta }}-{\boldsymbol {\mu }}_{0})\right).$
In the notation of the normal distribution, the conditional prior distribution is ${\mathcal {N}}\left({\boldsymbol {\mu }}_{0},\sigma ^{2}{\boldsymbol {\Lambda }}_{0}^{-1}\right).$

### Posterior distribution

With the prior now specified, the posterior distribution can be expressed as
${\begin{aligned}\rho ({\boldsymbol {\beta }},\sigma ^{2}\mid \mathbf {y} ,\mathbf {X} )&\propto \rho (\mathbf {y} \mid \mathbf {X} ,{\boldsymbol {\beta }},\sigma ^{2})\rho ({\boldsymbol {\beta }}\mid \sigma ^{2})\rho (\sigma ^{2})\\&\propto (\sigma ^{2})^{-n/2}\exp \left(-{\frac {1}{2{\sigma }^{2}}}(\mathbf {y} -\mathbf {X} {\boldsymbol {\beta }})^{\mathsf {T}}(\mathbf {y} -\mathbf {X} {\boldsymbol {\beta }})\right)(\sigma ^{2})^{-k/2}\exp \left(-{\frac {1}{2\sigma ^{2}}}({\boldsymbol {\beta }}-{\boldsymbol {\mu }}_{0})^{\mathsf {T}}{\boldsymbol {\Lambda }}_{0}({\boldsymbol {\beta }}-{\boldsymbol {\mu }}_{0})\right)(\sigma ^{2})^{-(a_{0}+1)}\exp \left(-{\frac {b_{0}}{\sigma ^{2}}}\right)\end{aligned}}$
With some re-arrangement, the posterior can be re-written so that the posterior mean ${\boldsymbol {\mu }}_{n}$ of the parameter vector ${\boldsymbol {\beta }}$ can be expressed in terms of the least squares estimator ${\hat {\boldsymbol {\beta }}}$ and the prior mean ${\boldsymbol {\mu }}_{0}$, with the strength of the prior indicated by the prior precision matrix ${\boldsymbol {\Lambda }}_{0}$
${\boldsymbol {\mu }}_{n}=(\mathbf {X} ^{\mathsf {T}}\mathbf {X} +{\boldsymbol {\Lambda }}_{0})^{-1}(\mathbf {X} ^{\mathsf {T}}\mathbf {X} {\hat {\boldsymbol {\beta }}}+{\boldsymbol {\Lambda }}_{0}{\boldsymbol {\mu }}_{0}).$
To justify that ${\boldsymbol {\mu }}_{n}$ is indeed the posterior mean, the quadratic terms in the exponential can be re-arranged as a quadratic form in ${\boldsymbol {\beta }}-{\boldsymbol {\mu }}_{n}$.
$(\mathbf {y} -\mathbf {X} {\boldsymbol {\beta }})^{\mathsf {T}}(\mathbf {y} -\mathbf {X} {\boldsymbol {\beta }})+({\boldsymbol {\beta }}-{\boldsymbol {\mu }}_{0})^{\mathsf {T}}{\boldsymbol {\Lambda }}_{0}({\boldsymbol {\beta }}-{\boldsymbol {\mu }}_{0})=({\boldsymbol {\beta }}-{\boldsymbol {\mu }}_{n})^{\mathsf {T}}(\mathbf {X} ^{\mathsf {T}}\mathbf {X} +{\boldsymbol {\Lambda }}_{0})({\boldsymbol {\beta }}-{\boldsymbol {\mu }}_{n})+\mathbf {y} ^{\mathsf {T}}\mathbf {y} -{\boldsymbol {\mu }}_{n}^{\mathsf {T}}(\mathbf {X} ^{\mathsf {T}}\mathbf {X} +{\boldsymbol {\Lambda }}_{0}){\boldsymbol {\mu }}_{n}+{\boldsymbol {\mu }}_{0}^{\mathsf {T}}{\boldsymbol {\Lambda }}_{0}{\boldsymbol {\mu }}_{0}.$
Now the posterior can be expressed as a normal distribution times an inverse-gamma distribution:
$\rho ({\boldsymbol {\beta }},\sigma ^{2}\mid \mathbf {y} ,\mathbf {X} )\propto (\sigma ^{2})^{-k/2}\exp \left(-{\frac {1}{2{\sigma }^{2}}}({\boldsymbol {\beta }}-{\boldsymbol {\mu }}_{n})^{\mathsf {T}}(\mathbf {X} ^{\mathsf {T}}\mathbf {X} +\mathbf {\Lambda } _{0})({\boldsymbol {\beta }}-{\boldsymbol {\mu }}_{n})\right)(\sigma ^{2})^{-{\frac {n+2a_{0}}{2}}-1}\exp \left(-{\frac {2b_{0}+\mathbf {y} ^{\mathsf {T}}\mathbf {y} -{\boldsymbol {\mu }}_{n}^{\mathsf {T}}(\mathbf {X} ^{\mathsf {T}}\mathbf {X} +{\boldsymbol {\Lambda }}_{0}){\boldsymbol {\mu }}_{n}+{\boldsymbol {\mu }}_{0}^{\mathsf {T}}{\boldsymbol {\Lambda }}_{0}{\boldsymbol {\mu }}_{0}}{2\sigma ^{2}}}\right).$
Therefore, the posterior distribution can be parametrized as follows. $\rho ({\boldsymbol {\beta }},\sigma ^{2}\mid \mathbf {y} ,\mathbf {X} )\propto \rho ({\boldsymbol {\beta }}\mid \sigma ^{2},\mathbf {y} ,\mathbf {X} )\rho (\sigma ^{2}\mid \mathbf {y} ,\mathbf {X} ),$ where the two factors correspond to the densities of ${\mathcal {N}}\left({\boldsymbol {\mu }}_{n},\sigma ^{2}{\boldsymbol {\Lambda }}_{n}^{-1}\right)\,$ and ${\text{Inv-Gamma}}\left(a_{n},b_{n}\right)$ distributions, with the parameters of these given by
${\boldsymbol {\Lambda }}_{n}=(\mathbf {X} ^{\mathsf {T}}\mathbf {X} +\mathbf {\Lambda } _{0}),\quad {\boldsymbol {\mu }}_{n}=({\boldsymbol {\Lambda }}_{n})^{-1}(\mathbf {X} ^{\mathsf {T}}\mathbf {X} {\hat {\boldsymbol {\beta }}}+{\boldsymbol {\Lambda }}_{0}{\boldsymbol {\mu }}_{0}),$ $a_{n}=a_{0}+{\frac {n}{2}},\qquad b_{n}=b_{0}+{\frac {1}{2}}(\mathbf {y} ^{\mathsf {T}}\mathbf {y} +{\boldsymbol {\mu }}_{0}^{\mathsf {T}}{\boldsymbol {\Lambda }}_{0}{\boldsymbol {\mu }}_{0}-{\boldsymbol {\mu }}_{n}^{\mathsf {T}}{\boldsymbol {\Lambda }}_{n}{\boldsymbol {\mu }}_{n}).$
which illustrates Bayesian inference being a compromise between the information contained in the prior and the information contained in the sample.

### Model evidence

The model evidence $p(\mathbf {y} \mid m)$ is the probability of the data given the model $m$. It is also known as the marginal likelihood, and as the prior predictive density. Here, the model is defined by the likelihood function $p(\mathbf {y} \mid \mathbf {X} ,{\boldsymbol {\beta }},\sigma )$ and the prior distribution on the parameters, i.e. $p({\boldsymbol {\beta }},\sigma )$. The model evidence captures in a single number how well such a model explains the observations. The model evidence of the Bayesian linear regression model presented in this section can be used to compare competing linear models by Bayes factors. These models may differ in the number and values of the predictor variables as well as in their priors on the model parameters. Model complexity is already taken into account by the model evidence, because it marginalizes out the parameters by integrating $p(\mathbf {y} ,{\boldsymbol {\beta }},\sigma \mid \mathbf {X} )$ over all possible values of ${\boldsymbol {\beta }}$ and $\sigma$. $p(\mathbf {y} |m)=\int p(\mathbf {y} \mid \mathbf {X} ,{\boldsymbol {\beta }},\sigma )\,p({\boldsymbol {\beta }},\sigma )\,d{\boldsymbol {\beta }}\,d\sigma$ This integral can be computed analytically and the solution is given in the following equation. $p(\mathbf {y} \mid m)={\frac {1}{(2\pi )^{n/2}}}{\sqrt {\frac {\det({\boldsymbol {\Lambda }}_{0})}{\det({\boldsymbol {\Lambda }}_{n})}}}\cdot {\frac {b_{0}^{a_{0}}}{b_{n}^{a_{n}}}}\cdot {\frac {\Gamma (a_{n})}{\Gamma (a_{0})}}$
Here $\Gamma$ denotes the gamma function. Because we have chosen a conjugate prior, the marginal likelihood can also be easily computed by evaluating the following equality for arbitrary values of ${\boldsymbol {\beta }}$ and $\sigma$. $p(\mathbf {y} \mid m)={\frac {p({\boldsymbol {\beta }},\sigma |m)\,p(\mathbf {y} \mid \mathbf {X} ,{\boldsymbol {\beta }},\sigma ,m)}{p({\boldsymbol {\beta }},\sigma \mid \mathbf {y} ,\mathbf {X} ,m)}}$ Note that this equation follows from a re-arrangement of Bayes theorem. Inserting the formulas for the prior, the likelihood, and the posterior and simplifying the resulting expression leads to the analytic expression given above.

## Other cases

In general, it may be impossible or impractical to derive the posterior distribution analytically. However, it is possible to approximate the posterior by an approximate Bayesian inference method such as Monte Carlo sampling, INLA or variational Bayes.
The special case ${\boldsymbol {\mu }}_{0}=0,\mathbf {\Lambda } _{0}=c\mathbf {I}$ is called ridge regression.
A similar analysis can be performed for the general case of the multivariate regression and part of this provides for Bayesian estimation of covariance matrices: see Bayesian multivariate linear regression.