-
Notifications
You must be signed in to change notification settings - Fork 1
/
BISN_missing.cpp
352 lines (294 loc) · 10.8 KB
/
BISN_missing.cpp
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
#include <mex.h>
#include "armaMex.hpp"
#include <armadillo>
#include <boost/math/special_functions/expint.hpp>
#include <boost/math/special_functions/trigamma.hpp>
#define POSITIVE_EPS 0.0001
#define epsilon1 1e-30
#define epsilon2 1e-7
double Lentz_Algorithm(double const x)
{
double f_prev = epsilon1, C_prev = epsilon1, D_prev = 0, delta = 2+epsilon2, D_curr, C_curr, f_curr;
double j = 1.0, tmp1, tmp2;
while (delta-1>=epsilon2 || 1-delta >= epsilon2)
{
j++;
tmp1 = x+2*j-1;
tmp2 = pow(j-1,2);
D_curr = 1/(tmp1-tmp2*D_prev);
C_curr = tmp1-tmp2/C_prev;
delta = C_curr*D_curr;
f_curr = f_prev*delta;
f_prev = f_curr;
C_prev = C_curr;
D_prev = D_curr;
}
return 1/(x+1+f_curr);
}
void mexFunction(int nlhs,mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
/* Input Interface*/
mat XDat = armaGetPr(prhs[0]);
uvec row_missing = conv_to<uvec>::from(armaGetPr(prhs[1])) - 1;
umat id_missing = conv_to<umat>::from(armaGetPr(prhs[2])) - 1;
uword maxIter, s, p = XDat.n_cols,n = XDat.n_rows;
double tol, eta, r;
if (nrhs>3)
{
eta = armaGetDouble(prhs[3]);
eta = eta/(p+eta);
}
else eta = 300.0/(p+300.0);//0.05; //4*sqrt(double(p)/(p^2-1));
if (nrhs>4) maxIter = (uword)armaGetDouble(prhs[4]);
else maxIter = 10000;
if (nrhs>5) tol = armaGetDouble(prhs[5]);
else tol = 0.01;
if (nrhs>6) r = armaGetDouble(prhs[6]);
else
{
r = 0.5; //(1-400.0/double(p))/(1+400.0/double(p));
//if(r<0.05) r = 0.05;
}
if (nrhs>7) s = (uword)armaGetDouble(prhs[7]);
else s = round((double)p/(1e-3*(p-1)+1)); //(pow((double)p,1.5)/(5e-2*(p-1)+sqrt(double(p))));
uword n_missing = row_missing.n_elem, i;
field<uvec> col_missing(n_missing);
uvec id_tmp(1, fill::ones);
for (i = 0; i < n_missing; i ++) {
col_missing(i) = id_missing(find(id_missing.col(0) == row_missing(i)),id_tmp);
//col(2).elem(find(id_missing.col(1) == row_missing(i)));
}
/* Initialization */
auto t_start = std::chrono::high_resolution_clock::now();
mat nS(p, p);
vec dnS;
uword pe = p*(p-1)/2;
uvec idl(pe);
uvec idu(pe);
uvec idr(pe);
uvec idc(pe);
uword k = 0, j, jp, kappa;
for (j = 0,jp = 0;j<p;j++,jp+=p)
{
for (i=j+1;i<p;i++)
{
idl(k) = jp+i;
idu(k) = i*p+j;
idr(k) = i;
idc(k) = j;
k++;
}
}
double psr = (double)p/s;
uvec id0, idd;
vec nd2pp = (double)n/2+linspace<vec>(p,1,p);
mat ML(p,p,fill::eye);
mat ML2(p,p,fill::eye);
mat ML2pVL(p,p,fill::eye);
mat VL(p,p,fill::zeros);
mat LAMBDA(p,p,fill::zeros);
vec h(pe,fill::zeros),zeta(pe),mL(pe,fill::zeros);
zeta.fill(10.0);
vec alpha(p,fill::ones);
vec beta(p,fill::ones);
double a = (double)pe/2, b = a/50;
vec mLold = h/zeta;
vec lambdaold(pe,fill::ones);
mat VL2(p,p,fill::zeros);
for (i=1;i<p;i++)
{
VL2(span(i,p-1),i) += i;
VL2(i,span(i,p-1)) += i;
}
VL2 *= 0.01;
vec d(pe,1);
d.fill(0.5);
vec mD, mD2, mD2pvD, vD, vL, lambda(pe), gmL, gvL, gmD, gvD, c5(p), c6, mLnew, lambdanew, alphatmp, betatmp, dtmp;//mL,
mat c1, c2, c3, c4, LDL, c2pc3, S_cond, K_tmp1, K_tmp2, LD, nS0 = XDat.t()*XDat;
double omega, difmL, diflambda, difmax;
vec d1h(pe,fill::zeros), d1zeta(pe,fill::zeros), d1alpha(p,fill::zeros), d1beta(p,fill::zeros), d1d(pe,fill::zeros);
double d2h = 0, d2zeta = 0, d2alpha = 0, d2beta = 0, d2d = 0, d1b = 0, d2b = 0;
double tau = 6e2; //, tauzeta = tauh, taualpha = tauh, taubeta = tauh, taub = tauh, taud = tauh;
double rho, gb, btmp;//1/eta;rho_ub, c7=(5*eta>0.25)?0.25:5*eta,
vec gh, gzeta, galpha, gbeta, gd;
arma_rng::set_seed(0);
/* KL proximal variational inference */
mexPrintf("Start Running BISN ...\n");
mexEvalString("drawnow;");
for (kappa=1;kappa<=maxIter;kappa++)
{
mD = alpha/beta;
mD2 = square(mD);
vD = mD/beta;
mD2pvD = mD2+vD;
vL = 1/zeta;
VL.elem(idl) = vL;
mL = h%vL;
ML.elem(idl) = mL;
ML2.elem(idl) = square(mL);
ML2pVL.elem(idl) = ML2.elem(idl)+vL;
omega = a/b;
lambda = d;
lambda.transform([](double val){return (val > 10) ? Lentz_Algorithm(val) : (- boost::math::expint(-val) * exp(val));});
lambda = 1/(d%lambda)-1;
LAMBDA.elem(idl) = omega*lambda;
LAMBDA.elem(idu) = LAMBDA.elem(idl);
LD = ML;
LD.each_row() %= mD.t();
nS.zeros();
for (i = 0; i < n_missing; i ++) {
id_tmp.fill(row_missing(i));
S_cond = LD.rows(col_missing(i)) * ML.rows(col_missing(i)).t();
S_cond = (S_cond + S_cond.t()) / 2;
S_cond = inv_sympd(S_cond);
XDat(id_tmp, col_missing(i)).zeros();
XDat(id_tmp, col_missing(i)) = - XDat.row(row_missing(i)) * LD
* ML.rows(col_missing(i)).t() * S_cond;
nS(col_missing(i), col_missing(i)) += S_cond +
XDat(id_tmp, col_missing(i)).t() * XDat(id_tmp, col_missing(i));
// S_cond.clear();
}
nS += nS0;
dnS = nS.diag();
if (kappa==1)
{
c1 = nS;
c2 = -VL;
c2.each_row() += sum(VL);
c2 *= LAMBDA(1);
//c2 = LAMBDA(1)*(repmat(sum(VL),p,1)-VL);
c3 = LAMBDA;
c4 = (VL+VL2)*mD2pvD(0);
VL2.clear();
}
else
{
LDL = LD.rows(id0)*ML.t();
c1 *= r;
c1.rows(id0) += psr*(nS.rows(id0)+LDL%LAMBDA.rows(id0))*ML;
c2 *= r;
c2.rows(id0) += psr*LAMBDA.rows(id0)*VL;
c3 *= r;
c3.rows(id0) += psr*LAMBDA.rows(id0)*ML2;
K_tmp1 = ML2pVL.rows(id0);
K_tmp1.each_row() %= mD2pvD.t();
K_tmp2 = ML2.rows(id0);
K_tmp2.each_row() %= mD2.t();
c4 *=r;
c4.rows(id0) += psr*(K_tmp1*ML2pVL.t()-K_tmp2*ML2.t()+square(LDL));
}
c2pc3 = c2+c3;
gmL = -c1.elem(idl)%mD.elem(idc)-ML.elem(idl)%(mD2pvD.elem(idc)%c2.elem(idl)+vD.elem(idc)%c3.elem(idl));
gvL = dnS.elem(idr)%mD.elem(idc)+c2pc3.elem(idl)%mD2pvD.elem(idc);
gh = gmL+mL%gvL - h;
gvL.elem(find(gvL<0)).zeros();
gzeta = gvL - zeta;
d1h = (1-1/tau)*d1h+gh/tau;
d2h = (1-1/tau)*d2h+mean(square(gh))/tau;
d1zeta = (1-1/tau)*d1zeta + gzeta/tau;
d2zeta = (1-1/tau)*d2zeta + mean(square(gzeta))/tau;
gmD = (VL.t()*dnS+trans(sum(ML%c1))+trans(sum(VL%(c2pc3+c3)))%mD)/2;
gvD = trans(sum(ML2pVL%c2pc3))/4;
c5 = alpha;
c5.transform([](double val) { return boost::math::trigamma(val); });
c6 = mD/(alpha%c5-1);
alphatmp = nd2pp+c6/beta%gvD;
alphatmp.elem(find(alphatmp<0)).zeros();
betatmp = gmD+(1/beta+c5%c6)%gvD;
betatmp.elem(find(betatmp<0)).zeros();
galpha = alphatmp - alpha;
gbeta = betatmp - beta;
d1alpha = (1-1/tau)*d1alpha + galpha/tau;
d2alpha = (1-1/tau)*d2alpha + mean(square(galpha))/tau;
d1beta = (1-1/tau)*d1beta + gbeta/tau;
d2beta = (1-1/tau)*d2beta + mean(square(gbeta))/tau;
dtmp = omega/2*c4.elem(idl);
dtmp.elem(find(dtmp<0)).zeros();
gd = dtmp - d;
d1d = (1-1/tau)*d1d + gd/tau;
d2d = (1-1/tau)*d2d + mean(square(gd))/tau;
btmp = sum(lambda%c4.elem(idl))/2;
if (btmp<0) btmp = 0;
gb = btmp - b;
d1b = (1-1/tau)*d1b + gb/tau;
d2b = (1-1/tau)*d2b + pow(gb,2)/tau;
rho = (mean(square(d1h))+mean(square(d1zeta))+p/pe*(mean(square(d1alpha))+mean(square(d1beta)))+mean(square(d1d))+pow(d1b,2)/pe)/(d2h+d2zeta+p/pe*(d2alpha+d2beta)+d2d+d2b/pe);
if (rho>eta) rho = eta;
tau = (1-rho)*tau + 1;
h += rho*gh;
zeta += rho*gzeta;
alpha += rho*galpha;
beta += rho*gbeta;
d += rho*gd;
b += rho*gb;
if (kappa%100 == 0)
{
mLnew = h/zeta;
lambdanew = lambda;
difmL = sqrt(mean(square(mLnew-mLold))/mean(square(mLold)));
diflambda = max(abs(lambdanew-lambdaold));
difmax = max(abs(mLnew-mLold));
mexPrintf("#no. of iterations = %d, difmL = %f, difmax = %f, diflambda = %f\n",kappa,difmL,difmax,diflambda);
mexEvalString("drawnow;");
if (difmL<tol)
break;
else
{
mLold = mLnew;
lambdaold = lambdanew;
}
}
id0 = randperm(p, s);
LDL = LD.rows(id0)*ML.t();
c1.rows(id0) -= psr*(nS.rows(id0)+LDL%LAMBDA.rows(id0))*ML;
c2.rows(id0) -= psr*LAMBDA.rows(id0)*VL;
c3.rows(id0) -= psr*LAMBDA.rows(id0)*ML2;
K_tmp1 = ML2pVL.rows(id0);
K_tmp1.each_row() %= mD2pvD.t();
K_tmp2 = ML2.rows(id0);
K_tmp2.each_row() %= mD2.t();
c4.rows(id0) -= psr*(K_tmp1*ML2pVL.t()-K_tmp2*ML2.t()+square(LDL));
}
ML.elem(idl) = h/zeta;
VL.elem(idl) = 1/zeta;
mD = alpha/beta;
vD = mD/beta;
auto t_end = std::chrono::high_resolution_clock::now();
double ElapsedTime = std::chrono::duration<double, std::milli>(t_end-t_start).count() / 1e3;
if (kappa < maxIter && difmL<tol) mexPrintf("BISN converges, elapsed time is %e seconds.\n",ElapsedTime);
else mexPrintf("BISN reaches the maximum number of iterations, elapsed time is %e seconds.\n",ElapsedTime);
/* Output Interface */
if (nlhs>3)
{
plhs[0] = armaCreateMxMatrix(p,p,mxDOUBLE_CLASS,mxREAL);
armaSetPr(plhs[0],ML);
plhs[1] = armaCreateMxMatrix(p,p,mxDOUBLE_CLASS,mxREAL);
armaSetPr(plhs[1],VL);
plhs[2] = armaCreateMxMatrix(p,1,mxDOUBLE_CLASS,mxREAL);
armaSetPr(plhs[2],mD);
plhs[3] = armaCreateMxMatrix(p,1,mxDOUBLE_CLASS,mxREAL);
armaSetPr(plhs[3],vD);
if (nlhs>4)
{
omega = a/b;
plhs[4] = mxCreateDoubleScalar(omega);
}
if (nlhs>5)
{
lambda = d;
lambda.transform([](double val){return (val > 10) ? Lentz_Algorithm(val) : (- boost::math::expint(-val) * exp(val));});
lambda = 1/(d%lambda)-1;
plhs[5] = armaCreateMxMatrix(pe,1,mxDOUBLE_CLASS,mxREAL);
armaSetPr(plhs[5],lambda);
}
if (nlhs>6) {
plhs[6] = armaCreateMxMatrix(n,p,mxDOUBLE_CLASS,mxREAL);
armaSetPr(plhs[6],XDat);
}
if (nlhs>7) plhs[7] = mxCreateDoubleScalar(ElapsedTime);
}
else
{
mexErrMsgIdAndTxt("BINS:output","Expected at least four output arguments");
}
}