-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBigARPS.R
More file actions
138 lines (74 loc) · 2.99 KB
/
BigARPS.R
File metadata and controls
138 lines (74 loc) · 2.99 KB
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
BigARPS = function(Y, M=NULL, nrho=NULL, rho.min.ratio = NULL, g=NULL, verbose=T){
# Accept-Rejest Regularization Selection
# Y = n x p data matrix
# M = nmb of A-R samples (default 1000)
# nrho = nmb of tuning parameters (default 10)
# rho.min.ratio = The smallest value for rho, as a fraction of the uppperbound (MAX) of the
# regularization/thresholding parameter which makes all estimates equal to 0. The default value is 0.1
# See the vigenete of the huge R package
# g = candidate density (default unif(min(rho), max(rho)))
# verbose = Tracking information. Default TRUE
library(glasso)
n = nrow(Y)
p = ncol(Y)
Y = scale(Y)
S = cor(Y)
if(is.null(rho.min.ratio)) rho.min.ratio = 0.1
if(is.null(M)) M = 1000
if(!is.null(g)){
g = match.fun(g)
gind = T
}
if(is.null(g)){
g = function(rho) 1/(max(rho) - min(rho))
gind = F
}
if(is.null(nrho)) nrho = 10
rhomax = max(max(S - diag(p)), -min(S - diag(p)))
rhomin = rho.min.ratio*rhomax
rho = exp(seq(log(rhomax), log(rhomin), length = nrho))
# First compute the solution path with glasso using warm start
Target = rep(0, nrho)
est = glasso(S, rho=rho[1])
Target[1] = p*(p + 1)*log(n*rho[1]/2) - n*rho[1]*sum(abs(est$wi))
dir.create("BigARPSSolPath", showWarnings = FALSE)
save(est, file="BigARPSSolPath/est.RDS")
for(i in 2:nrho){
est = glasso(S, rho=rho[i], w.init=est$w, wi.init=est$wi, start="warm")
Target[i] = p*(p + 1)*log(n*rho[i]/2) - n*rho[i]*sum(abs(est$wi))
# logarithm of the conditional posterior of the tuning parameter
saveRDS(est, file=paste("BigARPSSolPath/est", i, ".RDS", sep=""))
if (verbose) {
cat("Running glasso", round(100*i/nrho, 2), "%", "\r")
}
}
# Accept-reject algorithm:
pg = 1/(max(rho) - min(rho))
Target = Target + 2*log(pg)
Max = max(Target)*g(rho)
Propose = sample(1:nrho, M, replace=T)
if(gind == T){
Mg = max(g(rho))
s = rho[Propose]
s = Propose[runif(length(s)) <= g(s)/Mg]
IndProp = sample(1:nrho, 1, replace = T)
while(length(s) < M){
if(runif(1) <= g(rho[IndProp])/Mg) s = c(s, IndProp)
IndProp = sample(1:nrho, 1, replace = T)
}
Propose = s
}
U = runif(M, 0, Max)
# Hox! U is not the true logartihm transform we need log(exp(Max)*u) but it has the same maximum value and the most extreme
# upper quantiles are probably close to each other...
Target = Target[Propose]
indx = Propose[U <= Target] # No log-likelihood at all!
Sampledrhos = rho[indx]
Results = list()
Results$indx = indx
Results$Sampledrhos = Sampledrhos
Results$rhos = rho
Results$accept.rate = length(indx)/M
Results$n = n
return(Results)
}