/
em_vs_mixsqp.Rmd
157 lines (128 loc) · 4.27 KB
/
em_vs_mixsqp.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
---
title: Comparing two different ways of estimating the mixture weights in mr-ash
author: Peter Carbonetto
date: February 20, 2020
site: workflowr::wflow_site
output: workflowr::wflow_html
---
Here we compare two different ways of implementing the mixture weight
updates in mr-ash: EM and mix-SQP. In this example, we will see that
the mix-SQP updates provide a much better fit to the data.
```{r knitr, echo=FALSE}
knitr::opts_chunk$set(comment = "#",results = "hold",collapse = TRUE,
fig.align = "center")
```
## Script parameters
These are the data simulation settings.
```{r datasim-params}
n <- 100
p <- 400
sd <- c(0, 1, 2)
w <- c(0.9, 0.05, 0.05)
s <- 0.1
```
This specifies the variances for the mixture-of-normals prior on the
regression coefficients.
```{r set-s0}
s0 <- 10^seq(-4,0,length.out = 12)
```
## Load functions
These are the packages used in this analysis.
```{r load-pkgs, message=FALSE, warning=FALSE}
library(ggplot2)
library(cowplot)
library(MASS)
library(mixsqp)
```
This R code provides a simple implementation of the mr-ash algorithm.
```{r load-functions}
source("../code/misc.R")
source("../code/mr_ash.R")
source("../code/mr_ash_with_mixsqp.R")
```
## Simulate data
The predictors are drawn from the multivariate normal with zero mean
and covariance matrix S, in which all diagonal entries are 1, and
all off-diagonal entries are s. Setting `s = 0.5` reproduces the
simulation of the predictors used in Example 3 of Zou & Hastie
(2005).
```{r simulate-data}
set.seed(2)
S <- matrix(s,p,p)
diag(S) <- 1
X <- mvrnorm(n,rep(0,p),S)
k <- sample(length(w),p,replace = TRUE,prob = w)
beta <- sd[k] * rnorm(p)
y <- drop(X %*% beta + rnorm(n))
```
## Fit model
These are the initial estimates of residual variance (`s`), mixture
weights (`w0`), and posterior mean estimates of the regression
coefficients (b).
```{r init-model}
k <- length(s0)
se <- 1
w0 <- rep(1/k,k)
b <- rep(0,p)
```
Fit the model by running 200 EM updates for the mixture weights.
```{r fit-model-em}
fit1 <- mr_ash(X,y,se,s0,w0,b,maxiter = 200,verbose = FALSE)
```
Fit the model a second time using the mix-SQP updates for the mixture
weights. The "EM", "mix" and "alpha" columns give, for each iteration,
the number of co-ordinate ascent ("inner loop") updates run, the
number of mix-SQP iterations performed, and the step size for the
mix-SQP update (as determined by backtracking line search).
```{r fit-model-mixsqp}
fit2 <- mr_ash_with_mixsqp(X,y,se,s0,w0,b,numiter = 10)
```
## Review model fit
Plot the improvement in the solution over time.
```{r plot-progress, fig.height=4, fig.width=5}
elbo.best <- max(c(fit1$elbo,fit2$elbo))
pdat <- rbind(data.frame(update = "em",
iter = 1:length(fit1$elbo),
elbo = fit1$elbo),
data.frame(update = "mixsqp",
iter = cumsum(fit2$numem),
elbo = fit2$elbo))
pdat$elbo <- elbo.best - pdat$elbo + 1e-4
ggplot(pdat,aes(x = iter,y = elbo,color = update)) +
geom_line() +
geom_point() +
scale_y_log10() +
scale_color_manual(values = c("royalblue","darkorange")) +
labs(y = "distance to \"best\" elbo") +
theme_cowplot()
```
The algorithm with the mix-SQP updates of the mixture weights provides
a much better fit to the data.
Compare the posterior mean estimates by the against the values
used to simulate the data.
```{r plot-coefs-1, fig.height=3, fig.width=6}
p1 <- ggplot(data.frame(true = beta,em = fit1$b),
aes(x = true,y = em)) +
geom_point(color = "darkblue") +
geom_abline(intercept = 0,slope = 1,col = "magenta",lty = "dotted") +
xlim(-4,4) +
ylim(-4,4) +
theme_cowplot()
p2 <- ggplot(data.frame(true = beta,mixsqp = fit2$b),
aes(x = true,y = mixsqp)) +
geom_point(color = "darkblue") +
geom_abline(intercept = 0,slope = 1,col = "magenta",lty = "dotted") +
xlim(-4,4) +
ylim(-4,4) +
theme_cowplot()
plot_grid(p1,p2)
```
```{r plot-coefs-2, fig.height=3, fig.width=6}
ggplot(data.frame(em = fit1$b,mixsqp = fit2$b),
aes(x = em,y = mixsqp)) +
geom_point(color = "darkblue") +
geom_abline(intercept = 0,slope = 1,col = "magenta",lty = "dotted") +
xlim(-2.25,0.75) +
ylim(-2.25,0.75) +
theme_cowplot()
```