-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_bayesian_gp_means_std.py
executable file
·300 lines (218 loc) · 11.7 KB
/
get_bayesian_gp_means_std.py
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
import numpy as np
import pandas as pd
from tqdm import tqdm
from scipy.special import logsumexp
from scipy.stats import norm
from get_kalman import get_noise_nmll
def get_means_stdev(x_mu_rbf, x_sd_rbf, x_mu_lin, x_sd_lin, x_mu_kal, x_sd_kal,
rewards, lin_gp_data, rbf_gp_data, noise_nmll):
log_posterior = np.zeros((300, 3))
all_subj = []
for s in tqdm(set(noise_nmll.Subject)):
lin_gp_data_nll0 = lin_gp_data.loc[lin_gp_data.id == s, 'nlml'].values
rbf_gp_data_nll0 = rbf_gp_data.loc[rbf_gp_data.id == s, 'nlml'].values
noise_nll0 = noise_nmll.loc[noise_nmll.Subject == s, 'nlml'].values
# each of the loss scores includes an initialization of 1.0 for the first observation,
# and as loss is cumulative within a round, this iniatiliaztion needs to be removed from
# all of the loss values
lin_gp_data_nll0 -= 1.0
rbf_gp_data_nll0 -= 1.0
noise_nll0 -= 1.0
# loop through the trials
log_prior = np.zeros(3)
for t in range(len(noise_nll0)):
if (t % 10) == 0:
# for each of the GP model, we need to use an initialization over the model
# as it's prior. In the code Eric wrote, the log loss is set to -1.0 for
# the first observation, and it will vary depending on the prior
gp_loss = - norm(loc=25.0, scale=np.sqrt(5)).logpdf(rewards[t])
# this loss value needs to be added to each of the observations in the round,
# because the loss scores are cumulative within a round
if ((t % 10) == 0) & (t > 0):
# update the prior with the normalize posterior
log_prior[:] = log_posterior[t-1, :] - logsumexp(log_posterior[t-1, :])
log_posterior[t, :] = log_prior - np.array([
noise_nll0[t]+gp_loss, rbf_gp_data_nll0[t]+gp_loss, lin_gp_data_nll0[t]+gp_loss,
])
# now that we have the log posterior, use this to weight the means and stdevs
log_posterior -= np.tile(logsumexp(log_posterior, axis=1).reshape(-1, 1), (1, 3))
w = np.exp(log_posterior)
# subselect the trials
idx = np.arange(len(noise_nmll))[np.array(noise_nmll.Subject == s)]
w_kal = np.tile(np.array(w[:, 0]).reshape(-1, 1), (1, 8))
w_rbf = np.tile(np.array(w[:, 1]).reshape(-1, 1), (1, 8))
w_lin = np.tile(np.array(w[:, 2]).reshape(-1, 1), (1, 8))
mu_mix = x_mu_kal[idx, :] * w_kal + x_mu_rbf[idx, :] * w_rbf + x_mu_lin[idx, :] * w_lin
var0 = w_kal * (x_sd_kal[idx, :] ** 2) + \
w_rbf * (x_sd_rbf[idx, :] ** 2) + \
w_lin * (x_sd_lin[idx, :] ** 2)
var1 = w_kal * (x_mu_kal[idx, :] ** 2) + \
w_rbf * (x_mu_rbf[idx, :] ** 2) + \
w_lin * (x_mu_lin[idx, :] ** 2)
var2 = w_kal * x_mu_kal[idx, :] + \
w_rbf * x_mu_rbf[idx, :] + \
w_lin * x_mu_lin[idx, :]
std_mix = np.sqrt(var0 + var1 - (var2 ** 2))
subj_df = {
'Subject': [s] * len(mu_mix),
'Trial': range(len(mu_mix)),
'log p(Noise)': log_posterior[:, 0],
'log p(RBF)': log_posterior[:, 1],
'log p(Lin)': log_posterior[:, 2],
}
# calculate a weighted loss function
nmll = logsumexp([
noise_nll0 + log_posterior[:, 0],
rbf_gp_data_nll0 + log_posterior[:, 1],
lin_gp_data_nll0 + log_posterior[:, 2],
], axis=0)
subj_df['nmll'] = nmll
for a0 in range(np.shape(mu_mix)[1]):
subj_df['mu_%d' % a0] = mu_mix[:, a0]
subj_df['std_%d' % a0] = std_mix[:, a0]
all_subj.append(pd.DataFrame(subj_df))
return pd.concat(all_subj)
# N.B. each experiment needs a seperate function to prepare it's own data
def exp_lin():
lin_gp_data = pd.read_csv('Data/exp_linear/linpred.csv')
lin_gp_data.index = range(len(lin_gp_data))
rbf_gp_data = pd.read_csv('Data/exp_linear/rbfpred.csv')
rbf_gp_data.index = range(len(rbf_gp_data))
raw_data = pd.read_csv('Data/exp_linear/lindata.csv')
rewards = raw_data['out'].values
noise_nmll = get_noise_nmll(raw_data_path='Data/exp_linear/lindata.csv')
# drop subjects for which the RBF failed to converge
subjects_to_drop = set()
for s in set(noise_nmll.Subject):
if s not in set(rbf_gp_data.id):
subjects_to_drop.add(s)
for s in subjects_to_drop:
lin_gp_data = lin_gp_data[lin_gp_data.id != s].copy()
noise_nmll = noise_nmll[noise_nmll.Subject != s].copy()
x_mu_rbf = np.array([rbf_gp_data.loc[:, 'mu_ %d' % ii].values for ii in range(8)]).T
x_sd_rbf = np.array([rbf_gp_data.loc[:, 'sig_ %d' % ii].values for ii in range(8)]).T
x_mu_lin = np.array([lin_gp_data.loc[:, 'mu_ %d' % ii].values for ii in range(8)]).T
x_sd_lin = np.array([lin_gp_data.loc[:, 'sig_ %d' % ii].values for ii in range(8)]).T
x_mu_kal = np.array([noise_nmll.loc[:, 'mu_%d' % ii].values for ii in range(8)]).T
x_sd_kal = np.array([noise_nmll.loc[:, 'std_%d' % ii].values for ii in range(8)]).T
# get the posterior over models for each subject across time
# print lin_gp_data.columns
all_subjs = get_means_stdev(
x_mu_rbf, x_sd_rbf, x_mu_lin, x_sd_lin, x_mu_kal, x_sd_kal,
rewards, lin_gp_data, rbf_gp_data, noise_nmll
)
all_subjs.to_pickle('Data/exp_linear/bayes_gp_exp1.pkl')
def exp_shifted():
lin_gp_data = pd.read_csv('Data/exp_shifted/gplinshifted.csv')
lin_gp_data.index = range(len(lin_gp_data))
rbf_gp_data = pd.read_csv('Data/exp_shifted/gprbfshifted.csv')
rbf_gp_data.index = range(len(rbf_gp_data))
raw_data = pd.read_csv('Data/exp_shifted/datashifted_withoffset.csv')
rewards = raw_data['out'].values + raw_data['int'].values
noise_nmll = get_noise_nmll(raw_data_path='Data/exp_shifted/datashifted_withoffset.csv', intercept=True)
# drop subjects for which the RBF failed to converge
subjects_to_drop = set()
for s in set(noise_nmll.Subject):
if s not in set(rbf_gp_data.id):
subjects_to_drop.add(s)
for s in subjects_to_drop:
lin_gp_data = lin_gp_data[lin_gp_data.id != s].copy()
noise_nmll = noise_nmll[noise_nmll.Subject != s].copy()
x_mu_lin = np.array([lin_gp_data.loc[:, 'mu_%d' % ii].values + raw_data['int'].values for ii in range(8)]).T
x_sd_lin = np.array([lin_gp_data.loc[:, 'std_%d' % ii].values for ii in range(8)]).T
x_mu_rbf = np.array([rbf_gp_data.loc[:, 'mu_%d' % ii].values + raw_data['int'].values for ii in range(8)]).T
x_sd_rbf = np.array([rbf_gp_data.loc[:, 'std_%d' % ii].values for ii in range(8)]).T
x_mu_kal = np.array([noise_nmll.loc[:, 'mu_%d' % ii].values for ii in range(8)]).T
x_sd_kal = np.array([noise_nmll.loc[:, 'std_%d' % ii].values for ii in range(8)]).T
all_subjs = get_means_stdev(
x_mu_rbf, x_sd_rbf, x_mu_lin, x_sd_lin, x_mu_kal, x_sd_kal,
rewards, lin_gp_data, rbf_gp_data, noise_nmll
)
all_subjs.to_pickle('Data/exp_shifted/bayes_gp_exp_shifted.pkl')
def exp_cp():
lin_gp_data = pd.read_csv('Data/exp_changepoint/changelinpred.csv')
lin_gp_data.index = range(len(lin_gp_data))
rbf_gp_data = pd.read_csv('Data/exp_changepoint/changerbfpred.csv')
rbf_gp_data.index = range(len(rbf_gp_data))
raw_data = pd.read_csv('Data/exp_changepoint/changepoint.csv')
rewards = raw_data['out'].values
noise_nmll = get_noise_nmll(raw_data_path='Data/exp_changepoint/changepoint.csv')
# drop subjects for which the RBF failed to converge
subjects_to_drop = set()
for s in set(noise_nmll.Subject):
if s not in set(rbf_gp_data.id):
subjects_to_drop.add(s)
for s in subjects_to_drop:
lin_gp_data = lin_gp_data[lin_gp_data.id != s].copy()
noise_nmll = noise_nmll[noise_nmll.Subject != s].copy()
x_mu_lin = np.array([lin_gp_data.loc[:, 'mu_%d' % ii].values for ii in range(8)]).T
x_sd_lin = np.array([lin_gp_data.loc[:, 'sigma_%d' % ii].values for ii in range(8)]).T
x_mu_rbf = np.array([rbf_gp_data.loc[:, 'mu_%d' % ii].values for ii in range(8)]).T
x_sd_rbf = np.array([rbf_gp_data.loc[:, 'sigma_%d' % ii].values for ii in range(8)]).T
x_mu_kal = np.array([noise_nmll.loc[:, 'mu_%d' % ii].values for ii in range(8)]).T
x_sd_kal = np.array([noise_nmll.loc[:, 'std_%d' % ii].values for ii in range(8)]).T
all_subjs = get_means_stdev(
x_mu_rbf, x_sd_rbf, x_mu_lin, x_sd_lin, x_mu_kal, x_sd_kal,
rewards, lin_gp_data, rbf_gp_data, noise_nmll
)
all_subjs.to_pickle('Data/exp_changepoint/bayes_gp_exp_cp.pkl')
def exp_srs():
lin_gp_data = pd.read_csv('Data/exp_srs/gplinsrs.csv')
lin_gp_data.index = range(len(lin_gp_data))
rbf_gp_data = pd.read_csv('Data/exp_srs/gprbfsrs.csv')
rbf_gp_data.index = range(len(rbf_gp_data))
raw_data = pd.read_csv('Data/exp_srs/datasrs.csv')
rewards = raw_data['out'].values
noise_nmll = get_noise_nmll(raw_data_path='Data/exp_srs/datasrs.csv')
# drop subjects for which the RBF failed to converge
subjects_to_drop = set()
for s in set(noise_nmll.Subject):
if s not in set(rbf_gp_data.id):
subjects_to_drop.add(s)
for s in subjects_to_drop:
lin_gp_data = lin_gp_data[lin_gp_data.id != s].copy()
noise_nmll = noise_nmll[noise_nmll.Subject != s].copy()
x_mu_lin = np.array([lin_gp_data.loc[:, 'mu%d' % ii].values for ii in range(8)]).T
x_sd_lin = np.array([lin_gp_data.loc[:, 'sigma%d' % ii].values for ii in range(8)]).T
x_mu_rbf = np.array([rbf_gp_data.loc[:, 'mu%d' % ii].values for ii in range(8)]).T
x_sd_rbf = np.array([rbf_gp_data.loc[:, 'sigma%d' % ii].values for ii in range(8)]).T
x_mu_kal = np.array([noise_nmll.loc[:, 'mu_%d' % ii].values for ii in range(8)]).T
x_sd_kal = np.array([noise_nmll.loc[:, 'std_%d' % ii].values for ii in range(8)]).T
all_subjs = get_means_stdev(
x_mu_rbf, x_sd_rbf, x_mu_lin, x_sd_lin, x_mu_kal, x_sd_kal,
rewards, lin_gp_data, rbf_gp_data, noise_nmll
)
all_subjs.to_pickle('Data/exp_srs/bayes_gp_exp_srs.pkl')
def exp_scrambled():
lin_gp_data = pd.read_csv('Data/exp_scrambled/gplinscrambled.csv')
lin_gp_data.index = range(len(lin_gp_data))
rbf_gp_data = pd.read_csv('Data/exp_scrambled/gprbfscrambled.csv')
rbf_gp_data.index = range(len(rbf_gp_data))
raw_data = pd.read_csv('Data/exp_scrambled/datascrambled.csv')
rewards = raw_data['out'].values
noise_nmll = get_noise_nmll(raw_data_path='Data/exp_scrambled/datascrambled.csv')
# drop subjects for which the RBF failed to converge
subjects_to_drop = set()
for s in set(noise_nmll.Subject):
if s not in set(rbf_gp_data.id):
subjects_to_drop.add(s)
for s in subjects_to_drop:
lin_gp_data = lin_gp_data[lin_gp_data.id != s].copy()
noise_nmll = noise_nmll[noise_nmll.Subject != s].copy()
x_mu_lin = np.array([lin_gp_data.loc[:, 'mu_%d' % ii].values for ii in range(8)]).T
x_sd_lin = np.array([lin_gp_data.loc[:, 'std_%d' % ii].values for ii in range(8)]).T
x_mu_rbf = np.array([rbf_gp_data.loc[:, 'mu_%d' % ii].values for ii in range(8)]).T
x_sd_rbf = np.array([rbf_gp_data.loc[:, 'std_%d' % ii].values for ii in range(8)]).T
x_mu_kal = np.array([noise_nmll.loc[:, 'mu_%d' % ii].values for ii in range(8)]).T
x_sd_kal = np.array([noise_nmll.loc[:, 'std_%d' % ii].values for ii in range(8)]).T
all_subjs = get_means_stdev(
x_mu_rbf, x_sd_rbf, x_mu_lin, x_sd_lin, x_mu_kal, x_sd_kal,
rewards, lin_gp_data, rbf_gp_data, noise_nmll
)
all_subjs.to_pickle('Data/exp_scrambled/bayes_gp_exp_scram.pkl')
if __name__ == "__main__":
exp_lin()
exp_shifted()
exp_cp()
exp_srs()
exp_scrambled()