-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathbandit4arm_4par.stan
More file actions
176 lines (146 loc) · 4.79 KB
/
bandit4arm_4par.stan
File metadata and controls
176 lines (146 loc) · 4.79 KB
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
#include /pre/license.stan
// Seymour et al 2012 J neuro model, w/o C (chioce perseveration)
data {
int<lower=1> N;
int<lower=1> T;
int<lower=1, upper=T> Tsubj[N];
real rew[N, T];
real los[N, T];
int choice[N, T];
}
transformed data {
vector[4] initV;
initV = rep_vector(0.0, 4);
}
parameters {
// Declare all parameters as vectors for vectorizing
// Hyper(group)-parameters
vector[4] mu_pr;
vector<lower=0>[4] sigma;
// Subject-level raw parameters (for Matt trick)
vector[N] Arew_pr;
vector[N] Apun_pr;
vector[N] R_pr;
vector[N] P_pr;
}
transformed parameters {
// Transform subject-level raw parameters
vector<lower=0, upper=1>[N] Arew;
vector<lower=0, upper=1>[N] Apun;
vector<lower=0, upper=30>[N] R;
vector<lower=0, upper=30>[N] P;
for (i in 1:N) {
Arew[i] = Phi_approx(mu_pr[1] + sigma[1] * Arew_pr[i]);
Apun[i] = Phi_approx(mu_pr[2] + sigma[2] * Apun_pr[i]);
R[i] = Phi_approx(mu_pr[3] + sigma[3] * R_pr[i]) * 30;
P[i] = Phi_approx(mu_pr[4] + sigma[4] * P_pr[i]) * 30;
}
}
model {
// Hyperparameters
mu_pr ~ normal(0, 1);
sigma ~ normal(0, 0.2);
// individual parameters
Arew_pr ~ normal(0, 1.0);
Apun_pr ~ normal(0, 1.0);
R_pr ~ normal(0, 1.0);
P_pr ~ normal(0, 1.0);
for (i in 1:N) {
// Define values
vector[4] Qr;
vector[4] Qp;
vector[4] PEr_fic; // prediction error - for reward fictive updating (for unchosen options)
vector[4] PEp_fic; // prediction error - for punishment fictive updating (for unchosen options)
vector[4] Qsum; // Qsum = Qrew + Qpun + perseverance
real Qr_chosen;
real Qp_chosen;
real PEr; // prediction error - for reward of the chosen option
real PEp; // prediction error - for punishment of the chosen option
// Initialize values
Qr = initV;
Qp = initV;
Qsum = initV;
for (t in 1:Tsubj[i]) {
// softmax choice
choice[i, t] ~ categorical_logit(Qsum);
// Prediction error signals
PEr = R[i] * rew[i, t] - Qr[choice[i, t]];
PEp = P[i] * los[i, t] - Qp[choice[i, t]];
PEr_fic = -Qr;
PEp_fic = -Qp;
// store chosen deck Q values (rew and pun)
Qr_chosen = Qr[choice[i, t]];
Qp_chosen = Qp[choice[i, t]];
// First, update Qr & Qp for all decks w/ fictive updating
Qr += Arew[i] * PEr_fic;
Qp += Apun[i] * PEp_fic;
// Replace Q values of chosen deck with correct values using stored values
Qr[choice[i, t]] = Qr_chosen + Arew[i] * PEr;
Qp[choice[i, t]] = Qp_chosen + Apun[i] * PEp;
// Q(sum)
Qsum = Qr + Qp;
}
}
}
generated quantities {
// For group level parameters
real<lower=0, upper=1> mu_Arew;
real<lower=0, upper=1> mu_Apun;
real<lower=0, upper=30> mu_R;
real<lower=0, upper=30> mu_P;
// For log likelihood calculation
real log_lik[N];
// For posterior predictive check
real y_pred[N, T];
// Set all posterior predictions to 0 (avoids NULL values)
for (i in 1:N) {
for (t in 1:T) {
y_pred[i, t] = -1;
}
}
mu_Arew = Phi_approx(mu_pr[1]);
mu_Apun = Phi_approx(mu_pr[2]);
mu_R = Phi_approx(mu_pr[3]) * 30;
mu_P = Phi_approx(mu_pr[4]) * 30;
{ // local section, this saves time and space
for (i in 1:N) {
// Define values
vector[4] Qr;
vector[4] Qp;
vector[4] PEr_fic; // prediction error - for reward fictive updating (for unchosen options)
vector[4] PEp_fic; // prediction error - for punishment fictive updating (for unchosen options)
vector[4] Qsum; // Qsum = Qrew + Qpun + perseverance
real Qr_chosen;
real Qp_chosen;
real PEr; // prediction error - for reward of the chosen option
real PEp; // prediction error - for punishment of the chosen option
// Initialize values
Qr = initV;
Qp = initV;
Qsum = initV;
log_lik[i] = 0.0;
for (t in 1:Tsubj[i]) {
// compute log likelihood of current trial
log_lik[i] += categorical_logit_lpmf(choice[i, t] | Qsum);
// generate posterior prediction for current trial
y_pred[i, t] = categorical_rng(softmax(Qsum));
// Prediction error signals
PEr = R[i] * rew[i, t] - Qr[choice[i, t]];
PEp = P[i] * los[i, t] - Qp[choice[i, t]];
PEr_fic = -Qr;
PEp_fic = -Qp;
// store chosen deck Q values (rew and pun)
Qr_chosen = Qr[choice[i, t]];
Qp_chosen = Qp[choice[i, t]];
// First, update Qr & Qp for all decks w/ fictive updating
Qr += Arew[i] * PEr_fic;
Qp += Apun[i] * PEp_fic;
// Replace Q values of chosen deck with correct values using stored values
Qr[choice[i, t]] = Qr_chosen + Arew[i] * PEr;
Qp[choice[i, t]] = Qp_chosen + Apun[i] * PEp;
// Q(sum)
Qsum = Qr + Qp;
}
}
}
}