forked from bstewart/stm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTMCfuns.cpp
439 lines (383 loc) · 16.5 KB
/
STMCfuns.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
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
433
434
435
436
437
438
439
// [[Rcpp::depends(RcppArmadillo)]]
#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
double multilhoodcpp(SEXP eta,
SEXP beta,
SEXP doc_ct,
SEXP mu,
SEXP pi,
SEXP siginv){
Rcpp::NumericVector etav(eta);
arma::vec etas(etav.begin(), etav.size(), false);
Rcpp::NumericMatrix betam(beta);
arma::mat betas(betam.begin(), betam.nrow(), betam.ncol(), false);
Rcpp::NumericVector doc_ctv(doc_ct);
arma::vec doc_cts(doc_ctv.begin(), doc_ctv.size(), false);
Rcpp::NumericVector muv(mu);
arma::vec mus(muv.begin(), muv.size(), false);
Rcpp::NumericVector piv(pi);
arma::vec pis(piv.begin(), piv.size(), false);
Rcpp::NumericMatrix siginvm(siginv);
arma::mat siginvs(siginvm.begin(), siginvm.nrow(), siginvm.ncol(), false);
arma::rowvec expeta(etas.size()+1);
expeta.fill(1);
int neta = etav.size();
for(int j=0; j <neta; j++){
expeta(j) = exp(etas(j));
}
double ndoc = sum(doc_cts);
double part1 = arma::as_scalar(log(expeta*betas)*doc_cts - ndoc*log(sum(expeta)));
arma::vec diff = etas - mus - pis;
double part2 = .5*arma::as_scalar(diff.t()*siginvs*diff);
double out = part2 - part1;
return out;
}
// [[Rcpp::export]]
arma::vec multigradcpp(SEXP eta,
SEXP beta,
SEXP doc_ct,
SEXP mu,
SEXP pi,
SEXP siginv){
Rcpp::NumericVector etav(eta);
arma::vec etas(etav.begin(), etav.size(), false);
Rcpp::NumericMatrix betam(beta);
arma::mat betas(betam.begin(), betam.nrow(), betam.ncol());
Rcpp::NumericVector doc_ctv(doc_ct);
arma::vec doc_cts(doc_ctv.begin(), doc_ctv.size(), false);
Rcpp::NumericVector muv(mu);
arma::vec mus(muv.begin(), muv.size(), false);
Rcpp::NumericVector piv(pi);
arma::vec pis(piv.begin(), piv.size(), false);
Rcpp::NumericMatrix siginvm(siginv);
arma::mat siginvs(siginvm.begin(), siginvm.nrow(), siginvm.ncol(), false);
arma::colvec expeta(etas.size()+1);
expeta.fill(1);
int neta = etas.size();
for(int j=0; j <neta; j++){
expeta(j) = exp(etas(j));
}
betas.each_col() %= expeta;
arma::vec part1 = betas*(doc_cts/arma::trans(sum(betas,0))) - (sum(doc_cts)/sum(expeta))*expeta;
arma::vec part2 = siginvs*(etas - mus - pis);
part1.shed_row(neta);
return part2-part1;
}
// [[Rcpp::export]]
double lhoodcpp(SEXP eta,
SEXP beta,
SEXP doc_ct,
SEXP mu,
SEXP siginv){
Rcpp::NumericVector etav(eta);
arma::vec etas(etav.begin(), etav.size(), false);
Rcpp::NumericMatrix betam(beta);
arma::mat betas(betam.begin(), betam.nrow(), betam.ncol(), false);
Rcpp::NumericVector doc_ctv(doc_ct);
arma::vec doc_cts(doc_ctv.begin(), doc_ctv.size(), false);
Rcpp::NumericVector muv(mu);
arma::vec mus(muv.begin(), muv.size(), false);
Rcpp::NumericMatrix siginvm(siginv);
arma::mat siginvs(siginvm.begin(), siginvm.nrow(), siginvm.ncol(), false);
arma::rowvec expeta(etas.size()+1);
expeta.fill(1);
int neta = etav.size();
for(int j=0; j <neta; j++){
expeta(j) = exp(etas(j));
}
double ndoc = sum(doc_cts);
double part1 = arma::as_scalar(log(expeta*betas)*doc_cts - ndoc*log(sum(expeta)));
arma::vec diff = etas - mus;
double part2 = .5*arma::as_scalar(diff.t()*siginvs*diff);
double out = part2 - part1;
return out;
}
// [[Rcpp::export]]
arma::vec gradcpp(SEXP eta,
SEXP beta,
SEXP doc_ct,
SEXP mu,
SEXP siginv){
Rcpp::NumericVector etav(eta);
arma::vec etas(etav.begin(), etav.size(), false);
Rcpp::NumericMatrix betam(beta);
arma::mat betas(betam.begin(), betam.nrow(), betam.ncol());
Rcpp::NumericVector doc_ctv(doc_ct);
arma::vec doc_cts(doc_ctv.begin(), doc_ctv.size(), false);
Rcpp::NumericVector muv(mu);
arma::vec mus(muv.begin(), muv.size(), false);
Rcpp::NumericMatrix siginvm(siginv);
arma::mat siginvs(siginvm.begin(), siginvm.nrow(), siginvm.ncol(), false);
arma::colvec expeta(etas.size()+1);
expeta.fill(1);
int neta = etas.size();
for(int j=0; j <neta; j++){
expeta(j) = exp(etas(j));
}
betas.each_col() %= expeta;
arma::vec part1 = betas*(doc_cts/arma::trans(sum(betas,0))) - (sum(doc_cts)/sum(expeta))*expeta;
arma::vec part2 = siginvs*(etas - mus);
part1.shed_row(neta);
return part2-part1;
}
// [[Rcpp::export]]
SEXP multihpbcpp(SEXP eta,
SEXP beta,
SEXP doc_ct,
SEXP mu,
SEXP pi,
SEXP siginv,
SEXP sigmaentropy,
SEXP sigs,
SEXP sigsentropy,
SEXP omegaentropy,
SEXP sigs_inv,
SEXP omega){
Rcpp::NumericVector etav(eta);
arma::vec etas(etav.begin(), etav.size(), false);
Rcpp::NumericMatrix betam(beta);
arma::mat betas(betam.begin(), betam.nrow(), betam.ncol());
Rcpp::NumericVector doc_ctv(doc_ct);
arma::vec doc_cts(doc_ctv.begin(), doc_ctv.size(), false);
Rcpp::NumericVector muv(mu);
arma::vec mus(muv.begin(), muv.size(), false);
Rcpp::NumericVector piv(pi);
arma::vec pis(piv.begin(), piv.size(), false);
Rcpp::NumericMatrix siginvm(siginv);
arma::mat siginvs(siginvm.begin(), siginvm.nrow(), siginvm.ncol(), false);
Rcpp::NumericVector sigmaentropym(sigmaentropy);
arma::vec entropy(sigmaentropym);
Rcpp::NumericMatrix sigsm(sigs);
arma::mat sigIs(sigsm.begin(), sigsm.nrow(), sigsm.ncol(), false);
Rcpp::NumericVector sigsentropym(sigsentropy);
arma::vec sigs_entropy(sigsentropym);
Rcpp::NumericVector omegaentropym(omegaentropy);
arma::vec omega_entropy(omegaentropym);
Rcpp::NumericMatrix sigsinvm(sigs_inv);
arma::mat sigs_invs(sigsinvm.begin(), sigsinvm.nrow(), sigsinvm.ncol(), false);
Rcpp::NumericMatrix omegam(omega);
arma::mat omegas(omegam.begin(), omegam.nrow(), omegam.ncol(), false);
arma::colvec expeta(etas.size()+1);
expeta.fill(1);
int neta = etas.size();
for(int j=0; j <neta; j++){
expeta(j) = exp(etas(j));
}
arma::vec theta = expeta/sum(expeta);
//create a new version of the matrix so we can mess with it
arma::mat EB(betam.begin(), betam.nrow(), betam.ncol());
//multiply each column by expeta
EB.each_col() %= expeta; //this should be fastest as its column-major ordering
//divide out by the column sums
EB.each_row() %= arma::trans(sqrt(doc_cts))/sum(EB,0);
//Combine the pieces of the Hessian which are matrices
arma::mat hess = EB*EB.t() - sum(doc_cts)*(theta*theta.t());
//we don't need EB any more so we turn it into phi
EB.each_row() %= arma::trans(sqrt(doc_cts));
//Now alter just the diagonal of the Hessian
hess.diag() -= sum(EB,1) - sum(doc_cts)*theta;
//Drop the last row and column
hess.shed_row(neta);
hess.shed_col(neta);
//Now we can add in siginv
hess = hess + siginvs;
//At this point the Hessian is complete.
//This next bit of code is from http://arma.sourceforge.net/docs.html#logging
//It basically keeps arma from printing errors from chol to the console.
std::ostream nullstream(0);
//arma::set_stream_err2(nullstream);
//arma::arma_cerr_stream<char>(&nullstream);
// Invert via cholesky decomposition
//Start by initializing an object
arma::mat nu = arma::mat(hess.n_rows, hess.n_rows);
//This version of chol generates a boolean which tells us if it failed.
// bool worked = arma::chol(nu,hess);
// if(!worked) {
// //It failed! Oh Nos.
// // So the matrix wasn't positive definite. In practice this means that it hasn't
// // converged probably along some minor aspect of the dimension.
//
// //Here we make it positive definite through diagonal dominance
// arma::vec dvec = hess.diag();
// //find the magnitude of the diagonal
// arma::vec magnitudes = sum(abs(hess), 1) - abs(dvec);
// //iterate over each row and set the minimum value of the diagonal to be the magnitude of the other terms
// int Km1 = dvec.size();
// for(int j=0; j < Km1; j++){
// if(arma::as_scalar(dvec(j)) < arma::as_scalar(magnitudes(j))) dvec(j) = magnitudes(j) + 0.01; //enforce restrict diagonal dominance
// }
// //overwrite the diagonal of the hessian with our new object
// hess.diag() = dvec;
// //that was sufficient to ensure positive definiteness so we now do cholesky
// nu = arma::chol(hess);
// }
bool worked = arma::chol(nu, hess);
if(!worked) {
// First attempt to make the matrix positive definite by enforcing diagonal dominance without adding 0.01
arma::vec dvec = hess.diag();
arma::vec magnitudes = sum(abs(hess), 1) - abs(dvec);
int Km1 = dvec.size();
for(int j=0; j < Km1; j++){
if(arma::as_scalar(dvec(j)) < arma::as_scalar(magnitudes(j))) dvec(j) = magnitudes(j);
}
hess.diag() = dvec;
// Try Cholesky decomposition again
worked = arma::chol(nu, hess);
// If it still fails, add 0.01 to the diagonal elements
if(!worked) {
for(int j=0; j < Km1; j++){
dvec(j) = magnitudes(j) + 0.01;
}
hess.diag() = dvec;
// Try Cholesky decomposition again
worked = arma::chol(nu, hess);
}
// If it still fails, apply a more significant adjustment
if(!worked) {
double increment = 0.05; // Start with a small increment
bool success = false;
while(!success && increment < 1.0) {
for(int j=0; j < Km1; j++){
dvec(j) = magnitudes(j) + increment;
}
hess.diag() = dvec;
// Try Cholesky decomposition again
worked = arma::chol(nu, hess);
if(worked) {
success = true;
} else {
increment *= 2; // Double the increment if it fails
}
}
if(!success) {
return Rcpp::List::create(
Rcpp::Named("phis") = EB,
Rcpp::Named("eta") = Rcpp::List::create(Rcpp::Named("lambda") = etas, Rcpp::Named("nu") = NA_REAL),
Rcpp::Named("bound") = NA_REAL
);
}
}
}
//compute 1/2 the determinant from the cholesky decomposition
double detTerm_nu = -sum(log(nu.diag()));
//Now finish constructing nu
nu = arma::inv(arma::trimatu(nu));
nu = nu * nu.t(); //trimatu doesn't do anything for multiplication so it would just be timesink to signal here.
//Precompute the difference since we use it twice
arma::vec diff = etas - mus - pis;
double tr_sigtv = arma::trace(siginvs*nu);
//Now generate the bound and make it a scalar
// Calculate -phi * log(phi) for matrix EB
//arma::mat phi_term = -(EB + 0.01) % arma::log(EB + 0.01); // Element-wise multiplication and logarithm
//double phi_sum = arma::accu(phi_term); // Sum all elements
//define parameters wrt to psi
double tr_sigtw = arma::trace(siginvs*omegas);
double tr_sigsw = arma::trace(sigs_invs*omegas);
// double bound = arma::as_scalar(log(arma::trans(theta)*betas)*doc_cts + detTerm_nu
// - .5*diff.t()*siginvs*diff - entropy
// - 0.5*tr_sigtv - 0.5*tr_sigtw
// - sigs_entropy + omega_entropy
// - 0.5*pis.t()*sigs_invs*pis - 0.5*tr_sigsw);
double bound = arma::as_scalar(log(arma::trans(theta)*betas)*doc_cts + detTerm_nu
-0.5*(tr_sigtv + diff.t()*siginvs*diff
+ tr_sigtw + entropy)
- 0.5*(tr_sigsw + pis.t()*sigs_invs*pis + sigs_entropy - omega_entropy));
// double trace = arma::as_scalar(detTerm_nu - .5*diff.t()*siginvs*diff - entropy);
//
// double new_bound = arma::as_scalar(detTerm_nu - .5*diff.t()*siginvs*diff - entropy
// - 0.5*tr_sigtv - 0.5*tr_sigtw
// - sigs_entropy + omega_entropy
// - 0.5*pis.t()*sigs_invs*pis - 0.5*tr_sigsw);
//double impbound = bound + arma::as_scalar
// Generate a return list that mimics the R output
return Rcpp::List::create(
Rcpp::Named("phis") = EB,
Rcpp::Named("eta") = Rcpp::List::create(Rcpp::Named("lambda")=etas, Rcpp::Named("nu")=nu),
Rcpp::Named("bound") = bound
//Rcpp::Named("trace") = trace,
//Rcpp::Named("new_bound") = new_bound
);
}
// [[Rcpp::export]]
SEXP singlehpbcpp(SEXP eta,
SEXP beta,
SEXP doc_ct,
SEXP mu,
SEXP siginv,
SEXP sigmaentropy){
Rcpp::NumericVector etav(eta);
arma::vec etas(etav.begin(), etav.size(), false);
Rcpp::NumericMatrix betam(beta);
arma::mat betas(betam.begin(), betam.nrow(), betam.ncol());
Rcpp::NumericVector doc_ctv(doc_ct);
arma::vec doc_cts(doc_ctv.begin(), doc_ctv.size(), false);
Rcpp::NumericVector muv(mu);
arma::vec mus(muv.begin(), muv.size(), false);
Rcpp::NumericMatrix siginvm(siginv);
arma::mat siginvs(siginvm.begin(), siginvm.nrow(), siginvm.ncol(), false);
Rcpp::NumericVector sigmaentropym(sigmaentropy);
arma::vec entropy(sigmaentropym);
arma::colvec expeta(etas.size()+1);
expeta.fill(1);
int neta = etas.size();
for(int j=0; j <neta; j++){
expeta(j) = exp(etas(j));
}
arma::vec theta = expeta/sum(expeta);
//create a new version of the matrix so we can mess with it
arma::mat EB(betam.begin(), betam.nrow(), betam.ncol());
//multiply each column by expeta
EB.each_col() %= expeta; //this should be fastest as its column-major ordering
//divide out by the column sums
EB.each_row() %= arma::trans(sqrt(doc_cts))/sum(EB,0);
//Combine the pieces of the Hessian which are matrices
arma::mat hess = EB*EB.t() - sum(doc_cts)*(theta*theta.t());
//we don't need EB any more so we turn it into phi
EB.each_row() %= arma::trans(sqrt(doc_cts));
//Now alter just the diagonal of the Hessian
hess.diag() -= sum(EB,1) - sum(doc_cts)*theta;
//Drop the last row and column
hess.shed_row(neta);
hess.shed_col(neta);
//Now we can add in siginv
hess = hess + siginvs;
//At this point the Hessian is complete.
//This next bit of code is from http://arma.sourceforge.net/docs.html#logging
//It basically keeps arma from printing errors from chol to the console.
std::ostream nullstream(0);
//Start by initializing an object
arma::mat nu = arma::mat(hess.n_rows, hess.n_rows);
//This version of chol generates a boolean which tells us if it failed.
bool worked = arma::chol(nu,hess);
if(!worked) {
//Here we make it positive definite through diagonal dominance
arma::vec dvec = hess.diag();
//find the magnitude of the diagonal
arma::vec magnitudes = sum(abs(hess), 1) - abs(dvec);
//iterate over each row and set the minimum value of the diagonal to be the magnitude of the other terms
int Km1 = dvec.size();
for(int j=0; j < Km1; j++){
if(arma::as_scalar(dvec(j)) < arma::as_scalar(magnitudes(j))) dvec(j) = magnitudes(j) + 0.01; //enforce diagonal dominance
}
//overwrite the diagonal of the hessian with our new object
hess.diag() = dvec;
//that was sufficient to ensure positive definiteness so we now do cholesky
nu = arma::chol(hess);
}
//compute 1/2 the determinant from the cholesky decomposition
double detTerm = -sum(log(nu.diag()));
//Now finish constructing nu
nu = arma::inv(arma::trimatu(nu));
nu = nu * nu.t(); //trimatu doesn't do anything for multiplication so it would just be timesink to signal here.
//Precompute the difference since we use it twice
arma::vec diff = etas - mus;
//Now generate the bound and make it a scalar
double bound = arma::as_scalar(log(arma::trans(theta)*betas)*doc_cts + detTerm - .5*diff.t()*siginvs*diff - entropy);
// Generate a return list that mimics the R output
return Rcpp::List::create(
Rcpp::Named("phis") = EB,
Rcpp::Named("eta") = Rcpp::List::create(Rcpp::Named("lambda")=etas, Rcpp::Named("nu")=nu),
Rcpp::Named("bound") = bound
);
}