%Code for free recall
clear all; clf; format compact

%set the parameter values
c=10; thresh=0.6; slope=8;

retint=5; %this is the retention interval (here assumed the same for every item)

nitems = 16; %specifies number of items in the list


%Now provide the temporal distances of the items at the time of retrieval
d=[15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0];
d=d+retint; %add the retention interval


%log transform the temporal distances
for n=1:nitems; d(n)=log(d(n)); end


for itemnum=1:nitems %for each item in the list...
    ss=0; %set its summed sim to other items to zero
    for n=1:nitems %for each item, calc the similarity and add to the total
        ss = ss+ exp(-c*(abs(d(itemnum)-d(n))));
    end
    %ss now has the summed similarities for itemnum

    %Now we need a nitemns*nitems matrix, called prob, where each entry is the D
    %value for one item given another item as a cue
    for n=1:nitems
        prob(n,itemnum)=(exp(-c*(abs( d(itemnum)-d(n)))))/(ss);
    end
end


%implement the thresholding
for i=1:nitems+0; for j=1:nitems;
        prob(i,j)=1/(1+exp(-slope*(prob(i,j)-thresh)));
end; end



%Now sum across the matrix to reflect the fact
%that an item may be recalled in response to a cue for a different item
%serpos is the model serial position curve
serpos=sum(prob');


%Now cap recall probs at 1.0
for n=1:nitems; serpos(n)=min(1,serpos(n)); end


%Now just plot the graph of the serial position curve
plot(serpos)

axis([1 nitems 0 1])

