-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathslides_cls.html
463 lines (293 loc) · 13.1 KB
/
slides_cls.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<!DOCTYPE html>
<html>
<head>
<title>Linear Models</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/build/pure-min.css" integrity="sha384-oAOxQR6DkCoMliIh8yFnu25d7Eq/PHS21PClpwjOTeU2jRSq11vu66rf90/cZr47" crossorigin="anonymous"> <style>
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body { font-family: 'Droid Serif'; }
h1, h2, h3 {
font-family: 'Yanone Kaffeesatz';
font-weight: normal;
}
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
.reference{
font-size: 10px;
}
.smaller-font { font-size:14px }
@page {
size: 908px 681px;
margin: 0;
}
@media print {
.remark-slide-scaler {
width: 100% !important;
height: 100% !important;
transform: scale(1) !important;
top: 0 !important;
left: 0 !important;
}
}
.figure img{
height: 550px;
}
.figure-250 img{
height: 250px;
}
.figure-300 img{
height: 300px;
}
</style>
</head>
<body>
<textarea id="source">
class: center, middle
# Linear Models
## Classification
CS534 - Machine Learning
Yubin Park, PhD
---
class: center, middle
For a binary classification task
i.e. `\( y \in \{0, 1\} \)`
Is Squared Loss
a "good" loss function to use?
---
class: center, middle
How about this?
$$ y \in \\{ \text{Pos}, \text{Neg}\\} $$
or
$$ y \in \\{ \text{True}, \text{False}\\} $$
---
class: center, middle
Such classes may be generated from a coin toss
with a probability `\(p\)`.
For example,
$$ y = \begin{cases} 1, & \text{with probability } p \\\\ 0, & \text{with probability } (1-p) \end{cases} $$
Or,
$$ E[y] = p $$
---
class: center, middle
Good news is that
`\( p \)` is a real number,
so we may be able to model `\( p \)` with `\( \mathbf{x}^T\mathbf{\beta} \)`.
However, bad news is that `\( 0 \leq p \leq 1 \)`.
Remember: `\( \mathbf{x}^T\mathbf{\beta} \)` can be "any" real number.
---
class: center, middle
Here is a trick!
$$ 0 \leq \frac{1}{1 + \exp(-\mathbf{x}^T\mathbf{\beta})} \leq 1 $$
This is called a [logistic function](https://en.wikipedia.org/wiki/Logistic_function).
So, I can estimate `\( p \)` with
a logistic-transformed linear model.
And, this is called a [logistic regression](https://en.wikipedia.org/wiki/Logistic_regression).
---
class: center, middle
The log-likelihood of a coin toss is:
$$ y\log(p) + (1-y)\log(1-p) $$
Extending this form with our logistic regression for `\(n\)` samples:
$$\mathcal{L}(\mathbf{y}, \hat{\mathbf{y}}) = - \sum_{i=1}^{n} y_i\log(\frac{1}{1 + \exp(-\mathbf{x}_i^T\mathbf{\beta})}) + (1-y_i)\log(\frac{\exp(-\mathbf{x}_i^T\mathbf{\beta})}{1 + \exp(-\mathbf{x}_i^T\mathbf{\beta})})$$
---
class: center, middle
With some arithmetic,
$$ \mathcal{L}(\mathbf{y}, \hat{\mathbf{y}}) = - \sum_{i=1}^{n} y_i \mathbf{x}_i^T\mathbf{\beta} - \log(1+\exp(\mathbf{x}_i^T\mathbf{\beta}))$$
`\(\hat{\mathbf{\beta}}\)` that minimizes the above loss function is the solution of:
$$ \sum_{i=1}^{n} \mathbf{x}_i (y_i - \frac{1}{1+\exp(-\mathbf{x}_i^T\mathbf{\beta})}) = 0$$
---
class: center, middle
[Newton-Raphson algorithm](https://en.wikipedia.org/wiki/Newton%27s_method)
.figure-300[![newton method](img/NewtonRaphson.png)]
.reference[http://fourier.eng.hmc.edu/e176/lectures/NM/node20.html]
---
class: center, middle
Let `\( \mathbf{p} = \frac{1}{1+\exp(-\mathbf{X}^T\mathbf{\beta}^\text{old})} \)` and `\( \mathbf{W} = diag(\mathbf{p}(1-\mathbf{p})) \)`
then
$$ \mathbf{\beta}^{\text{new}} = \mathbf{\beta}^{\text{old}} + (\mathbf{X}^T\mathbf{W}\mathbf{X})^{-1} \mathbf{X}^T (\mathbf{y} - \mathbf{p})$$
This algorithm is known as
[Iterative Reweighted Least Squares (IRLS)](https://en.wikipedia.org/wiki/Iteratively_reweighted_least_squares).
---
## IRLS (1)
```python
data = load_breast_cancer()
...
def loss(y, X, beta):
ll = y * np.dot(X, beta) - np.log(1 + np.exp(np.dot(X, beta)))
return -np.sum(ll)
beta = np.zeros(m+1)
loss_lst = []
loss_lst.append(loss(y, X, beta))
for i in range(30):
p = expit(np.dot(X, beta))
W = np.diag(p * (1-p))
XWXinv = np.linalg.inv(np.dot(np.dot(X.T, W), X))
beta = beta + np.dot(XWXinv, np.dot(X.T, y-p))
loss_lst.append(loss(y, X, beta))
print(loss_lst)
```
```bash
[394.40074573860886, 134.65976155779023, 77.34464088185803, 50.213695232241705,
36.03778398459961, 27.9193029909705, 21.317288480218703, 17.693240538595294,
15.486294065947352, 13.69064323793789, 11.138891617659137, 8.972106451009168,
6.815535841033875, inf, inf, inf, inf, inf, inf, inf, inf, ...]
```
---
## IRLS (2)
The issues with `\(\lim_{x \to 0}\log x\)` and `\(\lim_{x \to 0} \frac{1}{x}\)`.
A heuristic fix:
```python
eps = 1e-5
beta = np.zeros(m+1)
loss_lst = []
loss_lst.append(loss(y, X, beta))
for i in range(30):
p = np.clip(expit(np.dot(X, beta)), eps, 1-eps) # NOTE: np.clip()
W = np.diag(p * (1-p))
XWXinv = np.linalg.inv(np.dot(np.dot(X.T, W), X))
beta = beta + np.dot(XWXinv, np.dot(X.T, y-p))
loss_lst.append(loss(y, X, beta))
print(loss_lst)
```
```bash
[394.40074573860886, 134.65976155779023, 77.34464088185803, 50.213695232241705,
36.037792317633716, 27.921357429458112, 21.363127719004243, 17.7941874322518,
15.712620105268597, 14.313956463333824, 13.059598232213126, 12.127395626682928,
11.44752822203149, 10.928370872078371, 10.516914209497251, 10.18009512545763,
9.89655605214852, 9.652059524611392, 9.436848965755562, ...]
```
---
## Log-odds
For a binary classification problem,
$$ \log \frac{p_i}{1-p_i} = \mathbf{x}_i^T \mathbf{\beta}$$
The left-hand term is called the log-odds or [logit](https://en.wikipedia.org/wiki/Logit) function.
$$ \text{logit}(p) = \log \frac{p}{1-p} $$
$$ \text{logit}^{-1}(q) = \frac{1}{1 + \exp(-q)} $$
Logistic regression can be viewed as a linear model for the log-odds target.
---
## Coefficients
A central limit theorem and the least sqaure update form can be used to derive:
$$ \hat{\mathbf{\beta}} \sim N(\mathbf{\beta}, (\mathbf{X}^T\mathbf{W}\mathbf{X})^{-1})$$
In logistic regression, the regression coefficient represents the change in the log-odds.
$$ \exp(\beta_j) = \frac{\text{Pr}(y=1 | x_j=1)/\text{Pr}(y=0 | x_j=1)}{\text{Pr}(y=1 | x_j=0)/\text{Pr}(y=0 | x_j=0)}$$
This is called as [Odds-ratio (OR)](https://en.wikipedia.org/wiki/Odds_ratio). In other words,
$$ \exp(\beta_j) = \frac{\text{Odds of }y=1\text{ with }x_j=1}{\text{Odds of }y=1\text{ with }x_j=0}$$
---
## Probabilistic Perspective
$$ y = \begin{cases} 1, & \text{if } \mathbf{x}\mathbf{\beta} + \epsilon > 0 \\\\ 0, & \text{otherwise}\end{cases} $$
$$ \text{Pr}(\epsilon > s) = \frac{1}{1 + \exp(-s)} $$
The noise is from the standard [logistic distribution](https://en.wikipedia.org/wiki/Logistic_distribution). To see how this works,
$$ \text{Pr}(\mathbf{x}\mathbf{\beta} + \epsilon > 0) = 1 - \text{Pr}(\epsilon < - \mathbf{x}\mathbf{\beta})$$
$$ \text{Pr}(y = 1) = \frac{1}{1 + \exp( - \mathbf{x}\mathbf{\beta})}$$
---
## Other Approaches for Binary Classification
$$ y = \begin{cases} 1, & \text{if } \mathbf{x}\mathbf{\beta} + \epsilon > 0 \\\\ 0, & \text{otherwise}\end{cases} $$
$$ \epsilon \sim N(0, 1) $$
This model is known as [Probit Regression](https://en.wikipedia.org/wiki/Probit_model).
If the cumulative distribution of the noise follows the standard [Gumbel distribution](https://en.wikipedia.org/wiki/Gumbel_distribution)
$$ \text{Pr}(\epsilon > s) = \exp(-\exp(-s))$$
then the model is known as the [complementary log-log regression](https://data.princeton.edu/wws509/notes/c3s7).
Note that a logistic regression is just **one way** to model a binary classification problem.
---
## Regularized Logistic Regression
Elastic-Net:
$$ \mathcal{L}(\mathbf{y}, \hat{\mathbf{y}}) = - \sum_{i=1}^{n} [y_i \mathbf{x}_i^T\mathbf{\beta} - \log(1+\exp(\mathbf{x}_i^T\mathbf{\beta}))] + \lambda ((1-\alpha) \mathbf{\beta}^T\mathbf{\beta} + \alpha | \mathbf{\beta} |)$$
Ridge:
$$ \mathcal{L}(\mathbf{y}, \hat{\mathbf{y}}) = - \sum_{i=1}^{n} [y_i \mathbf{x}_i^T\mathbf{\beta} - \log(1+\exp(\mathbf{x}_i^T\mathbf{\beta}))] + \lambda \mathbf{\beta}^T\mathbf{\beta} $$
Lasso:
$$ \mathcal{L}(\mathbf{y}, \hat{\mathbf{y}}) = - \sum_{i=1}^{n} [y_i \mathbf{x}_i^T\mathbf{\beta} - \log(1+\exp(\mathbf{x}_i^T\mathbf{\beta}))] + \lambda | \mathbf{\beta} | $$
---
## Taylor Approximation
Applying the quadratic [Taylor approximation](https://en.wikipedia.org/wiki/Taylor_series) to the logistic loss part,
$$ \mathcal{L}(\mathbf{y}, \hat{\mathbf{y}}) \approx \sum p_i (1-p_i)(\mathbf{x}_i^T\mathbf{\beta} - \frac{y_i - p_i}{p_i (1-p_i)} - \mathbf{x}_i^T \mathbf{\beta}^{\text{old}})^2 + \lambda |\mathbf{\beta}|$$
where `\( p_i = \frac{1}{1 + \exp(-\mathbf{x}_i^T\mathbf{\beta}^{\text{old}})}\)`.
Let `\( z_i = \frac{y_i - p_i}{p_i (1-p_i)} + \mathbf{x}_i^T \mathbf{\beta}^{\text{old}} \)` and `\( w_i = p_i (1-p_i) \)`, then
$$ \sum w_i (z_i - \mathbf{x}_i^T \mathbf{\beta})^2 + \lambda | \mathbf{\beta }| $$
This form is the same as a weighted Lasso loss function, which you can solve with a [proximal gradient method](https://en.wikipedia.org/wiki/Proximal_gradient_methods_for_learning).
You can extend this result to the Elastic-Net penalty - note that you can modify the loss function of an Elastic-Net regression to be equivalent to a lasso regression.
---
class: middle
## Generalized Linear Models
Multi-class and count targets
---
## Multi-class Target
Consider modeling `\( y \in \{ \text{Red}, \text{Green}, \text{Yellow}\} \)`.
How about
$$ \text{Pr}(y = \text{Red}) = \frac{\exp(\mathbf{x}^T\mathbf{\beta}^{R})}{\exp(\mathbf{x}^T\mathbf{\beta}^{R}) + \exp(\mathbf{x}^T\mathbf{\beta}^{G})+ \exp(\mathbf{x}^T\mathbf{\beta}^{Y})}$$
The above probability distribution is known as a [softmax function](https://en.wikipedia.org/wiki/Softmax_function).
The log-likelihood of this model is:
$$ \sum\_{i=1}^{n} \sum_{k=\\{R, G, Y\\}} I(y_i = k) \mathbf{x}_i^T\mathbf{\beta}^k - \log(1+\exp(\mathbf{x}_i^T\mathbf{\beta}^k)) $$
---
## Count Target
Consider modeling `\( y \in \{ 0, 1, 2, 3, ...\} \)`.
How about
$$ \text{Pr}(y = k) = \frac{1}{k!} \exp(k \mathbf{x}^T\mathbf{\beta}) \exp(-\exp(\mathbf{x}^T\mathbf{\beta}))$$
If we let `\(\lambda = \exp(\mathbf{x}\mathbf{\beta})\)`, then
$$ \text{Pr}(y = k) = \frac{1}{k!} \lambda^k \exp(-\lambda) $$
This distribution is known as [Poisson distribution](https://en.wikipedia.org/wiki/Poisson_distribution). The log-likelihood of this model is:
$$ \sum\_{i=1}^{n} y_i \mathbf{x}_i^T\mathbf{\beta} - \exp(\mathbf{x}_i^T\mathbf{\beta})$$
---
## Generalized Linear Model
$$ E[y] = \mu = g^{-1}(\mathbf{x}^T\mathbf{\beta})$$
where `\(g(\cdot)\)` is a link function.
.pure-table.pure-table-bordered.pure-table-striped[
| Distribution | Use Case | Link | Mean Function |
| ------------ | -------- | ---- | ------------- |
| Normal | Linear Response | `\(\mathbf{X}\mathbf{\beta}=\mu\)` | `\(\mu=\mathbf{X}\mathbf{\beta}\)`|
| Poisson | Count Response | `\(\mathbf{X}\mathbf{\beta}=\log(\mu)\)` | `\(\mu=\exp(\mathbf{X}\mathbf{\beta})\)`|
| Categorical | K Response | `\(\mathbf{X}\mathbf{\beta}=\log(\frac{\mu}{1-\mu})\)` | `\(\mu=\frac{1}{1+\exp(-\mathbf{X}\mathbf{\beta})}\)`|
]
The maximum likelihood estimator for `\(\mathbf{\beta}\)` can be found using IRLS or Coordinate Descent.
Please read "[GLMNET Vignette](https://web.stanford.edu/~hastie/glmnet/glmnet_beta.html)" for more details.
---
class: center, middle
## Questions?
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_HTML&delayStartupUntil=configured" type="text/javascript">
</script>
<script type="text/javascript">
var hljs = remark.highlighter.engine;
/*
Language: terminal console
Author: Josh Bode <[email protected]>
*/
hljs.registerLanguage('terminal', function() {
return {
contains: [
{
className: 'string',
begin: '^([\\w.]+)@([\\w.]+)'
},
{
className: 'constant',
begin: ' (.*) \\$ '
},
{
className: 'ansi',
begin: '<span style\\="([^"]+)">',
end: '<\\/span>'
}
]
}
});
var slideshow = remark.create({
highlightStyle: 'monokai'
});
// extract the embedded styling from ansi spans
var highlighted = document.querySelectorAll("code.terminal span.hljs-ansi");
Array.prototype.forEach.call(highlighted, function(next) {
next.insertAdjacentHTML("beforebegin", next.textContent);
next.parentNode.removeChild(next);
});
// Setup MathJax
MathJax.Hub.Config({
tex2jax: {
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
}
});
MathJax.Hub.Configured();
</script>
</body>
</html>