-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowrank.R
161 lines (140 loc) · 3.36 KB
/
lowrank.R
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
lowrank = function(T, p, k,
max_iter = 1000,
verbose = TRUE,
SVD = TRUE,
signal_mag = 3,
rand_init = FALSE){
# data generation
Theta = matrix(rnorm(T*k),T,k)
A = matrix(rnorm(k*p),p,k)
m = rnorm(p,signal_mag)
M = outer(rep(1,T),m)
Y = Theta %*% t(A) +
M +
matrix(rnorm(T*p), T, p)
# initialize
if(rand_init){
Theta_hat = matrix(0,T,k)
A_hat = matrix(0,p,k)
m_hat = rep(0,p)
} else {
Theta_hat = Theta
A_hat = A
m_hat = m
}
# loop
for(iter in 1:max_iter){
# adjusted mean
adj_Y = Y - Theta_hat %*% t(A_hat)
# CUSUM
m_new = apply(adj_Y,2,mean)
# adjusted mean
adj_Y = Y - outer(rep(1,T), m_new)
if(SVD){
# SVD
svd_hat = svd(adj_Y, nu=k, nv=k)
Theta_new = svd_hat$d[1:k] * t(svd_hat$u)
Theta_new = t(Theta_new)
A_new = svd_hat$v
} else {
# lm for theta, A
mlr = lm(t(adj_Y)~A_hat+0)
Theta_new = t(mlr$coefficients)
mlr = lm(adj_Y~Theta_new+0)
A_new = t(mlr$coefficients)
}
# check
mse = function(x) mean(x^2, na.rm=TRUE)
dt = mse(m_new - m_hat) + mse(Theta_new%*%t(A_new) - Theta_hat%*%t(A_hat))
if (dt < 1e-6){
if (verbose) cat("converged at iteration",iter,"\n")
break
} else {
Theta_hat = Theta_new
A_hat = A_new
m_hat = m_new
}
}
if(iter==max_iter){
if (verbose) cat("reached reached max iteration",max_iter,"\n")
}
out = list(
true = list(
Theta = Theta,
A = A,
factor = Theta %*% t(A),
M = outer(rep(1,T),m),
m = m,
Y = Theta %*% t(A) + outer(rep(1,T),m)
),
estimate = list(
Theta = Theta_new,
A = A_new,
factor = Theta_new %*% t(A_new),
M = outer(rep(1,T),m_new),
m = m_new,
Y = Theta_new %*% t(A_new) + outer(rep(1,T),m_new)
),
dim = list(
T = T,
p = p,
k = k
)
)
return(out)
}
lowrank0 = function(T, p, k){
# data generation
Theta = matrix(rnorm(T*k),T,k)
A = matrix(rnorm(k*p),p,k)
Y = Theta %*% t(A) + matrix(rnorm(T*p), T, p)
svd_hat = svd(Y, nu=k, nv=k)
Theta_new = svd_hat$d[1:k] * t(svd_hat$u)
Theta_new = t(Theta_new)
A_new = svd_hat$v
out = list(
true = list(
Theta = Theta,
A = A,
Y = Theta %*% t(A)
),
estimate = list(
Theta = Theta_new,
A = A_new,
Y = Theta_new %*% t(A_new)
),
dim = list(
T = T,
p = p,
k = k
)
)
return(out)
}
mse_factor = array(NA,dim = c(5,5,4,10))
mse_M = array(NA,dim = c(5,5,4,10))
mse_Y = array(NA,dim = c(5,5,4,10))
mse_Y2 = array(NA,dim = c(5,5,4,10))
prior = array(NA,dim = c(5,5,4,10))
distr=TRUE
param=TRUE
rand=TRUE
mag=3
mse = function(x) mean(x^2, na.rm=TRUE)
K = c(2,5,10,50)
for(rep in 1:10){
for(k in 1:3){
for(T in 1:3){
for(p in 1:3){
out = lowrank(T=T*200,p=p*200,k=K[k], rand_init=TRUE)
mse_factor[T,p,k,rep] = mse(out$true$factor - out$est$factor)
mse_M[T,p,k,rep] = mse(out$true$M - out$est$M)
mse_Y[T,p,k,rep] = mse(out$true$Y - out$est$Y)
out = lowrank0(T=T*200,p=p*200,k=K[k])
mse_Y2[T,p,k,rep] = mse(out$true$Y - out$est$Y)
save(mse_factor, mse_M, mse_Y, mse_Y2,
file = "baseline.rda")
}
}
}
}