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.

Consider the following function, and a simulated dataset obtained by adding noise to it.

<Figure size 1000x600 with 1 Axes>
<Figure size 640x480 with 1 Axes>

We will use the following model for this dataset:

yt=β0+β1I{t≥2}+β2I{t≥3}+⋯+βn−1I{t≥n}+ϵty_t = \beta_0 + \beta_1 I\{t \geq 2 \} + \beta_2 I\{t \geq 3 \} + \dots + \beta_{n-1} I\{t \ge n \} + \epsilon_t

This model is slightly different from the one used in class. It has indicators instead of ReLUs. This model is more appropriate in this problem because of the change point structure.

This model can be written as y=Xβ+ϵy = X \beta + \epsilon where

X=(100⋅⋅⋅0110⋅⋅⋅0111⋅⋅⋅0⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅111⋅⋅⋅1)X = \begin{pmatrix} 1 & 0 & 0 & \cdot & \cdot & \cdot & 0 \\ 1 & 1 & 0 & \cdot & \cdot & \cdot & 0 \\ 1 & 1 & 1 & \cdot & \cdot & \cdot & 0 \\ \cdot & \cdot & \cdot & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & \cdot & \cdot & \cdot \\ 1 & 1 & 1 & \cdot & \cdot & \cdot & 1 \end{pmatrix}

Derive that the unregularized estimate of β\beta is given by:

β^0=y1    and    β^t=yt+1−yt\hat{\beta}_0 = y_1 ~~~\text{ and }~~~ \hat{\beta}_t = y_{t+1} - y_t

for t=1,2,…,n−1t = 1, 2, \dots, n-1. This can be verified as follows.

[ 1.5235854  -6.72350593  8.95217651 ...  1.92407071  8.27388838
 -0.81259816]
[-6.72350593  8.95217651  0.9505676  ...  1.92407071  8.27388838
 -0.81259816]
1.5235853987721568

Let us now see the performance of the regularized estimators. The ridge estimator minimizes:

∑t=1n(yt−β0−β1I{t≥2}−β2I{t≥3}−⋯−βn−1I{t≥n})2+λ∑t=1n−1βt2\sum_{t=1}^n \left(y_t - \beta_0 - \beta_1 I\{t \geq 2 \} - \beta_2 I\{t \geq 3 \} - \dots - \beta_{n-1} I\{t \ge n \}\right)^2 + \lambda \sum_{t=1}^{n-1} \beta_t^2

and the LASSO estimator minimizes

∑t=1n(yt−β0−β1I{t≥2}−β2I{t≥3}−⋯−βn−1I{t≥n})2+λ∑t=1n−1∣βt∣\sum_{t=1}^n \left(y_t - \beta_0 - \beta_1 I\{t \geq 2 \} - \beta_2 I\{t \geq 3 \} - \dots - \beta_{n-1} I\{t \ge n \}\right)^2 + \lambda \sum_{t=1}^{n-1} |\beta_t|

We use the following functions (from class) to compute the ridge and lasso estimators.

<Figure size 1000x600 with 1 Axes>
<Figure size 1000x600 with 1 Axes>

The LASSO fit is piecewise constant while the ridge fit is smoother (unless λ\lambda is too small). Since the true function is also piecewise constant, LASSO gives better estimates in this problem compared to ridge regression.

Let us now use 5-fold cross validation to pick the tuning parameter λ\lambda. The CV method discussed in class does not seem to work well for this dataset. Instead, we use 5-fold cross-validation with random splits i.e., in each split, we randomly take 20% of the data to be the test set and the remaining is the training set.

10.0
CV errors for each lambda:
Lambda = 0.10, CV Error = 35.039675
Lambda = 1.00, CV Error = 30.739211
Lambda = 10.00, CV Error = 28.242132
Lambda = 100.00, CV Error = 29.608350
Lambda = 1000.00, CV Error = 36.707513
Lambda = 10000.00, CV Error = 48.736805
Lambda = 100000.00, CV Error = 67.014833

The best λ\lambda given by CV for ridge is 10. This value is small enough for the method to pick up the jumps well. But unfortunately the estimate will be too wiggly in the constant parts.

<Figure size 1000x600 with 1 Axes>
100.0
CV errors for each lambda:
Lambda = 0.10, CV Error = 36.578387
Lambda = 1.00, CV Error = 35.246306
Lambda = 10.00, CV Error = 28.782759
Lambda = 100.00, CV Error = 27.222506
Lambda = 1000.00, CV Error = 45.648172

The best λ\lambda chosen by cross validation is 100; which seems a good choice for this dataset.

<Figure size 1000x600 with 1 Axes>

What happens when we try the same model (with ReLU functions) as in class for this dataset?

[[ 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  2.045e+03  2.044e+03 ...  1.000e+00  0.000e+00 -0.000e+00]
 [ 1.000e+00  2.046e+03  2.045e+03 ...  2.000e+00  1.000e+00  0.000e+00]
 [ 1.000e+00  2.047e+03  2.046e+03 ...  3.000e+00  2.000e+00  1.000e+00]]

Let us try the LASSO estimator.

<Figure size 1000x600 with 1 Axes>
/Users/aditya/mambaforge/envs/stat153fall2026/lib/python3.10/site-packages/cvxpy/problems/problem.py:1539: UserWarning: Solution may be inaccurate. Try another solver, adjusting the solver settings, or solve with verbose=True for more information.
  warnings.warn(
<Figure size 1000x600 with 1 Axes>

When λ\lambda is small, this estimator will be too wiggly in the constant parts. On the other hand, when λ\lambda is large, it will not be able to sharply detect the jumps (compare λ=100\lambda = 100 vs λ=1000\lambda = 1000).