Siilats Robots
Regression lessons

Regularized Regression in Excel and MATLAB CVX | Worked Example

Build a regularized regression model from a small Excel dataset and reproduce it in MATLAB with CVX. The example uses synthetic scooter-rental data to show how residuals and categorical predictors affect the fit. The final step uses an L1 penalty to encourage selected coefficients to agree.

Watch on Siilats Robots

Screen times follow the original lesson. Edited YouTube versions may run shorter.

At 0:00 in the original

Build the input table

Arrange the response and predictors in columns with one row per observation. Export a rectangular numeric dataset for MATLAB.

Screen recording at 0:00
Screen at 0:00
At 0:55 in the original

Fit a baseline and inspect residuals

Use the CSV to reproduce the unregularized fit. A residual is the observed value minus the prediction.

Screen recording at 0:55
Screen at 0:55
At 1:50 in the original

Encode the categories

Build indicator columns for categories and choose a reference category when using an intercept.

Screen recording at 1:50
Screen at 1:50
At 2:48 in the original

Reproduce the fit in CVX

Express coefficients as variables and residual size as the objective. Compare the optimizer’s predictions with the baseline before adding penalties.

Screen recording at 2:48
Screen at 2:48
At 4:30 in the original

Add an L1 penalty

Penalize coefficient differences when groups should behave similarly. Larger penalty weights encourage agreement at the expense of some in-sample fit.

Screen recording at 4:30
Screen at 4:30