Commit 44fa2403 authored by laurencehunt's avatar laurencehunt
Browse files

Second commit

parent 4f2e20dc
**This repository will contain four tutorials on fitting computational models of learning to data from a study of reward-guided decision making, and how this can be applied in the context of analysing neuroimaging data.**
**The tutorials have been developed as part of the WIN graduate programme, by Laurence Hunt, Nils Kolling, Miriam Klein-Flugge and Jacqueline Scholl.** The 2019 course is being run by Laurence and Nils.
In the first tutorial (developed by Laurence), we introduce a simple reinforcement learning model for a task that is based around the paradigm of Behrens et al., 2007, but it has an additional (between-subjects) stress manipulation. This paradigm runs in Psychtoolbox, which needs to be downloaded for the paradigm to run properly: see http://psychtoolbox.org. The stress manipulation can be easily switched off, by setting trialvariables.playSound to be 0 for all trials (or by simply muting the sound).
Please let the tutorial authors know if you encounter any unintentional errors. Please also feel free to contact us if you have any questions, although we may not be able to respond to all enquiries.
\ No newline at end of file
function probOpt1 = RL_model(opt1Rewarded,alpha,startingProb)
% probOpt1 = RL_model(opt1Rewarded,alpha,startingProb)
%
% opt1rewarded = vector of outcomes (1 if opt 1 rewarded, 0 if opt 2 rewarded)
% alpha = fixed learning rate, greater than 0, less than/equal to 1
% startingProb = starting probability (defaults to 0.5)
%
% probOpt1 is returned as a vector of how likely option 1 is to be rewarded
%% CHECK THE INPUT ARGUMENTS
% check alpha has been set appropriately
if alpha <= 0
error('Learning rate (alpha) must be greater than 0');
elseif alpha > 1
error('Learning rate (alpha) must be less than or equal to 1');
end
%set the starting probability to 0.5 if it hasn't been given
if nargin < 3
startingProb = 0.5;
end
%set the first trial's prediction to be equal to the starting probability
probOpt1(1) = startingProb;
%calculate the number of trials
nTrials = length(opt1Rewarded);
%% STUDENTS - complete this code to finish the reinforcement learning model
for t = 1:nTrials %loop over trials
delta(t) = %%COMPLETE THIS LINE using opt1Rewarded, probOpt1 and equation 1 %%; % prediction error
probOpt1(t+1) = %%COMPLETE THIS LINE using probOpt1, delta, alpha and equation 2 %%; % prediction for next trial
end
\ No newline at end of file
function AdditionDistractorTask(monitor, rect, fontSize, fontColor, backColor, duration, ISI, filename)
%
% AdditionDistractorTask(monitor, rect, fontSize, fontColor, backColor, duration, ISI, filename)
%
% Presents 2-digit addition problems to the screen until at least DURATION
% seconds has passed. Note that (per Mike Diaz's original PTB-2 function)
% the task will not end until the last problem has completed, so there may
% be some variability in the exact amount of time particiants spend doing the task.
%
% ISI is the # of SECONDS between problems. The "answer" row of the problems is
% drawn at the center of RECT. If RECT is not specified, the experiment
% defaults to the whole screen. Similarly, leaving FONTSIZE unspecified
% defaults to the current font size.
%
% If FILENAME is provided, then all problems are saved to said file. Otherwise,
% problems are not saved or scored. If FILENAME is not a legal name for a file,
% an error will be thrown.
%
% The function currently does not display an instructions screen to the
% participant, so you will need to add your own.
%
% To end the program early, type 'p' followed by 'q'.
%
% 6.20.06 M.Diaz
% 11.20.09 S.Fraundorf - suppressed output of assignment operations.
% allowed user to not define a filename.
% fixed some problems I had with writing files.
% 02.22.10 S.Fraundorf - PTB-3 version
% 08.25.12 S.Fraundorf - properly clear the screen during the ISI.
% Clarified that ISI is specified in SECONDS.
% Allowed use of default RECT and FONTSIZE.
% Improved stimulus timing by getting the timing
% directly from the Flip statements.
%% CHECK INPUT ARGUMENTS
if nargin < 8
writeFile=false;
else
writeFile=true;
try
mathout=fopen(filename,'w');
catch
Screen('CloseAll');
error('CogToolbox:AdditionDistractorTask:InvalidFileName', ...
'File name in addition distractor task not valid')
end
end
%% BASIC SET-UP
rand('twister',sum(clock*100));
global endExperiment;
%% KEY SET-UP
KbName('UnifyKeyNames'); % otherwise diff btwn Mac and Windows
ListenChar(2);
% this makes it so that what the user types doesn't bleed through into the Matlab command window
% otherwise the user can execute Matlab commands during your experiment!
%% SCREEN SET-UP
% if display area is not defined, default to whole screen
if isempty(rect)
rect = Screen('Rect', monitor);
end
if isempty(fontSize)
fontSize = Screen('TextSize', monitor);
end
%create function handles for commonly used functions
wr=@WriteRight;
% create window with horizontal line
lineWin=CreateOffWin(monitor, backColor, 'Arial', fontSize);
% draw the line
norm = Screen('TextBounds',monitor,'xxxx');
w = norm(3);
xCent=(rect(3)+rect(1))/2;
yCent=(rect(4)+rect(2))/2;
Screen('DrawLine', lineWin, fontColor,xCent-w,yCent,xCent+w,yCent,2);
norm = Screen('TextBounds', monitor, 'x');
w = w-norm(3);
% create window where each problem will be written
probWin=CreateOffWin(monitor, backColor, 'Arial', fontSize);
signs='+';
%% RUN THE TASK
FlushEvents('keyDown', 'mouseDown'); % clears anything the user has typed before the display appears
start=GetSecs;
t1=start;
while true
nums=round(rand(1,2)*89)+10; %two nums to be added
% display the problem:
Screen('CopyWindow', lineWin, probWin);
wr(probWin, [signs ' ' num2str(nums(1))] , xCent+w, yCent-fontSize, fontColor);
wr(probWin, num2str(nums(2)) , xCent+w, yCent-2*fontSize, fontColor);
Screen('CopyWindow', probWin, monitor);
Screen('Flip', monitor, t1+ISI);
%get answer
str=[];
while true
s=double(GetChar);
switch lower(s)
case cellstr(['0':'9']')
check=0;
str=[str char(s)];
Screen('CopyWindow', probWin, monitor); %restore original formula
wr(monitor, str, xCent+w, yCent+fontSize, fontColor); %write current answer
Screen('Flip', monitor, 0); %display screen
case {8} %backspace
if ~isempty(str)
str=str(1:end-1);
Screen('CopyWindow', probWin, monitor); %restore original formula
wr(monitor, str , xCent+w, yCent+fontSize, fontColor); %write current answer
Screen('Flip', monitor, 0); %display screen
end
case {13,3,10} % return
break
case {'p'}
check=1;
case {'q'}
if check
if writeFile
fclose(mathout);
end
Screen('CloseAll');
endExperiment=1; %used to inform calling functions
return
end
end
end
Screen('CopyWindow', lineWin, monitor); %restore original display space
t1 = Screen('Flip', monitor, 0);
%write to text file
if writeFile
fprintf(mathout,'%d %s %d = %s\r\n', nums(2), signs, nums(1), str);
end
if t1-start > duration
% done, do clean-up
if writeFile
fclose(mathout);
end
Screen('Close', lineWin);
Screen('Close', probWin);
ListenChar; % turn character listening back on
return
end
end
\ No newline at end of file
function DivisionDistractorTask(monitor, rect, fontSize, fontColor, backColor, duration, ISI, filename)
%
% DivisionDistractorTask(monitor, rect, fontSize, fontColor, backColor, duration, ISI, filename)
%
% Presents 2-digit division problems to the screen until at least DURATION
% seconds has passed. Note that (per Mike Diaz's original PTB-2 function)
% the task will not end until the last problem has completed, so there may
% be some variability in the exact amount of time particiants spend doing the task.
%
% ISI is the # of SECONDS between problems. The "answer" row of the problems is
% drawn at the center of RECT. If RECT is not specified, the experiment
% defaults to the whole screen. Similarly, leaving FONTSIZE unspecified
% defaults to the current font size.
%
% If FILENAME is provided, then all problems are saved to said file. Otherwise,
% problems are not saved or scored. If FILENAME is not a legal name for a file,
% an error will be thrown.
%
% The function currently does not display an instructions screen to the
% participant, so you will need to add your own.
%
% To end the program early, type 'p' followed by 'q'. %
% 6.20.06 M.Diaz
% 11.20.09 S.Fraundorf - suppressed output of assignment operations.
% allowed user to not define a filename.
% fixed some problems I had with writing files.
% 02.22.10 S.Fraundorf - PTB-3 version
% 08.25.12 S.Fraundorf - properly clear the screen during the ISI.
% Clarified that ISI is specified in SECONDS.
% Allowed use of default RECT and FONTSIZE.
% Improved stimulus timing by getting the timing
% directly from the Flip statements.
%% CHECK INPUT ARGUMENTS
if nargin < 8
writeFile=false;
else
writeFile=true;
try
mathout=fopen(filename,'w');
catch
Screen('CloseAll');
error('file name not valid')
end
end
%% BASIC SET-UP
rand('twister',sum(clock*100));
global endExperiment;
%% KEY SET-UP
KbName('UnifyKeyNames'); % otherwise diff btwn Mac and Windows
ListenChar(2);
% this makes it so that what the user types doesn't bleed through into the Matlab command window
% otherwise the user can execute Matlab commands during your experiment!
%% SCREEN SET-UP
% if display area is not defined, default to whole screen
if isempty(rect)
rect = Screen('Rect', monitor);
end
if isempty(fontSize)
fontSize = Screen('TextSize', monitor);
end
%create function handles for commonly used functions
wr=@WriteRight;
% create window with horizontal line
lineWin=CreateOffWin(monitor, backColor, 'Arial', fontSize);
% draw the line
norm = Screen('TextBounds',monitor,'xxxx');
w = norm(3);
xCent=(rect(3)+rect(1))/2;
yCent=(rect(4)+rect(2))/2;
Screen('DrawLine', lineWin, fontColor,xCent-w,yCent,xCent+w,yCent,2);
norm = Screen('TextBounds', monitor, 'x');
w = w-norm(3);
% create window where each problem will be written
probWin=CreateOffWin(monitor, backColor, 'Arial', fontSize);
signs=char(247);
%% RUN THE TASK
FlushEvents('keyDown', 'mouseDown'); % clears anything the user has typed before the display appears
start=GetSecs;
t1=start;
while true
nums=round(rand(1)*9)+1; %divisor
nums(1,2)=round(rand(1)*floor(100/nums)+10)*nums;
Screen('CopyWindow', lineWin, probWin);
wr(probWin, [signs ' ' num2str(nums(1))] , xCent+w, yCent-fontSize, fontColor);
wr(probWin, num2str(nums(2)) , xCent+w, yCent-2*fontSize, fontColor);
Screen('CopyWindow', probWin, monitor);
Screen('Flip', monitor, t1+ISI);
%get answer
str=[];
while true
s=double(GetChar);
switch lower(s)
case cellstr(['0':'9']')
check=0;
str=[str char(s)];
Screen('CopyWindow', probWin, monitor); %restore original formula
wr(monitor, str, xCent+w, yCent+fontSize, fontColor); %write current answer
Screen('Flip', monitor, 0); %display screen
case {8} %backspace
if ~isempty(str)
str=str(1:end-1);
Screen('CopyWindow', probWin, monitor); %restore original formula
wr(monitor, str , xCent+w, yCent+fontSize, fontColor); %write current answer
Screen('Flip', monitor, 0); %display screen
end
case {13,3,10} % return
break
case {'p'}
check=1;
case {'q'}
if check
if writeFile
fclose(mathout);
end
Screen('CloseAll');
endExperiment=1; %used to inform calling functions
return
end
end
end
Screen('CopyWindow', lineWin, monitor); %restore original display space
t1 = Screen('Flip', monitor, 0);
%write to text file
if writeFile
fprintf(mathout,'%d %s %d = %s\r\n', nums(2), signs, nums(1), str);
end
if t1-start > duration
% done, do clean-up
if writeFile
fclose(mathout);
end
Screen('Close', lineWin);
Screen('Close', probWin);
ListenChar; % turn character listening back on
return
end
end
\ No newline at end of file
function MathDistractorTask(monitor, rect, fontSize, fontColor, backColor, duration, ISI, filename)
%
% MathDistractorTask(monitor, rect, fontSize, fontColor, backColor, duration, ISI, filename)
%
% Presents 2-digit addition & subtraction problems to the screen until at least DURATION
% seconds has passed. Note that (per Mike Diaz's original PTB-2 function)
% the task will not end until the last problem has completed, so there may
% be some variability in the exact amount of time particiants spend doing the task.
%
% ISI is the # of SECONDS between problems. The "answer" row of the problems is
% drawn at the center of RECT. If RECT is not specified, the experiment
% defaults to the whole screen. Similarly, leaving FONTSIZE unspecified
% defaults to the current font size.
%
% If FILENAME is provided, then all problems are saved to said file. Otherwise,
% problems are not saved or scored. If FILENAME is not a legal name for a file,
% an error will be thrown.
%
% The function currently does not display an instructions screen to the
% participant, so you will need to add your own.
%
% To end the program early, type 'p' followed by 'q'.
%
% 6.20.06 M.Diaz
% 11.20.09 S.Fraundorf - suppressed output of assignment operations.
% allowed user to not define a filename.
% fixed some problems I had with writing files.
% 02.22.10 S.Fraundorf - PTB-3 version
% 08.25.12 S.Fraundorf - properly clear the screen during the ISI.
% Clarified that ISI is specified in SECONDS.
% Allowed use of default RECT and FONTSIZE.
% Improved stimulus timing by getting the timing
% directly from the Flip statements.
%% CHECK INPUT ARGUMENTS
if nargin < 8
writeFile=false;
else
writeFile=true;
try
mathout=fopen(filename,'w');
catch
Screen('CloseAll');
error('CogToolbox:MathDistractorTask:InvalidFileName', 'File name in math distractor task not valid')
end
end
%% BASIC SET-UP
rand('twister',sum(clock*100));
global endExperiment;
%% KEY SET-UP
KbName('UnifyKeyNames'); % otherwise diff btwn Mac and Windows
ListenChar(2);
% this makes it so that what the user types doesn't bleed through into the Matlab command window
% otherwise the user can execute Matlab commands during your experiment!
%% SCREEN SET-UP
% if display area is not defined, default to whole screen
if isempty(rect)
rect = Screen('Rect', monitor);
end
if isempty(fontSize)
fontSize = Screen('TextSize', monitor);
end
%create function handles for commonly used functions
wr=@WriteRight;
% create window with horizontal line
lineWin=CreateOffWin(monitor, backColor, 'Arial', fontSize);
% draw the line
norm = Screen('TextBounds',monitor,'xxxx');
w = norm(3);
xCent=(rect(3)+rect(1))/2;
yCent=(rect(4)+rect(2))/2;
Screen('DrawLine', lineWin, fontColor,xCent-w,yCent,xCent+w,yCent,2);
norm = Screen('TextBounds', monitor, 'x');
w = w-norm(3);
% create window where each problem will be written
probWin=CreateOffWin(monitor, backColor, 'Arial', fontSize);
signs='+-';
%% RUN THE TASK
FlushEvents('keyDown', 'mouseDown'); % clears anything the user has typed before the display appears
start=GetSecs;
t1= start;
while true
probsign=round(rand(1))+1; %1=addition, 2=subtraction
nums=round(rand(1,2)*89)+10; %two nums to be added or subtracted
if probsign==2
nums=sort(nums); % smaller # is subtracted from larger
end
% display the problem
Screen('CopyWindow', lineWin, probWin);
wr(probWin, [signs(probsign) ' ' num2str(nums(1))] , xCent+w, yCent-fontSize, fontColor);
wr(probWin, num2str(nums(2)) , xCent+w, yCent-2*fontSize, fontColor);
Screen('CopyWindow', probWin, monitor);
Screen('Flip', monitor, t1+ISI);
%get answer
str=[];
while true
s=double(GetChar);
switch lower(s)
case cellstr(['0':'9']')
check=0;
str=[str char(s)];
Screen('CopyWindow', probWin, monitor); %restore original formula
wr(monitor, str, xCent+w, yCent+fontSize, fontColor); %write current answer
Screen('Flip', monitor, 0); %display screen
case {8} %backspace
if ~isempty(str)
str=str(1:end-1);
Screen('CopyWindow', probWin, monitor); %restore original formula
wr(monitor, str , xCent+w, yCent+fontSize, fontColor); %write current answer
Screen('Flip', monitor, 0); %display screen
end
case {13,3,10} % return
break
case {'p'}
check=1;
case {'q'}
if check
if writeFile
fclose(mathout);
end
Screen('CloseAll');
endExperiment=1; %used to inform calling functions
return
end
end
end
Screen('CopyWindow', lineWin, monitor); %restore original display space
t1 = Screen('Flip', monitor, 0);
%write to text file
if writeFile
fprintf(mathout,'%d %s %d = %s\r\n', nums(2), signs(probsign), nums(1), str);
end
if t1-start > duration
% done, do clean-up
if writeFile
fclose(mathout);
end
Screen('Close', lineWin);
Screen('Close', probWin);
ListenChar; % turn character listening back on
return
end
end
\ No newline at end of file
function MultiplicationDistractorTask(monitor, rect, fontSize, fontColor, backColor, duration, ISI, filename)
%
% MultiplicationDistractorTask(monitor, rect, fontSize, fontColor, backColor, duration, ISI, filename)
%
% Presents 2-digit multiplication problems to the screen until at least DURATION
% seconds has passed. Note that (per Mike Diaz's original PTB-2 function)
% the task will not end until the last problem has completed, so there may
% be some variability in the exact amount of time particiants spend doing the task.
%
% ISI is the # of SECONDS between problems. The "answer" row of the problems is
% drawn at the center of RECT. If RECT is not specified, the experiment
% defaults to the whole screen. Similarly, leaving FONTSIZE unspecified
% defaults to the current font size.
%
% If FILENAME is provided, then all problems are saved to said file. Otherwise,
% problems are not saved or scored. If FILENAME is not a legal name for a file,
% an error will be thrown.
%
% The function currently does not display an instructions screen to the
% participant, so you will need to add your own.
%
% To end the program early, type 'p' followed by 'q'.
%
% 6.20.06 M.Diaz
% 11.20.09 S.Fraundorf - suppressed output of assignment operations.
% allowed user to not define a filename.
% fixed some problems I had with writing files.
% 02.22.10 S.Fraundorf - PTB-3 version
% 08.25.12 S.Fraundorf - properly clear the screen during the ISI.
% Clarified that ISI is specified in SECONDS.
% Allowed use of default RECT and FONTSIZE.
% Improved stimulus timing by getting the timing
% directly from the Flip statements.
%% CHECK INPUT ARGUMENTS
if nargin < 8
writeFile=false;
else
writeFile=true;
try
mathout=fopen(filename,'w');
catch
Screen('CloseAll');