-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathbart_ewmv.stan
165 lines (135 loc) · 4.39 KB
/
bart_ewmv.stan
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
data {
int<lower=1> N; // Number of subjects
int<lower=1> T; // Maximum number of trials
int<lower=0> Tsubj[N]; // Number of trials for each subject
int<lower=2> P; // Number of max pump + 1 ** CAUTION **
int<lower=0> pumps[N, T]; // Number of pump
// int<lower=0> reward[N, T]; // Amount of rewards
int<lower=0,upper=1> explosion[N, T]; // Whether the balloon exploded (0 or 1)
}
transformed data {
// Whether a subject pump the button or not (0 or 1)
int d[N, T, P];
for (j in 1:N) {
for (k in 1:Tsubj[j]) {
for (l in 1:P) {
if (l <= pumps[j, k])
d[j, k, l] = 1;
else
d[j, k, l] = 0;
}
}
}
}
parameters {
// Group-level parameters
vector[5] mu_pr;
vector<lower=0>[5] sigma;
// Normally distributed error for Matt trick
vector[N] phi_pr;
vector[N] eta_pr;
vector[N] rho_pr;
vector[N] tau_pr;
vector[N] lambda_pr;
}
transformed parameters {
// Subject-level parameters with Matt trick
vector<lower=0,upper=1>[N] phi;
vector<lower=0>[N] eta;
vector<lower=-0.5,upper=0.5>[N] rho;
vector<lower=0>[N] tau;
vector<lower=0>[N] lambda;
phi = Phi_approx(mu_pr[1] + sigma[1] * phi_pr);
eta = Phi_approx(mu_pr[2] + sigma[2] * eta_pr);
rho = 0.5 - Phi_approx(mu_pr[3] + sigma[3] * rho_pr);
tau = exp(mu_pr[4] + sigma[4] * tau_pr);
lambda = exp(mu_pr[5] + sigma[5] * lambda_pr);
}
model {
// Prior
mu_pr ~ normal(0, 1);
sigma ~ normal(0, 0.2); // cauchy(0, 5);
phi_pr ~ normal(0, 1);
eta_pr ~ normal(0, 1);
rho_pr ~ normal(0, 1);
tau_pr ~ normal(0, 1);
lambda_pr ~ normal(0, 1);
// Likelihood
for (j in 1:N) {
// Initialize n_succ and n_pump for a subject
int n_succ = 0; // Number of successful pumps
int n_pump = 0; // Number of total pumps
real p_burst = phi[j];
for (k in 1:Tsubj[j]) {
real u_gain = 1;
real u_loss;
real u_pump;
real u_stop = 0;
real delta_u;
for (l in 1:(pumps[j, k] + 1 - explosion[j, k])) {
u_loss = (l - 1);
u_pump = (1 - p_burst) * u_gain - lambda[j] * p_burst * u_loss +
rho[j] * p_burst * (1 - p_burst) * (u_gain + lambda[j] * u_loss)^2;
// u_stop always equals 0.
delta_u = u_pump - u_stop;
// Calculate likelihood with bernoulli distribution
d[j, k, l] ~ bernoulli_logit(tau[j] * delta_u);
}
// Update n_succ and n_pump after each trial ends
n_succ += pumps[j, k] - explosion[j, k];
n_pump += pumps[j, k];
if(n_pump>0){
p_burst = phi[j] + (1 - exp(-n_pump * eta[j])) * ((0.0 + n_pump - n_succ) / n_pump - phi[j]);
}
}
}
}
generated quantities {
// Actual group-level mean
real<lower=0> mu_phi = Phi_approx(mu_pr[1]);
real<lower=0> mu_eta = Phi_approx(mu_pr[2]);
real<lower=-0.5,upper=0.5> mu_rho = 0.5 - Phi_approx(mu_pr[3]);
real<lower=0> mu_tau = exp(mu_pr[4]);
real<lower=0> mu_lambda = exp(mu_pr[5]);
// Log-likelihood for model fit
real log_lik[N];
// For posterior predictive check
real y_pred[N, T, P];
// Set all posterior predictions to 0 (avoids NULL values)
for (j in 1:N)
for (k in 1:T)
for(l in 1:P)
y_pred[j, k, l] = -1;
{ // Local section to save time and space
for (j in 1:N) {
// Initialize n_succ and n_pump for a subject
int n_succ = 0; // Number of successful pumps
int n_pump = 0; // Number of total pumps
real p_burst = phi[j];
log_lik[j] = 0;
for (k in 1:Tsubj[j]) {
real u_gain = 1;
real u_loss;
real u_pump;
real u_stop = 0;
real delta_u;
for (l in 1:(pumps[j, k] + 1 - explosion[j, k])) {
// u_gain always equals r ^ rho.
u_loss = (l - 1);
u_pump = (1 - p_burst) * u_gain - lambda[j] * p_burst * u_loss +
rho[j] * p_burst * (1 - p_burst) * (u_gain + lambda[j] * u_loss)^2;
// u_stop always equals 0.
delta_u = u_pump - u_stop;
log_lik[j] += bernoulli_logit_lpmf(d[j, k, l] | tau[j] * delta_u);
y_pred[j, k, l] = bernoulli_logit_rng(tau[j] * delta_u);
}
// Update n_succ and n_pump after each trial ends
n_succ += pumps[j, k] - explosion[j, k];
n_pump += pumps[j, k];
if(n_pump>0){
p_burst = phi[j] + (1 - exp(-n_pump * eta[j])) * ((0.0 + n_pump - n_succ) / n_pump - phi[j]);
}
}
}
}
}