Blame

e0287e lugonthier 2026-07-02 15:52:15
Add new content and images for machine learning and mathematics modules - Added images for regularization and high-dimensional inference. - Introduced Support Vector Machines (SVM) module with detailed explanations and images. - Created Decision Trees and Ensemble Methods module with comprehensive content and illustrations. - Added a Mathematics overview module and a refresher on mathematical concepts essential for machine learning. - Included SVG diagrams for Bayes' rule and multivariate Gaussian distribution.
1
# 5. Linear regression
2
3
Linear regression predicts a continuous target from a linear score $\theta^T x$. This module presents it probabilistically, building on the [Probabilistic formulation](/en/Machine%20Learning/04%20Probabilistic%20formulation) module: the model (extended to polynomial features), fitting by maximum likelihood, which turns out to be ordinary least squares, and fitting by maximum a posteriori, which adds a prior and yields a regularized fit.
4
5
**Objectives**
6
- Write the linear and polynomial regression model and fit it by least squares.
7
- Give regression a probabilistic formulation with Gaussian noise.
8
- See that maximum likelihood under that model is exactly least squares.
9
- Add a prior and fit by maximum a posteriori, recovering a regularized fit.
10
11
## 5.1 The linear and polynomial model
12
13
The hypothesis is linear in the augmented input $x \in \mathbb{R}^{n+1}$ with $x_0 = 1$ and parameters $\theta$:
14
15
$$\boxed{ h_\theta(x) = \theta^T x }$$
16
17
Polynomial regression is the same model applied to a feature map. Replacing $x$ by $\phi(x) = (1, x, x^2, \dots, x^d)$ fits a degree-$d$ polynomial while staying linear in the parameters:
18
19
$$\boxed{ h_\theta(x) = \theta^T \phi(x) = \sum_{j=0}^{d} \theta_j\, x^{j} }$$
20
21
so everything below applies unchanged once the design matrix $X$ stacks the transformed inputs $\phi(x^{(i)})$ as its rows.
22
23
## 5.2 Least squares
24
25
The cost is half the sum of squared residuals over the $m$ examples:
26
27
$$\boxed{ J(\theta) = \tfrac{1}{2}\sum_{i=1}^{m}\left(h_\theta(x^{(i)}) - y^{(i)}\right)^2 }$$
28
29
Setting $\nabla_\theta J = 0$ gives the closed-form normal equation, and gradient descent gives the equivalent iterative update:
30
31
$$\boxed{ \theta = (X^T X)^{-1}X^T y \qquad \theta_j \leftarrow \theta_j + \alpha\left(y^{(i)} - h_\theta(x^{(i)})\right)x_j^{(i)} }$$
32
33
![Linear regression fit](/en/Machine%20Learning/05%20Linear%20regression/a/linear-regression.png)
34
35
*Least squares fits the curve that minimizes the squared residuals (grey segments).*
36
37
## 5.3 Probabilistic formulation: maximum likelihood
38
39
Give the data a generative story: each target is the linear prediction plus independent Gaussian noise,
40
41
$$\boxed{ y^{(i)} = \theta^T x^{(i)} + \varepsilon^{(i)}, \quad \varepsilon^{(i)} \sim \mathcal{N}(0, \sigma^2) }$$
42
43
so $p(y^{(i)} \mid x^{(i)}; \theta) = \mathcal{N}(\theta^T x^{(i)}, \sigma^2)$. Maximizing the log-likelihood over the $m$ i.i.d. examples drops every term that does not depend on $\theta$ and leaves the least-squares cost:
44
45
$$\boxed{ \arg\max_\theta \ell(\theta) = \arg\min_\theta \sum_{i=1}^{m}\left(y^{(i)} - \theta^T x^{(i)}\right)^2 }$$
46
47
*Remark:* this is why least squares is a principled objective and not merely a convenient one. Ordinary least squares is the maximum-likelihood estimate under Gaussian noise, exactly the maximum-likelihood principle from the previous module.
48
49
## 5.4 Maximum a posteriori
50
51
Maximum likelihood can overfit, especially at high polynomial degree. Placing a zero-mean Gaussian prior on the parameters, $\theta \sim \mathcal{N}(0, \tau^2 I)$, and maximizing the posterior instead adds a penalty on their size:
52
53
$$\boxed{ \theta_{\mathrm{MAP}} = \arg\min_\theta \; \sum_{i=1}^{m}\left(y^{(i)} - \theta^T x^{(i)}\right)^2 + \lambda \lVert \theta \rVert_2^2, \quad \lambda = \frac{\sigma^2}{\tau^2} }$$
54
55
This is regularized (ridge) regression: the Gaussian prior becomes an L2 penalty, exactly the prior-to-penalty link noted in the previous module.
56
57
*Remark:* a stronger prior (small $\tau$) means a larger $\lambda$ and more shrinkage toward zero. With abundant data the likelihood dominates the prior and the maximum-a-posteriori fit approaches the maximum-likelihood one. Choosing the degree $d$ and the penalty $\lambda$ is a model-selection problem, settled by cross-validation from the [evaluation module](/en/Machine%20Learning/03%20Model%20evaluation%20and%20validation), and taken further in the [regularization module](/en/Machine%20Learning/07%20Regularization%20and%20high-dimensional%20inference).
58
59
*The same linear score, passed through a squashing function instead of read directly, turns regression into classification, the subject of the next module.*
60
61
---
62
Next: [Linear classification](/en/Machine%20Learning/06%20Linear%20classification) · [Course overview](/en/Machine%20Learning)