-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_critic_detailed_recording.jl
2453 lines (2127 loc) · 101 KB
/
multi_critic_detailed_recording.jl
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#TODO: try the simplified representation switcher as our critic learner
# the backprop looks like it may not work, need to see if this is just
# an issue with backprop or if our system has larger learning problems
######## External requirements ###########
using Distributions
using PyPlot,PyCall
#using Grid
#@pyimport seaborn as sns
#sns.set(font_scale=1.5)
#sns.set_context("poster")
########## Parameters #############
include("parameters_critic_simulations.jl");
######### Data Storage ##############
# RovingExperiment, Subject, Block and Trial defined externally
include("high_dim_array2.jl");
########## Main simulation functions #############
if !use_hard_coded_critic
# include("backprop_two_layer.jl")
# using backprop_two_layer
include("slowly_separating_representations.jl")
# using slowly_separating_representations
end
#include("detailed_simulation_code_herzog12.jl")
include("detailed_simulation_code_multi_critic.jl");
function reload_source()
include("multi_critic_detailed_recording.jl");
end
########################
# 10 subjects are examined
# 14 blocks are performed per subject
# 1 block is 80 trials
# first experiment: only doing task/problem 1 learning
# second experiment: switching between task difficulties is interleaved (randomly or in alternation??)
# these are the trial inputs
function generate_test_sequence(seq_length::Int64)
# linspace sequence:
#x = linspace(-1,1,seq_length);
# points are uniform randomly distributed:
if(use_cts_random_inputs)
x = rand(Uniform(problem_left_bound,problem_right_bound), seq_length);
end
# alternating +/-1 sequence
if(use_binary_alternating_inputs)
x = zeros(seq_length,1);
for i=1:seq_length
x[i] = -(-1)^i;
end
end
# randomly alternating +/-1 sequence
if(use_binary_random_inputs)
x = zeros(seq_length,1);
choice = rand(Uniform(0,1), seq_length);
for i = 1:seq_length
x[i] = (choice[i] > (0.5 + input_sequence_bias) ? -1.0 : 1.0)
end
end
# input interval is divided into two blocks, which meet at 0
# within each block distributions are uniform random
# but membership of each block is decided via biased random coin
# assume problem left and right bounds are +/-1 for now
if (use_biased_cts_random_inputs)
x = zeros(seq_length,1);
half_interval_membership = rand(Uniform(0,1), seq_length);
within_interval_x_value = rand(Uniform(0,1),seq_length);
for i = 1:seq_length
x[i] = (half_interval_membership[i] > (0.5 + input_sequence_bias) ? (-within_interval_x_value[i]) : (within_interval_x_value[i]) );
end
end
return x;
end
# this chooses whether a trial is of type 1 or type 2
function generate_task_sequence(seq_length::Int64)
# alternating false/true sequence
# x = Array(Bool, seq_length);
# for(i = 1:seq_length)
# x[i] = (i%2==0)
# end
# random arrangement of 1/2
x = Array{Int}(seq_length);
choice = rand(Uniform(0,1), seq_length);
for i = 1:seq_length
x[i] = (choice[i] < (0.5 + task_sequence_bias) ? 1 : 2)
end
return x;
end
function perform_learning_block_single_problem(task_id::Int, tuning_type::TuningSelector, block_dat::Block)
# generate 80 trial values for x
# loop through x: update_noise, update_weights
if(verbosity > 2)
#DEBUG reward:
# these variables are used as a back-communication channel from reward() and update_weights()
# in order to provide debugging output at the end of this function
global instance_correct = 0;
global instance_incorrect = 0;
global instance_reward = zeros(no_trials_in_block,1)
global instance_average_reward = zeros(no_trials_in_block,1)
global instance_choice = zeros(no_trials_in_block, 1)
global instance_correct_direction = zeros(no_trials_in_block, 1)
# end DEBUG
end
x = generate_test_sequence(no_trials_in_block);
# Warning: the size of the following should equal the number of distinctly identifiable
# sub-tasks
proportion_sub_task_correct = zeros(no_classifications_per_task);
sub_task_count = zeros(no_classifications_per_task);
monitor_reward = 0;
global average_reward;
global n_critic;
global average_block_reward = 0.0
global average_task_reward;
global n_post;
global average_post;
average_task_reward = zeros(no_input_tasks);
if(reset_average_reward_on_each_block)
for i = 1:no_task_critics
for j = 1:no_choices_per_task_critics
average_reward[i,j] = 0.;
n_critic[i,j] = 0;
end
end
end
if(use_reset_average_post_on_each_block)
n_post = 0;
for i = 1:no_post_neurons
average_post[i] = 0.;
end
end
if(reset_decision_criterion_monitor_on_each_block)
global decision_criterion_monitor = zeros(no_decision_monitors,1); #0.5;
end
global average_delta_reward = 0;
global average_choice = 0.0;
global n_within_block = 0;
global n_task_within_block = zeros(Int, no_input_tasks);
local_average_threshold = 0.0;
local_average_task_threshold = zeros(no_input_tasks);
local_average_decision_criterion_monitor = zeros(no_decision_monitors,1); #0.0;
#for(xi in x)
for i = 1:no_trials_in_block
update_noise()
local_reward = (update_weights(x[i], task_id, tuning_type, block_dat.trial[i]) / 2);
monitor_reward += local_reward;
local_monitor_task_id = min(task_id, no_decision_monitors);
local_average_decision_criterion_monitor[local_monitor_task_id] += decision_criterion_monitor[local_monitor_task_id];
# adding a monitor of sub-task performance (eg. L, R distinction)
# correct_answer contains +/- 1, need to correct for array indexing
# the cast/round to int splits at 1.5 between outputs 1 and 2
# This is one of the places where a non-zero classification offset should be corrected
# for (if ever implemented).
sub_task_id = round(Int, (block_dat.trial[i].correct_answer / 2.0) + 1.5) :: Int;
if(sub_task_id == 0)
print("correct answer: ", block_dat.trial[i].correct_answer)
end
# print(" sub_task_id: $sub_task_id\n")
sub_task_count[sub_task_id] += 1;
proportion_sub_task_correct[sub_task_id] += local_reward; # local_reward = {0,1}
if(perform_detection_threshold)
local_average_threshold += block_dat.trial[i].error_threshold;
local_average_task_threshold[task_id] += block_dat.trial[i].error_threshold;
end
if(verbosity > 0)
print("\n")
end
if (use_fixed_external_bias)
# this is where the external bias gets applied
bias_task_critic_id = 1;
if(no_task_critics > 1) # currently only cope with two task critics in the actual bias
bias_task_critic_id = ( (task_id % 2) == 0 ? 1 : 2) # bias is applied to the other task critic, so in mulit-critic setup no confusion can occur
end
# for probabilistic running of tasks
local_c = 0.5 + task_sequence_bias;
if (local_c != 1)
local_d = (local_c / (1-local_c));
else
print("Error: you want an infinite amount of hidden tasks for every one visible tasks\n");
exit(0);
end
#print("DEBUG: local_c $local_c, ratio of tasks 1:$local_d\n");
while(rand(Uniform(0,1)) < local_d)
# update running average of reward with bias_value, in task_critic_id, with choice_critic_id=1 since
# we don't want to start worrying here about which choice_critic_id should receive bias
multi_critic_running_av_reward(fixed_external_bias_value, bias_task_critic_id, 1)
#print("$local_d\n");
local_d -= 1;
end
end
end
proportion_correct = monitor_reward / no_trials_in_block;
proportion_sub_task_correct = proportion_sub_task_correct ./ sub_task_count;
local_average_decision_criterion_monitor /= no_trials_in_block; # single task in non-roved case
if(perform_detection_threshold)
local_average_threshold /= no_trials_in_block;
local_average_task_threshold[task_id] = local_average_task_threshold[task_id] ./ no_trials_in_block;
end
#global wfinal = deepcopy(w)
if(verbosity > 2)
# Note this changes how the final proportion_correct is calculated!
print("END of Learning Block, proportion correct: $proportion_correct, task_id: $task_id\n")
proportion_correct = instance_correct / (instance_correct + instance_incorrect);
print("DEBUG: instance_correct: $instance_correct, instance_incorrect: $instance_incorrect, new proportion correct: $proportion_correct\n")
end
block_dat.proportion_correct = proportion_correct;
#block_dat.proportion_task_correct[task_id] = proportion_correct;
# we'll store separate left and right task choices in this variable for single Task protocol,
# this is a hack but one which obviates the need for more storage variables
block_dat.proportion_task_correct = proportion_sub_task_correct;
block_dat.average_choice = average_choice;
block_dat.average_decision_criterion_monitor = deepcopy(local_average_decision_criterion_monitor);
block_dat.average_reward = average_block_reward;
block_dat.average_task_reward = average_task_reward;
if(perform_detection_threshold)
block_dat.average_threshold = local_average_threshold;
block_dat.average_task_threshold = local_average_task_threshold;
end
# calculate and record noise-free output for the given task, for the extremal potential inputs
#task_id is fixed as this is a single task block
block_dat.noise_free_positive_output[task_id, 1] = noise_free_output_positive_difference(problem_left_bound, task_id, tuning_type);
block_dat.noise_free_positive_output[task_id, 2] = noise_free_output_positive_difference(problem_right_bound, task_id, tuning_type);
# calculate and record Probability(correct | task, input) for the given task, for the extremal potential inputs
block_dat.probability_correct[task_id, 1] = probability_correct(problem_left_bound, task_id, tuning_type);
block_dat.probability_correct[task_id, 2] = probability_correct(problem_right_bound, task_id, tuning_type);
# store critic learner reward prediction at end of each block
if (!use_hard_coded_critic)
block_dat.reward_prediction[1] = get_reward_prediction(1)[1];
block_dat.reward_prediction[2] = get_reward_prediction(2)[1];
end
#print("Block end decision_criterion_monitor: $decision_criterion_monitor \n")
return proportion_correct;
end
function perform_learning_block_trial_switching(tuning_type::TuningSelector, block_dat::Block)
# generate 80 trial values for x
# loop through x: update_noise, update_weights
if(verbosity > 2)
#DEBUG reward:
# these variables are used as a back-communication channel from reward() and update_weights()
# in order to provide debugging output at the end of this function
global instance_correct = 0;
global instance_incorrect = 0;
global instance_reward = zeros(no_trials_in_block,1)
global instance_average_reward = zeros(no_trials_in_block,1)
global instance_choice = zeros(no_trials_in_block, 1)
global instance_correct_direction = zeros(no_trials_in_block, 1)
# end DEBUG
end
x = generate_test_sequence(no_trials_in_block);
task = generate_task_sequence(no_trials_in_block);
proportion_task_correct = zeros(no_input_tasks);
task_count = zeros(no_input_tasks);
monitor_reward = 0;
global average_reward;
global n_critic;
global n_post;
global average_post;
if(reset_average_reward_on_each_block)
for i = 1:no_task_critics
for j = 1:no_choices_per_task_critics
average_reward[i,j] = 0.;
n_critic[i,j] = 0;
end
end
end
if(use_reset_average_post_on_each_block)
n_post = 0;
for i = 1:no_post_neurons
average_post[i] = 0.;
end
end
if(reset_decision_criterion_monitor_on_each_block)
global decision_criterion_monitor = zeros(no_decision_monitors,1); #0.5;
end
global average_delta_reward = 0;
global average_choice = 0.0;
global n_within_block = 0;
global n_task_within_block = zeros(Int, no_input_tasks);
global average_task_reward;
global average_block_reward = 0.0;
average_task_reward = zeros(no_input_tasks);
local_average_task_choice = zeros(no_input_tasks);
local_average_threshold = 0.0;
local_average_task_threshold = zeros(no_input_tasks);
local_average_decision_criterion_monitor = zeros(no_decision_monitors,1); #0.0;
for i = 1:no_trials_in_block
update_noise()
local_reward = (update_weights(x[i], task[i], tuning_type, block_dat.trial[i]) / 2);
monitor_reward += local_reward;
local_monitor_task_id = min(no_decision_monitors, task[i]);
local_average_decision_criterion_monitor[local_monitor_task_id] += decision_criterion_monitor[local_monitor_task_id];
task_count[task[i]] += 1;
if plotting_hack_to_have_separate_choices_in_roving_example
if task[i] == 2
local_actual_sub_task = round(Int, (x[i] / 2) + 1.5)
proportion_task_correct[local_actual_sub_task] += local_reward; # local_reward = {0,1}
end
else
proportion_task_correct[task[i]] += local_reward; # local_reward = {0,1}
end
local_average_task_choice[task[i]] += block_dat.trial[i].chosen_answer;
if(perform_detection_threshold)
local_average_threshold += block_dat.trial[i].error_threshold;
local_average_task_threshold[task[i]] += block_dat.trial[i].error_threshold;
end
if(verbosity > 0)
print("\n")
end
end
proportion_correct = monitor_reward / no_trials_in_block;
if plotting_hack_to_have_separate_choices_in_roving_example
proportion_task_correct = proportion_task_correct ./ 40; # 40 is completely a hack!
else
proportion_task_correct = proportion_task_correct ./ task_count;
end
local_average_task_choice = local_average_task_choice ./ task_count;
if (no_decision_monitors == 1)
local_average_decision_criterion_monitor /= no_trials_in_block;
else
local_average_decision_criterion_monitor /= task_count;
end
if(perform_detection_threshold)
local_average_threshold /= no_trials_in_block;
local_average_task_threshold = local_average_task_threshold ./ task_count;
end
#global wfinal = deepcopy(w)
if(verbosity > 2)
# Note this changes how the final proportion_correct is calculated!
print("END of Learning Block, proportion correct: $proportion_correct, alternating task set.\nProportion task correct: $proportion_task_correct.\n")
print("DEBUG: task_1_count: $task_1_count, task_2_count: $task_2_count.\n")
proportion_correct = instance_correct / (instance_correct + instance_incorrect);
print("DEBUG: instance_correct: $instance_correct, instance_incorrect: $instance_incorrect, new proportion correct: $proportion_correct\n")
end
block_dat.proportion_correct = proportion_correct;
block_dat.proportion_task_correct = proportion_task_correct;
block_dat.average_choice = average_choice;
block_dat.average_task_choice = local_average_task_choice;
block_dat.average_decision_criterion_monitor = deepcopy(local_average_decision_criterion_monitor);
block_dat.average_reward = average_block_reward;
block_dat.average_task_reward = average_task_reward;
block_dat.average_threshold = local_average_threshold;
block_dat.average_task_threshold = local_average_task_threshold;
for local_task_id = 1:no_input_tasks
# calculate and record noise-free output for each of the given tasks, for the extremal potential inputs
block_dat.noise_free_positive_output[local_task_id, 1] = noise_free_output_positive_difference(problem_left_bound, local_task_id, tuning_type);
block_dat.noise_free_positive_output[local_task_id, 2] = noise_free_output_positive_difference(problem_right_bound, local_task_id, tuning_type);
# calculate and record Probability(correct | task, input) for each of the given tasks, for the extremal potential inputs
block_dat.probability_correct[local_task_id, 1] = probability_correct(problem_left_bound, local_task_id, tuning_type);
block_dat.probability_correct[local_task_id, 2] = probability_correct(problem_right_bound, local_task_id, tuning_type);
end
# store critic learner reward prediction at end of each block
if (!use_hard_coded_critic)
block_dat.reward_prediction[1] = get_reward_prediction(1)[1];
block_dat.reward_prediction[2] = get_reward_prediction(2)[1];
end
return proportion_correct;
end
function perform_single_subject_experiment(task_id::Int, tuning_type::TuningSelector, subjects_dat::Array{Subject,2}, subject_id::Int64=1, roving_experiment_id::Int64=1)
global enable_weight_updates;
typeassert(enable_weight_updates, Bool);
global average_reward;
global n_critic;
global n_post;
global average_post;
if (!use_fixed_external_bias)
local_save_task_id = task_id;
else
local_save_task_id = roving_experiment_id; # hard code for now, should be related to roving_task_id if we expand the number of 'experiments'
end
global a = deepcopy(subjects_dat[subject_id, local_save_task_id].a);
if( isa(tuning_type, linear_tc) )
global b = deepcopy(subjects_dat[subject_id, local_save_task_id].b);
end
initialise_weight_matrix(tuning_type) # must be called after a and b are setup
if (!use_fixed_external_bias)
subjects_dat[subject_id, local_save_task_id].w_initial = deepcopy(w);
else # the only thing this is doing is cutting down on copying of weights which are never used
subjects_dat[subject_id, local_save_task_id].w_initial[:,:,task_id] = deepcopy(w[:,:,task_id]);
end
if(disable_learning_on_first_block)
enable_weight_updates = false :: Bool;
end
# these guys need to be reset here in case they're never
# initialised in perform_single_block...()
# slightly risky if not reset as they may contain values from
# previous runs!
for i = 1:no_task_critics
for j = 1:no_choices_per_task_critics
average_reward[i,j] = 0.;
n_critic[i,j] = 0;
end
end
n_post = 0;
for i = 1:no_post_neurons
average_post[i] = 0.;
end
if !use_hard_coded_critic
initialise_critic_parameters()
end
if(use_reset_decision_criterion_monitor_each_subject || subject_id==1)
global decision_criterion_monitor = zeros(no_decision_monitors,1); #0.5 :: Float64;
end
for i = 1:no_blocks_in_experiment
#=if(i == no_blocks_in_experiment && subject_id == 9)
local_old_verbosity = verbosity;
global verbosity = 10;
end=#
if(verbosity > -1)
print("------------------ Block number $i --------------------\n")
end
perform_learning_block_single_problem(task_id, tuning_type, subjects_dat[subject_id, local_save_task_id].blocks[i])
if (save_reward_from_running_average)
# Remember, average_reward is a running average not a block average
local_average_reward = 0.;
local_sum_critics = 0;
for k = 1:no_task_critics
for j = 1:no_choices_per_task_critics
local_average_reward += average_reward[k,j] * n_critic[k,j];
local_sum_critics += n_critic[k,j];
end
end
subjects_dat[subject_id, local_save_task_id].blocks[i].average_reward = ( local_average_reward / local_sum_critics );
end
if(verbosity > -1)
print("Block $i completed. Task_id: $task_id.\n")
end
#=if(i == no_blocks_in_experiment && subject_id == 9)
verbosity = local_old_verbosity;
end=#
enable_weight_updates = true;
end
if (!use_fixed_external_bias)
subjects_dat[subject_id, task_id].w_final = deepcopy(w);
else # again we're cutting down on saving of weights which are never used
subjects_dat[subject_id, local_save_task_id].w_final[:,:,task_id] = deepcopy(w[:,:,task_id]);
end
#print(" End of Subject $subject_id \n") #useful for debug of decision_criterion_monitor
return 0;
end
function perform_single_subject_experiment_trial_switching(tuning_type::TuningSelector, subjects::Array{Subject,2}, subject_id::Int64=1)
global enable_weight_updates;
typeassert(enable_weight_updates, Bool);
global average_reward;
global n_critic;
global n_post;
global average_post;
roving_experiment_id = 1::Int;
global a = deepcopy(subjects[subject_id, roving_experiment_id].a);
if( isa(tuning_type, linear_tc) )
global b = deepcopy(subjects[subject_id, roving_experiment_id].b);
end
initialise_weight_matrix(tuning_type) # must be called after a and b are setup
subjects[subject_id, roving_experiment_id].w_initial = deepcopy(w);
if(disable_learning_on_first_block || use_simple_variance_normalised_critic)
enable_weight_updates = false :: Bool;
end
# these guys need to be reset here in case they're never
# initialised in perform_single_block...()
# slightly risky if not reset as they may contain values from
# previous runs!
for i = 1:no_task_critics
for j = 1:no_choices_per_task_critics
average_reward[i,j] = 0.;
n_critic[i,j] = 0;
end
end
n_post = 0;
for i = 1:no_post_neurons
average_post[i] = 0.;
end
if !use_hard_coded_critic
initialise_critic_parameters()
end
if(use_reset_decision_criterion_monitor_each_subject || subject_id==1)
global decision_criterion_monitor = zeros(no_decision_monitors,1); #0.5 :: Float64;
global transition_reset_of_decision_monitors = false;
end
if(double_no_of_trials_in_alternating_experiment)
global no_trials_in_block = (no_trials_in_block * 2) :: Int;
end
for i = 1:no_blocks_in_experiment
if(verbosity > -1)
print("-------------------------------------------\n")
end
# Effectively a hack to set the representation in the critic on block 31
if i == 31
if !use_hard_coded_critic
set_phase_id(3);
end
end
perform_learning_block_trial_switching(tuning_type, subjects[subject_id, roving_experiment_id].blocks[i])
if (save_reward_from_running_average)
# Remember, average_reward is a running average, not a block average.
local_average_reward = 0.;
local_sum_critics = 0;
for k = 1:no_task_critics
for j = 1:no_choices_per_task_critics
local_average_reward += average_reward[k,j] * n_critic[k,j];
local_sum_critics += n_critic[k,j];
end
end
subjects[subject_id, roving_experiment_id].blocks[i].average_reward = ( local_average_reward / local_sum_critics );
end
if(verbosity > -1)
print("Block $i completed. Alternating tasks.\n")
end
if (use_simple_variance_normalised_critic)
if (i > no_blocks_to_maintain_simple_variance_cut_off)
enable_weight_updates = true;
if(!transition_reset_of_decision_monitors)
global decision_criterion_monitor = zeros(no_decision_monitors,1);
global transition_reset_of_decision_monitors = true;
end
end
else
enable_weight_updates = true;
end
end
if(double_no_of_trials_in_alternating_experiment)
no_trials_in_block = round(Int, no_trials_in_block / 2);
end
subjects[subject_id, roving_experiment_id].w_final = deepcopy(w);
return 0;
end
function perform_multi_subject_experiment(task_id::Int, tuning_type::TuningSelector, subjects::Array{Subject,2}, no_subjects::Int64=no_subjects, roving_experiment_id::Int64=1)
#global subject = Array(Subject, no_subjects);
global debug_print_now = false;
global verbosity;
for i = 1:no_subjects
if(verbosity > -1)
print("-----------Subject number $i------------\n")
end
if (i == 1)
## Handy debugging code: reenable the following two lines
#debug_print_now = true;
#verbosity = 2;
else
verbosity = -1;
debug_print_now = false;
end
perform_single_subject_experiment(task_id, tuning_type, subjects, i, roving_experiment_id)
end
if(verbosity > -1)
print("No subjects completed: $no_subjects\n")
end
end
function perform_multi_subject_experiment_trial_switching(tuning_type::TuningSelector, subjects::Array{Subject,2}, no_subjects::Int64=no_subjects)
#global subject = Array(Subject, no_subjects);
for i = 1:no_subjects
if(verbosity > -1)
print("-----------Subject number $i------------\n")
end
perform_single_subject_experiment_trial_switching(tuning_type, subjects, i)
end
end
############# Output #####################
function compare_three_trial_types_with_multiple_subjects()
# figure()
# xlim((0,no_blocks_in_experiment))
# ylim((0,1))
# xlabel("Block number")
# ylabel("Proportion correct")
# title("For x in ($problem_left_bound, $problem_right_bound), proportion correct. Comparing three task types.")
#latest_experiment_results = Multi_subject_experiment_results(zeros(no_blocks_in_experiment), zeros(no_blocks_in_experiment), zeros(no_blocks_in_experiment), zeros(no_blocks_in_experiment), zeros(no_blocks_in_experiment));
if(use_gaussian_tuning_function)
# use gaussian basis functions
tuning_type = gaussian_tc();
elseif(use_linear_tuning_function)
# use linear tuning functions
tuning_type = linear_tc();
else
print("ERROR: you need to define a tuning function\n");
error(1);
end
no_roving_experiments = 1::Int;
latest_experiment_results = initialise_empty_roving_experiment(tuning_type, no_subjects, no_blocks_in_experiment, no_trials_in_block, no_roving_experiments);
if(use_ab_persistence)
for i = 1:no_subjects
initialise_pre_population(tuning_type);
for j = 1:no_input_tasks
latest_experiment_results.subjects_task[i,j].a = deepcopy(a);
if( isa(tuning_type, linear_tc) )
latest_experiment_results.subjects_task[i,j].b = deepcopy(b);
end
end
latest_experiment_results.subjects_roving_task[i,1].a = deepcopy(a);
if( isa(tuning_type, linear_tc) )
latest_experiment_results.subjects_roving_task[i,1].b = deepcopy(b);
end
initialise_pre_population(tuning_type);
initialise_pre_population(tuning_type);
end
else # experiment to have identical RND sequences
for i = 1:no_subjects
initialise_pre_population(tuning_type);
latest_experiment_results.subjects_task[i,1].a = deepcopy(a);
if( isa(tuning_type, linear_tc) )
latest_experiment_results.subjects_task[i,1].b = deepcopy(b);
end
initialise_pre_population(tuning_type);
latest_experiment_results.subjects_task[i,2].a = deepcopy(a);
if( isa(tuning_type, linear_tc) )
latest_experiment_results.subjects_task[i,2].b = deepcopy(b);
end
initialise_pre_population(tuning_type);
latest_experiment_results.subjects_roving_task[i,1].a = deepcopy(a);
if( isa(tuning_type, linear_tc) )
latest_experiment_results.subjects_roving_task[i,1].b = deepcopy(b);
end
end
end
print("-----Experiment: task 1------\n")
task_id = 1::Int;
perform_multi_subject_experiment(task_id, tuning_type, latest_experiment_results.subjects_task);
mean_correct = zeros(no_blocks_in_experiment);
range_correct = zeros(no_blocks_in_experiment);
err_correct = zeros(no_blocks_in_experiment);
for i = 1:no_blocks_in_experiment
local_prop = zeros(no_subjects);
for j = 1:no_subjects
local_prop[j] = latest_experiment_results.subjects_task[j,task_id].blocks[i].proportion_correct;
end
if(use_plot_mean)
# mean calculation
mean_correct[i] = mean(local_prop);
else
# median calculation
mean_correct[i] = median(local_prop);
end
# other deviation and range statistics
err_correct[i] = std(local_prop);
#err_correct[i] /= sqrt(no_subjects); # standard error correction to sample standard deviation
range_correct[i] = (maximum(local_prop) - minimum(local_prop)) / 2.0;
end
# plot(mean_correct, "r", linewidth=2, label="Task 1")
latest_experiment_results.task_correct[:,task_id] = mean_correct;
latest_experiment_results.task_error[:,task_id] = err_correct;
latest_experiment_results.task_range[:,task_id] = range_correct;
print("-----Experiment: task 2------\n")
task_id = 2::Int;
perform_multi_subject_experiment(task_id, tuning_type, latest_experiment_results.subjects_task);
mean_correct = zeros(no_blocks_in_experiment);
range_correct = zeros(no_blocks_in_experiment);
err_correct = zeros(no_blocks_in_experiment);
for i = 1:no_blocks_in_experiment
local_prop = zeros(no_subjects);
for j = 1:no_subjects
local_prop[j] = latest_experiment_results.subjects_task[j,task_id].blocks[i].proportion_correct;
end
if(use_plot_mean)
# mean calculation
mean_correct[i] = mean(local_prop);
else
# median calculation
mean_correct[i] = median(local_prop);
end
# other deviation and range statistics
err_correct[i] = std(local_prop);
#err_correct[i] /= sqrt(no_subjects); # standard error correction to sample standard deviation
range_correct[i] = (maximum(local_prop) - minimum(local_prop)) / 2.0;
end
# plot(mean_correct, "g", linewidth=2, label="Task 2")
latest_experiment_results.task_correct[:,task_id] = mean_correct;
latest_experiment_results.task_error[:,task_id] = err_correct;
latest_experiment_results.task_range[:,task_id] = range_correct;
print("-----Experiment: roving task------\n")
roving_experiment_id = 1 :: Int;
# there's no point expanding the following to generic multiple roving pop experiments until I have such an experiment
perform_multi_subject_experiment_trial_switching(tuning_type, latest_experiment_results.subjects_roving_task, no_subjects);
mean_correct = zeros(no_blocks_in_experiment);
mean_task_1_correct = zeros(no_blocks_in_experiment);
mean_task_2_correct = zeros(no_blocks_in_experiment);
err_correct = zeros(no_blocks_in_experiment);
range_correct = zeros(no_blocks_in_experiment);
for i = 1:no_blocks_in_experiment
# can increase dimensionality of the following when I want to expand task space
local_prop = zeros(no_subjects);
local_prop_1 = zeros(no_subjects);
local_prop_2 = zeros(no_subjects);
for j = 1:no_subjects
# save the proportions so that mean or median can be called
local_prop[j] = latest_experiment_results.subjects_roving_task[j, roving_experiment_id].blocks[i].proportion_correct;
local_prop_1[j] = latest_experiment_results.subjects_roving_task[j, roving_experiment_id].blocks[i].proportion_task_correct[1];
local_prop_2[j] = latest_experiment_results.subjects_roving_task[j, roving_experiment_id].blocks[i].proportion_task_correct[2];
end
if(use_plot_mean)
# mean calculation
mean_correct[i] = mean(local_prop)
mean_task_1_correct[i] = mean(local_prop_1)
mean_task_2_correct[i] = mean(local_prop_2)
else
# median calculation
mean_correct[i] = median(local_prop);
mean_task_1_correct[i] = median(local_prop_1);
mean_task_2_correct[i] = median(local_prop_2);
end
# other deviation and range statistics
err_correct[i] = std(local_prop);
#err_correct[i] /= sqrt(no_subjects); # standard error correction to sample standard deviation
range_correct[i] = (maximum(local_prop) - minimum(local_prop)) / 2.0;
end
# plot(mean_correct, "b", linewidth=3, label="RDM alternating tasks")
# plot(mean_task_1_correct, "k", linewidth=3, label="Task 1, from alternating tasks")
# plot(mean_task_2_correct, "k", linewidth=3, label="Task 2, from alternating tasks")
latest_experiment_results.roving_correct[:,roving_experiment_id] = mean_correct;
latest_experiment_results.roving_task_correct[:,1,roving_experiment_id] = mean_task_1_correct;
latest_experiment_results.roving_task_correct[:,2,roving_experiment_id] = mean_task_2_correct;
latest_experiment_results.roving_error[:,roving_experiment_id] = err_correct;
latest_experiment_results.roving_range[:,roving_experiment_id] = range_correct;
print("Plotting...\n")
# legend(loc=4)
#figure()
#plot_multi_subject_experiment(latest_experiment_results);
#restore next line
plot_multi_subject_experiment_as_subplots(latest_experiment_results);
if(perform_post_hoc_detection_threshold)
print("Calculating error detection thresholds...\n");
post_hoc_calculate_thresholds(tuning_type, latest_experiment_results.subjects_task);
post_hoc_calculate_thresholds(tuning_type, latest_experiment_results.subjects_roving_task);
end
global exp_results;
resize!(exp_results, length(exp_results)+1);
exp_results[length(exp_results)] = latest_experiment_results;
print("End\n");
end
function biased_compare_three_trial_types_with_multiple_subjects()
## fixed external bias sim comparison
global use_fixed_external_bias;
typeassert(use_fixed_external_bias, Bool);
if(use_gaussian_tuning_function)
# use gaussian basis functions
tuning_type = gaussian_tc();
elseif(use_linear_tuning_function)
# use linear tuning functions
tuning_type = linear_tc();
else
print("ERROR: you need to define a tuning function\n");
error(1);
end
no_roving_experiments = 2::Int;
latest_experiment_results = initialise_empty_roving_experiment(tuning_type, no_subjects, no_blocks_in_experiment, no_trials_in_block, no_roving_experiments);
if(use_ab_persistence) # each Subject is recycled across protocols
for i = 1:no_subjects
initialise_pre_population(tuning_type);
for j = 1:no_input_tasks
latest_experiment_results.subjects_task[i,j].a = deepcopy(a);
if( isa(tuning_type, linear_tc) )
latest_experiment_results.subjects_task[i,j].b = deepcopy(b);
end
end
for j = 1:no_roving_experiments
latest_experiment_results.subjects_roving_task[i,j].a = deepcopy(a);
if( isa(tuning_type, linear_tc) )
latest_experiment_results.subjects_roving_task[i,j].b = deepcopy(b);
end
end
initialise_pre_population(tuning_type);
initialise_pre_population(tuning_type);
end
else # new Subjects per protocol but identical random sequences (for comparison)
for i = 1:no_subjects
for j = 1:no_input_tasks
initialise_pre_population(tuning_type);
latest_experiment_results.subjects_task[i,j].a = deepcopy(a);
if( isa(tuning_type, linear_tc) )
latest_experiment_results.subjects_task[i,j].b = deepcopy(b);
end
end
for j = 1:no_input_tasks
initialise_pre_population(tuning_type);
latest_experiment_results.subjects_roving_task[i,j].a = deepcopy(a);
if( isa(tuning_type, linear_tc) )
latest_experiment_results.subjects_roving_task[i,j].b = deepcopy(b);
end
end
end
end
## insert srand() here and before biased experiments if you want identical population behaviour
print("-----Experiment: task 1------\n")
task_id = 1::Int;
use_fixed_external_bias = false; # initally don't use
perform_multi_subject_experiment(task_id, tuning_type, latest_experiment_results.subjects_task);
mean_correct = zeros(no_blocks_in_experiment);
range_correct = zeros(no_blocks_in_experiment);
err_correct = zeros(no_blocks_in_experiment);
for i = 1:no_blocks_in_experiment
local_prop = zeros(no_subjects);
for j = 1:no_subjects
local_prop[j] = latest_experiment_results.subjects_task[j,task_id].blocks[i].proportion_correct;
end
if(use_plot_mean)
# mean calculation
mean_correct[i] = mean(local_prop);
else
# median calculation
mean_correct[i] = median(local_prop);
end
# other deviation and range statistics
err_correct[i] = std(local_prop);
#err_correct[i] /= sqrt(no_subjects); # standard error correction to sample standard deviation
range_correct[i] = (maximum(local_prop) - minimum(local_prop)) / 2.0;
end
# plot(mean_correct, "r", linewidth=2, label="Task 1")
latest_experiment_results.task_correct[:,task_id] = mean_correct;
latest_experiment_results.task_error[:,task_id] = err_correct;
latest_experiment_results.task_range[:,task_id] = range_correct;
print("-----Experiment: task 2------\n")
task_id = 2::Int;
use_fixed_external_bias = false; # initally don't use
perform_multi_subject_experiment(task_id, tuning_type, latest_experiment_results.subjects_task);
mean_correct = zeros(no_blocks_in_experiment);
range_correct = zeros(no_blocks_in_experiment);
err_correct = zeros(no_blocks_in_experiment);
for i = 1:no_blocks_in_experiment
local_prop = zeros(no_subjects);
for j = 1:no_subjects
local_prop[j] = latest_experiment_results.subjects_task[j,task_id].blocks[i].proportion_correct;
end
if(use_plot_mean)
# mean calculation
mean_correct[i] = mean(local_prop);
else
# median calculation
mean_correct[i] = median(local_prop);
end
# other deviation and range statistics
err_correct[i] = std(local_prop);
#err_correct[i] /= sqrt(no_subjects); # standard error correction to sample standard deviation
range_correct[i] = (maximum(local_prop) - minimum(local_prop)) / 2.0;
end
# plot(mean_correct, "g", linewidth=2, label="Task 2")
latest_experiment_results.task_correct[:,task_id] = mean_correct;
latest_experiment_results.task_error[:,task_id] = err_correct;
latest_experiment_results.task_range[:,task_id] = range_correct;
print("-----Experiment: biased task 1------\n")
# Notation is going to be a bitch here as I'm hijacking the code from the roving_task
# there will be only one roving_experiment still
# I will simulate first task 1 (with a bias included into updates of running average reward)
# output will be stored as task 1 output of roving_experiment_id 1
# Then I will simulate task 2 (with similar inclusion of bias)
# this output goes into task 2 output of roving_experiment_id 2
# As long as careful accounting of task_id's is done then no overwriting of variables for
# the other task should occur.
# Finally, I will discard the averaging across tasks here as there is no commonality between
# what will now be separate experiments.
roving_experiment_id = 1 :: Int;
task_id = 1::Int;
use_fixed_external_bias = true; # initally don't use
perform_multi_subject_experiment(task_id, tuning_type, latest_experiment_results.subjects_roving_task, no_subjects, roving_experiment_id);
#mean_correct = zeros(no_blocks_in_experiment);
mean_task_1_correct = zeros(no_blocks_in_experiment);
#mean_task_2_correct = zeros(no_blocks_in_experiment);
err_correct = zeros(no_blocks_in_experiment);
range_correct = zeros(no_blocks_in_experiment);
for i = 1:no_blocks_in_experiment
# can increase dimensionality of the following when I want to expand task space
#local_prop = zeros(no_subjects);
local_prop_1 = zeros(no_subjects);
local_prop_2 = zeros(no_subjects);
for j = 1:no_subjects
# save the proportions so that mean or median can be called
#local_prop[j] = latest_experiment_results.subjects_roving_task[j, roving_experiment_id].blocks[i].proportion_correct;
local_prop_1[j] = latest_experiment_results.subjects_roving_task[j, roving_experiment_id].blocks[i].proportion_task_correct[1];
local_prop_2[j] = latest_experiment_results.subjects_roving_task[j, roving_experiment_id].blocks[i].proportion_task_correct[2];
end
if(use_plot_mean)
# mean calculation
#mean_correct[i] = mean(local_prop)
mean_task_1_correct[i] = mean( (local_prop_2 + local_prop_1) / 2.0 )
#mean_task_2_correct[i] = mean(local_prop_2)
else
# median calculation
#mean_correct[i] = median(local_prop);
mean_task_1_correct[i] = median( (local_prop_2 + local_prop_1) / 2.0 );
#mean_task_2_correct[i] = median(local_prop_2);
end
# other deviation and range statistics
## TODO