Blame

0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
1
# 4. Linear regression
2
3
Linear regression predicts a continuous target from a linear score. This module follows one thread from end to end: pose the model, fit it to noisy data by least squares, justify that objective by maximum likelihood, regularize it by maximum a posteriori (ridge, then its selecting cousin the lasso), then widen the model with basis functions and multiple outputs, where the same two closed forms return unchanged.
4
5
## 4.1 The linear model
6
7
The hypothesis is linear in the augmented input $x \in \mathbb{R}^{n+1}$ with $x_0 = 1$, the convention of the [Introduction](/en/Machine%20Learning/01%20Introduction):
8
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
9
$$\boxed{ h_w(x) = w^T x = w_0 + w_1 x_1 + \dots + w_n x_n }$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
10
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
11
$w_0$ is the bias (the intercept) and the remaining coordinates are the weights, and folding the bias into the dot product is exactly what the $x_0 = 1$ convention buys. Geometrically, the prediction is a line for $n = 1$, a plane for $n = 2$, and a hyperplane beyond.
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
12
13
![The prediction is a line, then a plane](/en/Machine%20Learning/04%20Linear%20regression/a/line-and-plane.png)
14
15
*With one feature the model draws a line through the data, with two a plane, and beyond that a hyperplane that can no longer be drawn.*
16
17
## 4.2 The problem to solve
18
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
19
Given the training set $\{(x^{(i)}, y^{(i)})\}_{i=1}^{m}$, ideally we would have $h_w(x^{(i)}) = y^{(i)}$ at every point. Real targets are noisy (measurement error, unmodelled factors), so no line passes through them all, and the goal becomes to make the smallest total error. Least squares takes the squared residual as the error and sums it over the training set:
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
20
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
21
$$\boxed{ w^{*} = \arg\min_w \; \sum_{i=1}^{m}\left(w^T x^{(i)} - y^{(i)}\right)^2 }$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
22
23
![Ideal versus noisy targets](/en/Machine%20Learning/04%20Linear%20regression/a/ideal-vs-noisy.png)
24
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
25
*Left: if the targets were noise-free, the model could pass through every point. Right: real targets scatter around the trend, so each point leaves a residual between $y^{(i)}$ and the prediction $h_w(x^{(i)})$, and the fit minimizes their sum of squares (grey segments).*
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
26
27
*Remark:* why the square rather than, say, the absolute value? Because this choice is provably optimal when the noise is Gaussian, a classic interview question that the next section unpacks.
28
29
## 4.3 Maximum likelihood: least squares justified
30
31
Give the data a generative story, using the estimation principle of [Probabilistic formulation](/en/Machine%20Learning/03%20Probabilistic%20formulation): each target is the linear prediction plus independent Gaussian noise,
32
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
33
$$\boxed{ y^{(i)} = w^T x^{(i)} + \varepsilon^{(i)}, \quad \varepsilon^{(i)} \sim \mathcal{N}(0, \sigma^2) }$$
34
35
so $p(y^{(i)} \mid x^{(i)}; w) = \mathcal{N}(w^T x^{(i)}, \sigma^2)$. Maximum likelihood picks the parameters under which the observed targets are the most probable, and it delivers two results. First, maximizing the likelihood is exactly minimizing the sum of squared errors:
36
37
$$\boxed{ w_{\mathrm{MLE}} = \arg\max_w \; p(y \mid X; w) = \arg\min_w \; \sum_{i=1}^{m}\left(y^{(i)} - w^T x^{(i)}\right)^2 }$$
38
39
Second, the minimizer has a closed form, the normal equation, with $X$ the design matrix whose rows are the $x^{(i)T}$:
40
41
$$\boxed{ w_{\mathrm{MLE}} = (X^T X)^{-1}X^T y }$$
42
43
one matrix solve away from the data.
44
45
*Remark:* the first box is the most important fact of the module. Least squares is not a convenient convention, it is the maximum-likelihood estimate under Gaussian noise.
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
46
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
47
<details class="proof">
48
<summary>Proof: maximizing the likelihood is minimizing the squared error</summary>
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
49
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
50
The examples are i.i.d., so the likelihood of the whole training set factorizes into a product of Gaussian densities:
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
51
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
52
$$p(y \mid X; w) = \prod_{i=1}^{m} p(y^{(i)} \mid x^{(i)}; w) = \prod_{i=1}^{m} \frac{1}{\sqrt{2\pi\sigma^2}}\, \exp\!\left(-\frac{\left(y^{(i)} - w^T x^{(i)}\right)^2}{2\sigma^2}\right)$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
53
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
54
The logarithm is increasing, so it preserves the argmax and turns the product into a sum, the log-likelihood, which separates into a constant and the sum of squares:
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
55
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
56
$$\ell(w) = \sum_{i=1}^{m} \log \mathcal{N}\!\left(y^{(i)} \mid w^T x^{(i)}, \sigma^2\right) = -\frac{m}{2}\log(2\pi\sigma^2) \;-\; \frac{1}{2\sigma^2}\sum_{i=1}^{m}\left(y^{(i)} - w^T x^{(i)}\right)^2$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
57
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
58
The first term does not depend on $w$ and the factor $\tfrac{1}{2\sigma^2}$ is a positive constant, so neither moves the argmax. Maximizing $\ell$ is therefore minimizing the sum of squared errors. $\blacksquare$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
59
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
60
</details>
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
61
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
62
<details class="proof">
63
<summary>Proof: the normal equation</summary>
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
64
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
65
With the design matrix, the sum of squares is the quadratic $\lVert Xw - y \rVert^2$, a convex function of $w$, so its global minimum is the point of zero gradient:
66
67
$$\nabla_w\, \lVert Xw - y \rVert^2 = 2\,X^T(Xw - y) = 0 \;\Longleftrightarrow\; X^T X\, w = X^T y$$
68
69
Provided $X^T X$ is invertible (independent features, more examples than features), isolating $w$ gives $w_{\mathrm{MLE}} = (X^T X)^{-1}X^T y$. $\blacksquare$
70
71
</details>
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
72
73
## 4.4 Maximum a posteriori: ridge regression
74
75
Maximum likelihood can overfit, especially when the model is flexible. The maximum a posteriori estimate maximizes the posterior instead, which by Bayes' rule is the likelihood times a prior on the parameters, here a zero-mean Gaussian:
76
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
77
$$w_{\mathrm{MAP}} = \arg\max_w \; p(y \mid X, w)\, p(w), \qquad w \sim \mathcal{N}(0, \tau^2 I)$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
78
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
79
Two results again. The Gaussian prior turns into an L2 penalty added to least squares:
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
80
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
81
$$\boxed{ w_{\mathrm{MAP}} = \arg\min_w \; \sum_{i=1}^{m}\left(y^{(i)} - w^T x^{(i)}\right)^2 + \lambda \lVert w \rVert_2^2, \quad \lambda = \frac{\sigma^2}{\tau^2} }$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
82
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
83
and the penalized minimizer keeps a closed form:
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
84
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
85
$$\boxed{ w_{\mathrm{MAP}} = (X^T X + \lambda I)^{-1}X^T y }$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
86
87
This is ridge regression, and the penalty is often called weight decay. The Gaussian prior became the L2 penalty of [General concepts](/en/Machine%20Learning/02%20General%20concepts), exactly the prior-to-penalty link of [Probabilistic formulation](/en/Machine%20Learning/03%20Probabilistic%20formulation).
88
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
89
*Remark:* $\lambda \to 0$ recovers maximum likelihood, and a growing $\lambda$ shrinks $w$ toward zero and fights overfitting. A stronger prior (small $\tau$) means a larger $\lambda$. Note also that $X^T X + \lambda I$ is always invertible for $\lambda > 0$, which rescues least squares exactly where it breaks down: strongly correlated features, or more features than examples.
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
90
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
91
<details class="proof">
92
<summary>Proof: the Gaussian prior becomes the L2 penalty</summary>
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
93
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
94
By Bayes' rule the posterior is
95
96
$$p(w \mid y, X) = \frac{p(y \mid X, w)\, p(w)}{p(y \mid X)}$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
97
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
98
and the denominator does not depend on $w$, so maximizing the posterior is maximizing the likelihood times the prior. The prior covariance is generally unknown, so it is assumed isotropic, $\tau^2 I$, which gives the density
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
99
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
100
$$p(w) = \frac{1}{(2\pi\tau^2)^{(n+1)/2}}\, \exp\!\left(-\frac{\lVert w \rVert^2}{2\tau^2}\right)$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
101
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
102
Taking logarithms and reusing the log-likelihood $\ell(w)$ from the previous proof,
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
103
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
104
$$\log p(y \mid X, w) + \log p(w) = \mathrm{const} \;-\; \frac{1}{2\sigma^2}\sum_{i=1}^{m}\left(y^{(i)} - w^T x^{(i)}\right)^2 \;-\; \frac{1}{2\tau^2}\lVert w \rVert^2$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
105
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
106
where the constant gathers every term independent of $w$. Multiplying by $-2\sigma^2$, a negative constant that flips the argmax into an argmin, leaves
107
108
$$w_{\mathrm{MAP}} = \arg\min_w \; \sum_{i=1}^{m}\left(y^{(i)} - w^T x^{(i)}\right)^2 + \frac{\sigma^2}{\tau^2}\, \lVert w \rVert^2$$
109
110
and $\lambda = \sigma^2 / \tau^2$ names the ratio: the noisier the data or the tighter the prior, the heavier the penalty. $\blacksquare$
111
112
</details>
113
114
<details class="proof">
115
<summary>Proof: the ridge closed form</summary>
116
117
In matrix form the objective is $\lVert Xw - y \rVert^2 + \lambda \lVert w \rVert^2$, still a convex quadratic, so the zero-gradient condition finds its global minimum:
118
119
$$\nabla_w \left( \lVert Xw - y \rVert^2 + \lambda \lVert w \rVert^2 \right) = 2\,X^T(Xw - y) + 2\lambda w = 0 \;\Longleftrightarrow\; (X^T X + \lambda I)\, w = X^T y$$
120
121
For $\lambda > 0$ the matrix $X^T X + \lambda I$ is positive definite, hence invertible, with no condition on $X$ this time: for any $v \neq 0$, $v^T (X^T X + \lambda I)\, v = \lVert X v \rVert^2 + \lambda \lVert v \rVert^2 > 0$. Isolating $w$ gives $w_{\mathrm{MAP}} = (X^T X + \lambda I)^{-1} X^T y$. $\blacksquare$
122
123
</details>
124
125
## 4.5 The lasso: a penalty that selects
126
127
The ridge penalty came from a Gaussian prior. A Laplace prior yields the L1 penalty instead, the link noted in [Probabilistic formulation](/en/Machine%20Learning/03%20Probabilistic%20formulation):
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
128
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
129
$$\boxed{ w_{\mathrm{lasso}} = \arg\min_w \; \sum_{i=1}^{m}\left(y^{(i)} - w^T x^{(i)}\right)^2 + \lambda \lVert w \rVert_1 }$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
130
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
131
The change looks small, its consequences are not:
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
132
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
133
- **It selects.** The lasso drives some coefficients to exactly zero, performing variable selection while it fits. Ridge only shrinks and never zeroes: ridge stabilizes, the lasso selects.
134
- **The reason is geometric.** The constraint region $\lVert w \rVert_1 \le t$ is a diamond with corners on the axes, and the elliptical contours of the squared error tend to touch a corner first, where a coordinate is zero. The rounded L2 ball has no corners to catch.
135
- **$\lambda$ traces a path.** As $\lambda$ grows, coefficients hit exactly zero one after another, from the full model down to the empty one. As always, $\lambda$ is chosen by the cross-validation of [General concepts](/en/Machine%20Learning/02%20General%20concepts), often the largest $\lambda$ within one standard error of the best.
136
- **No closed form.** The L1 penalty is not differentiable at zero, so the lasso is fitted by convex solvers rather than a formula.
137
- **The elastic net** blends the two penalties, $\lambda\left(\alpha \lVert w \rVert_1 + (1-\alpha)\lVert w \rVert_2^2\right)$, keeping the lasso's selection with the ridge's stability under correlated features.
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
138
139
*Remark:* prediction is not inference. Selecting variables with the lasso and then reporting textbook standard errors on the same data is invalid, the winner's curse again: the intervals ignore that the data already chose the variables. Honest inference needs sample splitting or a debiased estimator, the doorway to causal machine learning.
140
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
141
## 4.6 Basis functions: nonlinear in $x$, linear in $w$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
142
143
A straight line is often too rigid: the underfitting of [General concepts](/en/Machine%20Learning/02%20General%20concepts) appeared precisely when a low-capacity model met a curved trend. The fix is not to abandon the linear machinery but to project the input into a larger space, where the relationship is linear:
144
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
145
$$\boxed{ h_w(x) = w^T \phi(x) = \sum_{j=0}^{M-1} w_j\, \phi_j(x), \qquad \phi_0(x) = 1 }$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
146
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
147
The $\phi_j$ are basis functions, fixed before training. With $\phi(x) = (1, x, x^2, \dots, x^d)$ they give polynomial regression, the running example of [General concepts](/en/Machine%20Learning/02%20General%20concepts), and the identity $\phi(x) = x$ recovers everything above. The model can now be wildly nonlinear in $x$ yet stays linear in $w$, so nothing changes in the fit: stack the $\phi(x^{(i)})^T$ as the rows of the design matrix $\Phi \in \mathbb{R}^{m \times M}$ and the two closed forms return verbatim:
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
148
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
149
$$\boxed{ w_{\mathrm{MLE}} = (\Phi^T \Phi)^{-1}\Phi^T y, \qquad w_{\mathrm{MAP}} = (\Phi^T \Phi + \lambda I)^{-1}\Phi^T y }$$
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
150
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
151
*Remark:* the basis (its family and its size $M$) is a hyperparameter, chosen before training, while $w$ is learned. Choosing $M$ and $\lambda$ is the model-selection problem settled by the cross-validation of [General concepts](/en/Machine%20Learning/02%20General%20concepts).
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
152
153
## 4.7 Multiple outputs
154
155
Nothing restricts the target to a single number. To predict $K$ values at once (say a house's price, heating cost, and property tax from the same features), let $y^{(i)} \in \mathbb{R}^K$ and give each output its own parameter column, gathered in a matrix $W \in \mathbb{R}^{M \times K}$:
156
157
$$\boxed{ h_W(x) = W^T \phi(x) \in \mathbb{R}^{K} }$$
158
159
Stacking the targets as the rows of $Y \in \mathbb{R}^{m \times K}$, the same derivations give the same closed forms, now solving all $K$ regressions at once:
160
161
$$\boxed{ W_{\mathrm{MLE}} = (\Phi^T \Phi)^{-1}\Phi^T Y, \qquad W_{\mathrm{MAP}} = (\Phi^T \Phi + \lambda I)^{-1}\Phi^T Y }$$
162
163
*Remark:* the expensive factor $(\Phi^T \Phi)^{-1}$ does not depend on the targets, so it is computed once and shared by all $K$ outputs.
164
165
## 4.8 Summary
166
167
| | Formula |
168
| --- | --- |
6b31d5 lugonthier 2026-07-15 12:37:13
feat: Update "Decision trees and ensemble methods" module with new content and visuals - Revamped the introduction to ensemble methods, emphasizing the benefits of combining models. - Expanded sections on decision trees, bagging, and boosting, including detailed explanations and formulas. - Added new SVG diagrams illustrating the bagging process, the transition from stumps to trees, and variance reduction. - Introduced new images for AdaBoost rounds and variance reduction to enhance understanding.
169
| Model | $h_w(x) = w^T \phi(x)$ |
170
| Maximum likelihood (least squares) | $w_{\mathrm{MLE}} = (\Phi^T \Phi)^{-1}\Phi^T y$ |
171
| Maximum a posteriori (ridge) | $w_{\mathrm{MAP}} = (\Phi^T \Phi + \lambda I)^{-1}\Phi^T y$ |
172
| Parameters, learned | $w$ (or $W$ for $K$ outputs) |
0ad9b6 lugonthier 2026-07-10 12:03:30
Remove "07 Regularization and high-dimensional inference" chapter and add "07 Support Vector Machines" and "08 Decision trees and ensemble methods" chapters with corresponding images.
173
| Hyperparameters, chosen by validation | the basis $\phi$ and its size $M$, the penalty $\lambda$ |
174
175
*The same linear score, passed through a squashing function instead of read directly, turns regression into classification, the subject of the next module.*
176
177
---
178
Next: [Linear classification](/en/Machine%20Learning/05%20Linear%20classification) · [Course overview](/en/Machine%20Learning)