function babysim(caseno,groupno,qm,iter);

% function babysim(caseno,groupno,qm,iter);

% This MATLAB script accompanies the article
% "The Great Capitol Hill Baby-Sitting Co-op: Anecdote or Evidence for the Optimum Quantity of Money?"
% by Hens, Schenk-Hoppé, Vogt
% It calculates the average number of trades and time-values in games M.5 and M.2 

% Parameters
% caseno = 1 : game M.5 is simulated according to the strategies indicated in Table 3 of the paper
% caseno = 2 : game M.2 is simulated according to the strategies indicated in Table 3 of the paper
% groupno = 1,2,3,4,5,6 : respective group is simulated, see Figures 4 and 5 of the paper
% groupno = 7,8 : artifically groups for benchmarks
% qm = 1,2,3,4, ... : money units per capita
% iter = 1,2,... (>10000) is recommended: number of iterations 

% Author: Klaus Reiner Schenk-Hoppé (E-mail: k.r.schenk-hoppe@leeds.ac.uk, Web: www.schenk-hoppe.net)
% Last update: January 9, 2006


corr2 = 0.99999; % avoids problems with ceil, if rand0m number = 0
corr1 = 0.0000001; % avoids problems with ceil, if rand0m number = 0

l = 5.0;  % low time value
h = 10.0; % high time value

if caseno==1,  % game M.5
  ph = .5;
  if groupno==1,
    grp  = [ 6, 5, 4, 4, 5, 5, 6, 6;
             8, 8, 8, 8, 8, 8, 8, 8;       
            12,12,12,12,12,12,12,12;       
             5, 5, 5, 5, 5, 5, 5, 5;       
             3, 3, 3, 3, 3, 3, 3, 3;       
             2, 3, 3, 3, 3, 4, 4, 4];       
   end       
 if groupno==2,
    grp  = [ 4, 4, 4, 4, 4, 4, 4, 4;
             1, 2, 3, 4, 5, 7, 8, 9;       
             2, 3, 4, 4, 4, 3, 3, 3;       
             5, 5, 5, 5, 5, 5, 5, 5;       
             4, 4, 4, 4, 4, 4, 4, 4;       
             4, 5, 6, 6, 6, 6, 6, 6];       
   end          
 if groupno==3,
    grp  = [ 3, 5, 6, 5, 5, 4, 4, 3;
             3, 3, 3, 3, 3, 3, 3, 3;       
             3, 3, 3, 3, 3, 3, 3, 3;       
             7, 7, 7, 7, 7, 7, 7, 7;       
             3, 3, 3, 3, 3, 3, 3, 3;       
             4, 4, 4, 5, 6, 6, 7, 8];       
   end          
 if groupno==4,
    grp  = [ 3, 4, 4, 4, 4, 4, 4, 4;
             5, 5, 4, 5, 6, 6, 7, 8;       
             5, 5, 5, 5, 5, 5, 5, 5;       
             4, 4, 4, 4, 4, 4, 4, 4;       
             3, 3, 3, 3, 3, 3, 3, 3;       
             4, 4, 4, 4, 4, 4, 4, 4];       
   end          
 if groupno==5,
    grp  = [ 3, 5, 6, 7, 8, 8, 9,10;
             4, 6, 7, 7, 7, 7, 7, 7;       
             1, 2, 3, 3, 3, 3, 3, 3;       
             2, 3, 3, 3, 2, 2, 1, 1;       
             3, 3, 3, 3, 3, 3, 3, 3;       
             3, 3, 3, 3, 3, 3, 3, 3];       
   end          
 if groupno==6,
    grp  = [ 6, 9,11,12,12,13,13,14;
             3, 3, 3, 3, 3, 3, 3, 3;       
             1, 2, 3, 3, 3, 3, 3, 3;       
             2, 2, 2, 2, 2, 2, 2, 2;
            16,17,17,17,17,17,17,17;       
             5, 5, 4, 4, 5, 5, 6, 6];       
   end          
 if groupno==7,
    grp  = [ 4, 4, 4, 4, 4, 4, 4, 4;
             4, 4, 4, 4, 4, 4, 4, 4; 
             4, 4, 4, 4, 4, 4, 4, 4;
             4, 4, 4, 4, 4, 4, 4, 4;
             4, 4, 4, 4, 4, 4, 4, 4;
             4, 4, 4, 4, 4, 4, 4, 4];       
   end          
end
if caseno==2,
  ph = .2;
  if groupno==1,
    grp  = [ 5, 4, 3, 3, 4, 4, 5, 5;
             6, 6, 6, 6, 6, 6, 6, 6;       
            12,12,12,12,12,12,12,12;       
             2, 3, 4, 4, 4, 5, 5, 5;       
             2, 2, 2, 2, 2, 2, 2, 2;
             2, 2, 2, 2, 2, 2, 2, 2];       
   end       
 if groupno==2,
    grp  = [ 1, 3, 4, 3, 3, 2, 2, 1;
             1, 2, 3, 4, 5, 7, 8, 9;       
             2, 3, 4, 4, 4, 4, 4, 4;       
             3, 3, 3, 3, 3, 3, 3, 3;       
             3, 3, 3, 3, 3, 3, 3, 3;       
             2, 3, 3, 3, 3, 3, 3, 3];       
   end          
 if groupno==3,
    grp  = [ 3, 5, 7, 7, 6, 6, 5, 5;
             2, 2, 2, 2, 2, 2, 2, 2;
             2, 2, 2, 2, 2, 2, 2, 2;
             5, 5, 5, 5, 5, 5, 5, 5;       
             3, 3, 3, 3, 3, 3, 3, 3;       
             1, 2, 2, 2, 2, 2, 2, 2];       
   end          
 if groupno==4,
    grp  = [ 2, 2, 2, 2, 2, 2, 2, 2;
             3, 3, 3, 3, 3, 4, 4, 4;
             5, 5, 5, 5, 5, 5, 5, 5;       
             3, 3, 3, 3, 3, 3, 3, 3;       
             3, 3, 3, 3, 3, 3, 3, 3;       
             3, 3, 3, 3, 3, 3, 3, 3];       
   end          
 if groupno==5,
    grp  = [ 2, 3, 4, 5, 6, 7, 8, 9;
             3, 3, 3, 3, 3, 3, 3, 3;       
             3, 3, 2, 2, 2, 2, 2, 2;       
             2, 2, 2, 2, 2, 2, 2, 2;
             2, 2, 2, 2, 2, 2, 2, 2;       
            11,11,11,11,11,11,11,11];       
   end          
 if groupno==6,
    grp  = [ 6, 8, 9,10,11,11,12,13;
             1, 1, 1, 1, 1, 1, 1, 1;       
             1, 1, 1, 1, 1, 1, 1, 1;       
             2, 2, 1, 1, 1, 1, 1, 1;       
             7, 7, 7, 7, 7, 7, 7, 7;       
             5, 5, 5, 6, 6, 7, 7, 8];       
   end          
 if groupno==7,
    grp  = [ 2, 2, 2, 2, 2, 2, 2, 2;
             2, 2, 2, 2, 2, 2, 2, 2;
             2, 2, 2, 2, 2, 2, 2, 2;
             2, 2, 2, 2, 2, 2, 2, 2;
             2, 2, 2, 2, 2, 2, 2, 2;
             2, 2, 2, 2, 2, 2, 2, 2];       
   end          
 if groupno==8,
    grp  = [ 3, 3, 3, 3, 3, 3, 3, 3;
             3, 3, 3, 3, 3, 3, 3, 3;
             3, 3, 3, 3, 3, 3, 3, 3;
             3, 3, 3, 3, 3, 3, 3, 3;
             3, 3, 3, 3, 3, 3, 3, 3;
             3, 3, 3, 3, 3, 3, 3, 3];       
   end          
end

% caseM?.m contains all informations on the behavior of subjects in the respective game

grp 

M(1,:) = [qm,qm,qm,qm,qm,qm]; %uniform initial distribution of money
U(1:iter,1:6) = zeros(iter,6);


for i = 1:iter-1;
   %%% get time-values and actions
   Ttmp = rand(6);
   for p = 1:6;
      if M(i,p) > grp(p,qm)-.5,
        A(i,p) = 1;      % buy
        if Ttmp(p) < ph,
           T(i,p) = h;
        else
           T(i,p) = l;
        end
      elseif M(i,p) < 0.5,
        A(i,p) = 0;      % sell
      elseif M(i,p) < grp(p,qm),
        if Ttmp(p) < ph,
          A(i,p) = 1;      % buy
          T(i,p) = h;
        else
          A(i,p) = 0;      % sell
        end
      end
   end  

   length_buyside = sum(A(i,:));

%clear buyer;
%clear seller;

   btmp = 1;
   stmp = 1;
   for p = 1:6;
     if A(i,p) > 0.5,
       buyer(btmp) = p;
       btmp = btmp+1;
     else
       seller(stmp) = p;
       stmp = stmp+1;
     end
   end

   if length_buyside == 0 | length_buyside == 6,  % i.e. no trade possible 
       for itmp = 1:6,
           M(i+1,itmp) = M(i,itmp);
        end
       trades(i) = 0;
   end 
   if length_buyside == 1,  % i.e. sellers are rationed
       pickseller1 = ceil(5*rand);
       M(i+1,buyer(1)) = M(i,buyer(1))-1;
       U(i+1,buyer(1)) = T(i,buyer(1));
       M(i+1,seller(pickseller1)) = M(i,seller(pickseller1))+1;
       trades(i) = 1;
       for itmp = 1:5,
         if itmp ~= pickseller1,
           M(i+1,seller(itmp)) = M(i,seller(itmp));
         end
       end  
   elseif length_buyside == 2, 
       pickseller1 = ceil(4*rand);
       pickseller2 = pickseller1;
       while pickseller2 == pickseller1,
          pickseller2 = ceil(4*rand);
       end   
       M(i+1,buyer(1)) = M(i,buyer(1))-1;
       U(i+1,buyer(1)) = T(i,buyer(1));
       M(i+1,seller(pickseller1)) = M(i,seller(pickseller1))+1;
       M(i+1,buyer(2)) = M(i,buyer(2))-1;
       U(i+1,buyer(2)) = T(i,buyer(2));
       M(i+1,seller(pickseller2)) = M(i,seller(pickseller2))+1;
       trades(i) = 2;
       for itmp = 1:4,
         if itmp ~= pickseller1 & itmp ~= pickseller2,
           M(i+1,seller(itmp)) = M(i,seller(itmp));
         end
       end
   elseif length_buyside == 3, % i.e. no rationing
       M(i+1,seller(1)) = M(i,seller(1))+1;
       M(i+1,seller(2)) = M(i,seller(2))+1;
       M(i+1,seller(3)) = M(i,seller(3))+1;
       M(i+1,buyer(1)) = M(i,buyer(1))-1;
       M(i+1,buyer(2)) = M(i,buyer(2))-1;
       M(i+1,buyer(3)) = M(i,buyer(3))-1;
       U(i+1,buyer(1)) = T(i,buyer(1));
       U(i+1,buyer(2)) = T(i,buyer(2));
       U(i+1,buyer(3)) = T(i,buyer(3));
       trades(i) = 3;
   elseif length_buyside == 4, % i.e. buyers are rationed
       pickbuyer1 = ceil(4*rand);
       pickbuyer2 = pickbuyer1;
       while pickbuyer2 == pickbuyer1,
          pickbuyer2 = ceil(4*rand);
       end   
       M(i+1,seller(1)) = M(i,seller(1))+1;
       M(i+1,buyer(pickbuyer1)) = M(i,buyer(pickbuyer1))-1;
       U(i+1,buyer(pickbuyer1)) = T(i,buyer(pickbuyer1));
       M(i+1,seller(2)) = M(i,seller(2))+1;
       M(i+1,buyer(pickbuyer2)) = M(i,buyer(pickbuyer2))-1;
       U(i+1,buyer(pickbuyer2)) = T(i,buyer(pickbuyer2));
       trades(i) = 2;
       for itmp = 1:4,
         if itmp ~= pickbuyer1 & itmp ~= pickbuyer2,
           M(i+1,buyer(itmp)) = M(i,buyer(itmp));
         end
       end  
   elseif length_buyside == 5,  
       pickbuyer1 = ceil(5*rand);
       M(i+1,seller(1)) = M(i,seller(1))+1;
       M(i+1,buyer(pickbuyer1)) = M(i,buyer(pickbuyer1))-1;
       U(i+1,buyer(pickbuyer1)) = T(i,buyer(pickbuyer1));
       trades(i) = 1;
       for itmp = 1:5,
         if itmp ~= pickbuyer1,
           M(i+1,buyer(itmp)) = M(i,buyer(itmp));
         end
       end  
   end 
end

%for i = iter,
%fprintf('round %3.0f \t Money distr. %2.0f %2.0f %2.0f %2.0f %2.0f %2.0f\n',i,M(i,1),M(i,2),M(i,3),M(i,4),M(i,5),M(i,6));
%%fprintf('round %3.0f \t Trades %2.0f %2.0f %2.0f %2.0f %2.0f %2.0f\n',i,M(i,1),M(i,2),M(i,3),M(i,4),M(i,5),M(i,6));
%end

%fprintf('Total no. of trades = %6.4f \n',trades);
fprintf('Total realized time values in the economy\t %6.0f \n\n',sum(sum(U)));
fprintf('Total realized time values for each agent\t %6.0f\t %6.0f\t %6.0f\t %6.0f\t %6.0f\t %6.0f\n\n',sum(U(:,1)),sum(U(:,2)),sum(U(:,3)),sum(U(:,4)),sum(U(:,5)),sum(U(:,6)));

avu1 = sum(U(floor(.5*iter):iter,1))/(iter-floor(.5*iter)+1);
avu2 = sum(U(floor(.5*iter):iter,2))/(iter-floor(.5*iter)+1);
avu3 = sum(U(floor(.5*iter):iter,3))/(iter-floor(.5*iter)+1);
avu4 = sum(U(floor(.5*iter):iter,4))/(iter-floor(.5*iter)+1);
avu5 = sum(U(floor(.5*iter):iter,5))/(iter-floor(.5*iter)+1);
avu6 = sum(U(floor(.5*iter):iter,6))/(iter-floor(.5*iter)+1);
tavu = (avu1+avu2+avu3+avu4+avu5+avu6)/6.0;
tavt = sum(trades(floor(.5*iter)-1:iter-1))/(iter-floor(.5*iter)+1);

fprintf('Average realized time values for each agent\t %6.2f\t %6.2f\t %6.2f\t %6.2f\t %6.2f\t %6.2f\n\n',avu1,avu2,avu3,avu4,avu5,avu6);
fprintf('Total number of trades\t\t\t\t %6.0f \n\n',sum(trades));
fprintf('Average number of trades in the economy\t\t %6.3f \n\n',tavt);
fprintf('Average realized time values in the economy\t %6.3f \n',tavu);
