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.

We shall illustrate Bayesian ridge regression in the context of the following simulation example. There are two choices of the underlying true trend function ff in this code. The first choice corresponds to a smooth ff while the second choice is more wiggly.

<Figure size 1200x600 with 1 Axes>

We will fit our high-dimensional regression model:

yt=β0+β1(t−1)+β2(t−2)++⋯+βn−1(t−(n−1))++ϵt\begin{align*} y_t = \beta_0 + \beta_1 (t - 1) + \beta_2 (t - 2)_+ + \dots + \beta_{n-1}(t - (n-1))_+ + \epsilon_t \end{align*}

to this dataset. This can be written in the usual regression form as y=Xβ+ϵy = X \beta + \epsilon with the XX matrix calculated as follows.

1000
[[  1.   0.   0. ...   0.   0.   0.]
 [  1.   1.   0. ...   0.   0.   0.]
 [  1.   2.   1. ...   0.   0.   0.]
 ...
 [  1. 997. 996. ...   1.   0.   0.]
 [  1. 998. 997. ...   2.   1.   0.]
 [  1. 999. 998. ...   3.   2.   1.]]

Ridge regression is given by the minimizer of:

∥y−Xβ∥2+λ∑j=2n−1βj2\begin{align*} \|y - X \beta\|^2 + \lambda \sum_{j=2}^{n-1} \beta_j^2 \end{align*}

where λ\lambda is the tuning parameter. Small λ\lambda leads to fitted values close to the data (overfitting) and large λ\lambda leads to fitted values coming from a straight line (underfitting).

We can compute the ridge estimator using the formula: (XTX+λJ)−1XTy(X^T X + \lambda J)^{-1} X^T y. Here JJ is the diagonal matrix with first two diagonal entries 0 and all other diagonal entries equal to 1.

The following code computes the ridge regression estimator for a given value of λ\lambda.

<Figure size 640x480 with 1 Axes>

One important aspect in ridge regression is the choice of the regularization parameter λ\lambda. For this, the first step is to identify two values λmin⁡\lambda_{\min} and λmax⁡\lambda_{\max} which serve as the two extreme ends for λ\lambda. When λ=λmin⁡\lambda = \lambda_{\min}, the fitted values are basically the same as the raw data, while when λ=λmax⁡\lambda = \lambda_{\max}, the fitted values coincide with a simple line fit.

Using the above code, we can check that when λ=10−2\lambda = 10^{-2}, the fitted values are basically the same as the raw data; and when λ=1010\lambda = 10^{10}, the fitted values coincide with linear regression. So we take λmin⁡:=10−2\lambda_{\min} := 10^{-2} and λmax⁡=1010\lambda_{\max} = 10^{10}.

Then we take a grid of λ\lambda by taking log⁡λ\log \lambda values to be equally spaced between log⁡λmin⁡\log \lambda_{\min} and log⁡λmax⁡\log \lambda_{\max}.

[1.e-02 1.e-01 1.e+00 1.e+01 1.e+02 1.e+03 1.e+04 1.e+05 1.e+06 1.e+07
 1.e+08 1.e+09 1.e+10]

To choose λ\lambda, we used cross-validation (CV) in the last lecture via the following code.

10000.0
Lambda = 0.01, CV Error = 6832.208809
Lambda = 0.10, CV Error = 3913.131006
Lambda = 1.00, CV Error = 1294.868455
Lambda = 10.00, CV Error = 843.519238
Lambda = 100.00, CV Error = 248.158481
Lambda = 1000.00, CV Error = 38.426146
Lambda = 10000.00, CV Error = 1.984292
Lambda = 100000.00, CV Error = 2.751585
Lambda = 1000000.00, CV Error = 4.226234
Lambda = 10000000.00, CV Error = 4.653630
Lambda = 100000000.00, CV Error = 4.808029
Lambda = 1000000000.00, CV Error = 4.173930
Lambda = 10000000000.00, CV Error = 3.162977

Below we calculate the ridge estimator for λ\lambda chosen to be the best λ\lambda given by CV:

<Figure size 640x480 with 1 Axes>

Next we shall use Bayesian regularization for selecting λ\lambda.

Bayesian Regularization

We work with the following prior:

β0,β1∼i.i.dN(0,C)   and   β2,…,βn−1∼i.i.dN(0,σ2/λ).\begin{align*} \beta_0, \beta_1 \overset{\text{i.i.d}}{\sim} N(0, C) ~~ \text{ and } ~~ \beta_2, \dots, \beta_{n-1} \overset{\text{i.i.d}}{\sim} N(0, \sigma^2/\lambda). \end{align*}

We also treat σ\sigma and λ\lambda as unknown parameters and assign the prior:

λ∼fλ(λ)   and   log⁡σ∼unif(−C,C)\begin{align*} \lambda \sim f_{\lambda}(\lambda) ~~ \text{ and } ~~ \log \sigma \sim \text{unif}(-C, C) \end{align*}

where

fλ(λ)∝I(λmin⁡≤λ≤λmax⁡)λ  which corresponds to log⁡λ∼unif(log⁡λmin⁡,log⁡λmax⁡).\begin{align*} f_{\lambda}(\lambda) \propto \frac{I(\lambda_{\min} \leq \lambda \leq \lambda_{\max})}{\lambda} ~\text{ which corresponds to}~ \log \lambda \sim \text{unif}(\log \lambda_{\min}, \log \lambda_{\max}). \end{align*}

As derived in the lecture, the posterior of β,σ,λ\beta, \sigma, \lambda then becomes:

β∣data,σ,λ∼N((XTX+λJ)−1XTy,σ2(XTX+λJ)−1)\begin{align*} \beta \mid \text{data}, \sigma, \lambda \sim N\left((X^TX + \lambda J)^{-1} X^T y, \sigma^2 (X^T X + \lambda J)^{-1} \right) \end{align*}

and

1σ2∣data,λ∼Gamma(n2−1,yTy−yTX(XTX+λJ)−1XTy2).\begin{align*} \frac{1}{\sigma^2} \mid \text{data}, \lambda \sim \text{Gamma} \left(\frac{n}{2} - 1, \frac{y^T y - y^T X(X^T X + \lambda J)^{-1} X^T y}{2} \right). \end{align*}

and

fλ∣data(λ)∝fλ(λ)λ(n−2)/2∣XTX+λJ∣−1/2(yTy−yTX(XTX+λJ)−1XTy)(n/2)−1\begin{align*} f_{\lambda\mid\text{data}}(\lambda) \propto f_{\lambda}(\lambda) \frac{\lambda^{(n-2)/2}|X^TX+\lambda J|^{-1/2}} {\left(y^Ty-y^TX(X^TX+\lambda J)^{-1}X^Ty\right)^{(n/2)-1}} \end{align*}

The key here is the posterior for λ\lambda. We compute this posterior numerically below by taking a grid of values for log⁡λ\log \lambda. The computation proceeds more stably if we evaluate the posterior density of log⁡λ\log \lambda:

flog⁡λ∣data(x)∝e(n/2−1)x∣XTX+exJ∣−1/2(yTy−yTX(XTX+exJ)−1XTy)n/2−1I{log⁡λmin⁡≤x≤log⁡λmax⁡}.\begin{align*} f_{\log \lambda \mid \text{data}}(x) \propto \frac{e^{(n/2 - 1)x} |X^T X + e^x J|^{-1/2}}{\left( y^T y - y^T X (X^TX + e^x J)^{-1} X^T y \right)^{n/2 - 1}} I\{\log \lambda_{\min} \leq x \leq \log \lambda_{\max}\}. \end{align*}
0
50
100
150
200
250
300
350
400
450
500
550
600
650
700
750
800
850
900
950
<Figure size 640x480 with 1 Axes>

Take logs with base 10 for better comparison with the previous set of λ\lambda’s.’

<Figure size 640x480 with 1 Axes>

Below we compute the posterior mean and median (also uncertainty intervals) for λ\lambda.

Posterior mean: 1.275e+04
Posterior standard deviation: 8.549e+03
Posterior mode (grid approximation): 7.905e+03
Posterior median: 1.042e+04
95% credible interval: (4.546e+03, 3.520e+04)

Below we compute the regression fitted values when λ\lambda is chosen to be the posterior mean and median.

<Figure size 640x480 with 1 Axes>

Below we run full Bayesian inference by selecting N=1000N = 1000 posterior samples.

0
50
100
150
200
250
300
350
400
450
500
550
600
650
700
750
800
850
900
950

The posterior samples corresponding to the fitted curves are plotted below. Here by fitted curve, we mean:

μt(i)=β0(i)+β1i(t−1)+∑j=2n−1βj(i)(t−j)+\begin{align*} \mu_t^{(i)} = \beta^{(i)}_0 + \beta^{{i}}_1 (t - 1) + \sum_{j=2}^{n-1} \beta^{(i)}_j(t - j)_+ \end{align*}

where βj(i),j=0,1,…,n−1\beta^{(i)}_j, j = 0, 1, \dots, n-1 is the ii-th posterior sample.

<Figure size 1000x500 with 1 Axes>

The above plot gives an indication of the uncertainty present in the β\beta estimates.

The following is the histogram of σ\sigma values.

<Figure size 640x480 with 1 Axes>