-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatebeta_scalar.cpp
112 lines (110 loc) · 6.24 KB
/
updatebeta_scalar.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
#include <Rcpp.h>
#include <stdio.h>
#include <gsl_rng.h>
#include <gsl_randist.h>
#include <math.h>
#include <R_ext/Utils.h>
#include <stdlib.h>
using namespace std;
void updatebeta_scalar(vector<double>& xmust, vector<double>& xmuut, vector<int>& xn_s, vector<int>& xn_u, int xI, int xK, int xM,
int K1, int t1, double xp_var, double sqrt_var1, double sqrt_var2, int xtt, vector<int>& xgammat,
Rcpp::IntegerMatrix& xd, Rcpp::NumericMatrix& xybar_s, Rcpp::NumericMatrix& xybar_u, Rcpp::NumericMatrix& pt1, Rcpp::NumericMatrix& pt2,
Rcpp::NumericMatrix& pt3, double xlambda, Rcpp::NumericVector& beta, double xalpha, double xsig_beta1,
double xbeta1, Rcpp::IntegerVector& xAbeta)
{
double xbeta = beta[t1]; int tt=t1+1; int temp=0; beta[tt] = xbeta;
double delF=0.; double log1=0.; double log2=0.; int nik=0; double beta_np=0.; double alpha_np=0.;
double alb= xalpha*log(xbeta); double adb = xalpha/xbeta;
for (int p=0; p<xM; p++) {
for (int i=0; i<xI;i++) {
for(int k=0; k<K1; k++) {
temp = xI*k+i;
if(xd(k,p)==1) {
nik = xn_u[temp]+xn_s[temp];
if(xgammat[temp]==1) {
if (xn_u[temp]>0 && xn_s[temp]>0) {
beta_np = pt3(temp,p)+xbeta+pow((xmuut[p]-xybar_u(temp,p)),2)/(2*(xlambda+1/xn_u[temp]))+
pow((xmust[p]-xybar_s(temp,p)),2)/(2*(xlambda+1/xn_s[temp]));
alpha_np = nik/2+xalpha;
log2 += alb-alpha_np*log(beta_np);
delF+=adb-alpha_np/beta_np;
}else if (xn_u[temp]==0 && xn_s[temp]>0) {
beta_np = pt1(temp,p)+xbeta+pow((xmust[p]-xybar_s(temp,p)),2)/(2*(xlambda+1/xn_s[temp]));
alpha_np = nik/2+xalpha;
log2 += alb-alpha_np*log(beta_np);
delF+=adb-alpha_np/beta_np;
} else if (xn_u[temp]>0 && xn_s[temp]==0) {
beta_np = pt2(temp,p)+xbeta+pow((xmuut[p]-xybar_u(temp,p)),2)/(2*(xlambda+1/xn_u[temp]));
alpha_np = nik/2+xalpha;
log2 += alb-alpha_np*log(beta_np);
delF+=adb-alpha_np/beta_np;
}
}else{
if (nik>0) {
beta_np =pt3(temp,p)+xbeta+pow((xmuut[p]-(xn_s[temp]*xybar_s(temp,p)+xn_u[temp]*xybar_u(temp,p))/nik),2)/(2*(xlambda+1/nik))+
0.5*xn_s[temp]*pow(xybar_s(temp,p),2)+ 0.5*xn_u[temp]*pow(xybar_u(temp,p),2)-0.5*pow((xn_s[temp]*xybar_s(temp,p)+xn_u[temp]*xybar_u(temp,p)),2)/nik;
alpha_np = nik/2+xalpha;
log2 += alb-alpha_np*log(beta_np);
delF+=adb-alpha_np/beta_np;
}
}
}
}
}}
double mean_p = std::max(0.01, xbeta+delF/xtt);
Rcpp::NumericVector betap= Rcpp::rnorm(1, mean_p, sqrt_var1);
if (Rcpp::as<double>(Rcpp::rbinom(1,1,xp_var)) == 1) {
betap= Rcpp::rnorm(1, mean_p, sqrt_var1);}
else {betap = Rcpp::rnorm(1, mean_p, sqrt_var2);}
//if(betap[0][0]>0.0 && betap[0][0]<=xlambda_beta[p]) {
//std::cout <<"log2 = "<< log2 <<" betap[0] = "<< betap[0]<< std::endl;
if(betap[0]>0.0) {
log2+=log(xp_var*gsl_ran_gaussian_pdf(betap[0]-mean_p, sqrt_var1)+(1-xp_var)*gsl_ran_gaussian_pdf(betap[0]-mean_p, sqrt_var2));
delF = 0.; alb= xalpha*log(betap[0]); adb = xalpha/betap[0];
for (int p=0; p<xM; p++) {
for (int i=0; i<xI;i++) {
for(int k=0; k<K1; k++) {
temp=xI*k+i;
if(xd(k,p)==1) {
nik = xn_u[temp]+xn_s[temp];
if(xgammat[temp]==1) {
if (xn_u[temp] >0 && xn_s[temp]>0) {
beta_np = pt3(temp,p)+betap[0]+pow((xmuut[p]-xybar_u(temp,p)),2)/(2*(xlambda+1/xn_u[temp]))+
pow((xmust[p]-xybar_s(temp,p)),2)/(2*(xlambda+1/xn_s[temp]));
alpha_np = nik/2+xalpha;
log1 += alb-alpha_np*log(beta_np);
delF+=adb-alpha_np/beta_np;
}else if (xn_u[temp]==0 && xn_s[temp]>0) {
beta_np = pt1(temp,p)+betap[0]+pow((xmust[p]-xybar_s(temp,p)),2)/(2*(xlambda+1/xn_s[temp]));
alpha_np = nik/2+xalpha;
log1 += alb-alpha_np*log(beta_np);
delF+=adb-alpha_np/beta_np;
} else if (xn_u[temp]>0 && xn_s[temp]==0) {
beta_np =pt2(temp,p)+betap[0]+pow((xmuut[p]-xybar_u(temp,p)),2)/(2*(xlambda+1/xn_u[temp]));
alpha_np = nik/2+xalpha;
log1 += alb-alpha_np*log(beta_np);
delF+=adb-alpha_np/beta_np;
}
}else{
if (nik>0) {
beta_np = pt3(temp,p)+betap[0]+pow((xmuut[p]-(xn_s[temp]*xybar_s(temp,p)+xn_u[temp]*xybar_u(temp,p))/nik),2)/(2*(xlambda+1/nik))+
0.5*xn_s[temp]*pow(xybar_s(temp,p),2)+ 0.5*xn_u[temp]*pow(xybar_u(temp,p),2)-0.5*pow((xn_s[temp]*xybar_s(temp,p)+xn_u[temp]*xybar_u(temp,p)),2)/nik;
alpha_np = nik/2+xalpha;
log1 += alb-alpha_np*log(beta_np);
delF+=adb-alpha_np/beta_np;
}
}
}
}
}}
mean_p = std::max(0.01, betap[0]+delF/xtt);
log1+=log(xp_var*gsl_ran_gaussian_pdf(xbeta-mean_p, sqrt_var1)+(1-xp_var)*gsl_ran_gaussian_pdf(xbeta-mean_p, sqrt_var2));
log2+=log(gsl_ran_gaussian_pdf(xbeta-xbeta1, xsig_beta1));
log1+=log(gsl_ran_gaussian_pdf(betap[0]-xbeta1, xsig_beta1));
//std::cout <<"log1 = "<< log1 <<" log2 = "<< log2 << std::endl;
if (log(Rcpp::as<double>(Rcpp::runif(1)) ) <= (log1 - log2)) {
beta[tt] = betap[0];
xAbeta[tt] = 1;
}
}
}