-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.Rhistory
More file actions
512 lines (512 loc) · 24.8 KB
/
.Rhistory
File metadata and controls
512 lines (512 loc) · 24.8 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
side.pred = get.ys.control( side.cont, betas, side.mod, side )
side.out = side.pred %>% group_by(pred) %>%
summarise( y = mean(y) )
f_size = 12
lwds = 2
alph = 7/10
s.p = ggplot(side.out, aes( x = pred, y = y ) ) +
geom_point() + geom_line(size=lwds, alpha = alph, group=1, col=wesanderson::wes_palette("IsleofDogs1")[6]) +
ylab("Pred Y") + xlab("Target | Action side") +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
text=element_text(size=f_size),
axis.text.x = element_text(size=f_size),
axis.text.y = element_text(size=f_size),
legend.position=c(0.85, 0.75))
s.p
side.out = side.pred %>% group_by(pred) %>%
summarise( y = mean(y) )
f_size = 12
lwds = 2
alph = 7/10
s.p = ggplot(side.out, aes( x = pred, y = y ) ) +
geom_point() + geom_line(size=lwds, alpha = alph, group=1, col=wesanderson::wes_palette("IsleofDogs1")[6]) +
ylab("Pred Y") + xlab("Target | Action side") +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
text=element_text(size=f_size),
axis.text.x = element_text(size=f_size),
axis.text.y = element_text(size=f_size),
legend.position=c(0.85, 0.75))
s.p
? rbeta
# define the model parameters
# I have chosen to randomly sample them, given the prior distributions, taken from Ouder et al, 2013, Neuron
experience_decay_factor = rbeta(1, 1.2, 1.2)
payoff_decay_factor = rbeta(1, 1.2, 1.2)
inverse_temperature = rnorm(1, 0, 10)
? sample
outcomes = as.matrix(c(outcome_probs_blue, outcome_probs_orange), nrow = 2)
outcome_probs_blue = c(.8, .2) # blue is good
outcome_probs_orange = c(.2, .8) # orange is bad
outcomes = as.matrix(c(outcome_probs_blue, outcome_probs_orange), nrow = 2)
outcomes
outcomes = as.matrix(c(outcome_probs_blue, outcome_probs_orange), nrow = 2, ncol = 2)
? as.matrix
outcomes = matrix(c(outcome_probs_blue, outcome_probs_orange), nrow = 2, ncol = 2)
outcomes
? sample
# assuming first choice is 50:50, 0 is blue, 1 is orange
choice[i] = sample(choices, 1, c(.5, .5) )
# define weightings of probability of good or bad outcome for each choice
# assuming choice between two stimuli, a 'blue' and an 'orange'
choices = c("blue", "orange")
i = 1
# assuming first choice is 50:50, 0 is blue, 1 is orange
choice[i] = sample(choices, 1, c(.5, .5) )
# assuming first choice is 50:50, 0 is blue, 1 is orange
choice[i] = sample(choices, 1, prob = c(.5, .5) )
choice = c() # empty vector for choices
# assuming first choice is 50:50, 0 is blue, 1 is orange
choice[i] = sample(choices, 1, prob = c(.5, .5) )
choice[i]
get_trial_outcome <- function( choice, choice_probs ){
# sample the reward distribution lying beneath the choice,
if (choice == "blue"){
idx = 1
} else if (choice == "orange"){
idx = 2
}
outcome = sample(c(1, 0), 1, probs = choice_probs[idx, ])
}
outcomes = c() # empty vector for outcomes
# get trial outcome
outcomes[i] = get_trial_outcome(choice[i], outcomes)
? sample
get_trial_outcome <- function( choice, choice_probs ){
# sample the reward distribution lying beneath the choice,
if (choice == "blue"){
idx = 1
} else if (choice == "orange"){
idx = 2
}
outcome = sample(c(1, 0), 1, prob = choice_probs[idx, ])
}
# get trial outcome
outcomes[i] = get_trial_outcome(choice[i], outcomes)
outcomes
choice == "blue"
choice = sample(c("blue", "orange"), 1, prob = c(.5, .5))
choice
choice = sample(c("blue", "orange"), 10, prob = c(.5, .5))
choice = sample(c("blue", "orange"), 10, replace = TRUE, prob = c(.5, .5))
choice
choice == "orange"
which(choice == "orange")
max(which(idx))
max(which(choice == "orange"))
# compute ewa & action selection
tmp = values(c(max(which(choice == "blue")), max(which(choice == "orange"))
)
)
values
values = c()
# compute ewa & action selection
tmp = values(c(max(which(choice == "blue")), max(which(choice == "orange"))))
# compute ewa & action selection
tmp = values[c(max(which(choice == "blue")), max(which(choice == "orange")))]
tmp
? is.null
values[i] = .5 # because it is 50/50 at this point
# compute ewa & action selection
tmp = values[c(max(which(choice == "blue")), max(which(choice == "orange")))] # this gets the last value for each choice
tmp
choice = c() # empty vector for choices
values = c()
start_values = c(0.5, 0.5) # initialise with a value of 0.5 for each value (50:50) they'll give reward
outcomes = c() # empty vector for outcomes
ntrials = 50 # will do 50 before switching
i
# assuming first choice is 50:50, 0 is blue, 1 is orange
choice[i] = sample(choices, 1, prob = c(.5, .5) )
values[i] = .5 # because it is 50/50 at this point
choice
values
choice
max(which(choice == "orange")))
max(which(choice == "orange"))
max(which(choice == "blue"))
values[c(max(which(choice == "blue")), max(which(choice == "orange")))]
# compute ewa & action selection
tmp = values[c(max(which(choice == "blue")), max(which(choice == "orange")))] # this gets the last value for each choice
is.na(tmp)
tmp[is.na(tmp)] = .5 # because not assigned a value yet
tmp
outcome
outcomes
# get trial outcome
outcomes[i] = get_trial_outcome(choice[i], outcomes)
outcomes
i = 2
which(choices == choice[i-1])
weight_for_update = tmp[ which(choices == choice[i-1]) ]
tmp
wight_for_update
weight_for_update
# this code is written for readability over effieciency
rm(list=ls())
ewa_update <- function(experience_decay_factor, payoff_decay_factor, last_experience_weight, last_choice_value, previous_outcome){
# given an experience decay factor, and a payoff_decay _factor, compute the
# experience weight and the value for the choice, given the value, weight and outcome
# for the previous choice
experience_weight = last_experience_weight * experience_decay_factor + 1
choice_value = (last_choice_value * payoff_decay_factor * last_experience_weight + previous_outcome) / experience_weight
choice_value
}
action_selection <- function( choice_value, other_choice_value, inverse_temperature ){
# compute the probability of choice, given the value for that choice, the value for the other choice, and the
# inverse temperature parameter -
probability_choice = exp( beta*choice_value ) / sum( exp( beta*choice_value ), exp( beta*other_choice_value ) )
probability_choice
}
get_trial_outcome <- function( choice, choice_probs ){
# sample the reward distribution lying beneath the choice,
if (choice == "blue"){
idx = 1
} else if (choice == "orange"){
idx = 2
}
outcome = sample(c(1, 0), 1, prob = choice_probs[idx, ])
}
experience_decay_factor = rbeta(1, 1.2, 1.2)
payoff_decay_factor = rbeta(1, 1.2, 1.2)
inverse_temperature = rnorm(1, 0, 10)
# define weightings of probability of good or bad outcome for each choice
# assuming choice between two stimuli, a 'blue' and an 'orange'
choices = c("blue", "orange")
outcome_probs_blue = c(.8, .2) # blue is good
outcome_probs_orange = c(.2, .8) # orange is bad
outcomes = matrix(c(outcome_probs_blue, outcome_probs_orange), nrow = 2, ncol = 2)
# set up empty vectors to collect generated data
choice = c() # empty vector for choices
action_values = c()
experience_weights = c()
outcomes = c() # empty vector for outcomes
ntrials = 50 # will do 50 before switching
ewa_update(experience_decay_factor, payoff_decay_factor, weight_for_update, )
rm(list=ls())
###############################################################################################################################
# define model functions
ewa_update <- function(experience_decay_factor, payoff_decay_factor, last_experience_weight, last_choice_value, previous_outcome){
# given an experience decay factor, and a payoff_decay _factor, compute the
# experience weight and the value for the choice, given the value, weight and outcome
# for the previous choice
experience_weight = last_experience_weight * experience_decay_factor + 1
choice_value = (last_choice_value * payoff_decay_factor * last_experience_weight + previous_outcome) / experience_weight
choice_value
}
action_selection <- function( choice_value, other_choice_value, inverse_temperature ){
# compute the probability of choice, given the value for that choice, the value for the other choice, and the
# inverse temperature parameter -
probability_choice = exp( beta*choice_value ) / sum( exp( beta*choice_value ), exp( beta*other_choice_value ) )
probability_choice
}
get_trial_outcome <- function( choice, choice_probs ){
# sample the reward distribution lying beneath the choice,
if (choice == "blue"){
idx = 1
} else if (choice == "orange"){
idx = 2
}
outcome = sample(c(1, 0), 1, prob = choice_probs[idx, ])
}
experience_decay_factor = rbeta(1, 1.2, 1.2)
payoff_decay_factor = rbeta(1, 1.2, 1.2)
inverse_temperature = rnorm(1, 0, 10)
choices = c("blue", "orange")
outcome_probs_blue = c(.8, .2) # blue is good
outcome_probs_orange = c(.2, .8) # orange is bad
outcomes = matrix(c(outcome_probs_blue, outcome_probs_orange), nrow = 2, ncol = 2)
choice = c() # empty vector for choices
action_values = c()
experience_weights = c()
rm(action_values)
choice_values = c()
experience_weights = c()
outcomes = c() # empty vector for outcomes
ntrials = 50 # will do 50 before switching
i = 1
# assuming first choice is 50:50, 0 is blue, 1 is orange
choice[i] = sample(choices, 1, prob = c(.5, .5) )
choice
action_values[i] = .5 # because it is 50/50 at this point
choice_values[i] = .5 # because it is 50/50 at this point
experience_weights[i] = .5
i = 2
tmp_choice_values = values[c(max(which(choice == "blue")), max(which(choice == "orange")))]
choice_values[c(max(which(choice == "blue")), max(which(choice == "orange")))]
tmp_choice_values = choice_values[c(max(which(choice == "blue")), max(which(choice == "orange")))] #
tmp[is.na(tmp)] = .5
tmp_choice_value[is.na(tmp)] = .5
tmp_choice_values[is.na(tmp)] = .5 # just in case one has not assigned a value yet
tmp_choice_values[is.na(tmp_choice_value)] = .5 # just in case one has not assigned a value yet
tmp_choice_values[is.na(tmp_choice_values)] = .5 # just in case one has not assigned a value yet
tmp_choice_values
tmp_experience_weights = experience_weights[ c(max(which(choice == "blue")), max(which(choice == "orange"))) ]
tmp_experience_weights[is.na(tmp_experience_weights)] = .5
tmp_experience_weights
# convert each of these into the new weight value (i.e. apply update rule to last chosen, but pass through next value)
choice_value_for_update = tmp[ which(choices == choice[i-1]) ]
# convert each of these into the new weight value (i.e. apply update rule to last chosen, but pass through next value)
choice_value_for_update = tmp_choice_values[ which(choices == choice[i-1]) ]
choice_value_for_update
experience_weight_for_update = tmp_experience_weights[ which( choices == choice[i-1]) ]
new_choice_value = ewa_update( experience_decay_factor, payoff_decay_factor, experience_weight_for_update, choice_value_for_update)
new_choice_value = ewa_update( experience_decay_factor, payoff_decay_factor, experience_weight_for_update, choice_value_for_update, outcomes[i-1] )
new_choice_value
outcomes[i-1]
outcome
outcomes
outcome_probs = matrix(c(outcome_probs_blue, outcome_probs_orange), nrow = 2, ncol = 2)
# get trial outcome
outcomes[i] = get_trial_outcome(choice[i], outcome_probs)
rm(list=ls())
###############################################################################################################################
# define model functions
ewa_update <- function(experience_decay_factor, payoff_decay_factor, last_experience_weight, last_choice_value, previous_outcome){
# given an experience decay factor, and a payoff_decay _factor, compute the
# experience weight and the value for the choice, given the value, weight and outcome
# for the previous choice
experience_weight = last_experience_weight * experience_decay_factor + 1
choice_value = (last_choice_value * payoff_decay_factor * last_experience_weight + previous_outcome) / experience_weight
choice_value
}
action_selection <- function( choice_value, other_choice_value, inverse_temperature ){
# compute the probability of choice, given the value for that choice, the value for the other choice, and the
# inverse temperature parameter -
probability_choice = exp( beta*choice_value ) / sum( exp( beta*choice_value ), exp( beta*other_choice_value ) )
probability_choice
}
get_trial_outcome <- function( choice, choice_probs ){
# sample the reward distribution lying beneath the choice,
if (choice == "blue"){
idx = 1
} else if (choice == "orange"){
idx = 2
}
outcome = sample(c(1, 0), 1, prob = choice_probs[idx, ])
}
experience_decay_factor = rbeta(1, 1.2, 1.2)
payoff_decay_factor = rbeta(1, 1.2, 1.2)
inverse_temperature = rnorm(1, 0, 10)
# define weightings of probability of good or bad outcome for each choice
# assuming choice between two stimuli, a 'blue' and an 'orange'
choices = c("blue", "orange")
outcome_probs_blue = c(.8, .2) # blue is good
outcome_probs_orange = c(.2, .8) # orange is bad
outcome_probs = matrix(c(outcome_probs_blue, outcome_probs_orange), nrow = 2, ncol = 2)
choice = c() # empty vector for choices
choice_values = c()
experience_weights = c()
outcomes = c() # empty vector for outcomes
ntrials = 50 # will do 50 before switching
# assuming first choice is 50:50, 0 is blue, 1 is orange
choice[i] = sample(choices, 1, prob = c(.5, .5) )
i = 1
# assuming first choice is 50:50, 0 is blue, 1 is orange
choice[i] = sample(choices, 1, prob = c(.5, .5) )
choice_values[i] = .5 # because it is 50/50 at this point
experience_weights[i] = .5
# get trial outcome
outcomes[i] = get_trial_outcome(choice[i], outcome_probs)
outcomes
i = 2
# compute ewa & action selection
tmp_choice_values = choice_values[c(max(which(choice == "blue")), max(which(choice == "orange")))] # this gets the last value for each choice (don't worry about the warning msg)
tmp_choice_values[is.na(tmp_choice_values)] = .5 # just in case one has not assigned a value yet
tmp_experience_weights = experience_weights[ c(max(which(choice == "blue")), max(which(choice == "orange"))) ]
tmp_experience_weights[is.na(tmp_experience_weights)] = .5
# convert each of these into the new weight value (i.e. apply update rule to last chosen, but pass through next value)
choice_value_for_update = tmp_choice_values[ which(choices == choice[i-1]) ]
experience_weight_for_update = tmp_experience_weights[ which( choices == choice[i-1]) ]
new_choice_value = ewa_update( experience_decay_factor, payoff_decay_factor, experience_weight_for_update, choice_value_for_update, outcomes[i-1] )
new_choice_value
# assign to the choice values to be compared for action generation
tmp_choice_values[ which(choices == choice[i-1]) ] = new_choice_value
tmp_choice_values
# feed in to get action probabilities
this_trials_action_probs = c( action_selection( tmp_choice_values[1], tmp_choice_values[2], inverse_temperature ) )
rm(list=ls())
###############################################################################################################################
# define model functions
ewa_update <- function(experience_decay_factor, payoff_decay_factor, last_experience_weight, last_choice_value, previous_outcome){
# given an experience decay factor, and a payoff_decay _factor, compute the
# experience weight and the value for the choice, given the value, weight and outcome
# for the previous choice
experience_weight = last_experience_weight * experience_decay_factor + 1
choice_value = (last_choice_value * payoff_decay_factor * last_experience_weight + previous_outcome) / experience_weight
choice_value
}
action_selection <- function( choice_value, other_choice_value, inverse_temperature ){
# compute the probability of choice, given the value for that choice, the value for the other choice, and the
# inverse temperature parameter -
probability_choice = exp( inverse_temperature*choice_value ) / sum( exp( inverse_temperature*choice_value ), exp( inverse_temperature*other_choice_value ) )
probability_choice
}
get_trial_outcome <- function( choice, choice_probs ){
# sample the reward distribution lying beneath the choice,
if (choice == "blue"){
idx = 1
} else if (choice == "orange"){
idx = 2
}
outcome = sample(c(1, 0), 1, prob = choice_probs[idx, ])
}
experience_decay_factor = rbeta(1, 1.2, 1.2)
payoff_decay_factor = rbeta(1, 1.2, 1.2)
inverse_temperature = rnorm(1, 0, 10)
# define weightings of probability of good or bad outcome for each choice
# assuming choice between two stimuli, a 'blue' and an 'orange'
choices = c("blue", "orange")
outcome_probs_blue = c(.8, .2) # blue is good
outcome_probs_orange = c(.2, .8) # orange is bad
outcome_probs = matrix(c(outcome_probs_blue, outcome_probs_orange), nrow = 2, ncol = 2)
# set up empty vectors to collect generated data
choice = c() # empty vector for choices
choice_values = c()
experience_weights = c()
outcomes = c() # empty vector for outcomes
ntrials = 50 # will do 50 before switching
i = 1
choice[i] = sample(choices, 1, prob = c(.5, .5) )
choice_values[i] = .5 # because it is 50/50 at this point
experience_weights[i] = .5
# get trial outcome
outcomes[i] = get_trial_outcome(choice[i], outcome_probs)
choice
choice_values
experience_weights
outcomes
i = 2
# compute ewa & action selection
tmp_choice_values = choice_values[c(max(which(choice == "blue")), max(which(choice == "orange")))] # this gets the last value for each choice (don't worry about the warning msg)
tmp_choice_values[is.na(tmp_choice_values)] = .5 # just in case one has not assigned a value yet
tmp_choice_values
tmp_experience_weights = experience_weights[ c(max(which(choice == "blue")), max(which(choice == "orange"))) ]
tmp_experience_weights[is.na(tmp_experience_weights)] = .5
# convert each of these into the new weight value (i.e. apply update rule to last chosen, but pass through next value)
choice_value_for_update = tmp_choice_values[ which(choices == choice[i-1]) ]
experience_weight_for_update = tmp_experience_weights[ which( choices == choice[i-1]) ]
new_choice_value = ewa_update( experience_decay_factor, payoff_decay_factor, experience_weight_for_update, choice_value_for_update, outcomes[i-1] )
# assign to the choice values to be compared for action generation
tmp_choice_values[ which(choices == choice[i-1]) ] = new_choice_value
tmp_choice_values
# feed in to get action probabilities
this_trials_action_probs = c( action_selection( tmp_choice_values[1], tmp_choice_values[2], inverse_temperature ) )
this_trials_action_probs
# feed in to get action probabilities
this_trials_action_probs = c( action_selection( tmp_choice_values[1], tmp_choice_values[2], inverse_temperature ), action_selection( tmp_choice_values[2], tmp_choice_values[1], inverse_temperature ) )
this_trials_action_probs
choice[i] = sample(choices, 1, prob = this_trials_action_probs)
choice
for (i in 1:ntrials)
if (i == 1){
# assuming first choice is 50:50, 0 is blue, 1 is orange
choice[i] = sample(choices, 1, prob = c(.5, .5) )
choice_values[i] = .5 # because it is 50/50 at this point
experience_weights[i] = .5
}
else {
# compute ewa & action selection
tmp_choice_values = choice_values[c(max(which(choice == "blue")), max(which(choice == "orange")))] # this gets the last value for each choice (don't worry about the warning msg)
tmp_choice_values[is.na(tmp_choice_values)] = .5 # just in case one has not assigned a value yet
tmp_experience_weights = experience_weights[ c(max(which(choice == "blue")), max(which(choice == "orange"))) ]
tmp_experience_weights[is.na(tmp_experience_weights)] = .5
# convert each of these into the new weight value (i.e. apply update rule to last chosen, but pass through next value)
choice_value_for_update = tmp_choice_values[ which(choices == choice[i-1]) ]
experience_weight_for_update = tmp_experience_weights[ which( choices == choice[i-1]) ]
new_choice_value = ewa_update( experience_decay_factor, payoff_decay_factor, experience_weight_for_update, choice_value_for_update, outcomes[i-1] )
# assign to the choice values to be compared for action generation
tmp_choice_values[ which(choices == choice[i-1]) ] = new_choice_value
# feed in to get action probabilities
this_trials_action_probs = c( action_selection( tmp_choice_values[1], tmp_choice_values[2], inverse_temperature ), action_selection( tmp_choice_values[2], tmp_choice_values[1], inverse_temperature ) )
choice[i] = sample(choices, 1, prob = this_trials_action_probs)
}
# get trial outcome
outcomes[i] = get_trial_outcome(choice[i], outcome_probs)
}
rm(list=ls())
###############################################################################################################################
# define model functions
ewa_update <- function(experience_decay_factor, payoff_decay_factor, last_experience_weight, last_choice_value, previous_outcome){
# given an experience decay factor, and a payoff_decay _factor, compute the
# experience weight and the value for the choice, given the value, weight and outcome
# for the previous choice
experience_weight = last_experience_weight * experience_decay_factor + 1
choice_value = (last_choice_value * payoff_decay_factor * last_experience_weight + previous_outcome) / experience_weight
choice_value
}
action_selection <- function( choice_value, other_choice_value, inverse_temperature ){
# compute the probability of choice, given the value for that choice, the value for the other choice, and the
# inverse temperature parameter -
probability_choice = exp( inverse_temperature*choice_value ) / sum( exp( inverse_temperature*choice_value ), exp( inverse_temperature*other_choice_value ) )
probability_choice
}
get_trial_outcome <- function( choice, choice_probs ){
# sample the reward distribution lying beneath the choice,
if (choice == "blue"){
idx = 1
} else if (choice == "orange"){
idx = 2
}
outcome = sample(c(1, 0), 1, prob = choice_probs[idx, ])
}
###############################################################################################################################
# now define parameters for simulation
###############################################################################################################################
# define the model parameters
# I have chosen to randomly sample them, given the prior distributions, taken from Ouder et al, 2013, Neuron
experience_decay_factor = rbeta(1, 1.2, 1.2)
payoff_decay_factor = rbeta(1, 1.2, 1.2)
inverse_temperature = rnorm(1, 0, 10)
# define weightings of probability of good or bad outcome for each choice
# assuming choice between two stimuli, a 'blue' and an 'orange'
choices = c("blue", "orange")
outcome_probs_blue = c(.8, .2) # blue is good
outcome_probs_orange = c(.2, .8) # orange is bad
outcome_probs = matrix(c(outcome_probs_blue, outcome_probs_orange), nrow = 2, ncol = 2)
# set up empty vectors to collect generated data
choice = c() # empty vector for choices
choice_values = c()
experience_weights = c()
outcomes = c() # empty vector for outcomes
ntrials = 50 # will do 50 before switching
###############################################################################################################################
# run simulation
###############################################################################################################################
for (i in 1:ntrials){
if (i == 1){
# assuming first choice is 50:50, 0 is blue, 1 is orange
choice[i] = sample(choices, 1, prob = c(.5, .5) )
choice_values[i] = .5 # because it is 50/50 at this point
experience_weights[i] = .5
}
else {
# compute ewa & action selection
tmp_choice_values = choice_values[c(max(which(choice == "blue")), max(which(choice == "orange")))] # this gets the last value for each choice (don't worry about the warning msg)
tmp_choice_values[is.na(tmp_choice_values)] = .5 # just in case one has not assigned a value yet
tmp_experience_weights = experience_weights[ c(max(which(choice == "blue")), max(which(choice == "orange"))) ]
tmp_experience_weights[is.na(tmp_experience_weights)] = .5
# convert each of these into the new weight value (i.e. apply update rule to last chosen, but pass through next value)
choice_value_for_update = tmp_choice_values[ which(choices == choice[i-1]) ]
experience_weight_for_update = tmp_experience_weights[ which( choices == choice[i-1]) ]
new_choice_value = ewa_update( experience_decay_factor, payoff_decay_factor, experience_weight_for_update, choice_value_for_update, outcomes[i-1] )
# assign to the choice values to be compared for action generation
tmp_choice_values[ which(choices == choice[i-1]) ] = new_choice_value
# feed in to get action probabilities
this_trials_action_probs = c( action_selection( tmp_choice_values[1], tmp_choice_values[2], inverse_temperature ), action_selection( tmp_choice_values[2], tmp_choice_values[1], inverse_temperature ) )
choice[i] = sample(choices, 1, prob = this_trials_action_probs)
}
# get trial outcome
outcomes[i] = get_trial_outcome(choice[i], outcome_probs)
}
outcomes
choices
choice
###############################################################################################################################
# make into a dataframe and save to wideform for further use
###############################################################################################################################
data <- data.frame(choices = choice, outcomes = outcomes)
write.csv(data, "example.csv")
setwd("~/Dropbox/learning_model_sims")
write.csv(data, "example.csv")