# 数理社会学: なぜ協力するのか

09章：なぜ裏切ったほうが得なのに協力するのか

payoff matrix =
-20(defect,defect) -25(coop,defect)
0(defect,coop) -5(coop, coop)

```# player1   0:defect  1:coop
# player2 -10:defect 10:coop
#  -10:defDef, 10:defCoop
#    9:coopDef 11:coopCoop

calcPO<-function(points,payMat){
n_play<-length(points)
pO<-matrix(0,nrow=2,ncol=n_play)
pO[,points==-10]=c(payMat[1,1],payMat[1,1])
pO[,points==10]=c(payMat[2,1],payMat[1,2])
pO[,points==-9]=c(payMat[1,2],payMat[2,1])
pO[,points==11]=c(payMat[2,2],payMat[2,2])
return(t(pO))
}

# env set-up
n_play=1000000
payMat=matrix(c(-20,0,-25,-5),nrow=2)
result=matrix(0,4,4)
colnames(result)<-c("random","tit-for-tat","defector","cooperator")
rownames(result)<-c("random","tit-for-tat","defector","cooperator")

# scenario01 random vs. random
p1<-sample(c(0,1),n_play,replace=T)
p2<-sample(c(-10,10),n_play,replace=T)
pO<-calcPO(rowSums(cbind(p1,p2)),payMat)
result[1,1]=mean(colSums(pO))
# scenario02 random vs. tit for tat
p1<-sample(c(0,1),n_play,replace=T)
p2<-c(10,p1[1:99]*20-10)
pO<-calcPO(rowSums(cbind(p1,p2)),payMat)
result[2,1]=sum(pO[,1])
result[1,2]=sum(pO[,2])
# scenario03 random vs. defector
p1<-sample(c(0,1),n_play,replace=T)
p2<-rep(-10,n_play)
pO<-calcPO(rowSums(cbind(p1,p2)),payMat)
result[3,1]=sum(pO[,1])
result[1,3]=sum(pO[,2])
# scenario04 random vs. cooperator
p1<-sample(c(0,1),n_play,replace=T)
p2<-rep(10,n_play)
pO<-calcPO(rowSums(cbind(p1,p2)),payMat)
result[4,1]=sum(pO[,1])
result[1,4]=sum(pO[,2])
# scenario05&07&10 {tit for tat}{cooperator} vs. {tit for tat}{cooperator}
pO<-calcPO(rep(11,n_play),payMat)
result[2,2]=sum(pO[,1])
result[2,4]=sum(pO[,2])
result[4,2]=sum(pO[,2])
result[4,4]=sum(pO[,2])
# scenario06 tit for tat vs. defector
pO<-calcPO(c(-9,rep(-10,n_play-1)),payMat)
result[3,2]=sum(pO[,1])
result[2,3]=sum(pO[,2])
# scenario08 defector vs. defector
pO<-calcPO(rep(-10,n_play),payMat)
result[3,3]=sum(pO[,1])
# scenario09 defector vs. cooperator
pO<-calcPO(rep(10,n_play),payMat)
result[4,3]=sum(pO[,1])
result[3,4]=sum(pO[,2])

> colMeans(result)
random tit-for-tat    defector  cooperator
-12349020   -10660476   -12504475   -12498240
```