数理社会学・第10章:喧嘩しても分かれない二人

mathsoc_ch11
社会を<モデル>でみる:数理社会学への招待
10章:なぜ好きなのにケンカするのか
x:xさんの行為、正の値:2人にとって良い行為;負の値:2人もしくはyさんにとって悪い行為
y:yさんの行為、正の値:2人にとって良い行為;負の値:2人もしくはxさんにとって悪い行為
x(t+1)=x(t)+a*x(t)+b*y(t)
y(t+1)=y(t)+c*x(t)+d*y(t)

# なぜ好きなのにけんかをするのか
fighting_couple<-function(a,b,c,d) {
  timeSep=0.05;ts=seq(1,50,timeSep);n_ts=length(ts)
  x=matrix(0,nrow=n_ts,ncol=1)
  y=matrix(0,nrow=n_ts,,ncol=1)
  initX=c(rep(-40,5),rep(-20,5),rep(0,5),rep(20,5),rep(40,5))
  initY=c(rep(c(-40,-20,0,20,40),5))
  initX=initX[-13]
  initY=initY[-13]
  lengthINI=length(initX)
  for (i_ini in 1:lengthINI) {  
    x[1]=initX[i_ini];y[1]=initY[i_ini];
    for (i_gen in 2:n_ts) {
       x[i_gen]=x[i_gen-1]+(a*x[i_gen-1]+b*y[i_gen-1])*timeSep
       y[i_gen]=y[i_gen-1]+(c*x[i_gen-1]+d*y[i_gen-1])*timeSep
    }  
    if (i_ini==1) {  
       plot(x,y,xlim=c(-50,50),ylim=c(-50,50),col=4,type='l',lwd=2,
         xlab="X's Action",ylab="Y's Action")
       arrows(x[2],y[2],x[3],y[3],col=4,lwd=2,length=0.15)} else {  
       lines(x, y, col=4, lwd=2)
       arrows(x[2], y[2], x[3], y[3], col=4,lwd=2, length=0.15)
    }  
  }  
}  

par(mfrow=c(2,3))
fighting_couple(-1,0.0,0.5,-1)
fighting_couple(-1,2,-1,-1)
fighting_couple(0,-1,1,0)
fighting_couple(1,-2,2,0)
fighting_couple(1,0,0.5,1)
fighting_couple(1,-4,-4,0)
fun1<-function(a,b,c,d,N,initX,initY) {
  dt=0.05;
  x=matrix(0,nrow=N,ncol=1);y=matrix(0,nrow=N,ncol=1);
  x[1]=initX;y[1]=initY;
  for (i_rep in 2:N) {
    x[i_rep]=x[i_rep-1]+(a*x[i_rep-1]+b*y[i_rep-1])*dt
    y[i_rep]=y[i_rep-1]+(c*x[i_rep-1]+d*y[i_rep-1])*dt
  }
  return(data.frame(x,y))
}
fun2<-function(a,b,c,d,N,initX, initY) { 
  res<-fun1(a,b,c,d,N,initX[1],initY[1]);
  nXYs=length(initX)
  plot(res$x,res$y,xlim=c(-50,100),ylim=c(-50,100),type='l')
  for (i_loop in 2:nXYs){
    res<-fun1(a,b,c,d,N,initX[i_loop],initY[i_loop]);
    lines(res$x,res$y,type='l')
  }
}
# 実行例
initX=rep(seq(10,50,10),5)
initY=sort(rep(seq(10,50,10),5))
fun2(0,-1,1,0,500,initX,initY) 

CLT, LLN, GCD

# CLT
nSample=10;nRep=10^5;
x=matrix(runif(nSample*nRep),nrow=nSample);
x.means<-colMeans(x)
hist(x.means,50,main='Distribution of Means of Uniform Distribution', 
 xlab='Means', probability=T)
x.means.dens<-density(x.means)
lines(x.means.dens,lwd=3,lty=1,col='red')
x2=seq(0,1,0.001);CLT=dnorm(x2,mean=0.5,sd=(sqrt(1/12))/(sqrt(nSample)))
lines(x2,CLT,lwd=3,lty=3,col='cyan')
legend("topright",c("Density of Actual Means","Normal Distribution"), 
 lty=c(1,3), col=c('red','cyan'),lwd=3)

# LAW of LARGE NUMBER
dat<-sample(1:6,1000,replace=T)
prob6<-cumsum(dat==6)/(1:1000)
par(mfrow=c(2,1))
plot(dat,type='p',col="red",main="results of 1000 rolls of a die",xlab="rolls",
 ylab="outcome", pch=20)
plot(prob6,type='l',main="Cumulative probability that rolls of a die come out SIX",
 xlab="rolls", ylab="probability",lwd=3,ylim=c(0.0,0.5))
abline(h=1/6,col='red',lwd=2,lty=2)


# WHILE
r=-99;v1=1633;v2=355
while (r!=0){
  r=v1%%v2
  print(paste('v1 =',v1,', v2 = ',v2,',remainder = ',r))
  v1=v2
  v2=r
}

# REPEAT
v1=1633;v2=355;
repeat {
  r=v1%%v2
  print(paste("v1 =",v1,",v2 = ",v2,",remainder = ",r))
  v1=v2;v2=r
  if (r==0){ break}
 }

個体群生態学 人口の推移モデル

人口の推移モデル
N_t+1=r*N_t*(1-N_t)

FUNpop_t<-function(r=2.5,n0=0.5,gen=100) {
  nt=n0;x=seq(0, 1, 0.01);y=r*x*(1-x)
  maxY=max(y,1);i_gen=1
  plot(x,y,type='l',xlim=c(-0.1, 1.1),ylim=c(0,maxY),xlab="Prop. @ time t", 
   ylab="Prop @ time t+1");
  lines(x,x,type='l')
  while ((i_gen < gen) & (nt>0)) {
    nt1=r*nt*(1-nt);
    lines(c(nt,nt),c(nt, nt1),type='l',col='red')
    lines(c(nt,nt1),c(nt1,nt1),type='l',col='red')
    nt=nt1;
    i_gen=i_gen+1;
  }
}
# example
par(mfrow=c(2,2))
FUNpop_t(r=2.7,n0=0.15,gen=100)
FUNpop_t(r=3.1,n0=0.15,gen=100)
FUNpop_t(r=3.8,n0=0.15,gen=100)
FUNpop_t(r=4.2,n0=0.15,gen=100)

population01

周期性の検証

FUNpop_t2<-function(gen=100,digit=5,n0=0.5){
  r=seq(2.5,4,0.001)
  res=matrix(0,ncol=2,nrow=1)
  r.length=length(r)
  for (i_r in 1:r.length) {
    nt=matrix(0,nrow=gen,ncol=1)
    nt[1]=n0;
    for (i_loop in 2:gen)
      {nt[i_loop]=r[i_r]*nt[i_loop-1]*(1-nt[i_loop-1])}
    bunnki<-unique(round(nt[duplicated(round(nt,digits=digit))],digits=digit))
    bunnki.length<-length(bunnki);
    res=rbind(res,cbind(rep(r[i_r],bunnki.length),bunnki))
  }
return(res[-1, ])
}
# example
res<-FUNpop_t2(gen=10000, digit=5)
plot(res[,1],res[,2],pch=19,cex=0.1,col="red",xlab="r values",ylab="cyclic points")

population02

数理社会学:なぜ社会は狭いのか?

ネットワークの作成方法の例と距離の測りかた
Regular Networks, Small-World Networks

mkRegG=function(n_node,n_edge){
# input
# n_node: number of nodes
# n_edge: number of edges / 2  
  M=matrix(0,n_node,n_node) 
  for (i_loop in 1:n_edge){
    M=M+diag(1,n_node,n_node)[, c((i_loop+1):n_node,1:i_loop)]
  }
  return(M+t(M))
}

# small-world network
G=mkRegG(100,2);
n_node=ncol(G);
prob=0.05;
for (i_node in 1:n_node) {
  node=G[i_node,]
  linked=which(node==1) 
  woLinked=which(node==0) 
  woLinked=woLinked[woLinked!=i_node]
  rwVec=linked[which(runif(length(linked)) < prob)]
  nRW=length(rwVec)
  if (nRW>0) {
    newLink=sample(woLinked,nRW)
    G[i_node,newLink]=1;G[newLink,i_node]=1
    G[i_node,rwVec]=0;G[rwVec,i_node]=0
  }
}  

# cal. shortest path
dijkstra2<-function(G,nodeID){
n_node=nrow(G)
G[which(G==0)]=Inf;diag(G)=0
d=rep(Inf,n_node);d[nodeID]=0
M=1:n_node;M=M[-nodeID]
while(length(M)>0) {
  for (j in 1:n_node) 
    {
      d[j]=min(d[j],d[nodeID]+G[nodeID,j])
    }
    nodeID=M[which(d[M]==min(d[M]))]
    n_remove=length(nodeID)
    for (i_remove in 1:n_remove){
      M=M[-which(M==nodeID[i_remove])]
    }
  }
 return(d)
}

昔のバージョン(多分)

# regular network 
mkRegG=function(n_node,n_edge){
# input
# n_node: number of nodes
# n_edge: number of edges / 2  
  M=matrix(0,n_node,n_node)	
  for (i_loop in 1:n_edge){
    M=M+diag(1,n_node,n_node)[, c((i_loop+1):n_node,1:i_loop)]
  }
  return(M+t(M))
}
# WS model (small-world)
mkWSG=function(regG,prob){
# input
# regG: regular network
# prob: probability of rewiring / 2
n_node=ncol(regG)
M=regG;
  for (i_node in 1:n_node){
    edge=which(M[i_node,]==1)
    rwVec=edge[which(runif(length(edge)) < prob)]
    nRW=length(rwVec);
    if (nRW>0) {
      newEdge=sample(seq(n_node)[-i_node],nRW);
      while (any(M[i_node,newEdge]==1) & any(M[i_node,rwVec]==1)){
        newEdge=sample(seq(n_node)[-i_node],nRW);
    }
    M[i_node,newEdge]=1;M[newEdge,i_node]=1;
    M[i_node,rwVec]=0;M[rwVec,i_node]=0;	
    }
  }
  return(M);
}
# scale-free network
mkFSG=function(n_node,n_edge) {
# input
# n_node: number of nodes
# n_edge: minimum number of edges
  M=matrix(1,n_edge+1,n_edge+1)-diag(n_edge+1)
  for (i_node in (n_edge+2):n_node) {
    Pnode=rowSums(M)/sum(M)	
    cumPnode=c(0,cumsum(Pnode))
    vec=matrix(0,1,i_node-1)
      while (sum(vec) < n_edge) {
        vec[max(which(cumPnode<=runif(1)))]=1
      }
    M=rbind(M,vec);
    M=cbind(M,c(vec,0));
  }
  return(M)
}

CLT simulation

中央極限定理の実験

source("http://peach.l.chiba-u.ac.jp/course_folder/ckCLT.txt")

# r command
genR<-function(n_rep,n_sample,prob,distID){
switch(distID,
  binom=rbinom(n_sample*n_rep,n_sample,prob),
  normal=rnorm(n_rep*n_sample),
  uniform=runif(n_rep*n_sample))
}
ckCLT=function(n_rep,n_sample,prob,Distr){
  vecR<-genR(n_rep,n_sample,prob,Distr)
  dat<-matrix(vecR,nrow=n_rep,ncol=n_sample);
  means<-rowMeans(dat)
  par(mfrow=c(2,1))
  hist(vecR,main="Dist. of the original data set")
  hist(means,main="Dist. of sample meanx",xlab="sample mean",probability=T)
  if (Distr=="binom"){
    denS=density(means,bw=0.125)
  } else {denS=density(means)}
  lines(denS,col='blue',lwd=2)
 return(means);
}

タカハトゲーム 

タカ・ハトゲーム
タカ戦略とハト戦略のみが存在する世界において進化的に安定な状態を探るゲーム。
### 設定
# pay-offは以下の通り:
タカ vs. ハト、餌(food)を総取り
タカ vs. タカ、餌からコストを引いたものを2分割
ハト vs. タカ、無し(0)
ハト vs. ハト、餌を2分割
# 引数:
個体数の初期値([タカ、ハト])、世代数、コスト、餌
# 個体数の推移
p.hawk[i_gen]=(p.hawk[i_gen-1]*(p.hawk[i_gen-1]*eHawkHawk+p.dove[i_gen-1]*eHawkDove))
 /mean.W;

HDgame<-function(N,generation,cost,food){
  N.hawk=N[1]; N.dove=N[2]; 
  p.hawk=rep(0,generation);p.hawk[1]=N.hawk/(N.dove+N.hawk)
  p.dove=rep(0,generation);p.dove[1]=1-p.hawk[1]
  eHawkHawk=0.5*(food-cost);eHawkDove=food
  eDoveHawk=0;eDoveDove=0.5*food
  for (i_gen in 2:generation){
    mean.W=p.hawk[i_gen-1]^2*eHawkHawk+p.hawk[i_gen-1]*p.dove[i_gen-1]*
     (eHawkDove+eDoveHawk)+p.dove[i_gen-1]^2*eDoveDove
    p.hawk[i_gen]=(p.hawk[i_gen-1]*(p.hawk[i_gen-1]*eHawkHawk
      +p.dove[i_gen-1]*eHawkDove))/mean.W;
    p.dove[i_gen]=1-p.hawk[i_gen]
  }
  plot(1:generation,p.hawk,type='o',col='red',ylim=c(0,1),pch=19,
    main="Result of Hawk-Dove Game",ylab="Proportion",xlab="Generation")
  lines(1:generation, p.dove,type='o',col='blue',pch=17)
  legend("topright",c("Hawk","Dove"),pch=c(19,17),col=c('red','blue'))
}
# 実行例
HDgame(c(20,80),50,10,6)

# 実装例、その2
HDgameDE<-function(N,generation,cost,food){
  N.hawk=N[1]; N.dove=N[2]; 
  tStep=0.01;ts=seq(0,generation,tStep);Nts=length(ts);
  p.hawk=rep(0,Nts);p.hawk[1]=N.hawk/(N.dove+N.hawk)
  p.dove=rep(0,Nts);p.dove[1]=1-p.hawk[1]
  eHawkHawk=0.5*(food-cost);eHawkDove=food
  eDoveHawk=0;eDoveDove=0.5*food
  for (i_gen in 2:Nts){
    mean.W=p.hawk[i_gen-1]^2*eHawkHawk+p.hawk[i_gen-1]*p.dove[i_gen-1]*
     (eHawkDove+eDoveHawk)+p.dove[i_gen-1]^2*eDoveDove
    p.hawk[i_gen]=p.hawk[i_gen-1]+(p.hawk[i_gen-1]*(p.hawk[i_gen-1]*eHawkHawk
     +p.dove[i_gen-1]*eHawkDove-mean.W)/mean.W)*tStep;
    p.dove[i_gen]=1-p.hawk[i_gen]
  }
  plot(1:Nts,p.hawk,type='l',col='red',ylim=c(0,1),lwd=5,
   main="Result of Hawk-Dove Game",ylab="Proportion",xlab="Generation")
  lines(1:Nts, p.dove,type='l',col='blue',lwd=5)
  legend("topright",c("Hawk","Dove"),lwd=5,col=c('red','blue'))
}
# 実行例
HDgameDE(c(20,80),50,10,6)

D_H01

読みやすい?例

## a simple version ##
HDgame<-function(N,food,cost,n_gen){
  p.hawk=N[1]/sum(N);p.dove=N[2]/sum(N)
  e.HH=1/2*(food-cost);e.HD=food;e.DH=0;e.DD=1/2*(food)
  p.vec=c(p.hawk,p.dove);p.mat<-outer(p.vec,p.vec)
  payMat<-matrix(c(e.HH,e.DH,e.HD,e.DD),ncol=2)
  hist=matrix(0,n_gen,2);hist[1,]=p.vec
  for (i_gen in 2:n_gen){
    w.H=sum(p.vec*payMat[1,])
    w.D=sum(p.vec*payMat[2,])
    w.mean=sum(p.mat*payMat)
    p.vec=c(p.vec[1]*w.H/w.mean,p.vec[2]*w.D/w.mean)
    p.mat<-outer(p.vec,p.vec)
    hist[i_gen,]=p.vec
  }
  plot(1:n_gen,hist[,1],pch=20,type='o',lwd=2,cex=2,col='red',
   ylim=c(0,1),ylim=c(0,1.25),xlab="time",ylab="proportion",cex.lab=2)
  lines(1:n_gen,hist[,2],pch=20,type='o',lwd=2,cex=2,col='blue')
  legend("topright",c("Hawk","Dove"),col=c('red','blue'),lwd=2,pch=20)
}

## DE version ##
HDgameDE<-function(N,food,cost,n_gen){
  p.hawk=N[1]/sum(N);p.dove=N[2]/sum(N)
  e.HH=1/2*(food-cost);e.HD=food;e.DH=0;e.DD=1/2*(food)
  p.vec=c(p.hawk,p.dove);p.mat<-outer(p.vec,p.vec)
  payMat<-matrix(c(e.HH,e.DH,e.HD,e.DD),ncol=2)
  ts=0.01;Nts=length(seq(1,n_gen,ts));
  hist=matrix(0,Nts,2);hist[1,]=p.vec
  for (i_gen in 2:Nts){
    w.H=sum(p.vec*payMat[1,])
    w.D=sum(p.vec*payMat[2,])
    w.mean=sum(p.mat*payMat)
    p.vec[1]=p.vec[1]+ts*p.vec[1]*(w.H-w.mean)/w.mean
    p.vec[2]=p.vec[2]+ts*p.vec[2]*(w.D-w.mean)/w.mean
    p.mat<-outer(p.vec,p.vec)
    hist[i_gen,]=p.vec
  }
  plot(1:Nts,hist[,1],type='l',lwd=4,cex=2,col='red',ylim=c(0,1.25),
    xlab="time",ylab="proportion",cex.lab=2)
  lines(1:Nts,hist[,2],type='l',lwd=4,cex=2,col='blue')
  legend("topright",c("Hawk","Dove"),col=c('red','blue'),lwd=4)
  return(hist)
}

認知情報解析 デマの拡散モデル

Dema_WO_cr<-function(N,ps,ave.followers,n_rep){
  # N - numbers of S, Ig, Is
  # ps - probs for S->I, Ig->I
  dt=0.01;ts=seq(1,n_rep,dt);n_ts=length(ts);
  S=matrix(0,nrow=n_ts,ncol=1);S[1]=N[1];
  Ig=matrix(0,nrow=n_ts,ncol=1);Ig[1]=N[2];
  I=matrix(0,nrow=n_ts,ncol=1);I[1]=N[3];
  N=S[1]+Ig[1]+I[1];
  F=ave.followers;
  p.S2I=ps[1];p.Ig2I=ps[2]

  # main 
  for (i_time in 1:(n_ts-1)) {
    S[i_time+1]=S[i_time]-F/N*I[i_time]*S[i_time]*dt
    Ig[i_time+1]=Ig[i_time]+((1-p.S2I)*F/N*I[i_time]*S[i_time]-p.Ig2I*F/N*Ig[i_time]*I[i_time])*dt
    I[i_time+1]=I[i_time]+(p.S2I*F/N*I[i_time]*S[i_time]+p.Ig2I*F/N*Ig[i_time]*I[i_time])*dt
  }
  # plotting results
  plot(ts,S,type="l",lwd=4,col="red", main="Distribution of rumors in Twitter "
    ,xlab="time", ylab="Proportions of People",cex.lab=1.3,ylim=c(0,S[1]))
  lines(ts,Ig,type="l",lwd=4,col="blue")
  lines(ts,I,type="l",lwd=4,col="green")
  legend("right", c("Not seen rumors nor corrections","Seen rumors", "tweeted rumours"), 
  col=c("red","blue","green"),lty=rep(1,3),lwd=4)
}

Dema_W_cr<-function(N,ps,ave.followers,n_rep){
  # N - numbers of S, Ig, Is
  # ps - probs for S->I, Ig->I
  dt=0.01;ts=seq(1,n_rep,dt);n_ts=length(ts);
  S=matrix(0,nrow=n_ts,ncol=1);S[1]=N[1];
  Ig=matrix(0,nrow=n_ts,ncol=1);Ig[1]=N[2];
  I=matrix(0,nrow=n_ts,ncol=1);I[1]=N[3];
  Rg=matrix(0,nrow=n_ts,ncol=1);Rg[1]=N[4];
  R=matrix(0,nrow=n_ts,ncol=1);R[1]=N[5];
  N=S[1]+Ig[1]+I[1]+Rg[1]+R[1];
  F=ave.followers;
  p.S2I=ps[1];p.Ig2I=ps[2];p.S2R=ps[3];p.Ig2R=ps[4];p.I2R=ps[5];p.Rg2R=ps[6];

  # main 
  for (i_time in 1:(n_ts-1)) {
    S[i_time+1]=S[i_time]+(-F/N*I[i_time]*S[i_time]-F/N*R[i_time]*S[i_time])*dt
    Ig[i_time+1]=Ig[i_time]+((1-p.S2I)*F/N*I[i_time]*S[i_time]-p.Ig2I*F/N*Ig[i_time]*I[i_time]
      -F/N*Ig[i_time]*R[i_time])*dt
    I[i_time+1]=I[i_time]+(p.S2I*F/N*I[i_time]*S[i_time]+p.Ig2I*F/N*Ig[i_time]*I[i_time]
      -F/N*I[i_time]*R[i_time])*dt
    Rg[i_time+1]=Rg[i_time]+((1-p.S2R)*F/N*R[i_time]*S[i_time]+(1-p.Ig2R)*F/N*Ig[i_time]*R[i_time]
      +(1-p.I2R)*F/N*I[i_time]*R[i_time]-p.Rg2R*F/N*Rg[i_time]*R[i_time])*dt
    R[i_time+1]=R[i_time]+(p.S2R*F/N*R[i_time]*S[i_time]+p.Ig2R*F/N*Ig[i_time]*R[i_time]
      +p.I2R*F/N*I[i_time]*R[i_time]+p.Rg2R*F/N*Rg[i_time]*R[i_time])*dt
  }
  # plotting results
  plot(ts,S,type="l",lwd=4,col="red", main="Distribution of rumors in Twitter "
    ,xlab="time", ylab="Proportions of People",cex.lab=1.3,ylim=c(0,S[1]*1.25))
  lines(ts,Ig,type="l",lwd=4,col="blue")
  lines(ts,I,type="l",lwd=4,col="green")
  lines(ts,Rg,type="l",lwd=4,col="magenta")
  lines(ts,R,type="l",lwd=4,col="cyan")
  legend("topright", c("Not seen rumors nor corrections","Seen rumors", "tweeted rumours",
    "Seen corrections","tweeted corrections"), 
  col=c("red","blue","green","magenta","cyan"),lty=rep(1,5),lwd=4)
  return(data.frame(S,Ig,I,Rg,R))
}

w=Dema_W_cr(c(10000,0,100,0,1),c(0.001,0.01,0.01,0.01,0.01,0.01),20,50)

twitterDema

evo game – fairness

10切れのケーキを2人で分け合うゲーム
戦略は0~10個を相手に要求するといった11種類。
利得は2人の要求数の和が10以下の場合、要求した数だけで、そうでない場合は0。

cakeG

# a simple implementation
N_game=50;N_slice=10;
PropDemand=runif(N_slice+1);
PropDemand=PropDemand/sum(PropDemand);
PayMat=rbind(c(rep(0,11)),c(rep(1,10),0),c(rep(2,9),0,0),c(rep(3,8),0,0,0),
 c(rep(4,7),rep(0,4)),c(rep(5,6),rep(0,5)),c(rep(6,5),rep(0,6)),c(rep(7,4),rep(0,7)),
 c(rep(8,3),rep(0,8)),c(rep(9,2),rep(0,9)),c(rep(10,1),rep(0,10)))
histPD=matrix(0,nrow=N_game,ncol=N_slice+1);
histPD[1,]=PropDemand;
for (i_gen in 2:N_game) {
  PD2=matrix(PropDemand,nrow=N_slice+1,ncol=N_slice+1,byrow=T)
  Ws=rowSums(PD2*PayMat);
  meanW=sum(outer(PropDemand,PropDemand)*PayMat);
  PropDemand=PropDemand*(Ws/meanW)
  histPD[i_gen,]=PropDemand;
}
cols=c("black","red","cyan","green","blue","magenta","pink","brown","gray","orange","black")
plot(histPD[,1],xlim=c(1,N_game),ylim=c(0,1),type='l',col='black');
for (i_slice in 2:(N_slice+1)) {
  lines(histPD[,i_slice],type='l',col=cols[i_slice])
}

# in a form of differential equations
PropDemand=runif(N_slice+1);
PropDemand=PropDemand/sum(PropDemand);
tStep=0.01;ts=seq(0,30,tStep);Nts=length(ts)
histPD=matrix(0,nrow=Nts,ncol=N_slice+1);
histPD[1,]=PropDemand;
for (i_gen in 2:Nts) {
  PD2=matrix(PropDemand,nrow=N_slice+1,ncol=N_slice+1,byrow=T)
  Ws=rowSums(PD2*PayMat);
  meanW=sum(outer(PropDemand,PropDemand)*PayMat);
  PropDemand=PropDemand+PropDemand*((Ws-meanW)/meanW)*tStep
  histPD[i_gen,]=PropDemand;
}
cols=c("black","red","cyan","green","blue","magenta","pink","brown","gray","orange","black")
plot(histPD[,1],xlim=c(1,Nts),ylim=c(0,1),type='l',col='black',lwd=3);
for (i_slice in 2:(N_slice+1)) {
  lines(histPD[,i_slice],type='l',col=cols[i_slice],lwd=3)
}

数理社会学:軍拡モデル

軍拡モデル
2国間の軍事拡大モデル

### 設定
# parameters
a: X国の自国の戦力・軍事に対する抑制率
k: Y国に戦力に対するX国の戦力の増加率
g: X国の定常的な軍事費(戦力の増加量)
b: Y国の自国の戦力・軍事に対する抑制率
l: X国に戦力に対するY国の戦力の増加率
h: Y国の定常的な軍事費(戦力の増加量)
# 戦力の推移設定
X国:dx/dt=-ax+ky+g
y国:dy/dt=lx-by+h
# 引数:
a,b,k,l,g,h (上記)
dt 時間の推移

mil_exp<-function(a,b,k,l,g,h,dt) {
  timeSep=dt;ts=seq(1,50,timeSep);n_ts=length(ts)
  x=matrix(0,nrow=n_ts,ncol=1);y=matrix(0,nrow=n_ts,,ncol=1)
  initX=c(rep(-40,5),rep(-20,5),rep(0,5),rep(20,5),rep(40,5))
  initY=c(rep(c(-40,-20,0,20,40),5))
  initX=initX[-13];initY=initY[-13]
  lengthINI=length(initX)
  for (i_ini in 1:lengthINI) {  
    x[1]=initX[i_ini];y[1]=initY[i_ini];
    for (i_gen in 2:n_ts) {
       x[i_gen]=x[i_gen-1]+(-a*x[i_gen-1]+k*y[i_gen-1]+g)*timeSep
       y[i_gen]=y[i_gen-1]+(l*x[i_gen-1]-b*y[i_gen-1]+h)*timeSep
    }  
    if (i_ini==1) {  
       plot(x,y,xlim=c(-40,40),ylim=c(-40,40),col=4,type='l',lwd=2,
         xlab="Nation X's Military Force",ylab="Nation X's Military Force")
       arrows(x[2],y[2],x[3],y[3],col=4,lwd=2,length=0.15)
  } else {  
       lines(x, y, col=4, lwd=2)
       arrows(x[2], y[2], x[3], y[3], col=4,lwd=2, length=0.15)
    }  
  }  
}  
mil_exp(1,2,0.5,0.1,10,10,0.05)

mil_exp