-
Notifications
You must be signed in to change notification settings - Fork 2
/
ridge_em.Rmd
435 lines (319 loc) · 10.1 KB
/
ridge_em.Rmd
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
---
title: "ridge_em"
author: "Matthew Stephens"
date: "2020-05-29"
output: workflowr::wflow_html
editor_options:
chunk_output_type: console
---
## Introduction
Here I am going to experiment with EM algorithm for estimating parameters
of ridge regression in different parameterizations.
Initial derivations of EM updates are [here](EM_Ridge.pdf).
I initially implemented 1,2, and 5 in that document.
A futher derivation for another parameterization is [here](EM_Ridge_02.pdf).
## Simple parameterization
$$y \sim N(Xb,s^2)$$
$$b \sim N(0,s_b^2I)$$
```{r}
ridge_em1 = function(y,X, s2,sb2, niter=10){
XtX = t(X) %*% X
Xty = t(X) %*% y
yty = t(y) %*% y
n = length(y)
p = ncol(X)
loglik = rep(0,niter)
for(i in 1:niter){
V = chol2inv(chol(XtX+ diag(s2/sb2,p)))
SigmaY = sb2 *(X %*% t(X)) + diag(s2,n)
loglik[i] = mvtnorm::dmvnorm(as.vector(y),sigma = SigmaY,log=TRUE)
Sigma1 = s2*V # posterior variance of b
mu1 = as.vector(V %*% Xty) # posterior mean of b
s2 = as.vector((yty + sum(diag(XtX %*% (mu1 %*% t(mu1) + Sigma1)))- 2*sum(Xty*mu1))/n)
sb2 = mean(mu1^2+diag(Sigma1))
}
return(list(s2=s2,sb2=sb2,loglik=loglik,postmean=mu1))
}
```
## Scaled parameterization
In this parameterization I take the $s_b$ out of the prior and put it
$$y \sim N(s_b Xb,s^2)$$
$$b \sim N(0,I)$$.
```{r}
ridge_em2 = function(y,X, s2,sb2, niter=10){
XtX = t(X) %*% X
Xty = t(X) %*% y
yty = t(y) %*% y
n = length(y)
p = ncol(X)
loglik = rep(0,niter)
for(i in 1:niter){
V = chol2inv(chol(XtX+ diag(s2/sb2,p)))
SigmaY = sb2 *(X %*% t(X)) + diag(s2,n)
loglik[i] = mvtnorm::dmvnorm(as.vector(y),sigma = SigmaY,log=TRUE)
Sigma1 = (s2/sb2)*V # posterior variance of b
mu1 = (sqrt(sb2)/s2)*as.vector(Sigma1 %*% Xty) # posterior mean of b
sb2 = (sum(mu1*Xty)/sum(diag(XtX %*% (mu1 %*% t(mu1) + Sigma1))))^2
s2 = as.vector((yty + sb2*sum(diag(XtX %*% (mu1 %*% t(mu1) + Sigma1)))- 2*sqrt(sb2)*sum(Xty*mu1))/n)
}
return(list(s2=s2,sb2=sb2,loglik=loglik,postmean=mu1*sqrt(sb2)))
}
```
## A hybrid/redundant parameterization
Motivated by initial observations that 1 and 2 can converge
well in different settings I implemented a hybrid of the two:
$$y \sim N(s_b Xb,s^2)$$
$$b \sim N(0,\lambda^2).$$
Note that there is a redundancy/non-identifiability here as the likelihood depends
only on $s_b^2 \lambda^2$. The hope is to get the
best of both worlds...
```{r}
ridge_em3 = function(y,X, s2, sb2, l2, niter=10){
XtX = t(X) %*% X
Xty = t(X) %*% y
yty = t(y) %*% y
n = length(y)
p = ncol(X)
loglik = rep(0,niter)
for(i in 1:niter){
V = chol2inv(chol(XtX+ diag(s2/(sb2*l2),p)))
SigmaY = l2*sb2 *(X %*% t(X)) + diag(s2,n)
loglik[i] = mvtnorm::dmvnorm(as.vector(y),sigma = SigmaY,log=TRUE)
Sigma1 = (s2/sb2)*V # posterior variance of b
mu1 = (1/sqrt(sb2))*as.vector(V %*% Xty) # posterior mean of b
sb2 = (sum(mu1*Xty)/sum(diag(XtX %*% (mu1 %*% t(mu1) + Sigma1))))^2
s2 = as.vector((yty + sb2*sum(diag(XtX %*% (mu1 %*% t(mu1) + Sigma1)))- 2*sqrt(sb2)*sum(Xty*mu1))/n)
l2 = mean(mu1^2+diag(Sigma1))
}
return(list(s2=s2,sb2=sb2,l2=l2,loglik=loglik,postmean=mu1*sqrt(sb2)))
}
```
## Avoiding large 2nd moment computation
The previous parameterizations require the full second moment of $b$,
which is a $p$ times $p$ matrix. This can be expensive to compute if $p$ is big.
The following parameterization avoids this.
$$y \sim N(sXb, s^2 I)$$
$$b \sim N(0,s_b^2I)$$
(Note that for simplicity I still do compute the $p \times p$ matrix, as for now it
is the easiest way to implement the ridge regression).
```{r}
dot = function(x,y){sum(x*y)}
ridge_em4 = function(y, X, s2, sb2, niter=10){
XtX = t(X) %*% X
Xty = t(X) %*% y
yty = t(y) %*% y
n = length(y)
p = ncol(X)
loglik = rep(0,niter)
for(i in 1:niter){
SigmaY = s2*sb2 *(X %*% t(X)) + diag(s2,n)
loglik[i] = mvtnorm::dmvnorm(as.vector(y),sigma = SigmaY,log=TRUE)
Sigma1 = chol2inv(chol(XtX + diag(1/sb2,p))) # posterior variance of b
mu1 = (1/sqrt(s2))*as.vector(Sigma1 %*% Xty) # posterior mean of b
sb2 = mean(mu1^2+diag(Sigma1))
yhat = X %*% mu1
s2 = drop((0.5/n)* (sqrt(dot(y,yhat)^2 + 4*n*yty) - dot(y,yhat)))^2
}
return(list(s2=s2,sb2=sb2,loglik=loglik,postmean=mu1*sqrt(s2)))
}
```
## Another redundant parameterization
Here I consider
$$y \sim N(s_b Xb, s^2 I)$$
where
$$b \sim N(0,s^2 \lambda^2I).$$
This is like the redundant parameterization above,
except that the prior on $b$ is scaled by the
residual variance ($s^2$).
This is motivated by the result in the Blasso paper that this makes
the posterior on $s^2,b$ convex.
```{r}
ridge_em5 = function(y,X, s2, sb2, l2, niter=10){
XtX = t(X) %*% X
Xty = t(X) %*% y
yty = t(y) %*% y
n = length(y)
p = ncol(X)
loglik = rep(0,niter)
for(i in 1:niter){
SigmaY = l2* s2* sb2 *(X %*% t(X)) + diag(s2,n)
loglik[i] = mvtnorm::dmvnorm(as.vector(y),sigma = SigmaY,log=TRUE)
#V = chol2inv(chol(XtX+ diag(s2/(sb2*l2),p)))
Sigma1 = chol2inv(chol((sb2/s2) * XtX + diag(1/(s2*l2),p) )) # posterior variance of b
mu1 = (sqrt(sb2)/s2)*as.vector(Sigma1 %*% Xty) # posterior mean of b
sb2 = (sum(mu1*Xty)/sum(diag(XtX %*% (mu1 %*% t(mu1) + Sigma1))))^2 #same as em3
s2 = as.vector((sum((mu1^2+diag(Sigma1))/l2)+ yty + sb2*sum(diag(XtX %*% (mu1 %*% t(mu1) + Sigma1)))- 2*sqrt(sb2)*sum(Xty*mu1))/(n+p))
# as in em3 but adds sum(mu1^2/l2) to numerator and p to demoninator
l2 = mean(mu1^2+diag(Sigma1))/s2 #as in em3 but divided by s2
}
return(list(s2=s2,sb2=sb2,l2=l2,loglik=loglik,postmean=mu1*sqrt(sb2)))
}
```
## Simple Simulations
This is a simple simulation with independent design matrix.
### High signal:
This simulation has high signal:
```{r}
set.seed(100)
sd = 1
n = 100
p = n
X = matrix(rnorm(n*p),ncol=n)
btrue = rnorm(n)
y = X %*% btrue + sd*rnorm(n)
plot(X %*% btrue, y)
```
```{r}
y.em1 = ridge_em1(y,X,1,1,100)
y.em2 = ridge_em2(y,X,1,1,100)
y.em3 = ridge_em3(y,X,1,1,1,100)
y.em4 = ridge_em4(y,X,1,1,100)
y.em5 = ridge_em5(y,X,1,1,1,100)
plot_loglik = function(res){
maxloglik = max(res[[1]]$loglik)
minloglik = min(res[[1]]$loglik)
maxlen =length(res[[1]]$loglik)
for(i in 2:length(res)){
maxloglik = max(c(maxloglik,res[[i]]$loglik))
minloglik = min(c(minloglik,res[[i]]$loglik))
maxlen= max(maxlen, length(res[[i]]$loglik))
}
plot(res[[1]]$loglik,type="n",ylim=c(minloglik,maxloglik),xlim=c(0,maxlen),ylab="log-likelihood",
xlab="iteration")
for(i in 1:length(res)){
lines(res[[i]]$loglik,col=i,lwd=2)
}
}
res = list(y.em1,y.em2,y.em3,y.em4,y.em5)
plot_loglik(res)
```
## No signal
This simulation has no signal (b=0):
```{r}
btrue = rep(0,n)
y = X %*% btrue + sd*rnorm(n)
y.em1 = ridge_em1(y,X,1,1,100)
y.em2 = ridge_em2(y,X,1,1,100)
y.em3 = ridge_em3(y,X,1,1,1,100)
y.em4 = ridge_em4(y,X,1,1,100)
y.em5 = ridge_em5(y,X,1,1,100)
plot_loglik(list(y.em1,y.em2,y.em3,y.em4,y.em5))
```
## Trendfiltering Simulations
This is more challenging example (in that the design matrix is correlated)
### High Signal
```{r}
set.seed(100)
sd = 1
n = 100
p = n
X = matrix(0,nrow=n,ncol=n)
for(i in 1:n){
X[i:n,i] = 1:(n-i+1)
}
btrue = rep(0,n)
btrue[40] = 8
btrue[41] = -8
y = X %*% btrue + sd*rnorm(n)
plot(y)
lines(X %*% btrue)
y.em1 = ridge_em1(y,X,1,1,100)
lines(X %*% y.em1$postmean,col=1,lwd=2)
y.em2 = ridge_em2(y,X,1,1,100)
lines(X %*% y.em2$postmean,col=2,lwd=2)
y.em3 = ridge_em3(y,X,1,1,1,100)
lines(X %*% y.em3$postmean,col=3,lwd=2)
y.em4 = ridge_em4(y,X,1,1,100)
lines(X %*% y.em4$postmean,col=4,lwd=2)
y.em5 = ridge_em5(y,X,1,1,1,100)
lines(X %*% y.em4$postmean,col=5,lwd=2)
```
Look at the likelihoods:
```{r}
plot_loglik(list(y.em1,y.em2,y.em3,y.em4,y.em5))
```
Run the second one longer and check it:
```{r}
y.em2 = ridge_em2(y,X,1,1,1000)
plot_loglik(list(y.em1,y.em2,y.em3,y.em4))
y.em1$sb2
y.em2$sb2
y.em3$sb2 * y.em3$l2
y.em4$sb2 * y.em4$s2
y.em5$sb2 * y.em5$l2 * y.em5$s2
y.em1$s2
y.em2$s2
y.em3$s2
y.em4$s2
y.em5$s2
```
## Different initializations
Try starting $s$ in wrong place
```{r}
y.em1 = ridge_em1(y,X,10,1,100)
y.em2 = ridge_em2(y,X,10,1,100)
y.em3 = ridge_em3(y,X,10,1,1,100)
y.em4 = ridge_em4(y,X,10,1,100)
y.em5 = ridge_em5(y,X,10,1,1,100)
plot_loglik(list(y.em1,y.em2,y.em3,y.em4,y.em5))
```
Try starting $s2$ in wrong place
```{r}
y.em1 = ridge_em1(y,X,1,10,100)
y.em2 = ridge_em2(y,X,1,10,100)
y.em3 = ridge_em3(y,X,1,10,10,100)
y.em4 = ridge_em4(y,X,1,10,100)
y.em5 = ridge_em5(y,X,1,10,10,100)
plot_loglik(list(y.em1,y.em2,y.em3,y.em4,y.em5))
```
Try starting both in wrong place. Interestingly in this example em4 seems to converge
to a local optimum?
```{r}
y.em1 = ridge_em1(y,X,.1,10,100)
y.em2 = ridge_em2(y,X,.1,10,100)
y.em3 = ridge_em3(y,X,.1,10,10,100)
y.em4 = ridge_em4(y,X,.1,10,100)
y.em5 = ridge_em5(y,X,.1,10,10,100)
plot_loglik(list(y.em1,y.em2,y.em3,y.em4,y.em5))
y.em4$s2
y.em1$s2
```
### No signal case
Try no signal case -- the convergence issues are reversed!
```{r}
sd = 1
n = 100
p = n
X = matrix(0,nrow=n,ncol=n)
for(i in 1:n){
X[i:n,i] = 1:(n-i+1)
}
btrue = rep(0,n)
y = X %*% btrue + sd*rnorm(n)
plot(y)
lines(X %*% btrue)
y.em1 = ridge_em1(y,X,1,1,100)
lines(X %*% y.em1$postmean,col=1,lwd=2)
y.em2 = ridge_em2(y,X,1,1,100)
lines(X %*% y.em2$postmean,col=2,lwd=2)
y.em3 = ridge_em3(y,X,1,1,1,100)
lines(X %*% y.em3$postmean,col=3,lwd=2)
y.em4 = ridge_em4(y,X,1,1,100)
lines(X %*% y.em4$postmean,col=4,lwd=2)
y.em5 = ridge_em5(y,X,1,1,1,100)
lines(X %*% y.em5$postmean,col=5,lwd=2)
```
The EM2 and EM3 and EM5 converge faster here:
```{r}
plot_loglik(list(y.em1,y.em2,y.em3,y.em4,y.em5))
```
Try starting the expanded algorithm from very large lambda... it still seems to work.
```{r}
y.em3b = ridge_em3(y,X,1,1,100,100)
y.em5b = ridge_em5(y,X,1,1,100,100)
plot_loglik(list(y.em1,y.em2,y.em3b,y.em4,y.em5b))
```
### Possible next steps
It might be interesting to combine the expanded idea with algorithm em4.
It might also be interesting to add another redundant parameter multiplying the residual variance in the second redundant parameterization, so that some of the residual variance
is tied to the prior variance and some is not.