-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
997 lines (794 loc) · 26.7 KB
/
index.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
<!DOCTYPE html>
<html>
<head>
<title>Pyglmnet</title>
<meta charset="utf-8">
<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'; }
</style>
<link rel="stylesheet" href="font-awesome-4.6.3/css/font-awesome.min.css">
</head>
<body>
<textarea id="source">
[//]: (----------------------------------------------------------------------)
class: center middle
# Pyglmnet
.center[
<img src="figures/pyglmnet.png" style="width: 700px;"/>
<div>
<i class="fa fa-github"></i> github.com/glm-tools/pyglmnet
</div>
<div>
<i class="fa fa-newspaper-o"></i> glm-tools.github.io/pyglmnet
</div>
]
<br>
<br>
.left[
<ul class="fa-ul">
<li><i class="fa-li fa fa-github"></i>pavanramkumar</li>
<li><i class="fa-li fa fa-twitter"></i>@desipoika</li>
<li><i class="fa-li fa fa-home"></i>pavanramkumar.github.io</li>
</ul>
]
---
[//]: (----------------------------------------------------------------------)
# Why this package?
**Generalized linear models** encompass a broad class of models.
<img src="figures/glmpaper-00.png" style="width: 500px;"/>
**Elastic net** is an excellent algorithm for regularization.
<img src="figures/enetpaper-00.png" style="width: 500px;"/>
---
[//]: (----------------------------------------------------------------------)
# Why this package?
Fast implementation in widely popular **R** package **`glmnet`** for
**Generalized Linear Models**.
<img src="figures/enetpaper-01.png" style="width: 500px;"/>
Scikit-learn has an implementation only for linear and logistic models.
| | `normal` | `logistic` | `poisson` | `softplus` | `multinomial` | `cox` |
|:--------------|:---------:|:----------:|:----------:|:----------:|:-------------:|:--------:|
|`R` | `x` | `x` | `x` | -- | -- | `x` |
|`scikit-learn` | `x` | `x` | -- | -- | -- | -- |
|`pyglmnet` | `x` | `x` | `x` | `x` | `x` | -- |
---
[//]: (----------------------------------------------------------------------)
# @KordingLab, Northwestern
.center[
<img src="figures/pidata.png" style="width: 800px;"/>
]
---
[//]: (----------------------------------------------------------------------)
# Machine Learning for Brain and Behavior
.center[
<img src="figures/about-00.png" style="width: 600px;"/>
]
---
# We often want to model spike counts
.center[
<img src="figures/about-01.png" style="width: 600px;"/>
]
---
# Count statistics abound in the real world
<img src="figures/githubstars-01.png" style="width: 700px;"/>
---
[//]: (----------------------------------------------------------------------)
# Example neurons
<img src="figures/neurons-00.png" style="width: 800px;"/>
---
# Linear vs. Poisson Models
<img src="figures/neurons-03.png" style="width: 600px;"/>
--
<img src="figures/neurons-04.png" style="width: 600px;"/>
See [Linear vs. Poisson notebook](https://nbviewer.jupyter.org/github/pavanramkumar/pydata-chicago-2016/blob/master/notebooks/Linear_vs_Poisson.ipynb).
---
[//]: (----------------------------------------------------------------------)
# Linear Regression
### Predict
- a target variable, `\(y\)`
- as a linear combination of `\(p\)` predictors, `\(\mathbf{x} = [x_{1}, \dots, x_{p}]\)`.
- from a set of `\(n\)` observations `\(\left\{\mathbf{x}_i, y_i\right\}\)`.
$$
\hat{y} = \beta_0 + \beta_1 x_1 + \dots + \beta_p x_p
$$
- In matrix form:
$$
\mathbf{\hat{y}} = \beta_0 + \mathbf{X} \mathbf{\beta}
$$
where `\(\mathbf{\hat{y}} = [\hat{y}_1, \dots, \hat{y}_n]\)`
and `\(\mathbf{X} = [\mathbf{x}_1^T, \dots, \mathbf{x}_n^T]\)`.
```python
def predict(X, beta0, beta):
y = beta0 + np.dot(X, beta)
return y
```
---
[//]: (----------------------------------------------------------------------)
# Linear Regression
### Mean Squared Loss
- To fit the parameters `\(\left\{\beta_0, \beta\right\}\)`,
we minimize the mean squared loss:
$$
J = \frac{1}{2n} \sum_i (y_i - \hat{y}_i)^2
= \frac{1}{2n} \sum_i (y_i - (\beta_0 + \mathbf{x}_i^T \mathbf{\beta}))^2
$$
- In matrix form:
$$
J = \frac{1}{2n} (\mathbf{y} - (\beta_0 + \mathbf{X} \mathbf{\beta}))^T
(\mathbf{y} - (\beta_0 + \mathbf{X} \mathbf{\beta}))
$$
```python
def loss(X, y, beta0, beta):
n = y.shape[0]
yhat = predict(X, beta0, beta)
err = y - yhat
J = 1. / (2. * n) * np.dot(err.T , err)
return J
```
---
[//]: (----------------------------------------------------------------------)
# Linear Regression
### Negative Log Likelihood Loss
- Real data is noisy. Let's assume random Gaussian noise.
$$
\mathbf{y} = \beta_0 + \mathbf{X}\beta + \epsilon; \epsilon \sim \mathcal{N}(\mathbf{0}, \sigma^2\mathbf{I})
$$
- The likelihood of observing the target variables, given the predictors is:
$$
P(\mathbf{y} | \mathbf{X}; \beta_0, \beta) \propto \prod_i \exp\left\[-\frac{1}{2} (y_i - \beta_0 - \mathbf{x}_i^T \beta)^2\right\]
$$
- The negative log-likelihood function is identical to the mean squared loss!
$$
-\mathcal{L}(\beta_0, \beta) = \frac{1}{2n}\sum_i (y_i - \hat{y}_i)^2
$$
???
Thus, we can write out the loss as negative log-likelihood for any distribution!
---
[//]: (----------------------------------------------------------------------)
# Generalized Linear Models
Recall that for linear regression
$$
\mathbf{y} = \beta_0 + \mathbf{X}\beta + \epsilon; \epsilon \sim \mathcal{N}(\mathbf{0}, \sigma^2\mathbf{I})
$$
Thus, `\(\mathbf{y}\)` is a multivariate normal distribution with mean function `\(\mu\)`.
$$
\mathbf{y} \sim \mathcal{N}(\mathbf{\mu}, \sigma^2\mathbf{I}); \mathbf{\mu} = \beta_0 + \mathbf{X}\beta
$$
This is a special case of GLMs:
$$
\mathbf{y} \sim \text{ExpFamily}(\mathbf{\mu}, \Theta); \mathbf{\mu} = q(\beta_0 + \mathbf{X}\beta)
$$
where
- `\(q(.)\)` is a pointwise nonlinearity, and
- `\(\text{ExpFamily}(\mathbf{\mu}, \Theta)\)` is any pdf in the exponential family,
with mean function `\(\mathbf{\mu}\)` and parameters `\(\Theta\)`.
???
Using this paramterization, GLMs allow us to model target variables `\(\mathbf{y}\)`
that take on different ranges (e.g. binary variables, counts)
and distributions, using real-valued predictors `\(\mathbf{X}\)`.
---
[//]: (----------------------------------------------------------------------)
# Example: Poisson Regression
Consider the prediction problem where `\(y_i\)`'s are count variables.
`\(\mathbf{y} = \{y_i\}\)` where `\(y_i \in \{0, 1, 2, \dots \}\)`.
#### Assumption 1: Nonlinearity
We set `\(q(z) = \exp(z)\)`,
with domain `\((-\infty, \infty)\)`, and range `\([0, \infty)\)`.
```python
def predict(X, beta0, beta):
y = np.exp(beta0 + np.dot(X, beta))
return y
```
#### Assumption 2: Noise distribution
We assume that `\(y_i\)`'s are Poisson random variables with mean `\(\lambda\)`.
We model this as
`\(\lambda \sim \hat{y}_i = q(\beta_0 + \mathbf{x}_i^T\beta)\)`.
---
[//]: (----------------------------------------------------------------------)
# Example: Poisson Regression
Let's write out the negative log-likelihood of the data given these assumptions.
$$
P(y_i = k) = \frac{e^{-\lambda} \lambda^k}{k!}
$$
$$
P(\mathbf{y} | \mathbf{X}; \beta_0, \beta) \propto
\prod_i e^{-\lambda_i} (\lambda_i)^{y_i}
$$
$$
-\mathcal{L}(\beta_0, \beta) = -\Bigg[\sum_i y_i log(\hat{y}_i) -\hat{y}_i \Bigg]
$$
This is known as the Poisson loss.
```python
def loss(X, y, beta0, beta):
yhat = predict(X, beta0, beta)
J = -np.sum(y * np.log(yhat) - yhat)
return J
```
---
[//]: (----------------------------------------------------------------------)
# Example: Logistic Regression
Consider the binary classification scenario, where `\(y_i\)`'s are binary random variables.
`\(\mathbf{y} = \{y_i\}\)` where `\(y_i \in \{0, 1\}\)`.
#### Assumption 1: Nonlinearity
We set `\(q(z) = \sigma(z) = \frac{1}{1 + e^{-z}}\)`,
which is the sigmoid function, with domain `\((-\infty, \infty)\)`, and range `\([0, 1]\)`.
```python
def sigmoid(z):
s = np.exp(z) / (1 + np.exp(z))
return s
def predict(X, beta0, beta):
y = sigmoid(beta0 + np.dot(X, beta))
return y
```
---
[//]: (----------------------------------------------------------------------)
# Example: Logistic Regression
#### Assumption 2: Noise distribution
We assume that `\(y_i\)`'s are Bernoulli random variables with mean `\(p\)`.
`\(P(y_i = 1) = p; P(y_i = 0) = 1-p\)`.
We model `\(p \sim \hat{y}_i = q(\beta_0 + \mathbf{x}_i^T\beta)\)`.
Let's write out the negative log-likelihood of the data given these assumptions.
$$
P(\mathbf{y} | \mathbf{X}; \beta_0, \beta) =
\prod_i p^{y_i} (1-p)^{(1-y_i)}
$$
$$
-\mathcal{L}(\beta_0, \beta) = -\Bigg[\sum_i y_i log(\hat{y}_i) + (1-y_i) log(1-\hat{y}_i) \Bigg]
$$
This is known as the cross entropy loss.
```python
def loss(X, y, beta0, beta):
yhat = predict(X, beta0, beta)
J = -np.sum(y * np.log(yhat) + \
(1 - y) * np.log(1 - yhat))
return J
```
---
[//]: (----------------------------------------------------------------------)
# Optimization
Recalling our calculus 101, we minimize the loss `\(J\)` by taking the derivative
(aka gradient) and setting it to zero.
#### Linear Regression
.left[`\(
J = \frac{1}{2n} (\mathbf{y} - \mathbf{\hat{y}})^T
(\mathbf{y} - \mathbf{\hat{y}})
\)`]
.left[`\(
\frac{\partial J}{\partial \beta_0}
= -\frac{1}{n}(\mathbf{y} - \mathbf{\hat{y}})
= -\frac{1}{n}(\mathbf{y} - \beta_0 - \mathbf{X}\beta)
= 0
\)`]
.left[`\(
\frac{\partial J}{\partial \beta}
= -\frac{1}{n}\mathbf{X}^T(\mathbf{y} - \mathbf{\hat{y}})
= -\frac{1}{n}\mathbf{X}^T(\mathbf{y} - \beta_0 - \mathbf{X}\beta)
= 0
\)`]
.left[
```python
def grad_loss(X, y, beta0, beta):
n = y.shape[0]
yhat = predict(X, beta0, beta)
err = y - yhat
grad_beta0 = -1. / n * err
grad_beta = -1. / n * np.dot(X.T, err)
return grad_beta0, grad_beta
```
]
---
[//]: (----------------------------------------------------------------------)
# Optimization
Recalling our calculus 101, we minimize the loss `\(J\)` by taking the derivative
(aka gradient) and setting it to zero.
#### Logistic Regression
.left[`\(
J = -\frac{1}{n} \left\{ \mathbf{y}^T \log\left\{\mathbf{\hat{y}}\right\} +
(\mathbf{1} - \mathbf{y})^T \log\left\{\mathbf{1} - \mathbf{\hat{y}}\right\}
\right\}
\)`]
.left[`\(
\frac{\partial J}{\partial \beta_0}
= -\frac{1}{n}(\mathbf{y} - \mathbf{\hat{y}})
= -\frac{1}{n}(\mathbf{y} - \mathbf{1}./ (\mathbf{1} + \exp(-\beta_0 - \mathbf{X}\beta)))
= 0
\)`]
.left[`\(
\frac{\partial J}{\partial \beta}
= -\frac{1}{n}\mathbf{X}^T(\mathbf{y} - \mathbf{\hat{y}})
= -\frac{1}{n}\mathbf{X}^T(\mathbf{y} - \mathbf{1}./ (\mathbf{1} + \exp(-\beta_0 - \mathbf{X}\beta)))
= 0
\)`]
.left[
```python
def grad_loss(X, y, beta0, beta):
n = y.shape[0]
yhat = predict(X, beta0, beta)
err = y - yhat
grad_beta0 = -1. / n * err
grad_beta = -1. / n * np.dot(X.T, err)
return grad_beta0, grad_beta
```
]
---
[//]: (----------------------------------------------------------------------)
# Gradient Descent
We cannot always solve this system of equations in closed form.
But we can arrive at a unique solution by guessing a starting point and
iteratively taking small steps against the gradient.
This is called gradient descent.
.center[
`\(
\beta_0 \leftarrow \beta_0 - \zeta * \frac{\partial J}{\partial \beta_0}
\)`
`\(
\beta \leftarrow \beta - \zeta * \frac{\partial J}{\partial \beta}
\)`
<br>
<br>
<img src="figures/gd-start.png" style="width: 350px;"/>
]
---
# Gradient Descent
We cannot always solve this system of equations in closed form.
But we can arrive at a unique solution by guessing a starting point and
iteratively taking small steps against the gradient.
This is called gradient descent.
.center[
`\(
\beta_0 \leftarrow \beta_0 - \zeta * \frac{\partial J}{\partial \beta_0}
\)`
`\(
\beta \leftarrow \beta - \zeta * \frac{\partial J}{\partial \beta}
\)`
<br>
<br>
<img src="figures/gd-00.png" style="width: 350px;"/>
]
---
# Gradient Descent
We cannot always solve this system of equations in closed form.
But we can arrive at a unique solution by guessing a starting point and
iteratively taking small steps against the gradient.
This is called gradient descent.
.center[
`\(
\beta_0 \leftarrow \beta_0 - \zeta * \frac{\partial J}{\partial \beta_0}
\)`
`\(
\beta \leftarrow \beta - \zeta * \frac{\partial J}{\partial \beta}
\)`
<br>
<br>
<img src="figures/gd-01.png" style="width: 350px;"/>
]
---
# Gradient Descent
We cannot always solve this system of equations in closed form.
But we can arrive at a unique solution by guessing a starting point and
iteratively taking small steps against the gradient.
This is called gradient descent.
.center[
`\(
\beta_0 \leftarrow \beta_0 - \zeta * \frac{\partial J}{\partial \beta_0}
\)`
`\(
\beta \leftarrow \beta - \zeta * \frac{\partial J}{\partial \beta}
\)`
<br>
<br>
<img src="figures/gd-05.png" style="width: 350px;"/>
]
---
# Gradient Descent
We cannot always solve this system of equations in closed form.
But we can arrive at a unique solution by guessing a starting point and
iteratively taking small steps against the gradient.
This is called gradient descent.
.center[
`\(
\beta_0 \leftarrow \beta_0 - \zeta * \frac{\partial J}{\partial \beta_0}
\)`
`\(
\beta \leftarrow \beta - \zeta * \frac{\partial J}{\partial \beta}
\)`
<br>
<br>
<img src="figures/gd-15.png" style="width: 350px;"/>
]
---
# Gradient Descent
We cannot always solve this system of equations in closed form.
But we can arrive at a unique solution by guessing a starting point and
iteratively taking small steps against the gradient.
This is called gradient descent.
.center[
`\(
\beta_0 \leftarrow \beta_0 - \zeta * \frac{\partial J}{\partial \beta_0}
\)`
`\(
\beta \leftarrow \beta - \zeta * \frac{\partial J}{\partial \beta}
\)`
<br>
<br>
<img src="figures/gd-50.png" style="width: 350px;"/>
]
---
# Gradient Descent
We cannot always solve this system of equations in closed form.
But we can arrive at a unique solution by guessing a starting point and
iteratively taking small steps against the gradient.
This is called gradient descent.
.center[
`\(
\beta_0 \leftarrow \beta_0 - \zeta * \frac{\partial J}{\partial \beta_0}
\)`
`\(
\beta \leftarrow \beta - \zeta * \frac{\partial J}{\partial \beta}
\)`
]
```python
def fit(X, y, max_iter, learning_rate):
p = X.shape[1]
beta0_hat = (1. / (p + 1)) * np.random.randn(1)
beta_hat = (1. / (p + 1)) * np.random.randn(p, 1)
for iter in np.range(max_iter):
grad_beta0, grad_beta = grad_loss(X, y, beta0_hat, beta_hat):
beta0_hat += -learning_rate * grad_beta0
beta_hat += -learning_rate * grad_beta
return beta0_hat, beta_hat
```
---
[//]: (----------------------------------------------------------------------)
# Regularization
.center[
<img src="figures/reg.png" style="width: 400px;"/>
]
On the same dataset size (\(N\)), a model with more parameters (\(q > p\))
will fit better on the training set but may generalize worse on a test set.
This needs to be penalized.
.left[
$$
L_2 \text{ penalized loss: }
-\mathcal{L}(\beta_0, \beta) + \lambda \frac{1}{2}||\beta||^2
$$
]
```python
def L2loss(X, y, beta0, beta, reg_lambda):
J = loss(X, y, beta0, beta) + 0.5 * reg_lambda * np.sum(beta ** 2)
return J
def grad_L2loss(X, y, beta0, beta, reg_lambda):
grad_beta0, grad_beta = grad_loss(X, y, beta0, beta)
grad_beta += reg_lambda * beta
return grad_beta0, grad_beta
```
---
[//]: (----------------------------------------------------------------------)
# Ridge Regression
Imposing an `\(L_2\)` penalty is known as ridge regression. It penalizes large
`\(\beta\)`'s.
Example logistic regression: `\(N = 500, p = 100\)`.
.center[
<img src="figures/reg-00-L2-001.png" style="width: 350px;"/>
<img src="figures/reg-00-L2-003.png" style="width: 350px;"/>
]
See [Regularization notebook](https://nbviewer.jupyter.org/github/pavanramkumar/pydata-chicago-2016/blob/master/notebooks/Regularization.ipynb).
---
[//]: (----------------------------------------------------------------------)
# Lasso Regression
However, ridge regression doesn't work well when only a few features are predictive.
We want something that zeroes out a large number of `\(\beta\)`'s.
For this, we can impose an `\(L_1\)` penalty.
.left[
$$
L_1\text{ penalized loss: }
-\mathcal{L}(\beta_0, \beta) + \lambda ||\beta||_1
$$
]
```python
def L1loss(X, y, beta0, beta, reg_lambda):
J = loss(X, y, beta0, beta) + reg_lambda * np.sum(np.abs(beta))
```
---
[//]: (----------------------------------------------------------------------)
# Ridge vs. Lasso
Example logistic regression: `\(N = 200, p = 500\)`.
See [Regularization notebook](https://nbviewer.jupyter.org/github/pavanramkumar/pydata-chicago-2016/blob/master/notebooks/Regularization.ipynb).
.center[
<img src="figures/reg-01-L2-001.png" style="width: 250px;"/>
<img src="figures/reg-01-L2-003.png" style="width: 250px;"/>
]
.center[
<img src="figures/reg-01-L1-001.png" style="width: 250px;"/>
<img src="figures/reg-01-L1-003.png" style="width: 250px;"/>
]
---
[//]: (----------------------------------------------------------------------)
# Elastic net Regression
Best of both worlds!
.left[
$$
\text{elastic net loss: }
-\mathcal{L}(\beta_0, \beta) + \lambda \Big\[ \frac{1}{2}(1-\alpha)||\beta||^2 + \alpha ||\beta||_1 \Big \]
$$
]
`\(\alpha\)` is known as the L1-ratio.
```python
def enetloss(X, y, beta0, beta, reg_lambda, alpha):
J = loss(X, y, beta0, beta) + reg_lambda * \
((1 - alpha) * 0.5 * np.sum(beta ** 2) + \
alpha * np.sum(np.abs(beta)))
return J
```
---
[//]: (----------------------------------------------------------------------)
# Advanced Optimization
#### Projected gradient
Note that `\(||\beta||_1\)` is not differentiable at `\(\beta = 0\)`.
To deal with this, we use a technique called projected gradient.
We update:
`\(
\beta \leftarrow \mathcal{S}_{\lambda}\left(\beta - \zeta * \frac{\partial J}{\partial \beta}\right),
\)`
where
`\(
\mathcal{S}_{\lambda}(x) =
0 \text{, if } |x| \lt \lambda,
sgn(x) (|x| - \lambda) \text{, otherwise}.
\)`
`\(\mathcal{S}_{\lambda}(x)\)` is known as the soft threshold or proximal operator.
```python
def prox(x, l):
return np.sign(X) * (np.abs(X) - l) * (np.abs(X) > l)
def fit(X, y):
# ...
beta_hat = prox(beta_hat - learning_rate * grad_beta, reg_lambda)
```
---
[//]: (----------------------------------------------------------------------)
# Advanced Optimization
#### Warm Restarts
In practice, we fit the model not for a single value of `\(\lambda\)` but for a
**regularization path**, `\(\lambda = \left\{\lambda_{max}, \dots, \lambda_{min}\right\}\)`.
Starting with `\(\lambda_{max}\)`, we solve for `\(\lambda_l\)`, and then initialize for
the subsequent `\(\lambda_{l+1}\)` with this solution:
`\(
\beta^0(\lambda_{l+1}) = \beta^{opt}(\lambda_l).
\)`
This is known as a **warm restart** and leads to much faster convergence.
```python
# Warm initialize parameters
def fit(X, y):
#...
for l, rl in enumerate(reg_lambda):
fit_params.append({'beta0': beta0_hat, 'beta': beta_hat})
if l == 0:
fit_params[-1]['beta0'] = beta0_hat
fit_params[-1]['beta'] = beta_hat
else:
fit_params[-1]['beta0'] = fit_params[-2]['beta0']
fit_params[-1]['beta'] = fit_params[-2]['beta']
```
---
[//]: (----------------------------------------------------------------------)
# Advanced Optimization
#### Active Set
For large `\(\lambda\)`'s, the projected gradient method
`\(\beta \leftarrow \mathcal{S}_\lambda(\beta - \zeta * J'(\beta))\)`
sets a lot of `\(\beta\)`'s to zero.
For problems with many predictors `\(p\)`, it's much faster to store an active set
`\(\mathcal{K}\)` of parameter indices and only update non-zero parameters
in each iteration.
If `\(\beta_k\)` has been zeroed out at iteration `\(t\)`, we remove its index
from the active set: `\(\mathcal{K} \leftarrow \mathcal{K} - \left\{k\right\}\)`.
Thus, we only update a smaller and smaller subset of `\(\beta\)`'s with an
increasing number of iterations.
---
[//]: (----------------------------------------------------------------------)
# Advanced Optimization
#### Cyclic Coordinate Descent with Newton update
[Optimization notebook](https://nbviewer.jupyter.org/github/pavanramkumar/pydata-chicago-2016/blob/master/notebooks/Optimization.ipynb).
The active set strategy allows us to do better than vanilla gradient descent.
In each iteration, we update each `\(\beta_k \in \mathcal{K}\)`
with a Newton step.
Gradient step:
`\(\beta - \zeta * J'(\beta)\)`
vs.
Newton step:
`\(\beta - [J''(\beta)]^{-1} * J'(\beta)\)`
<img src="figures/graddesc.png" style="width: 300px;"/>
---
[//]: (----------------------------------------------------------------------)
# Advanced Optimization: Sympy
[Sympy notebook](https://nbviewer.jupyter.org/github/pavanramkumar/pydata-chicago-2016/blob/master/notebooks/Sympy.ipynb).
Take the opportunity to check your calculus work with `sympy`.
#### Linear Regression
```python
from sympy import symbols, diff, simplify
# Define symbols
x1, x2, y, b0, b1, b2 = symbols("x1 x2 y b0 b1 b2")
# Predict
yhat = b0 + b1 * x1 + b2 * x2
# Compute loss
loss = 0.5 * (y - yhat) ** 2
# Differentiate
print simplify(diff(loss, b0))
print simplify(diff(loss, b1))
print simplify(diff(loss, b2))
```
```
1.0*b0 + 1.0*b1*x1 + 1.0*b2*x2 - 1.0*y
1.0*x1*(b0 + b1*x1 + b2*x2 - y)
1.0*x2*(b0 + b1*x1 + b2*x2 - y)
```
---
[//]: (----------------------------------------------------------------------)
# Advanced Optimization: Sympy
#### Logistic Regression
```python
from sympy import symbols, diff, simplify
# Define symbols
x1, x2, y, b0, b1, b2 = symbols("x1 x2 y b0 b1 b2")
# Predict
z = b0 + b1 * x1 + b2 * x2
yhat = 1 / (1 + exp(-z))
# Compute loss
loss = y * log(yhat) + (1 - y) * log(1 - yhat)
# Differentiate
print simplify(diff(loss, b0))
print simplify(diff(loss, b1))
print simplify(diff(loss, b2))
```
```
(y*exp(-b0 - b1*x1 - b2*x2) + y - 1)/(exp(-b0 - b1*x1 - b2*x2) + 1)
x1*(y*exp(-b0 - b1*x1 - b2*x2) + y - 1)/(exp(-b0 - b1*x1 - b2*x2) + 1)
x2*(y*exp(-b0 - b1*x1 - b2*x2) + y - 1)/(exp(-b0 - b1*x1 - b2*x2) + 1)
```
---
[//]: (----------------------------------------------------------------------)
# Advanced Optimization: Theano
[Theano notebook](https://nbviewer.jupyter.org/github/pavanramkumar/pydata-chicago-2016/blob/master/notebooks/Theano.ipynb).
`theano` does all the differentiation for you!
```python
import theano
# Data as symbolic variables (theano tensors)
X = theano.tensor.dmatrix('X')
y = theano.tensor.dvector('y')
# Parameters as shared variables
beta = theano.shared(1. / (p + 1) * np.random.randn(p), name='beta')
beta0 = theano.shared(0., name='beta0')
```
```python
# Construct Theano expression graph
# Compute the prediction
yhat = theano.tensor.dot(X, beta) + beta0
# Define the mean square error cost
loss = 1. / (2. * n) * (y -yhat) ** 2
# Compute the gradient of the loss
grad_beta0, grad_beta = theano.tensor.grad(loss, [beta0, beta])
```
---
[//]: (----------------------------------------------------------------------)
# Advanced Optimization: Theano
```python
# Compile
fit = theano.function(
inputs=[X, y],
outputs=[yhat, loss],
updates=((beta0, beta0 - 0.01 * grad_beta0),
(beta, beta - 0.01 * grad_beta)))
predict = theano.function(inputs=[X], outputs=yhat)
```
```python
# Fit on training set
max_iter = 1000
for i in range(max_iter):
ytrain_hat, train_err = fit(Xtrain, ytrain)
# Predict on test set
ytest_hat = predict(Xtest)
```
---
# Implementation
- Object-oriented: single `GLM()` class for all methods.
- Docstrings in conformity to `scikit-learn` best practices.
- `fit()`, `predict()`, and `score()` methods.
- `simulate()` method for drawing samples from a model.
- Estimators can be sliced: `glm[0].predict(X_test)`.
- Unit tests and continuous integration.
```python
class GLM(object):
"""Generalized Linear Model (GLM)
This class implements elastic-net regularized generalized linear models.
The core algorithm is defined in the article.
min_(beta0, beta) [-L + lamda * P]
where
L is log-likelihood term
P is elastic-net penalty term
Parameters
----------
distr: str
distribution family can be one of the following
'poisson' or 'poissonexp' or 'normal' or 'binomial' or 'multinomial'
default: 'poisson'
```
---
# Documentation
.center[
<img src="figures/docpages.png" style="width: 800px;"/>
]
.right[
<div>
<i class="fa fa-newspaper-o"></i> glm-tools.github.io/pyglmnet
</div>
]
---
# Summary
#### What did we learn?
- Mean Squared Error is special case of Negative Log Likelihood (NLL)
- GLMs = Linear regression + Nonlinearity + Noise model
- Regularize with elastic net = Ridge + Lasso
- Optimize penalized NLL with gradient descent
- Optimize better with active set + coordinate descent + Newton update
- Use `sympy` for your calculus or leave it all to `theano`!
#### What next?
- [Slides and notebooks](https://github.com/pavanramkumar/pydata-chicago-2016)
- Clone `pyglmnet` and start applying it to your data!
- Code for `pyglmnet` and submit pull requests!
.right[
<div>
<i class="fa fa-github"></i> github.com/glm-tools/pyglmnet
</div>
<div>
<i class="fa fa-newspaper-o"></i> glm-tools.github.io/pyglmnet
</div>
]
---
[//]: (----------------------------------------------------------------------)
# Credits
#### Development and Tests
- Mainak Jas (Alex Gramfort's Group, Paris)
- Titipat Achakulvisut (KordingLab, Chicago)
- Daniel Acuna (Assistant Professor, iSchool, Syracuse)
- Hugo Fernandes (Data Science Fellow, Insight)
#### Documentation
- Mark Albert's interns (Loyola U, Chicago)
#### My GLM gurus
- Konrad Kording (Professor, Northwestern)
- Sara Solla (Professor, Northwestern)
.right[
<div>
<i class="fa fa-github"></i> github.com/glm-tools/pyglmnet
</div>
<div>
<i class="fa fa-newspaper-o"></i> glm-tools.github.io/pyglmnet
</div>
]
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js">
</script>
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML&delayStartupUntil=configured" type="text/javascript">
</script>
<script>
var slideshow = remark.create();
// Setup MathJax
MathJax.Hub.Config({
tex2jax: {
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
}
});
MathJax.Hub.Queue(function() {
$(MathJax.Hub.getAllJax()).map(function(index, elem) {
return(elem.SourceElement());
}).parent().addClass('has-jax');
});
MathJax.Hub.Configured();
</script>
</body>
</html>