clear all;
% cooperation: 0
% defect: 1
B=[1,0.8,0.7,0.65]% hyperbolic discount factors
Q=[0.01,0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.09,0.1]% probability of forgiveness
N=200000; % number of games
A=50; Z=12; D=25; AC=40;% payoff matrix
deltaE=0.5;
%in the model, the expected value of R(i+1) is (1-s)R(i)+AC*s
R(1)=AC; sen=0.1; DEV=8;
for i=2:N;
r1(i)=rand;
r2(i)=rand;
R(i)=R(i-1)+sen*((AC+DEV)-R(i-1))*r1(i) - sen*(R(i-1)-(AC-DEV))*r2(i) ;
end
for bi=1:length(B);
beta=B(bi);
for qi=1:length(Q)
q=Q(qi);
beta
q
delta=deltaE/(beta + deltaE*(1-beta));
% threthold value:
WA = @(d,C) (A*d*(1-d)*(1-q)^2+C*q*(1+d*(1-q))+Z*(1-d)*(1-q)) / ((1-d)*(1-d^2*(1-q)^2));
LE= @(d,C) C + beta*d*C/(1-d);
RE = @(d,C) A + beta*d*WA(d,C);
d=delta;
fun = @(C) LE(d,C)-RE(d,C);
x = fzero(fun,40);
%DR=sum(R < x)/length(R)
%De=(R x; s(1,i)=0;
else s(1,i)=1; end
else
if rand