345. Regression

345.1. Linear Regression

345.1.1. Simple Linear Regression

345.1.1.1. Data and Model

We observe a dataset of 𝑛 samples:

(𝑥𝑖,𝑦𝑖)𝑖=1,,𝑛

We assume a linear relationship between 𝑥 and 𝑦:

𝑦=𝛼+𝛽𝑥𝑖+𝜀𝑖

Where:

345.1.1.2. Optimization Problem

The parameters 𝛼 and 𝛽 are estimated by minimizing the sum of square errors (SSE)

min𝛼,𝛽𝑓(𝛼,𝛽)=𝑖=1𝑛[𝑦𝑖(𝛼+𝛽𝑥1)]2

This is an unconstrained convex optimization problem

345.1.1.3. Gradient and Hessian

Compute the partial derivatives:

𝜕𝑓𝜕𝛼=2𝑖=1𝑛[𝑦𝑖(𝛼+𝛽𝑥1)]𝜕𝑓𝜕𝛽=2𝑖=1𝑛[𝑦𝑖(𝛼+𝛽𝑥1)]𝑥𝑖

Thus, the gradient is:

𝑓(𝛼,𝛽)=[2𝑖=1𝑛[𝑦𝑖(𝛼+𝛽𝑥1)]2𝑖=1𝑛[𝑦𝑖(𝛼+𝛽𝑥1)]𝑥𝑖]

and the Hessian matrix is:

2𝑓(𝛼,𝛽)=[2𝑛2𝑖=1𝑛𝑥𝑖2𝑖=1𝑛𝑥𝑖2𝑖=1𝑛𝑥𝑖2]=2[𝑛𝑖=1𝑛𝑥𝑖𝑖=1𝑛𝑥𝑖𝑖=1𝑛𝑥𝑖2]
345.1.1.4. Convexity

The Hessian is constant (does not depend on 𝛼, 𝛽) and symmetric. We check positive semidefiniteness via the determinant:

det(2𝑓)=4(𝑛𝑖=1𝑛𝑥𝑖2(𝑖=1𝑛𝑥𝑖)2)=4𝑖<𝑗(𝑥𝑖𝑥𝑗)20

Sincle this expression is nonnegative for all 𝑥𝑖, 𝑓 is convex

345.1.1.5. Optimality Conditions

Setting the gradient equal to zero gives the normal equaltions:

{𝑛𝛼+(𝑖=1𝑛𝑥𝑖)𝛽=𝑖=1𝑛𝑦𝑖(𝑖=1𝑛𝑥𝑖)𝛼+(𝑖=1𝑛𝑥𝑖2)𝛽=𝑖=1𝑛𝑥𝑖𝑦𝑖

Solving this linear system yields the closed-form solution:

𝛽̂=𝑖(𝑥𝑖𝑥̄)(𝑦𝑖𝑦̄)𝑖(𝑥𝑖𝑥̄)2𝛼̂=𝑦̄𝛽̂𝑥̄

where:

𝑥̄=1𝑛𝑖=1𝑛𝑥𝑖𝑦̄=1𝑛𝑖=1𝑛𝑦𝑖
345.1.1.6. Convexity by Decomposition

Alternatively, decompose 𝑓(𝛼,𝛽) as:

𝑓(𝛼,𝛽)=𝑖=1𝑛[𝑦𝑖(𝛼+𝛽𝑥𝑖)]2=𝑖=1𝑛𝑓𝑖(𝛼,𝛽)𝑓𝑖(𝛼,𝛽)=[𝑦𝑖(𝛼+𝛽𝑥𝑖)]2

Expanding 𝑓𝑖:

𝑓𝑖(𝛼,𝛽)=𝑦𝑖2+𝛼2+𝛽2𝑥𝑖22𝛼𝑦𝑖2𝛽𝑥𝑖𝑦𝑖+2𝛼𝛽𝑥𝑖

Each 𝑓𝑖 is a quadratic function in (𝛼, 𝛽)

Each 𝑓𝑖 has Hessian:

2𝑓𝑖(𝛼,𝛽)=[22𝑥𝑖2𝑥𝑖2𝑥𝑖2]=2[1𝑥𝑖𝑥𝑖𝑥𝑖2]

Since each 2𝑓𝑖 is positive semidefinite, and sums of convex functions are convex, 𝑓 is convex

Example

345.1.2. Multiple Linear Regression

345.1.2.1. Data and Model

For 𝑛 samples and 𝑝 predictors:

(𝑥1(𝑖),𝑥2(𝑖),,𝑥𝑝(𝑖),𝑦𝑖)𝑖=1,,𝑛

The model is:

𝑦𝑖=𝛼+𝛽1𝑥1(𝑖)++𝛽𝑝𝑥𝑝(𝑖)+𝜀𝑖=𝛼+𝜷𝑇𝐱(𝑖)+𝜀𝑖
345.1.2.2. Optimization Problem
min𝛼,𝛽𝑓(𝛼,𝛽)

where

𝑓(𝛼,𝛽)=𝑖=1𝑛[𝑦𝑖(𝛼+𝑗=1𝑝𝛽𝑗𝑥𝑗𝑖)]2=𝑖=1𝑛[𝑦𝑖(𝛼+𝜷𝑇𝐱(𝑖))]2

In matrix form, letting 𝑋𝑛×𝑝 be the data matrix, 𝒚𝑛, and 𝟏 the column of ones:

min𝛼,𝛽𝒚𝛼𝟏𝑋𝛽22

The closed-form solution is:

𝛽̂=(𝑋𝑇𝑋)1𝑋𝑇(𝐲𝑦̄𝟏)𝛼̂=𝑦̄𝛽̂𝑇𝐱̄

provided 𝑋𝑇𝑋 is invertible

345.1.3. Regularization

Regularization penalizes large coefficients to prevent overfitting and improve numerical stability.

Let 𝜆>0 be a regularization parameter.

345.1.3.1. Ridge Regression (L2 Regularization)
min𝛼,𝛽𝑖=1𝑛[𝑦𝑖(𝛼+𝜷𝑇𝐱𝑖)]2+𝜆𝑗=1𝑝𝛽𝑗2

In matrix form:

min𝛽𝐲𝑋𝜷22+𝜆𝜷22

Closed-form solution (for centered data):

𝜷̂=(𝑋𝑇𝑋+𝜆𝐼𝑝)1𝑋𝑇𝐲

Ridge regression shrinks coefficients toward zero but does not set them exactly to zero.

345.1.3.2. LASSO Regression (L1 Regularization)
min𝛼,𝛽𝑖=1𝑛[𝑦𝑖(𝛼+𝛽𝑇𝑥𝑖)]2+𝜆𝑗=1𝑝|𝛽𝑗|

LASSO performs feature selection because some coefficients can become exactly zero.

This problem is still convex but non-differentiable due to the absolute value term.

Example