%QRE in symmetric normal form games
%clear all variables
clear
%start with the parameters
lamda=0.1; 
conv=10^-10; %convergence criterion, meaning how close the vectors have to be to stop searching
%define the game payoffs, for the row player
game=[1 3 5; 8 6 7;5 4 7];
%starting beliefs, it is a vector of probabilites
bel=[1/3;1/3;1/3];


for i=1:30
i
%if in second step of iteration use the new probablities instead of
%starting beliefs
if i>1
    bel=lprob';
end

%find expected utilities given beliefs
for j=1:length(game)
    expu(:,j)=game(j,:)*bel;
end

%convert to logit probabilities of play
lprob= exp(lamda*expu)/sum(exp(lamda*expu));

%if difference between starting and final probabilities small, stop the
% %loop
if sum(abs(lprob-bel'))<conv
    disp ('QRE found')
    disp ('Number of iterations')
    disp(i)
    disp('probabilities of play are')
    disp (lprob)
    break
end    
    
    
    
end