-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_eigen_markov.py
233 lines (170 loc) · 5.68 KB
/
test_eigen_markov.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
from __future__ import division, print_function
import mpmath as mp
from eigen_markov import stoch_eig, gth_solve
mp.stoch_eig = mp.mp.stoch_eig
mp.gth_solve = mp.mp.gth_solve
import pytest
VERBOSE = 1
# Generate test cases
def KMR_Markov_matrix_sequential(N, p, epsilon):
"""
Generate a Markov matrix arising from a certain game-theoretic model
Parameters
----------
N : int
p : float
Between 0 and 1
epsilon : float
Between 0 and 1
Returns
-------
P : matrix of shape (N+1, N+1)
"""
P = mp.zeros(N+1, N+1)
P[0, 0], P[0, 1] = 1 - epsilon * (1/2), epsilon * (1/2)
for n in range(1, N):
P[n, n-1] = \
(n/N) * (epsilon * (1/2) +
(1 - epsilon) * (((n-1)/(N-1) < p) + ((n-1)/(N-1) == p) * (1/2))
)
P[n, n+1] = \
((N-n)/N) * (epsilon * (1/2) +
(1 - epsilon) * ((n/(N-1) > p) + (n/(N-1) == p) * (1/2))
)
P[n, n] = 1 - P[n, n-1] - P[n, n+1]
P[N, N-1], P[N, N] = epsilon * (1/2), 1 - epsilon * (1/2)
return P
# Stochastic matrix instances
Ps = []
Ps.append(
mp.matrix([[0.9 , 0.075, 0.025],
[0.15, 0.8 , 0.05 ],
[0.25, 0.25 , 0.5 ]])
)
Ps.append(KMR_Markov_matrix_sequential(N=3, p=1./3, epsilon=1e-14))
Ps.append(KMR_Markov_matrix_sequential(N=27, p=1./3, epsilon=1e-2))
# Transition rate matrix instances
As = [P.copy() for P in Ps]
for A in As:
for i in range(A.rows):
A[i, i] = -mp.fsum((A[i, j] for j in range(A.cols) if j != i))
def run_stoch_eig(P, verbose=0):
"""
stoch_eig returns a stochastic vector x such that x P = x
for an irreducible stochstic matrix P.
"""
if verbose > 1:
print("original matrix (stoch_eig):\n", P)
x = mp.stoch_eig(P)
if verbose > 1:
print("x\n", x)
eps = mp.exp(0.8 * mp.log(mp.eps)) # From test_eigen.py
# x is a left eigenvector of P with eigenvalue unity
err0 = mp.norm(x*P-x, p=1)
if verbose > 0:
print("|xP - x| (stoch_eig):", err0)
assert err0 < eps
# x is a nonnegative vector
if verbose > 0:
print("min(x) (stoch_eig):", min(x))
assert min(x) >= 0 - eps
# 1-norm of x is one
err1 = mp.fabs(mp.norm(x, p=1) - 1)
if verbose > 0:
print("||x| - 1| (stoch_eig):", err1)
assert err1 < eps
def run_gth_solve(A, verbose=0):
"""
gth_solve returns a stochastic vector x such that x A = 0
for an irreducible transition rate matrix A.
"""
if verbose > 1:
print("original matrix (gth_solve):\n", A)
x = mp.gth_solve(A)
if verbose > 1:
print("x\n", x)
eps = mp.exp(0.8 * mp.log(mp.eps)) # test_eigen.py
# x is a solution to x A = 0
err0 = mp.norm(x*A, p=1)
if verbose > 0:
print("|xA| (gth_solve):", err0)
assert err0 < eps
# x is a nonnegative vector
if verbose > 0:
print("min(x) (gth_solve):", min(x))
assert min(x) >= 0 - eps
# 1-norm of x is one
err1 = mp.fabs(mp.norm(x, p=1) - 1)
if verbose > 0:
print("||x| - 1| (gth_solve):", err1)
assert err1 < eps
#######################
def test_stoch_eig_fixed_matrix():
for P in Ps:
run_stoch_eig(P, verbose=VERBOSE)
def test_gth_solve_fixed_matrix():
for A in As:
run_gth_solve(A, verbose=VERBOSE)
def test_stoch_eig_randmatrix():
N = 5
for j in range(10):
P = mp.randmatrix(N, N)
for i in range(N):
P[i, :] /= mp.fsum(P[i, :])
run_stoch_eig(P, verbose=VERBOSE)
def test_gth_solve_randmatrix():
N = 5
for j in range(10):
A = mp.randmatrix(N, N)
for i in range(N):
A[i, :] /= mp.fsum(A[i, :])
A[i, i] = -mp.fsum((A[i, j] for j in range(N) if j != i))
run_gth_solve(A, verbose=VERBOSE)
def test_stoch_eig_high_prec():
n = 1e-100
with mp.workdps(100):
P = mp.matrix([[1-3*(mp.exp(n)-1), 3*(mp.exp(n)-1)],
[mp.exp(n)-1 , 1-(mp.exp(n)-1)]])
run_stoch_eig(P, verbose=VERBOSE)
def test_gth_solve_high_prec():
n = 1e-100
with mp.workdps(100):
P = mp.matrix([[-3*(mp.exp(n)-1), 3*(mp.exp(n)-1)],
[mp.exp(n)-1 , -(mp.exp(n)-1) ]])
run_gth_solve(P, verbose=VERBOSE)
def test_stoch_eig_fp():
P = mp.fp.matrix([[0.9 , 0.075, 0.025],
[0.15, 0.8 , 0.05 ],
[0.25, 0.25 , 0.5 ]])
x_expected = mp.fp.matrix([[0.625, 0.3125, 0.0625]])
x = mp.fp.stoch_eig(P)
eps = mp.exp(0.8 * mp.log(mp.eps)) # test_eigen.py
err0 = mp.norm(x-x_expected, p=1)
assert err0 < eps
def test_gth_solve_fp():
P = mp.fp.matrix([[-0.1, 0.075, 0.025],
[0.15, -0.2 , 0.05 ],
[0.25, 0.25 , -0.5 ]])
x_expected = mp.fp.matrix([[0.625, 0.3125, 0.0625]])
x = mp.fp.gth_solve(P)
eps = mp.exp(0.8 * mp.log(mp.eps)) # test_eigen.py
err0 = mp.norm(x-x_expected, p=1)
assert err0 < eps
def test_stoch_eig_iv():
P = mp.iv.matrix([[0.9 , 0.075, 0.025],
[0.15, 0.8 , 0.05 ],
[0.25, 0.25 , 0.5 ]])
x_expected = mp.matrix([[0.625, 0.3125, 0.0625]])
x_iv = mp.iv.stoch_eig(P)
for value, interval in zip(x_expected, x_iv):
assert value in interval
def test_gth_solve_iv():
P = mp.iv.matrix([[-0.1, 0.075, 0.025],
[0.15, -0.2 , 0.05 ],
[0.25, 0.25 , -0.5 ]])
x_expected = mp.matrix([[0.625, 0.3125, 0.0625]])
x_iv = mp.iv.gth_solve(P)
for value, interval in zip(x_expected, x_iv):
assert value in interval
if __name__ == '__main__':
pytest.main()