Blame

1c3139 Lucas Gonthier 2026-06-30 12:04:21
Initial commit: course content (Machine Learning, MLOps) in EN and FR Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1
# Linear models
2
3
Linear models predict from a linear score $\theta^T x$. This module covers linear regression (continuous targets), logistic regression (binary classification), and the generalized linear model framework that unifies both through the exponential family. Each model is fit by maximum likelihood and shares the same gradient-based update.
4
5
**Objectives**
6
- Define the linear hypothesis and fit $\theta$ by the LMS update or the closed-form normal equation.
7
- See why least squares is the maximum-likelihood estimate under Gaussian noise.
8
- Map the linear score through the sigmoid and fit it by gradient ascent or Newton's method.
9
- Classify with the perceptron and know when its learning rule converges.
10
- Recognize the exponential-family form and build a GLM from its three assumptions.
11
- Recover linear, logistic, and softmax regression as special cases.
12
13
## Linear regression
14
15
### Hypothesis
16
17
The hypothesis is linear in the augmented input $x \in \mathbb{R}^{n+1}$ with $x_0 = 1$ and parameters $\theta \in \mathbb{R}^{n+1}$:
18
19
$$\boxed{ h_\theta(x) = \theta^T x }$$
20
21
### Cost function
22
23
The cost is defined as half the sum of squared residuals over the $m$ examples:
24
25
$$\boxed{ J(\theta) = \tfrac{1}{2}\sum_{i=1}^{m}\left(h_\theta(x^{(i)}) - y^{(i)}\right)^2 }$$
26
27
### LMS update
28
29
Gradient descent on $J$ gives the least-mean-squares (Widrow-Hoff) update, applied per example $(x^{(i)}, y^{(i)})$:
30
31
$$\boxed{ \theta_j \leftarrow \theta_j + \alpha\left(y^{(i)} - h_\theta(x^{(i)})\right)x_j^{(i)} }$$
32
33
with learning rate $\alpha > 0$.
34
35
| variant | update rule | per step | use when |
36
| --- | --- | --- | --- |
37
| Batch GD | sum over all $m$ examples | $O(mn)$ | $m$ small to moderate |
38
| Stochastic GD (SGD) | one example at a time | $O(n)$ | $m$ large, streaming |
39
40
### Normal equation
41
42
Setting $\nabla_\theta J(\theta) = 0$ gives a closed-form solution from the design matrix $X$ and target vector $y$:
43
44
$$\boxed{ \theta = (X^T X)^{-1}X^T y }$$
45
46
*Remark:* the normal equation needs no learning rate and no iteration, but inverting $X^T X$ costs $O(n^3)$, so for large $n$ the iterative LMS update is preferred.
47
48
### Probabilistic interpretation
49
50
Assume $y^{(i)} = \theta^T x^{(i)} + \varepsilon^{(i)}$ with i.i.d. Gaussian noise $\varepsilon^{(i)} \sim \mathcal{N}(0, \sigma^2)$. Maximizing the log-likelihood then coincides with minimizing the least-squares cost:
51
52
$$\boxed{ \arg\max_\theta \ell(\theta) = \arg\min_\theta J(\theta) }$$
53
54
*Remark:* this is why least squares is a principled objective and not merely a convenient one.
55
56
![Linear regression fit](/en/Machine%20Learning/03%20Linear%20models/a/linear-regression.png)
57
58
*Least squares fits the line that minimizes the squared residuals (grey segments).*
59
60
## Logistic regression
61
62
### Sigmoid
63
64
The sigmoid (logistic) function squashes a raw score $z \in \mathbb{R}$ into a probability:
65
66
$$\boxed{ g(z) = \frac{1}{1 + e^{-z}} \in (0, 1) }$$
67
68
Its derivative has the convenient form $g'(z) = g(z)\left(1 - g(z)\right)$.
69
70
### Model
71
72
The hypothesis outputs the probability of the positive class, with $\phi$ the predicted probability:
73
74
$$\boxed{ \phi = h_\theta(x) = g(\theta^T x) = p(y = 1 \mid x; \theta) }$$
75
76
Labels are $y \in \{0, 1\}$, so the conditional law is Bernoulli:
77
78
$$\boxed{ p(y \mid x; \theta) = \phi^{y}(1 - \phi)^{1 - y} }$$
79
80
### Log-likelihood
81
82
Over $m$ i.i.d. examples the log-likelihood is the negative cross-entropy summed over the data:
83
84
$$\boxed{ \ell(\theta) = \sum_{i=1}^{m}\left[ y^{(i)}\log \phi^{(i)} + (1 - y^{(i)})\log(1 - \phi^{(i)}) \right] }$$
85
86
with $\phi^{(i)} = h_\theta(x^{(i)})$.
87
88
### Gradient ascent
89
90
Maximizing $\ell$ by gradient ascent gives the same form as the LMS update:
91
92
$$\boxed{ \theta_j \leftarrow \theta_j + \alpha\left(y^{(i)} - h_\theta(x^{(i)})\right)x_j^{(i)} }$$
93
94
*Remark:* the update matches linear regression in form, even though $h_\theta$ is now the sigmoid. This is no coincidence, both are generalized linear models.
95
96
### Newton's method
97
98
Newton's method converges faster near the optimum. In one dimension:
99
100
$$\boxed{ \theta \leftarrow \theta - \frac{\ell'(\theta)}{\ell''(\theta)} }$$
101
102
In the vector case it uses the Hessian $H$ of $\ell$:
103
104
$$\boxed{ \theta \leftarrow \theta - H^{-1}\nabla_\theta \ell(\theta) }$$
105
106
*Remark:* logistic regression has no closed-form solution for $\theta$, so it is always fit iteratively (gradient ascent or Newton).
107
108
![Sigmoid and logistic decision boundary](/en/Machine%20Learning/03%20Linear%20models/a/logistic-regression.png)
109
110
*Left: the sigmoid maps scores into the interval (0,1). Right: the decision boundary and predicted probability.*
111
112
## Perceptron
113
114
The perceptron is the original linear classifier. It keeps the linear score $\theta^T x$ of logistic regression but replaces the sigmoid with a hard threshold, so the output is a class label rather than a probability. Labels are $y \in \{0, 1\}$.
115
116
### Activation and hypothesis
117
118
The activation is the step function:
119
120
$$\boxed{ g(z) = \begin{cases} 1 & \text{if } z \ge 0 \\ 0 & \text{otherwise} \end{cases} }$$
121
122
and the hypothesis applies it to the linear score:
123
124
$$\boxed{ h_\theta(x) = g(\theta^T x) }$$
125
126
### Learning rule
127
128
The perceptron is trained online, one example at a time, and corrects $\theta$ only on a misclassified point:
129
130
$$\boxed{ \theta_j \leftarrow \theta_j + \alpha\left(y^{(i)} - h_\theta(x^{(i)})\right)x_j^{(i)} }$$
131
132
*Remark:* this is the same form as the LMS update and the logistic gradient-ascent update. Only the activation $g$ differs (identity, sigmoid, step). When the prediction is right the factor $y^{(i)} - h_\theta(x^{(i)})$ is zero, so correctly classified points leave $\theta$ unchanged.
133
134
![Perceptron decision boundary](/en/Machine%20Learning/03%20Linear%20models/a/perceptron.png)
135
136
*The perceptron finds one separating hyperplane. It is not necessarily the maximum-margin one the SVM will choose.*
137
138
### Convergence
139
140
| data | behaviour |
141
| --- | --- |
142
| linearly separable | converges in a finite number of updates |
143
| not separable | never converges, the weights keep oscillating |
144
145
*Remark:* the perceptron stops at the first hyperplane that separates the data, usually not the one with the widest margin. This gap motivates the support vector machine (which maximizes the margin) and, stacked into layers, the neural network (a perceptron is a single unit).
146
147
## Generalized linear models
148
149
### Exponential family
150
151
A distribution is in the exponential family if its density can be written with natural parameter $\eta$, sufficient statistic $T(y)$, log-partition $a(\eta)$, and base measure $b(y)$:
152
153
$$\boxed{ p(y; \eta) = b(y)\exp\left(\eta\, T(y) - a(\eta)\right) }$$
154
155
### GLM assumptions
156
157
A GLM rests on three choices. The response is in the exponential family, the natural parameter is linear in the input, and the prediction is the expected sufficient statistic:
158
159
$$\boxed{ \eta = \theta^T x }$$
160
161
$$\boxed{ h_\theta(x) = \mathbb{E}\left[T(y) \mid x; \theta\right] }$$
162
163
### Family table
164
165
| Distribution | $\eta$ | $T(y)$ | $a(\eta)$ | $b(y)$ |
166
| --- | --- | --- | --- | --- |
167
| Bernoulli | $\log\dfrac{\phi}{1-\phi}$ | $y$ | $\log(1 + e^{\eta})$ | $1$ |
168
| Gaussian ($\sigma^2 = 1$) | $\mu$ | $y$ | $\tfrac{1}{2}\eta^2$ | $\dfrac{1}{\sqrt{2\pi}}e^{-y^2/2}$ |
169
| Poisson | $\log\lambda$ | $y$ | $e^{\eta}$ | $\dfrac{1}{y!}$ |
170
| Geometric | $\log(1-\phi)$ | $y$ | $\log\dfrac{e^{\eta}}{1 - e^{\eta}}$ | $1$ |
171
172
*Remark:* for the Bernoulli, $\eta$ is the log-odds and its inverse is the sigmoid, $\phi = g(\eta)$. This is why logistic regression has the form it does.
173
174
### Softmax regression
175
176
For multiclass labels $y \in \{1, \dots, k\}$ the GLM gives softmax regression, with one parameter vector $\theta_k$ per class:
177
178
$$\boxed{ p(y = k \mid x; \theta) = \frac{\exp(\theta_k^T x)}{\sum_{j}\exp(\theta_j^T x)} }$$
179
180
### GLM recipe
181
182
```mermaid
183
graph TD
184
A["pick a response distribution"] --> B["write it in exponential-family form"]
185
B --> C["set natural parameter eta linear in x"]
186
C --> D["prediction is expected sufficient statistic"]
187
D --> E["fit theta by maximum likelihood"]
188
```
189
190
*Linear models, including the perceptron, settle for any boundary that separates the classes. The next part asks for the best one: the support vector machine maximizes the margin.*
191
192
---
193
Next: [Support Vector Machines](/en/Machine%20Learning/04%20Support%20Vector%20Machines) · [Course overview](/en/Machine%20Learning)