In [1]:
import numpy as np
from mc_compute_stationary_mpmath import mc_compute_stationary_mpmath
In [2]:
A = [[.4, .6], [.2, .8]]
mc_compute_stationary_mpmath(A) # default precision = 17
Out[2]:
In [3]:
type(_[0][0])
Out[3]:
In [4]:
mc_compute_stationary_mpmath(A, precision=100)
Out[4]:
In [5]:
B = np.identity(2)
mc_compute_stationary_mpmath(B)
Out[5]:
In [6]:
C = np.identity(3)
mc_compute_stationary_mpmath(C)
Out[6]:
Let us try matrices generated by the KMR model where action 1 is risk-dominant, so that the theory says that the unique stationary distribution is close to the distribution that concentrates on the state $N$ (where the state is the number of players playing action 1).
In [7]:
from test_mc_compute_stationary import KMR_Markov_matrix_sequential, KMR_Markov_matrix_simultaneous
In [8]:
P = KMR_Markov_matrix_sequential(N=3, p=1./3, epsilon=1e-14)
eigvecs_P = mc_compute_stationary_mpmath(P)
In [9]:
eigvecs_P
Out[9]:
In [10]:
[np.dot(vec, P) for vec in eigvecs_P]
Out[10]:
In [11]:
Q = KMR_Markov_matrix_simultaneous(N=5, p=1./3, epsilon=1e-15)
mc_compute_stationary_mpmath(Q)
Out[11]:
In [12]:
mc_compute_stationary_mpmath(Q, precision=100)
Out[12]:
In [13]:
R = KMR_Markov_matrix_sequential(N=27, p=1./3, epsilon=1e-2)
eigvecs_R = mc_compute_stationary_mpmath(R)
In [14]:
eigvecs_R
Out[14]:
In [15]:
[np.dot(vec, R) for vec in eigvecs_R]
Out[15]:
mc_compute_stationary_mpmath uses mpmath.eig
from mpmath.
In [16]:
import sympy.mpmath as mp
In [17]:
mp.__version__
Out[17]:
The working precision is controlled by a context object called mp
.
See: Setting the precision
In [18]:
print mp.mp
The routine eig
solves the eigenvalue problem.
See: The eigenvalue problem
In [19]:
E_A, EL_A = mp.eig(mp.matrix(A), left=True, right=False)
E_A, EL_A[0, :]/sum(EL_A[0, :]), EL_A[1, :]/sum(EL_A[1, :])
Out[19]:
Let us see what happens if we increase the precision.
See: Setting the precision
In [20]:
mp.mp.dps += 15
In [21]:
E_A, EL_A = mp.eig(mp.matrix(A), left=True, right=False)
E_A, EL_A[0, :]/sum(EL_A[0, :]), EL_A[1, :]/sum(EL_A[1, :])
Out[21]:
Arithemtic seems to be more accurate if the mpf
values are created from strings.
See: Providing correct input
In [22]:
AA = np.array(A).astype('str')
AA
Out[22]:
In [23]:
E_AA, EL_AA = mp.eig(mp.matrix(AA), left=True, right=False)
E_AA, EL_AA
Out[23]:
In [24]:
EL_AA[1, :]/sum(EL_AA[1, :])
Out[24]:
KMR Markov matrices:
In [25]:
E_P, EL_P = mp.eig(mp.matrix(P), left=True, right=False)
In [26]:
E_P, EL_P
Out[26]:
In [27]:
E_Q, EL_Q = mp.eig(mp.matrix(Q), left=True, right=False)
In [28]:
E_Q
Out[28]:
In [29]:
E_R, EL_R = mp.eig(mp.matrix(R), left=True, right=False)
In [30]:
E_R
Out[30]:
In [30]: