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.

Recap: Ridge Regression

Let us first recap ridge regression from last lecture by considering one of the temperature anomalies datasets from last lecture.

      year  month  value
0     1895      1   43.1
1     1895      2   37.4
2     1895      3   54.5
3     1895      4   63.4
4     1895      5   69.5
...    ...    ...    ...
1575  2026      4   67.0
1576  2026      5   70.5
1577  2026      6   77.8
1578  2026      7   81.7
1579  2026      8   81.7

[1580 rows x 3 columns]
<Figure size 1000x500 with 1 Axes>

Instead of working with the raw temperature data, we shall we work with anomalies (relative to the 1901-2000 average).

<Figure size 1000x500 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.

1580
[[1.000e+00 0.000e+00 0.000e+00 ... 0.000e+00 0.000e+00 0.000e+00]
 [1.000e+00 1.000e+00 0.000e+00 ... 0.000e+00 0.000e+00 0.000e+00]
 [1.000e+00 2.000e+00 1.000e+00 ... 0.000e+00 0.000e+00 0.000e+00]
 ...
 [1.000e+00 1.577e+03 1.576e+03 ... 1.000e+00 0.000e+00 0.000e+00]
 [1.000e+00 1.578e+03 1.577e+03 ... 2.000e+00 1.000e+00 0.000e+00]
 [1.000e+00 1.579e+03 1.578e+03 ... 3.000e+00 2.000e+00 1.000e+00]]

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 used the following code for computing the ridge estimator for fixed λ\lambda (note that, instead of using this code, we can also use the formula derived in Lecture 12: (XTX+λJ)−1XTy(X^T X + \lambda J)^{-1} X^T y to compute the ridge estimator).

Instead of the above function which use cvxpy, we can directly 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 code below can be used to check that both these approaches give the same estimator.

<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 λ=1011\lambda = 10^{11}, the fitted values coincide with linear regression. So we take λmin⁡:=10−2\lambda_{\min} := 10^{-2} and λmax=1011\lambda_{max} = 10^{11}.

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 1.e+11]

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

100000000.0
Lambda = 0.01, CV Error = 129699.733663
Lambda = 0.10, CV Error = 86557.405815
Lambda = 1.00, CV Error = 22373.552499
Lambda = 10.00, CV Error = 2646.339789
Lambda = 100.00, CV Error = 303.444306
Lambda = 1000.00, CV Error = 27.187489
Lambda = 10000.00, CV Error = 16.733096
Lambda = 100000.00, CV Error = 11.931114
Lambda = 1000000.00, CV Error = 4.639443
Lambda = 10000000.00, CV Error = 3.038500
Lambda = 100000000.00, CV Error = 2.849374
Lambda = 1000000000.00, CV Error = 2.917165
Lambda = 10000000000.00, CV Error = 2.957611
Lambda = 100000000000.00, CV Error = 2.978341

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*}

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

Below is the posterior for log⁡λ\log \lambda:

<Figure size 640x480 with 1 Axes>

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

<Figure size 640x480 with 1 Axes>

Using this posterior, we can compute posterior mean and mode for λ\lambda:

Posterior mean: 1.228e+08
Posterior standard deviation: 1.013e+08
Posterior median: 9.572e+07
95% credible interval: (1.788e+07, 3.914e+08)

We can visualize the fitted curves when λ\lambda is taken to be the posterior mean and the posterior mode

<Figure size 640x480 with 1 Axes>

For full uncertainty quantification, we can generate posterior samples from the full posterior of (β,λ,σ)(\beta, \lambda, \sigma). This is done in the following way: first sample from the numerical discrete posterior from log⁡λ\log \lambda (leading to a sample of λ\lambda), use this sample of λ\lambda to obtain a sample from σ\sigma, then use the two values of λ,σ\lambda, \sigma to sample from β\beta.

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>

We can also visualize the posterior σ\sigma values:

<Figure size 640x480 with 1 Axes>