# Bayesian regression

The ridge regression estimator is equivalent to a Bayesian regression estimator. On one hand this equivalence provides another interpretation of the ridge regression estimator. But it also shows that within the Bayesian framework the high-dimensionality of the data need not frustrate the numerical evaluation of the estimator. In addition, the framework provides ways to quantify the consequences of high-dimensionality on the uncertainty of the estimator. Within this chapter focus is on the equivalence of Bayesian and ridge regression. In this particular case, the connection is immediate from the analytic formulation of the Bayesian regression estimator. After this has been presented, it is shown how this estimator may also be obtained by means of sampling. The relevance of the sampling for the evaluation of the estimator and its uncertainty becomes apparent in subsequent chapters when discussing other penalized estimators for which the connection cannot be captured in analytic form.

## A minimum of prior knowledge on Bayesian statistics

The schism between Bayesian and frequentist statistics centers on the interpretation of the concept of probability. A frequentist views the probability of an event as the limiting frequency of observing the event among a large number of trials. In contrast, a Bayesian considers it to be a measure of believe (in the occurence) of the event. The difference between the two interpretations becomes clear when considering events that can occur only once (or a small number of times). A Bayesian would happily discuss the probability of this (possibly hypothetical) event happening, which would be meaningless to a frequentist.

What are the consequences of this schism for the current purpose of estimating a regression model? Exponents from both paradigms assume a statistical model, e.g. here the linear regression model, to describe the data generating mechanism. But a frequentist treats the parameters as platonic quantities for which a true value exists that is to be estimated from the data. A Bayesian, however, first formalizes his/her current believe/knowledge on the parameters in the form of probability distributions. This is referred to as the prior -- to the experiment -- distribution. The parameters are thus random variables and their distributions are to be interpreted as reflecting the (relative) likelihood of the parameters' values. From the Bayesian point it then makes sense to talk about the random behaviour of the parameter, e.g., what is probability of that the parameter is larger than a particular value. In the frequentist context one may ask this of the estimator, but not of the parameter. The parameter either is or is not larger than this value as a platonic quantity is not governed by a chance process. Then, having specified his/her believe on the parameters, a Bayesian uses the data to update this believe of the parameters of the statistical model, which again is a distribution and called the posterior -- to the experiment -- distribution.

To illustrate this process of updating assume that the data $Y_1, \ldots, Y_n \sim \{0, 1 \}$ are assumed to be independently and identically drawn from a Bernouilli distribution with parameter $\theta = P(Y_i = 1)$. The likelihood of these data for a given choice of the parameter $\theta$ then is: $L(\mathbf{Y} = \mathbf{y}; \theta) = P(\mathbf{Y} = \mathbf{y} \, | \, \theta) = P(Y_1 = y_1, \ldots, Y_n = y_n \, | \, \theta) = \prod_{i=1}^n P(Y_i = y_i \, | \, \theta) = \prod_{i=1}^n \theta^{y_i} (1-\theta)^{1-y_i}$. A Bayesian now needs to specificy the prior distribution, denoted $\pi_{\theta}(\cdot)$, on the parameter $\theta$. A common choice in this case would be a beta-distribution: $\theta \sim \mathcal{B}(\alpha, \beta)$. The posterior distribution is now arrived at by the use of Bayes' rule:

[$] \begin{eqnarray*} \pi ( \theta \, | \, \mathbf{Y} = \mathbf{y}) & = & \frac{P(\mathbf{Y} = \mathbf{y} \, | \, \theta) \, \pi (\theta) }{ P(\mathbf{Y} = \mathbf{y} ) } \, \, \, = \, \, \, \frac{P(\mathbf{Y} = \mathbf{y} \, | \, \theta) \, \pi (\theta) }{ \int_{0,1} P(\mathbf{Y} = \mathbf{y} \, | \, \theta) \, \pi (\theta) \, d\theta }. \end{eqnarray*} [$]

The posterior distribution thus contains all knowledge on the parameter. As the denomenator -- referred to as the distribution's normalizing constant -- in the expression of the posterior distribution does not involve the parameter $\theta$, one often writes $\pi ( \theta \, | \, \mathbf{Y} = \mathbf{y}) \propto P(\mathbf{Y} = \mathbf{y} \, | \, \theta) \, \pi (\theta)$, thus specifying the (relative) density of the posterior distribution of $\theta$ as ‘likelihood $\times$ prior’.

While the posterior distribution is all one wishes to know, often a point estimate is required. A point estimate of $\theta$ can be obtained from the posterior by taking the mean or the mode. Formally, the Bayesian point estimator of a parameter $\ttheta$ is defined as the estimator that minimizes the Bayes risk over a prior distribution of the parameter $\theta$. The Bayes risk is defined as $\int_{\theta} \mathbb{E} [(\hat{\theta} - \theta)^2] \pi_{\theta}(\theta; \alpha) d\theta$, where $\pi_{\theta}(\theta; \alpha)$ is the prior distribution of $\theta$ with hyperparameter $\alpha$. It is thus a weighted average of the Mean Squared Error, with weights specified through the prior. The Bayes risk is minimized by the posterior mean:

[$] \begin{eqnarray*} \mathbb{E}_{\theta}(\theta \, | \, \mbox{data}) & = & \frac{ \int_{\theta} \theta \, P(\mathbf{Y} = \mathbf{y} \, | \, \theta) \, \pi (\theta) \, d \theta }{ \int_{\theta} P(\mathbf{Y} = \mathbf{y} \, | \, \theta) \, \pi (\theta) \, d\theta }. \end{eqnarray*} [$]

(cf., e.g., [1]). The Bayesian point estimator of $\theta$ yields the smallest possible expected MSE, under the assumption of the employed prior. This estimator thus depends on the likelihood and the prior, and a different prior yields a different estimator.

A point estimator is preferably accompanied by a quantification of its uncertainty. Within the Bayesian context this is done by so-called credible intervals or regions, the Bayesian equivalent of confidence intervals or regions. A Bayesian credible interval encompass a certain percentage of the probability mass of the posterior. For instance, a subset of the parameter space $\mathcal{C}$ forms an $100 (1-\alpha) \%$ credible interval if $\int_{\mathcal{C}} \pi(\theta \, | \, \mbox{data}) \, d \theta = 1- \alpha$.

In the example above it is important to note the role of the prior in the updating of the knowledge on $\theta$. First, when the prior distribution is uniform on the unit interval, the mode of the posterior coincides with the maximum likelihood estimator. Furthermore, when $n$ is large the influence of the prior in the posterior is negligible. It is therefore that for large sample sizes frequentist and Bayesian analyses tend to produce similar results. However, when $n$ is small, the prior's contribution to the posterior distribution cannot be neglected. The choice of the prior is then crucial as it determines the shape of the posterior distribution and, consequently, the posterior estimates. It is this strong dependence of the posterior on the arbitrary (?) choice of the prior that causes unease with a frequentist (leading some to accuse Bayesians of subjectivity). In the high-dimensional setting the sample size is usually small, especially in relation to the parameter dimension, and the choice of the prior distribution then matters. Interest here is not in resolving the frequentist's unease, but in identifying or illustrating the effect of the choice of the prior distribution on the parameter estimates.

## Relation to ridge regression

Ridge regression has a close connection to Bayesian linear regression. Bayesian linear regression assumes the parameters $\bbeta$ and $\sigma^2$ to be the random variables, while at the same time considering $\mathbf{X}$ and $\mathbf{Y}$ as fixed. Within the regression context, the commonly chosen priors of $\bbeta$ and $\sigma^2$ are $\bbeta \, | \, \sigma^2 \sim \mathcal{N}(\mathbf{0}_p, \sigma^2 \lambda^{-1} \mathbf{I}_{pp})$ and $\sigma^2 \sim \mathcal{IG}(\alpha_0, \beta_0)$, where $\mathcal{IG}$ denotes the inverse Gamma distribution with shape parameter $\alpha_0$ and scale parameter $\beta_0$. The penalty parameter can be interpreted as the (scaled) precision of the prior of $\beta$, determining how informative the prior should be. A smaller penalty (i.e. precision) corresponds to a wider prior, and a larger penalty to a more informative, concentrated prior (Figure).

The joint posterior distribution of $\bbeta$ and $\sigma^2$ is, under the assumptions of the (likelihood of) the linear regression model and the priors above:

[$] \begin{eqnarray*} f_{\bbeta, \sigma^2} (\bbeta, \sigma^2 \, | \, \mathbf{Y}, \mathbf{X}) & \propto & f_Y (\mathbf{Y} \, | \, \mathbf{X}, \bbeta, \sigma^2) \, f_{\beta}(\bbeta | \sigma^2) \, f_{\sigma}(\sigma^2) \\ & \propto & \sigma^{-n} \exp [ - \tfrac{1}{2}\sigma^{-2} ( \mathbf{Y} - \mathbf{X} \bbeta)^{\top} ( \mathbf{Y} - \mathbf{X} \bbeta) ] \\ & & \times \, \, \sigma^{-p} \exp ( - \tfrac{1}{2} \sigma^{-2} \lambda \bbeta^{\top} \bbeta ) \, \times \, \, (\sigma^2)^{-\alpha_0-1} \exp ( - \tfrac{1}{2} \sigma^{-2} \beta_0 ). \end{eqnarray*} [$]

The posterior distribution can be expressed as a multivariate normal distribution. Hereto group the terms from the exponential functions that involve $\bbeta$ and manipulate as follows:

[$] \begin{eqnarray*} & & \hspace{-1.5cm} ( \mathbf{Y} - \mathbf{X} \bbeta)^{\top} ( \mathbf{Y} - \mathbf{X} \bbeta) + \lambda \bbeta^{\top} \bbeta \\ & = & \mathbf{Y}^{\top} \mathbf{Y} - \bbeta^{\top} \mathbf{X}^{\top} \mathbf{Y} - \mathbf{Y}^{\top} \mathbf{X} \bbeta + \bbeta^{\top} \mathbf{X}^{\top} \mathbf{X} \bbeta + \lambda \bbeta^{\top} \bbeta \\ & = & \mathbf{Y}^{\top} \mathbf{Y} - \bbeta^{\top} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp}) (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} \mathbf{X}^{\top} \mathbf{Y} \\ & & - \, \mathbf{Y}^{\top} \mathbf{X} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp}) \bbeta + \bbeta^{\top} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp}) \bbeta \\ & = & \mathbf{Y}^{\top} \mathbf{Y} - \bbeta^{\top} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp}) \hat{\bbeta} (\lambda) \\ & & - \, [ \hat{\bbeta} (\lambda) ]^{\top} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp}) \bbeta + \bbeta^{\top} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp}) \bbeta \\ & = & \mathbf{Y}^{\top} \mathbf{Y} - \mathbf{Y}^{\top} \mathbf{X} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} \mathbf{X}^{\top} \mathbf{Y} \\ & & + \, [ \bbeta - \hat{\bbeta}(\lambda) ]^{\top} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp}) [ \bbeta - \hat{\bbeta}(\lambda) ]. \end{eqnarray*} [$]

Using this result, the posterior distribution can be rewritten to:

[$] \begin{eqnarray*} f_{\bbeta, \sigma^2} (\bbeta, \sigma^2 \, | \, \mathbf{Y}, \mathbf{X}) & \propto & g_{\bbeta} (\bbeta \, | \, \sigma^2, \mathbf{Y}, \mathbf{X}) \, g_{\sigma^2} (\sigma^2 \, | \, \mathbf{Y}, \mathbf{X}) \end{eqnarray*} [$]

with

[$] \begin{eqnarray*} g_{\bbeta} (\bbeta \, | \, \sigma^2, \mathbf{Y}, \mathbf{X}) & \propto & \exp \big\{ - \tfrac{1}{2} \sigma^{-2} \big[ \bbeta - \hat{\bbeta}(\lambda) \big]^{\top} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp}) \big[ \bbeta - \hat{\bbeta}(\lambda) \big] \big\}. \end{eqnarray*} [$]

Clearly, after having recognized the form of a multivariate normal density in the expression of the preceeding display, the conditional posterior mean of $\bbeta$ is $\mathbb{E}(\bbeta \, | \, \sigma^2, \mathbf{Y}, \mathbf{X}) = \hat{\bbeta}(\lambda)$. Hence, the ridge regression estimator can be viewed as the Bayesian posterior mean estimator of $\bbeta$ when imposing a Gaussian prior on the regression parameter.

The conditional posterior distribution of $\bbeta$, $g_{\bbeta} (\bbeta \, | \, \sigma^2, \mathbf{Y}, \mathbf{X})$, related to a high-dimensional situation ($n=1$, $p=2$) is depicted in Figure for two choices of the penalty parameter $\lambda$. Put differently, for two different priors. In one, the left panel, $\lambda$ is arbitrarily set equal to one. The choice of the employed $\lambda$, i.e. $\lambda=1$, is irrelevant, the resulting posterior distribution only serves as a reference. This choice results in a posterior distribution that is concentrated around the Bayesian point estimate, which coincides with the ridge regression estimate. The almost spherical level sets around the point estimate may be interpreted as credible intervals for $\bbeta$. The grey dashed line, spanned by the row of the design matrix, represents the support of the degenerated normal distribution of the frequentist's ridge regression estimate (cf. end of Section Variance ). The contrast between the degenerated frequentist and well-defined Bayesian normal distribution illustrates that -- for a suitable choice of the prior -- within the Bayesian context high-dimensionality need not be an issue with respect to the evaluation of the posterior. In the right panel of Figure the penalty parameter $\lambda$ is set equal to a very small value, i.e. $0 \lt \lambda \ll 1$. This represents a very imprecise or uninformative prior. It results in a posterior distribution with level sets that are far from spherical and are very streched in the direction orthogonal to the subspace spanned by the row of the design matrix and indicates in which direction there is most uncertainty with respect to $\bbeta$. In particular, when $\lambda \downarrow 0$, the level sets loose their ellipsoid form and the posterior collapses to the degenerated normal distribution of the frequentist's ridge regression estimate. Finally, in combination with the left-hand plot this illustrates the large effect of the prior in the high-dimensional context.

With little extra work we may also obtain the conditional posterior of $\sigma^2$ from the joint posterior distribution:

[$] \begin{eqnarray*} f_{\sigma^2} (\sigma^2 \, | \, \bbeta, \mathbf{Y}, \mathbf{X}) & \propto & (\sigma^2)^{-[(n+p)/2 + \alpha_0 + 1]} \exp [ - \tfrac{1}{2} \sigma^{-2} ( \| \mathbf{Y} - \mathbf{X} \bbeta \|_2^2 + \lambda \| \bbeta \|_2^2 + \beta_0) ], \end{eqnarray*} [$]

in which one can recognize the shape of an inverse gamma distribution. The relevance of this conditional distribution will be made clear in Section Markov chain Monte Carlo .

The Bayesian point estimator minimizes the Bayes risk, over a prior distribution. The risk cannot be used to choose the prior c.q. the penalty parameter $\lambda$. This can be seen from the risk of the Bayesian regression estimator. The Bayes risk of the Bayesian regression estimator over the normal prior $\bbeta \sim \mathcal{N}(\mathbf{0}_p, \sigma^2 \lambda^{-1} \mathbf{I}_{pp})$ is:

[$] \begin{eqnarray*} \mathbb{E}_{\bbeta} \{\mbox{MSE}[\hat{\bbeta}(\lambda)] \, | \, \sigma^2, \mathbf{Y}, \mathbf{X} \} & = & \sigma^2 \, \mbox{tr}\big\{ \mathbf{W}_{\lambda} \, (\mathbf{X}^{\top} \mathbf{X})^{-1} \, \mathbf{W}_{\lambda}^{\top} \big\} + \mathbb{E}_{\bbeta} [ \bbeta^{\top} (\mathbf{W}_{\lambda} - \mathbf{I}_{pp})^{\top} (\mathbf{W}_{\lambda} - \mathbf{I}_{pp}) \, \bbeta] \\ & = & \sigma^2 \, \big\{ \mbox{tr} \big[ \mathbf{W}_{\lambda} \, (\mathbf{X}^{\top} \mathbf{X})^{-1} \, \mathbf{W}_{\lambda}^{\top} \big] + \lambda^{-1} \mbox{tr} [(\mathbf{W}_{\lambda} - \mathbf{I}_{pp})^{\top} (\mathbf{W}_{\lambda} - \mathbf{I}_{pp})] \big\} \\ & = & \sigma^2 \sum\nolimits_{j=1}^p (d_{jj}^2 + \lambda)^{-1}, \end{eqnarray*} [$]

in which $\mathbf{W}_{\lambda} = (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} \mathbf{X}^{\top} \mathbf{X}$ and we have used i) the previously derived explicit expression of the ridge estimator's MSE, ii) the expectation of the quadratic form of a multivariate random variable [2], iii) the singular value decomposition of $\mathbf{X}$ with singular values $d_{jj}$, and iv) the fact that the trace of a square matrix equals the sum of its eigenvalues. As the ridge estimator coincides with the posterior mean, this is the minimal achievable MSE under a zero-centered normal prior with an uncorrelated and equivariant covariance matrix.

One may now be tempted to choose the $\lambda$ that minimizes the Bayes risk as optimal and use it in the evaluation of the ridge regression estimator. However, above the Bayes risk of the ridge estimator factorizes with respect to $\sigma^2$ and $\lambda$. Hence, the larger the hyperparameter $\lambda$ the lower the Bayes risk of the ridge estimator. In particular, its Bayes risk converges to zero as $\lambda \rightarrow \infty$. This can be understood as follows. The limit corresponds to an infinite precision of the prior, thus reducing the contribution of the MSE's variance term to zero. Moreover, as the ridge estimator shrinks towards zero and the prior distribution of $\bbeta$ has a zero mean, the bias too vanishes as $\lambda \rightarrow \infty$. In particular, the $\lambda$ that minimizes the Bayes risk is thus a useless suggestion for the ridge regression estimator. The failure to produce a useful choice of the penalty (or hyper-) paramer is due to the fact that the Bayes estimator is defined with respect to a specific prior, i.e. corresponding to a specific value of $\lambda$, and not a class of priors relating to all $\lambda \in \mathbb{R}_{\gt0}$.

The calculation of the Bayes risk above relates the Bayesian and frequentist statements on the MSE of the ridge estimator. For the latter revisit Theorem of Section Mean squared error , which warrants the existence of a $\lambda$ such that the resulting ridge estimator has a superior MSE over that of the ML estimator. This result made no assumption on (the distribution of) $\bbeta$. In fact, it can be viewed as a statement of the MSE conditional on $\bbeta$. The Bayesian result integrates out the uncertainty -- specified by the prior -- in $\bbeta$ from the (frequentist's) conditional MSE to arrive at the unconditional MSE.

In general, any penalized estimator has a Bayesian equivalent. That is generally not the posterior mean as for the ridge regression estimator, which is more a coincide due to the normal form of its likelihood and conjugate prior. A penalized estimator always coincides with the Bayesian MAP (Mode A Posteriori) estimator. Then, the MAP estimates estimates a parameter $\ttheta$ by the mode, i.e. the maximum, of the posterior density. To see the equivalence of both estimators we derive the MAP estimator. The latter is defined as:

[$] \begin{eqnarray*} \hat{\ttheta}_{\mbox{{\tiny map}}} \, \, \, = \, \, \, \arg \max_{\ttheta} \, \pi ( \ttheta \, | \, \mathbf{Y} = \mathbf{y}) & = & \arg \max_{\ttheta} \frac{P(\mathbf{Y} = \mathbf{y} \, | \, \ttheta) \, \pi (\theta) }{ \int P(\mathbf{Y} = \mathbf{y} \, | \, \ttheta) \, \pi (\ttheta) \, d\ttheta }. \end{eqnarray*} [$]

To find the maximum of the posterior density take the logarithm (which does not change the location of the maximum) and drop terms that do not involve $\ttheta$. The MAP estimator then is:

[$] \begin{eqnarray*} \hat{\ttheta}_{\mbox{{\tiny map}}} & = & \arg \max_{\ttheta} \, \log[ P(\mathbf{Y} = \mathbf{y} \, | \, \ttheta) ] + \log[ \pi (\ttheta) ]. \end{eqnarray*} [$]

The first summand on the right-hand side is the loglikelihood, while the second is the logarithm of the prior density. The latter, in case of a normal prior, is proportional to $\sigma^{-2} \lambda \| \bbeta \|_2^2$. This is -- up to some factors -- exactly the loss function of the ridge regression estimator. If the quadratic ridge penalty is replaced by different one, it is easy to see what form the prior density should have in order for both estimators -- the penalized and MAP -- to coincide.

## Markov chain Monte Carlo

The ridge regression estimator was shown to coincide with the posterior mean of the regression parameter when it is endowed with a normal prior. This particular choice of the prior yielded a closed form expression of the posterior, and its mean. Prior distributions that result in well-characterized posterior ones are referred to as conjugate. E.g., for the standard linear regression model a normal prior is conjugate. Conjugate priors, however, are the exception rather than the rule. In general, an arbitrary prior distribution will not result in a posterior distribution that is familiar (amongst others due to the fact that its normalizing constant is not analytically evaluable). For instance, would a wildly different prior for the regression parameter be chosen, its posterior is unlikely to be analytically known. Then, although analytically unknown, such posteriors can be investigated in silico by means of the Markov chain Monte Carlo (MCMC) method. In this subsection the MCMC method is explained and illustrated -- despite its conjugancy -- on the linear regression model with a normal prior.

Markov chain Monte Carlo is a general purpose technique for sampling from complex probabilistic models/distributions. It draws a sample from a Markov chain that has been constructed such that its stationary distribution equals the desired distribution (read: the analytically untractable posterior distribution). The chain is, after an initial number of iterations (called the burn-in period), believed to have converged and reached stationarity. A sample from the stationary chain is then representative of the desired distribution. Statistics derived from this sample can be used to characterize the desired distribution. Hence, estimation is reduced to setting up and running a Markov process on a computer.

Recall Monte Carlo integration. Assume the analytical evaluation of $\mathbb{E}[h(\mathbf{Y})] = \int h(\mathbf{y}) \pi(\mathbf{y}) d \mathbf{y}$ is impossible. This quantity can nonetheless be estimated when samples from $\pi(\cdot)$ can be drawn. For, if $\mathbf{Y}^{(1)}, \ldots, \mathbf{Y}^{(T)} \sim \pi(\mathbf{y})$, the integral may be estimated by: $\widehat{\mathbb{E}}[h(\mathbf{Y})] = T^{-1} \sum_{t=1}^T h(\mathbf{Y}^{(t)})$. By the law of large numbers this is a consistent estimator, i.e. if $T \rightarrow \infty$ the estimator converges (in probability) to its true value. Thus, irrespective of the specifics of the posterior distribution $\pi(\cdot)$, if one is able to sample from it, its moments (or other quantities) can be estimated. Or, by the same principle it may be used in the Bayesian regression framework of Section Relation to ridge regression to sample from the marginal posterior of $\bbeta$,

[$] \begin{eqnarray*} f_{\bbeta}( \bbeta \, | \, \mathbf{Y}, \mathbf{X}) & = & \int_{0}^{\infty} f_{\bbeta, \sigma^2}( \bbeta, \sigma^2 \, | \, \mathbf{Y}, \mathbf{X}) \, d \sigma^2 \, \, \, = \, \, \, \int_{0}^{\infty} g_{\bbeta}( \bbeta \, | \, \sigma^2, \mathbf{Y}, \mathbf{X}) g_{\sigma^2}(\sigma^2 \, | \, \mathbf{Y}, \mathbf{X}) \, d \sigma^2, \end{eqnarray*} [$]

given that it of $\sigma^2$ is known. By drawing a sample $\sigma^{2,(1)}, \ldots, \sigma^{2,(T)}$ of $g_{\sigma^2}(\sigma^2 \, | \, \mathbf{Y}, \mathbf{X})$ and, subsequently, a sample $\{ \bbeta^{(t)} \}_{t=1}^T$ from $g_{\bbeta}( \bbeta \, | \, \sigma^{2,(t)}, \mathbf{Y}, \mathbf{X})$ for $t=1, \ldots, T$, the parameter $\sigma^2$ has effectively been integrated out and the resulting sample of $\bbeta^{(1)}, \bbeta^{(2)}, \ldots, \bbeta^{(T)}$ is from $f_{\bbeta}( \bbeta \, | \, \mathbf{Y}, \mathbf{X})$.

It is clear from the above that the ability to sample from the posterior distribution is key to the estimation of the parameters. But this sampling is hampered by knowledge of the normalizing constant of the posterior. This is overcome by the acceptance-rejection sampler. This sampler generates independent draws from a target density $\pi(\mathbf{y}) = f(\mathbf{y}) / K$, where $f(\mathbf{y})$ is an unnormalized density and $K$ its (unknown) normalizing constant. The sampler relies on the existence of a density $h(\mathbf{y})$ that dominates $f(\mathbf{y})$, i.e., $f(\mathbf{y}) \leq c \, h(\mathbf{y})$ for some known $c \in \mathbb{R}_{\gt0}$, from which it is possible to simulate. Then, in order to draw a $\mathbf{Y}$ from the posterior $\pi(\cdot)$ run Algorithm Acceptance-rejection sampling:

Acceptance-rejection sampling
input:
• densities $f(\cdot)$ and $h(\cdot)$;
• constant $c \gt0$;
output: a draw from $\pi(\cdot)$.
1. Generate $\mathbf{Y}$ from $h(\cdot)$
2. Generate $U$ from the uniform distribution $\mathcal{U}[0,1]$
3. if $U \leq f(\mathbf{Y}) \, / \,[ c \times h(\mathbf{Y})]$ then
4. Return $\mathbf{Y}$.
5. else
6. Go back to line 1.
7. end

A proof that this indeed yields a random sample from $\pi(\cdot)$ is given in [3]. For this method to be computationally efficient, $c$ is best set equal to $\sup_{\mathbf{y}} \{ f(\mathbf{y})/h(\mathbf{y}) \}$. The R-script below illustrates this sampler for the unnormalized density $f(y) = \cos^2(2 \pi y)$ on the unit interval. As $\max_{y \in [0,1]} \cos^2(2 \pi y) = 1$, the density $f(y)$ is dominated by the density function of the uniform distribution $\mathcal{U}[0,1]$ and, hence, the script uses $h(y) = 1$ for all $y \in [0, 1]$.

# define unnormalized target density
cos2density <- function(x){ (cos(2*pi*x))^2 }

# define acceptance-rejection sampler
acSampler <- function(n){
draw <- numeric()
for(i in 1:n){
accept <- FALSE
while (!accept){
Y <- runif(1)
if (runif(1) <= cos2density(Y)){
accept <- TRUE
draw   <- c(draw, Y)
}
}
}
return(draw)
}

# verify by eye-balling
hist(acSampler(100000), n=100)



In practice, it may not be possible to find a density $h(x)$ that dominates the unnormalized target density $f(\mathbf{y})$ over the whole domain. Or, the constant $c$ is too large, yielding a rather small acceptance probability, which would make the acceptance-rejection sampler impractically slow. A different sampler is then required.

The Metropolis-Hastings sampler overcomes the problems of the acceptance-rejection sampler and generates a sample from a target distribution that is known up to its normalizing constant. Hereto it constructs a Markov chain that converges to the target distribution. A Markov chain is a sequence of random variables $\{ \mathbf{Y}_t \}_{t=1}^{\infty}$ with $\mathbf{Y}_t \in \mathbb{R}^p$ for all $t$ that exihibit a simple dependence relationship among subsequent random variables in the sequence. Here that simple relationship refers to the fact/assumption that the distribution of $\mathbf{Y}_{t+1}$ only depends on $\mathbf{Y}_t$ and not on the part of the sequence preceeding it. The Markov chain's random walk is usually initiated by a single draw from some distribution, resulting in $\mathbf{Y}_1$. From then on the evolution of the Markov chain is described by the transition kernel. The transition kernel is a the conditional density $g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{Y}_{t+1} = \mathbf{y}_a \, | \, \mathbf{Y}_t =\mathbf{y}_b)$ that specifies the distribution of the random variable at the next instance given the realization of the current one. Under some conditions (akin to aperiodicity and irreducibility for Markov chains in discrete time and with a discrete state space), the influence of the initiation washes out and the random walk converges. Not to a specific value, but to a distribution. This is called the stationary distribution, denoted by $\varphi (\mathbf{y})$, and $\mathbf{Y}_t \sim \varphi (\mathbf{y})$ for large enough $t$. The stationary distribution satisfies:

[$] \begin{eqnarray} \label{form.stationary.dist.markov.chain} \varphi (\mathbf{y}_a) & = & \int_{\mathbb{R}^p} g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{Y}_{t+1} = \mathbf{y}_a \, | \, \mathbf{Y}_t =\mathbf{y}_b) \, \varphi(\mathbf{y}_b) \, d\mathbf{y}_b. \end{eqnarray} [$]

That is, the distribution of $\mathbf{Y}_{t+1}$, obtained by marginalization over $\mathbf{Y}_{t}$, coincides with that of $\mathbf{Y}_{t}$. Put differently, the mixing by the transition kernel does not affect the distribution of individual random variables of the chain. To verify that a particular distribution $\varphi(\mathbf{y})$ is the stationary one it is sufficient to verify it satisfies the detailed balance equations:

[$] \begin{eqnarray*} g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_b \, | \, \mathbf{y}_a) \, \varphi(\mathbf{y}_a) & = & g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_a \, | \, \mathbf{y}_b) \, \varphi(\mathbf{y}_b), \end{eqnarray*} [$]

for all choices of $\mathbf{y}_a, \mathbf{y}_b \in \mathbb{R}^p$. If a Markov chain satisfies this detailed balance equations, it is said to be reversible. Reversibility means so much as that, from the realizations, the direction of the chain cannot be discerned as: $f_{\mathbf{Y}_t,\mathbf{Y}_{t+1}} (\mathbf{y}_a, \mathbf{y}_b ) = f_{\mathbf{Y}_t,\mathbf{Y}_{t+1}} (\mathbf{y}_b, \mathbf{y}_a )$, that is, the probability of starting in state $\mathbf{y}_a$ and finishing in state $\mathbf{y}_b$ equals that of starting in $\mathbf{y}_b$ and finishing in $\mathbf{y}_a$. The sufficiency of this condition is evident after its integration on both sides with respect to $\mathbf{y}_b$, from which condition (\ref{form.stationary.dist.markov.chain}) follows.

MCMC assumes the stationary distribution of the Markov chain to be known up to a scalar -- this is the target density from which is to be sampled -- but the transition kernel is unknown. This poses a problem as the transition kernel is required to produce a sample from the target density. An arbitrary kernel is unlikely to satisfy the detailed balance equations with the desired distribution as its stationary one. If indeed it does not, then:

[$] \begin{eqnarray*} g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_b \, | \, \mathbf{y}_a) \, \varphi(\mathbf{y}_a) & \gt & g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_a \, | \, \mathbf{y}_b) \, \varphi(\mathbf{y}_b), \end{eqnarray*} [$]

for some $\mathbf{y}_a$ and $\mathbf{y}_b$. This may (loosely) be interpreted as that the process moves from $\mathbf{y}_a$ to $\mathbf{y}_b$ too often and from $\mathbf{y}_b$ to $\mathbf{y}_a$ too rarely. To correct this the probability of moving from $\mathbf{y}_b$ to $\mathbf{y}_b$ is introduced to reduce the number of moves from $\mathbf{y}_a$ to $\mathbf{y}_b$. As a consequence not always a new value is generated, the algorithm may decide to stay in $\mathbf{y}_a$ (as opposed to acceptance-rejection sampling). To this end a kernel is constructed and comprises compound transitions that propose a possible next state which is simultaneously judged to be acceptable or not. Hereto take an arbitrary candidate-generating density function $g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_{t+1} \, | \, \mathbf{y}_{t})$. Given the current state $\mathbf{Y}_{t} = \mathbf{y}_{t}$, this kernel produces a suggestion $\mathbf{y}_{t+1}$ for the next point in the Markov chain. This suggestion may take the random walk too far afield from the desired and known stationary distribution. If so, the point is to be rejected in favour of the current state, until a new suggestion is produced that is satisfactory close to or representative of the stationary distribution. Let the probability of an acceptable suggestion $\mathbf{y}_{t+1}$, given the current state $\mathbf{Y}_t = \mathbf{y}_t$, be denoted by $\mathcal{A}(\mathbf{y}_{t+1} \, | \, \mathbf{y}_t)$. The thus constructed transition kernel then formalizes to:

[$] \begin{eqnarray*} h_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_{t}}(\mathbf{y}_{a} \, | \, \mathbf{y}_b) & = & g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_a \, | \, \mathbf{y}_b) \, \mathcal{A} (\mathbf{y}_{a} \, | \, \mathbf{y_b}) + r(\mathbf{y}_a) \, \delta_{\mathbf{y_b}} ( \mathbf{y}_a ) , \end{eqnarray*} [$]

where $\mathcal{A} (\mathbf{y}_{a} \, | \, \mathbf{y_b})$ is the acceptance probability of the suggestion $\mathbf{y}_a$ given the current state $\mathbf{y}_b$ and is defined as:

[$] \begin{eqnarray*} \mathcal{A} & = & \min \Big\{ 1, \frac{\varphi(\mathbf{y}_{a})}{\varphi(\mathbf{y}_{b})} \frac{g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_{b} \, | \, \mathbf{y}_a)}{g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_a \, | \, \mathbf{y}_b)} \Big\}. \end{eqnarray*} [$]

Furthermore, $\delta_{\mathbf{y_b}} (\cdot)$ is the Dirac delta function and $r(\mathbf{y}_a) = 1 - \int_{\mathbb{R}^p} g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_a \, | \, \mathbf{y}_b) \mathcal{A} (\mathbf{y}_{a} \, | \, \mathbf{y_b}) \, d\mathbf{y}_a$, which is associated with the rejection. The transition kernel $h_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_{t}}(\cdot \, | \, \cdot)$ equipped with the above specified acceptance probability is referred to as the Metropolis-Hastings kernel. Note that, although the normalizing constant of the stationary distribution may be unknown, the acceptance probability can be evaluated as this constant cancels out in the $\varphi(\mathbf{y}_{a}) \, [\varphi(\mathbf{y}_{b})]^{-1}$ term.

It rests now to verify that $\varphi(\cdot)$ is the stationary distribution of the thus defined Markov process. Hereto first the reversibility of the proposed kernel is assessed. The contribution of the second summand of the kernel to the detailed balance equations, $r(\mathbf{y}_a) \, \delta_{\mathbf{y_b}} ( \mathbf{y}_a ) \varphi ( \mathbf{y}_b)$, exists only if $\mathbf{y}_a = \mathbf{y}_b$. Thus, if it contributes to the detailed balance equations, it does so equally on both sides of the equation. It is now left to assess whether:

[$] \begin{eqnarray*} g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_a \, | \, \mathbf{y}_b) \, \mathcal{A} (\mathbf{y}_{a} \, | \, \mathbf{y_b}) \varphi(\mathbf{y}_b) & = & g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_b \, | \, \mathbf{y}_a) \, \mathcal{A} (\mathbf{y}_{b} \, | \, \mathbf{y_a}) \varphi(\mathbf{y}_a). \end{eqnarray*} [$]

To verify this equality use the definition of the acceptance probability from which we note that if $\mathcal{A} (\mathbf{y}_{a} \, | \, \mathbf{y_b}) \leq 1$ (and thus $\mathcal{A} (\mathbf{y}_{a} \, | \, \mathbf{y_b}) = [ \varphi(\mathbf{y}_{a}) g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_{b} \, | \, \mathbf{y}_a) ] [ \varphi(\mathbf{y}_{b}) g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_a \, | \, \mathbf{y}_b) ]^{-1}$), then $\mathcal{A} (\mathbf{y}_{b} \, | \, \mathbf{y}_a) = 1$ (and vice versa). In either case, the equality in the preceeding display holds, and thereby, together with the observation regarding the second summand, so do the detailed balance equations for Metropolis-Hastings kernel. Then, $\varphi(\cdot)$ is indeed the stationary distribution of the constructed transition kernel $h_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_a \, | \, \mathbf{y}_b)$ as can be verified from Condition (\ref{form.stationary.dist.markov.chain}):

[$] \begin{eqnarray*} & & \hspace{-1.5cm} \int_{\mathbb{R}^p} h_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_a \, | \, \mathbf{y}_b) \, \varphi(\mathbf{y}_b) \, d\mathbf{y}_b \, \, \, = \, \, \, \int_{\mathbb{R}^p} h_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_b \, | \, \mathbf{y}_a) \, \varphi(\mathbf{y}_a) \, d\mathbf{y}_b \\ & = & \int_{\mathbb{R}^p} g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_b \, | \, \mathbf{y}_a) \, \mathcal{A} (\mathbf{y}_b \, | \, \mathbf{y}_a) \, \varphi(\mathbf{y}_a) \, d \mathbf{y}_b + \int_{\mathbb{R}^p} r(\mathbf{y}_b) \delta_{\mathbf{y}_a} (\mathbf{y}_b) \, \varphi(\mathbf{y}_a) \, d \mathbf{y}_b \\ & = & \varphi(\mathbf{y}_a) \int_{\mathbb{R}^p} g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_b \, | \, \mathbf{y}_a) \, \mathcal{A} (\mathbf{y}_b \, | \, \mathbf{y}_a) \, d \mathbf{y}_b + \varphi(\mathbf{y}_a) \int_{\mathbb{R}^p} r(\mathbf{y}_b) \delta_{\mathbf{y}_a} (\mathbf{y}_b) \, d \mathbf{y}_b \\ & = & \varphi(\mathbf{y}_a) [ 1- r(\mathbf{y}_a)] + r(\mathbf{y}_a) \, \varphi(\mathbf{y}_a) \, \, \, = \, \, \, \varphi(\mathbf{y}_a), \end{eqnarray*} [$]

in which the reversibility of $h_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\cdot, \cdot)$, the definition of $r(\cdot)$, and the properties of the Dirac delta function have been used.

The Metropolis-Hastings algorithm is now described by:

Metropolis-Hastings algorithm
input:
• densities $f(\cdot)$ and $h(\cdot)$;
• constant $c \gt0$;
output: a draw from $\pi(\cdot)$.
1. Choose starting value $x^{(0)}$
2. Generate $y$ from $q(x^{(j)}, \cdot)$ and $U$ from $\mathcal{U}[0,1]$
3. if $U \leq \alpha(X^{(j)}, y)$ then
4. set $x^{(j+1)} = y$.
5. else
6. set $x^{(j+1)} = x^{(j)}$.
7. end
8. Return the values $\{ x^{(1)}, x^{(2)}, x^{(3)}, \ldots, \}$

The convergence rate of this sequence is subject of on-going research.

The following R-script shows how the Metropolis-Hastings sampler can be used to from a mixture of two normals $f(y) = \theta \phi_{\mu=2, \sigma^2=0.5}(y) + (1-\theta) \phi_{\mu=-2, \sigma^2=2}(y)$ with $\theta = {1}{4}$. The sampler assumes this distribution is known up to its normalizing constant and uses its unnormalized density $\exp[-(y-2)^2] + 3\exp[-(y+2)^2/4]$. The sampler employs the Cauchy distribution centered at the current state as transition kernel from a candidate for the next state is drawn. The chain is initiated arbitrarily with $Y^{(1)} = 1$.

# define unnormalized mixture of two normals
mixDensity <- function(x){
# unnormalized mixture of two normals
exp(-(x-2)^2) + 3*exp(-(x+2)^2/4)
}

# define the MCMC sampler
mcmcSampler <- function(n, initDraw){
draw <- initDraw
for (i in 1:(n-1)){
# sample a candidate
candidate <- rcauchy(1, draw[i])

# calculate acceptance probability:
alpha <- min(1, mixDensity(candidate) / mixDensity(draw[i]) *
dcauchy(draw[i], candidate) / dcauchy(candidate, draw[i]))

# accept/reject candidate
if (runif(1) < alpha){
draw <- c(draw, candidate)
} else {
draw <- c(draw, draw[i])
}
}
return(draw)
}

# verify by eye-balling
Y <- mcmcSampler(100000, 1)
hist(Y[-c(1:1000)], n=100)


The histogram (not shown) shows that the sampler, although using a unimodal kernel, yields a sample from the bimodal mixture distribution.

The Gibbs sampler is a particular version of the MCMC algorithm. The Gibbs sampler enhances convergence to the stationary distribution (i.e. the posterior distribution) of the Markov chain. It requires, however, the full conditionals of all random variables (here: model parameters), i.e. the conditional distributions of one random variable given all others, to be known analytically. Let the random vector $\mathbf{Y}$ for simplicity -- more refined partitions possible -- be partioned as $(\mathbf{Y}_{a}, \mathbf{Y}_{b})$ with subscripts $a$ and $b$ now refering to index sets that partition the random vector $\mathbf{Y}$ (instead of the previously employed meaning of referring to two -- possibly different -- elements from the state space). The Gibbs sampler thus requires that both $f_{\mathbf{Y}_{a} \, | \, \mathbf{Y}_b} (\mathbf{y}_{a}, \mathbf{y}_{b})$ and $f_{\mathbf{Y}_b \, | \, \mathbf{Y}_a} (\mathbf{y}_a, \mathbf{y}_b)$ are known. Being a specific form of the MCMC algorithm the Gibbs sampler seeks to draw $\mathbf{Y}_{t+1} = (\mathbf{Y}_{a,t+1}, \mathbf{Y}_{b,t+1})$ given the current state $\mathbf{Y}_{t} = (\mathbf{Y}_{a,t}, \mathbf{Y}_{b,t})$. It draws, however, only a new instance for a single element of the partition (e.g. $\mathbf{Y}_{a,t+1}$) keeping the remainder of the partition (e.g. $\mathbf{Y}_{b,t}$) temporarily fixed. Hereto define the transition kernel:

[$] \begin{eqnarray*} g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_{t} } (\mathbf{Y}_{t+1} = \mathbf{y}_{t+1} \, | \, \mathbf{Y}_{t} = \mathbf{y}_{t+1}) & = & \left\{ \begin{array}{ll} f_{\mathbf{Y}_{a,t+1} \, | \, \mathbf{Y}_{b,t+1}} (\mathbf{y}_{a,t+1}, \mathbf{y}_{b,t+1}) & \mbox{if } \mathbf{y}_{b,t+1} = \mathbf{y}_{b,t}, \\ 0 & \mbox{otherwise.} \end{array} \right. \end{eqnarray*} [$]

Using the definition of the conditional density the acceptance probability for the $t+1$-th proposal of the subvector $\mathbf{Y}_a$ then is:

[$] \begin{eqnarray*} \mathcal{A}(\mathbf{y}_{t+1} \, | \, \mathbf{y_t}) & = & \min \Big\{ 1, \frac{\varphi(\mathbf{y}_{t+1})}{\varphi(\mathbf{y}_{t})} \frac{g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_{t}, \mathbf{y}_{t+1})}{g_{\mathbf{Y}_{t+1} \, | \, \mathbf{Y}_t} (\mathbf{y}_{t+1}, \mathbf{y}_t)} \Big\} \\ & = & \min \Big\{ 1, \frac{\varphi(\mathbf{y}_{t+1})}{\varphi(\mathbf{y}_{t})} \frac{f_{\mathbf{Y}_{a,t+1} \, | \, \mathbf{Y}_{b,t+1}} (\mathbf{y}_{a,t}, \mathbf{y}_{b,t}) }{f_{\mathbf{Y}_{a,t+1} \, | \, \mathbf{Y}_{b,t+1}} (\mathbf{y}_{a,t+1}, \mathbf{y}_{b,t+1}) } \Big\} \\ & = & \min \Big\{ 1, \frac{\varphi(\mathbf{y}_{t+1})}{\varphi(\mathbf{y}_{t})} \frac{f_{\mathbf{Y}_{a,t+1}, \mathbf{Y}_{b,t+1}} (\mathbf{y}_{a,t}, \mathbf{y}_{b,t}) / f_{\mathbf{Y}_{b,t+1}} (\mathbf{y}_{b,t}) }{f_{\mathbf{Y}_{a,t+1}, \mathbf{Y}_{b,t+1}} (\mathbf{y}_{a,t+1}, \mathbf{y}_{a,t+1}) / f_{\mathbf{Y}_{b,t+1}} (\mathbf{y}_{b,t+1}) } \Big\} \\ & = & \min \Big\{ 1, \frac{f_{\mathbf{Y}_{b,t+1}} (\mathbf{y}_{b,t+1}) }{f_{\mathbf{Y}_{b,t+1}} (\mathbf{y}_{b,t}) } \Big\} \, \, \, = \, \, \, 1. \end{eqnarray*} [$]

The acceptance probability of each proposal is thus one (which contributes to the enhanced convergence of the Gibbs sampler to the joint posterior). Having drawn an acceptable proposal for this element of the partition, the Gibbs sampler then draws a new instance for the next element of the partition, i.e. now $\mathbf{Y}_b$, keeping $\mathbf{Y}_a$ fixed. This process of subsequently sampling each partition element is repeated until enough samples have been drawn.

To illustrate the Gibbs sampler revisit Bayesian regression. In Section Relation to ridge regression the full conditional distributions of $\bbeta$ and $\sigma^2$ were derived. The Gibbs sampler now draws in alternating fashion from these conditional distributions (see Algorithm for its pseudo-code).

Pseudocode of the Gibbs sampler of the joint posterior of the Bayesian regression parameters
input:
• sample size $T$
• length of burn in period $T_{\mbox{{\tiny burn-in}}}$
• thinning factor $f_{{\mbox{{\tiny thinning}}}}$
• data $\{\mathbf{x}_i, \mathbf{y}_i \}_{i=1}^n$;
• conditional distributions $f_{\sigma^2 \, | \, \bbeta} (\sigma^2, \bbeta; \{\mathbf{x}_i, \mathbf{y}_i \}_{i=1}^n)$ and $f_{\beta \, | \, \sigma^2} (\bbeta, \sigma^2; \{\mathbf{x}_i, \mathbf{y}_i \}_{i=1}^n)$.
output: draws from the joint posterior $\pi(\bbeta, \sigma^2 \, | \, \{\mathbf{x}_i, \mathbf{y}_i \}_{i=1}^n)$.
1. initialize $(\bbeta_0, \sigma^2_{0})$.
2. for $t = 1$ to $T_{\mbox{{\tiny burn-in}}} + T f_{{\mbox{{\tiny thinning}}}}$ do
3. draw $\bbeta_{t}$ from conditional distribution $f_{\bbeta \, | \, \sigma^2} (\bbeta, \sigma_{t-1}^2; \{\mathbf{x}_i, \mathbf{y}_i \}_{i=1}^n)$,
4. draw $\sigma_{t}^2$ from conditional distribution $f_{\sigma^2 \, | \, \bbeta} (\sigma^2, \bbeta_t; \, \{\mathbf{x}_i, \mathbf{y}_i \}_{i=1}^n)$.
5. end
6. Remove the first $T_{\mbox{{\tiny burn-in}}}$ draws (representing the burn-in phase).
7. Select every $f_{\mbox{{\tiny thinning}}}$-th sample (thinning).

## Empirical Bayes

Empirical Bayes (EB) is a branch of Bayesian statistics that meets the subjectivity criticism of frequentists. Instead of fully specifying the prior distribution empirical Bayesians identify only its form. The hyper parameters of this prior distribution are left unspecified and are to be found empirically. In practice, these hyper-parameters are estimated from the data at hand. However, the thus estimated hyper parameters are used to obtain the Bayes estimator of the model parameters. As such the data are then used multiple times. This is usually considered an inappropriate practice but is deemed acceptable when the number of model parameters is large in comparison to the number of hyper parameters. Then, the data are not used twice but ‘$1+\varepsilon$’-times (i.e. once-and-a-little-bit) and only little information from the data is spent on the estimation of the hyper parameters. Having obtained an estimate of the hyper parameters, the prior is fully known, and the posterior distribution (and summary statistics thereof) are readily obtained by Bayes' formula.

The most commonly used procedure for the estimation of the hyper parameters is marginal likelihood maximization, which is a maximum likelihood-type procedure. But the likelihood cannot directly be maximized with respect to the hyper parameters as it contains the model parameters that are assumed to be random within the Bayesian framework. This may be circumvented by choosing a specific value for the model parameter but would render the resulting hyper parameter estimates dependent on this choice. Instead of maximization with the model parameter set to a particular value one would preferably maximize over all possible realizations. The latter is achieved by marginalization with respect to the random model parameter, in which the (prior) distribution of the model parameter is taken into account. This amounts to integrating out the model parameter from the posterior distribution, i.e. $\int P(\mathbf{Y} = \mathbf{y} \, | \, \theta) \, \pi (\theta) \, d \theta$, resulting in the so-called marginal posterior. After marginalization the specifics of the model parameter have been discarded and the marginal posterior is a function of the observed data and the hyper parameters. The estimator of the hyper parameter is now defined as the maximizer of this marginal posterior.

To illustrate the estimation of hyper parameters of the Bayesian linear regression model through marginal likelihood maximization assume the regression parameter $\bbeta$ and and the error variance $\sigma^2$ to be endowed with conjugate priors: $\bbeta \, | \, \sigma^2 \sim \mathcal{N}(\mathbf{0}_p, \sigma^2 \lambda^{-1} \mathbf{I}_{pp})$ and $\sigma^2 \sim \mathcal{IG}(\alpha_0, \beta_0)$. Three hyper parameters are thus to be estimated: the shape and scale parameters, $\alpha_0$ and $\beta_0$, of the inverse gamma distribution and the $\lambda$ parameter related to the variance of the regression coefficients. Straightforward application of the outlined marginal likelihood principle does, however, not work here. The joint prior, $\pi(\bbeta \, | \, \sigma^2) \pi (\sigma^2)$, is too flexible and does not yield sensible estimates of the hyper parameters. As interest is primarily in $\lambda$, this is resolved by setting the hyper parameters of $\pi (\sigma^2)$ such that the resulting prior is uninformative, i.e. as objectively as possible. This is operationalized as a very flat distribution. Then, with the particular choices of $\alpha_0$ and $\beta_0$ that produce an uninformative prior for $\sigma^2$, the empirical Bayes estimate of $\lambda$ is:

[$] \begin{eqnarray*} \hat{\lambda}_{eb} & = & \arg \max_{\lambda} \int_{0}^{\infty} \int_{\mathbb{R}^p} f_{\bbeta, \sigma^2} (\bbeta, \sigma^2 \, | \, \mathbf{Y}, \mathbf{X}) \, d \bbeta \, d \sigma^2 \\ & = & \arg \max_{\lambda} \int_{0}^{\infty} \int_{\mathbb{R}^p} \sigma^{-n} \exp [ - \tfrac{1}{2}\sigma^{-2} ( \mathbf{Y} - \mathbf{X} \bbeta)^{\top} ( \mathbf{Y} - \mathbf{X} \bbeta) ] \\ & & \qquad \qquad \qquad \qquad \times \, \, \sigma^{-p} \exp ( - \tfrac{1}{2}\sigma^{-2} \lambda \bbeta^{\top} \bbeta ) \, \times \, \, (\sigma^2)^{-\alpha_0 - 1} \exp ( - \beta_0 \sigma^{-2} ) \, d \bbeta \, d \sigma^2 \\ & = & \arg \max_{\lambda} \int_{0}^{\infty} \int_{\mathbb{R}^p} \sigma^{-n} \exp \{ - \tfrac{1}{2} \sigma^{-2} [ \mathbf{Y}^{\top} \mathbf{Y} - \mathbf{Y}^{\top} \mathbf{X} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} \mathbf{X}^{\top} \mathbf{Y} \} \\ & & \qquad \qquad \qquad \qquad \times \, \, \sigma^{-p} \exp \{ - \tfrac{1}{2} \sigma^{-2} \, [ \bbeta - \hat{\bbeta}(\lambda) ]^{\top} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp}) [ \bbeta - \hat{\bbeta}(\lambda) ] \} \\ & & \qquad \qquad \qquad \qquad \times \, \, (\sigma^2)^{-\alpha_0-1} \exp ( - \beta_0 \sigma^{-2} ) \, d \bbeta \, d \sigma^2 \\ & = & \arg \max_{\lambda} \int_{0}^{\infty} \sigma^{-n} \exp \{ - \tfrac{1}{2} \sigma^{-2} [ \mathbf{Y}^{\top} \mathbf{Y} - \mathbf{Y}^{\top} \mathbf{X} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} \mathbf{X}^{\top} \mathbf{Y} \} \\ & & \qquad \qquad \qquad \qquad \times \, \, | \mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp}|^{-1/2} \, (\sigma^2)^{-\alpha_0-1} \exp ( - \beta_0\sigma^{-2} ) \, d \sigma^2 \\ & = & \arg \max_{\lambda} \, | \mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp}|^{-1/2} \int_{0}^{\infty} \exp \big( - \sigma^{-2} \{ \beta_0 + \tfrac{1}{2} [ \mathbf{Y}^{\top} \mathbf{Y} - \mathbf{Y}^{\top} \mathbf{X} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp})^{-1} \mathbf{X}^{\top} \mathbf{Y} ] \} \big) \\ & & \qquad \qquad \qquad \qquad \qquad \qquad \times \, \, \, (\sigma^2)^{-\alpha_0-n/2 - 1} \, d \sigma^2 \\ & = & \arg \max_{\lambda} \, |\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp}|^{-1/2} \, b_{1}^{-a_0 - n/2}, \end{eqnarray*} [$]

where the factors not involving $\lambda$ have been dropped throughout and $b_{1} = \beta_0 + \tfrac{1}{2} [ \mathbf{Y}^{\top} \mathbf{Y} - \mathbf{Y}^{\top} \mathbf{X} (\mathbf{X}^{\top} \mathbf{X} + \lambda \mathbf{I}_{pp} )^{-1} \mathbf{X}^{\top} \mathbf{Y} ]$. Prior to the maximization of the marginal likelihood the logarithm is taken. That changes the maximum, but not its location, and yields an expression that is simpler to maximize. With the empirical Bayes estimate $\hat{\lambda}_{eb}$ at hand, the Bayesian estimate of the regression parameter $\bbeta$ is $\hat{\bbeta}_{eb} = (\mathbf{X}^{\top} \mathbf{X} + \hat{\lambda}_{eb} \mathbf{I}_{pp})^{-1} \mathbf{X}^{\top} \mathbf{Y}$. Finally, the particular choice of the hyper parameters of the prior on $\sigma^2$ is not too relevant. Most values of $\alpha_0$ and $\beta_0$ that correspond to a rather flat inverse gamma distribution yield resulting point estimates $\hat{\bbeta}_{eb}$ that do not differ too much numerically.

## Conclusion

Bayesian regression was introduced and shown to be closely connected to ridge regression. Under a conjugate Gaussian prior on the regression parameter the Bayesian regression estimator coincides with the ridge regression estimator, which endows the ridge penalty with the interpretation of this prior. While an analytic expression of these estimators is available, a substantial part of this chapter was dedicated to evaluation of the estimator through resampling. The use of this resampling will be evident when other penalties and non-conjugate priors will be studied (cf. Exercise \ref{} and Sections The Bayesian connection and The Bayesian connection ). Finally, another informative procedure, empirical Bayes, to choose the penalty parameter was presented.

## General References

van Wieringen, Wessel N. (2021). "Lecture notes on ridge regression". arXiv:1509.09169 [stat.ME].

## References

1. Bijma, F., Jonker, M. A., and van der Vaart, A. W. (2017).An Introduction to Mathematical Statistics.Amsterdam University Press
2. Mathai, A. M. and Provost, S. B. (1992).Quadratic Forms in Random Variables: Theory and Applications.Dekker
3. Flury, B. D. (1990).Acceptance--rejection sampling made easy.SIAM Review, 32(3), 474--476