In [1]:
%matplotlib inline
In [2]:
from __future__ import division
import numpy as np
In [73]:
def kmr_markov_matrix(p=1/3, N=6, epsilon=0.0001):
"""
Generate the transition probability matrix for the KMR dynamics with
two acitons.
"""
P = np.zeros((N+1, N+1))
P[0,0] = 1 - epsilon
P[0,1] = epsilon
P[N,N] = 1 - epsilon
P[N,N-1] = epsilon
for n in range(1, N):
if (n-1)/(N-1) < p:
a1_ratio = (n-1)/(N-1)
elif (n-1)/(N-1) == p:
a1_ratio = (n-1)/(N-1)/2
else:
a1_ratio = 0
P[n, n-1] = (n/N) * ((epsilon/2) + (1-epsilon)*(a1_ratio))
if (n / (N-1)) > p:
a2_ratio = n / (N-1)
elif n / (N-1) == p:
a2_ratio = n / (N-1) / 2
else:
a2_ratio = 0
P[n, n+1] = ((N-n)/N) * ((epsilon/2) + ((1-epsilon) * a2_ratio))
P[n, n] = 1 - P[n, n-1] - P[n, n+1]
return P
In [50]:
import quantecon as qe
P = kmr_markov_matrix()
mc = qe.MarkovChain(P)
In [62]:
mc
Out[62]:
In [63]:
mc.stationary_distributions
Out[63]:
In [90]:
mc.simulate(0, 100)
Out[90]:
In [65]:
#MarkovChain クラスのメソッドを使うところまでできました。
In [66]:
import matplotlib.pyplot as plt
In [83]:
fig, ax = plt.subplots()
ax.plot(mc.simulate(0, 10000))
show = plt.show()
In [96]:
t = 10**4
x = np.array(range(t))
y = mc.simulate(0, t)
plt.plot(x, y)
plt.show()
In [ ]: