%written by Ryan Jessup, 20090218 %This script demonstrates a comparison between a filtering method and a %loop method for generating expectations from a reinformcent learning %model. Functions fastrl.m and fastrln.m should be used fitting and %predicting purposes when there is one and n actions, respectively. %Generate some sample data (observed outcomes) nTrials = 1000; mu=13; sig=4; v0=5; %initial expectation x=((randn(1,nTrials)*sig) + mu); lambda= .05; %learning rate %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% Filter Method %%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% disp('The following elapsed time is for the filter method') tic %start time a=[1 lambda-1]; %modify the v0; this must be done because otherwise the filter does not %correctly apply the initial expectation modv0=v0/lambda; x=[modv0 x]; %append the intial expectation onto the dataset v = filter(lambda,a,x); %trim the initial v, which has been trasformed back into the orig v0 yFilt = v(2:end); toc %end time %cf. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%% Loop Method %%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% disp('The following elapsed time is for the loop method') tic %start time yLoop=v0; for i=2:length(x) yLoop(i) = (1-lambda)*yLoop(i-1) + lambda*x(i); end %remove the initial y yLoop=yLoop(2:end); toc %end time %Now, test to see whether any cell differs; if min yields 1 then they're %all equivalent disp('If the following yields 1 then all cells from the two methods are equivalent') min(yFilt==yLoop)