-
Notifications
You must be signed in to change notification settings - Fork 0
/
updategammak_noPu.cpp
321 lines (297 loc) · 13 KB
/
updategammak_noPu.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
#include <Rcpp.h>
#include <R_ext/Utils.h>
#include <stdlib.h>
RcppExport SEXP updategammak_noPu(SEXP n_s,SEXP n_u, SEXP gammat, SEXP I, SEXP K, SEXP SS, SEXP alphau, SEXP alphas, SEXP alpha, SEXP mk, SEXP Istar,
SEXP mKstar, SEXP pp, SEXP pb1, SEXP pb2, SEXP indi)
{
BEGIN_RCPP
Rcpp::IntegerMatrix xn_s(n_s);
Rcpp::IntegerMatrix xn_u(n_u);
Rcpp::NumericVector xalphas(alphas);
Rcpp::IntegerMatrix xindicator(indi);
Rcpp::NumericVector xalphau(alphau);
double xalpha = Rcpp::as<double>(alpha);
int xK = Rcpp::as<int>(K);
int xI = Rcpp::as<int>(I);
int xSS = Rcpp::as<int>(SS);
Rcpp::IntegerVector xAg(xI);
Rcpp::IntegerVector gamma_old(xK);
Rcpp::IntegerMatrix gamma_tt(gammat);
Rcpp::IntegerVector xmk(mk);
int xIstar = Rcpp::as<int>(Istar);
int xmKstar = Rcpp::as<int>(mKstar);
Rcpp::NumericVector xpp(pp);
double xpb1 = Rcpp::as<double>(pb1);
double xpb2 = Rcpp::as<double>(pb2);
Rcpp::RNGScope scope;
Rcpp::NumericVector m(1);
int K1 = xK-1;
int array1K[K1];
int mik[K1];
double alphaK = xalpha/xK;
int xindiK1 = 0;
int miKstar = 0;
int Iistar = 0;
int flag =0; int flag_old = 0;
double log_likeli_deno = 0.; double log_likeli_nume = 0.0;
int ct=0;
double log_probK = 0.;
double log_1probK = 0.;
int vec = 0; int arr = 0;
int lplace0_new = 0; int lplace0 = 0;
int flag_prop = 0;
int lplace1_new = 0;
int lplace1 = 0;
for (int i = 0; i < xI; i++) {
xindiK1 = xindicator(i,K1);
int array1K[xindiK1];
for (int j = 0; j < xK; j++) {
gamma_old[j] = gamma_tt(i,j);
}
for (int ss = 0; ss < xSS; ss++) {
if(xindiK1>0) {
miKstar = xmKstar;
Iistar = xIstar;
flag =0; flag_old = 0;
log_likeli_deno = 0.;
ct=0;
for (int j = 0; j < K1; j++) {
gamma_tt(i,j) = gamma_old[j];
mik[j] = xmk[j]-gamma_old[j];
flag +=gamma_tt(i,j);
if (xindicator(i,j) ==1) {array1K[ct] = j; ct +=1;}
log_likeli_deno+=gamma_old[j]*(log(mik[j]+alphaK)-log(xI+alphaK))+(1-gamma_old[j])*(log(xI-mik[j])-log(xI+alphaK));
}
if (flag >1 ) {//*
Iistar = xIstar-1;
miKstar = xmKstar-gamma_old[K1];
flag_old = 1;
}
gamma_tt(i,K1) = gamma_old[K1];
log_probK =log(miKstar+alphaK)-log(Iistar+alphaK+1);
log_1probK = log(Iistar-miKstar+1)-log(Iistar+alphaK+1);
if(flag_old == 1) {
log_likeli_deno+=gamma_old[K1]*(log_probK)+(1-gamma_old[K1])*(log_1probK);
}
if(xindiK1<=2) {m=1;}
else{
if (Rcpp::as<double>(Rcpp::runif(1)) <=xpp[i])
{ m = Rcpp::rbinom(1,xindiK1,xpb1); }//m[0] +=1;}
else {m =Rcpp::rbinom(1,xindiK1,xpb2); }//m[0] +=1;}
}
/* random sample */
if (m[0]>0) {
for (int k = 0; k<m[0]; k++) {
Rcpp::NumericVector tmp = Rcpp::runif(1,k,xindiK1);
vec = (int) tmp[0];
arr = array1K[k];
array1K[k] = array1K[vec];
array1K[vec] = arr;
gamma_tt(i,array1K[k]) = 1-gamma_tt(i,array1K[k]);
//if (Rcpp::as<double>(Rcpp::runif(1)) < 0.5) {gamma_tt(i,array1K[k]) = 1;}
//else {gamma_tt(i,array1K[k]) = 0;}
}
}
lplace0_new = 0; lplace0 = 0;
log_likeli_nume = 0.0;
for (int k = 0; k < K1; k++) {
log_likeli_nume += gamma_tt(i,k)*(log(mik[k]+alphaK)-log(xI+alphaK))+(1-gamma_tt(i,k))*(log(xI-mik[k])-log(xI+alphaK));
if (gamma_tt(i,k) == 0) { lplace0_new +=1;}
if (gamma_old[k] == 0){ lplace0 += 1;}
}
lplace1_new = K1-lplace0_new;
lplace1 = K1-lplace0;
flag_prop = 0;
if (lplace1_new == 1) { //*
gamma_tt(i,K1) = 1;
lplace1_new +=1;
} else if (lplace1_new == 0) {
gamma_tt(i,K1) = 0;
lplace0_new += 1;
} else {
flag_prop = 1;
log_likeli_deno += log(0.5); // proposal
if (Rcpp::as<double>(Rcpp::runif(1)) <= 0.5) {
gamma_tt(i,K1) = 1;
}else{
gamma_tt(i,K1) = 0;
}
if (gamma_tt(i,K1) == 1) {
lplace1_new +=1;
log_likeli_nume +=log_probK;
} else {
lplace0_new += 1;
log_likeli_nume += log_1probK;
}
}
if ( lplace1 == 1) { //*
lplace1 +=1;
} else if (lplace1 == 0) {
lplace0+= 1;
} else {
log_likeli_nume += log(0.5); // proposal
if (gamma_old[K1] == 1) {
lplace1 +=1;
} else {
lplace0 += 1;
}
}
int place0_new[lplace0_new];
int place0[lplace0];
int place1[lplace1];
int flag0 = 0; int flag1 = 0;
int flag0_old = 0; int flag1_old = 0;
int place1_new[lplace1_new];
for (int k= 0; k < xK; k++) {
if (gamma_tt(i,k) == 0) {
place0_new[flag0] = k;
flag0 += 1;
} else {
place1_new[flag1] = k;
flag1 += 1;
}
if (gamma_old[k] == 0) {
place0[flag0_old] = k;
flag0_old += 1;
} else{
place1[flag1_old] = k;
flag1_old +=1;
}
}
if (lplace1_new == 0) {
double sum_alphau_nus = 0.0;
double sum_gammaln_nu = 0.0;
for ( int k = 0; k<xK; k++) {
double summ = xalphau[k]+xn_u(i,k)+xn_s(i,k);
sum_alphau_nus += summ;
sum_gammaln_nu +=lgamma(summ);
}
log_likeli_nume +=(sum_gammaln_nu-lgamma(sum_alphau_nus));
} else if (lplace0_new == 0) {
double sum_alphau_nus = 0.0;
double sum_gammaln_nu = 0.0;
double sum_gammaln = 0.0;
double sum_alphas = 0.0;
double sum_gammaln_ns = 0.0;
double sum_alphas_ns = 0.0;
for ( int k=0; k<xK; k++) {
double summ = xalphau[k]+xn_u(i,k);
sum_alphau_nus += summ;
sum_gammaln_nu +=lgamma(summ);
sum_gammaln +=lgamma(xalphas[k]);
sum_alphas +=xalphas[k];
sum_gammaln_ns += (lgamma(xalphas[k] + xn_s(i,k)));
sum_alphas_ns += (xalphas[k]+xn_s(i,k));
}
log_likeli_nume +=(sum_gammaln_nu-lgamma(sum_alphau_nus));
log_likeli_nume -= (sum_gammaln - lgamma(sum_alphas));
log_likeli_nume +=(sum_gammaln_ns -lgamma(sum_alphas_ns));
} else {
double sum_gammaln = 0.0;
double sum_alphas = 0.0;
double sum_gammaln_ns = 0.0;
double sum_alphas_ns = 0.0;
double sum_alphau_nu = 0.0;
double sum_alphau_nus = 0.0;
double sum_gammaln_nu = 0.0;
for (int k = 0; k<lplace1_new; k++) {
sum_alphau_nu +=(xalphau[place1_new[k]]+xn_u(i,place1_new[k]));
sum_alphau_nus+=(xalphau[place1_new[k]]+xn_u(i,place1_new[k])+xn_s(i,place1_new[k]));
sum_gammaln_nu +=lgamma(xalphau[place1_new[k]]+xn_u(i,place1_new[k]));
sum_gammaln +=lgamma(xalphas[place1_new[k]]);
sum_alphas +=xalphas[place1_new[k]];
sum_gammaln_ns += (lgamma(xalphas[place1_new[k]] + xn_s(i,place1_new[k])));
sum_alphas_ns += (xalphas[place1_new[k]]+xn_s(i,place1_new[k]));
}
log_likeli_nume -= (sum_gammaln - lgamma(sum_alphas));
log_likeli_nume +=(sum_gammaln_ns -lgamma(sum_alphas_ns));
log_likeli_nume += (sum_gammaln_nu-lgamma(sum_alphau_nu));
log_likeli_nume +=lgamma(sum_alphau_nus+1);
sum_gammaln_nu = 0.0;
for (int k = 0; k<lplace0_new; k++) {
sum_alphau_nus+=(xalphau[place0_new[k]]+xn_u(i,place0_new[k])+xn_s(i,place0_new[k]));
sum_gammaln_nu +=lgamma(xalphau[place0_new[k]]+xn_u(i,place0_new[k])+xn_s(i,place0_new[k]));
}
log_likeli_nume +=sum_gammaln_nu;
log_likeli_nume -=lgamma(sum_alphau_nus+1);
}
//
if (lplace1 == 0) {
double sum_alphau_nus = 0.0;
double sum_gammaln_nu = 0.0;
for ( int k = 0; k<xK; k ++) {
double summ = xalphau[k]+xn_u(i,k)+xn_s(i,k);
sum_alphau_nus += summ;
sum_gammaln_nu +=lgamma(summ);
}
log_likeli_deno +=(sum_gammaln_nu-lgamma(sum_alphau_nus));
} else if (lplace0 == 0) {
double sum_alphau_nus = 0.0;
double sum_gammaln_nu = 0.0;
double sum_gammaln = 0.0;
double sum_alphas = 0.0;
double sum_gammaln_ns = 0.0;
double sum_alphas_ns = 0.0;
for ( int k=0; k<xK; k++) {
double summ = xalphau[k]+xn_u(i,k);
sum_alphau_nus += summ;
sum_gammaln_nu +=lgamma(summ);
sum_gammaln +=lgamma(xalphas[k]);
sum_alphas +=xalphas[k];
sum_gammaln_ns += (lgamma(xalphas[k] + xn_s(i,k)));
sum_alphas_ns += (xalphas[k]+xn_s(i,k));
}
log_likeli_deno +=(sum_gammaln_nu-lgamma(sum_alphau_nus));
log_likeli_deno -= (sum_gammaln - lgamma(sum_alphas));
log_likeli_deno +=(sum_gammaln_ns -lgamma(sum_alphas_ns));
} else {
double sum_gammaln = 0.0;
double sum_alphas = 0.0;
double sum_gammaln_ns = 0.0;
double sum_alphas_ns = 0.0;
double sum_alphau_nu = 0.0;
double sum_alphau_nus = 0.0;
double sum_gammaln_nu = 0.0;
for (int k = 0; k<lplace1; k++) {
sum_alphau_nu +=(xalphau[place1[k]]+xn_u(i,place1[k]));
sum_alphau_nus+=(xalphau[place1[k]]+xn_u(i,place1[k])+xn_s(i,place1[k]));
sum_gammaln_nu +=lgamma(xalphau[place1[k]]+xn_u(i,place1[k]));
sum_gammaln +=lgamma(xalphas[place1[k]]);
sum_alphas +=xalphas[place1[k]];
sum_gammaln_ns += (lgamma(xalphas[place1[k]] + xn_s(i,place1[k])));
sum_alphas_ns += (xalphas[place1[k]]+xn_s(i,place1[k]));
}
log_likeli_deno -= (sum_gammaln - lgamma(sum_alphas));
log_likeli_deno +=(sum_gammaln_ns -lgamma(sum_alphas_ns));
log_likeli_deno += (sum_gammaln_nu-lgamma(sum_alphau_nu));
log_likeli_deno +=lgamma(sum_alphau_nus+1);
sum_gammaln_nu = 0.0;
for (int k = 0; k<lplace0; k++) {
sum_alphau_nus+=(xalphau[place0[k]]+xn_u(i,place0[k])+xn_s(i,place0[k]));
sum_gammaln_nu +=lgamma(xalphau[place0[k]]+xn_u(i,place0[k])+xn_s(i,place0[k]));
}
log_likeli_deno +=sum_gammaln_nu;
log_likeli_deno -=lgamma(sum_alphau_nus+1);
}
//std::cout <<"log_nume = "<< log_likeli_nume << std::endl;
//std::cout <<"log_deo = "<< log_likeli_deno << std::endl;
if (log(Rcpp::as<double>(Rcpp::runif(1)) ) <= (log_likeli_nume - log_likeli_deno)) {
for (int j = 0; j < K1; j++) {
xmk[j] = xmk[j]-gamma_old[j]+gamma_tt(i,j);
gamma_old[j] = gamma_tt(i,j);
}
gamma_old[K1] = gamma_tt(i,K1);
xIstar = Iistar+flag_prop;
xmKstar = miKstar+flag_prop*gamma_tt(i,K1);
xAg[i] = 1;
} else {
for (int j = 0; j < xK; j++) {
gamma_tt(i,j) = gamma_old[j];
}
}
}
}
}
return Rcpp::List::create(Rcpp::Named("gamma_tt") = gamma_tt,Rcpp::Named("Ag") = xAg, Rcpp::Named("mKstar") = xmKstar, Rcpp::Named("xmk") = xmk, Rcpp::Named("xIstar")=xIstar);
END_RCPP
}