updating scripts

This commit is contained in:
2019-07-12 02:00:12 +01:00
parent 0b1cb6ea2f
commit 22a999e502
3 changed files with 1187 additions and 0 deletions

36
markov/test.py Normal file
View File

@@ -0,0 +1,36 @@
import numpy as np
np.random.seed(1234)
n_states = 5
n_steps = 50
tolerance = 1e-5
# Random transition matrix and state vector
transitionMatrix = np.array([[0.6, 0.1, 0.3],
[0.1, 0.7, 0.2],
[0.2, 0.2, 0.6]])
# initial dist = [L, w, W]
intital_dist = np.array([1, 0, 0])
# Normalize rows in transitionMatrix
# transitionMatrix /= transitionMatrix.sum(axis=1)[:, np.newaxis]
# # Normalize p
# p /= p.sum()
# Take steps
for k in range(n_steps):
intital_dist = transitionMatrix.T.dot(intital_dist)
p_50 = intital_dist
print(f'p {p_50}')
# Compute stationary state
w, v = np.linalg.eig(transitionMatrix.T)
j_stationary = np.argmin(abs(w - 1.0))
p_stationary = v[:, j_stationary].real
p_stationary /= p_stationary.sum()
print(p_stationary)