% RL_tutorial_NSCI850.m % % learn how to use a simple RL mechanisms to fit data % Dataset: from a gambling task % - 2 choices % - 70%-30% reward probabilities % - high/low volatility (2 separate subjects) % i.e. fast or slow switching of probabilities % % written by G. Blohm for NSCI 850 (2017) clear %% load data cd('C:\Users\Gunnar\Documents\Teaching\CompNeurosci-NSCI850\Week13-RL\Matlab') load('alldataRL.mat','d') N = length(d{1}.choice); % number trials in experiment %% plot data for i = 1:2, figure(1) subplot(2,1,i) hold on plot(d{i}.prep.feedbackprob,'b-') plot(2-d{i}.choice,'*k') plot(2-conv(d{i}.choice,ones(6,1)/6,'same'),'k-') outcome = d{i}.prep.feedback; % choice outcome %% simulate RL process % parameters alp = .4; % sensitivity to reward and punishment bet = 2; % inverse learning rate % initialize v = .5*ones(1,2); % initial value % loop through value function computation... for t = 1:N, % compute likelihood of the each choice option (softmax) ev = exp(bet*v(t,:)); sev = sum(ev); p(t,:) = ev/sev; % Do a weighted coinflip to make a choice: choose stim 1 if random % number is in the [0 p(1)] interval, and 2 otherwise if rand(1)