稳态概率(马尔可夫链)Python 实施
Steady State Probabilities (Markov Chain) Python Implementation
您好,我正在尝试为转移概率矩阵生成稳态概率。这是我正在使用的代码:
import numpy as np
one_step_transition = array([[0.125 , 0.42857143, 0.75 ],
[0.75 , 0.14285714, 0.25 ],
[0.125 , 0.42857143, 0. ]])
def steady_state_prop(p):
dim = p.shape[0]
q = (p-np.eye(dim))
ones = np.ones(dim)
q = np.c_[q,ones]
QTQ = np.dot(q, q.T)
bQT = np.ones(dim)
return np.linalg.solve(QTQ,bQT)
steady_state_matrix = steady_state_prop(one_step_transition.transpose())
print (steady_state_matrix)
#result is :
#array([0.38268793, 0.39863326, 0.21867882])
#Expected Result = (0.4,0.4,0.2)
我的问题是为什么结果与确切答案略有不同?
预期的结果是错误的。对于稳定状态,转移矩阵与稳定状态的乘积必须再次成为稳定状态。
tobe = np.array(((0.4, 0.4, 0.2)))
print(tobe)
print(np.dot(one_step_transition.T, tobe))
print()
result = steady_state_prop(one_step_transition)
print(result)
print(np.dot(one_step_transition.T, result))
print()
输出为
[0.4 0.4 0.2]
[0.37142857 0.40714286 0.22142857]
[0.38268793 0.39863326 0.21867882]
[0.38268793 0.39863326 0.21867882]
所以你的函数似乎是正确的,你期望的结果不是。
我使用了不同的方法来解决问题:
def Markov_Steady_State_Prop(p):
p = p - np.eye(p.shape[0])
for ii in range(p.shape[0]):
p[0,ii] = 1
P0 = np.zeros((p.shape[0],1))
P0[0] = 1
return np.matmul(np.linalg.inv(p),P0)
结果与你的相同,我认为你的预期结果有某种错误或者它们是近似版本。
您好,我正在尝试为转移概率矩阵生成稳态概率。这是我正在使用的代码:
import numpy as np
one_step_transition = array([[0.125 , 0.42857143, 0.75 ],
[0.75 , 0.14285714, 0.25 ],
[0.125 , 0.42857143, 0. ]])
def steady_state_prop(p):
dim = p.shape[0]
q = (p-np.eye(dim))
ones = np.ones(dim)
q = np.c_[q,ones]
QTQ = np.dot(q, q.T)
bQT = np.ones(dim)
return np.linalg.solve(QTQ,bQT)
steady_state_matrix = steady_state_prop(one_step_transition.transpose())
print (steady_state_matrix)
#result is :
#array([0.38268793, 0.39863326, 0.21867882])
#Expected Result = (0.4,0.4,0.2)
我的问题是为什么结果与确切答案略有不同?
预期的结果是错误的。对于稳定状态,转移矩阵与稳定状态的乘积必须再次成为稳定状态。
tobe = np.array(((0.4, 0.4, 0.2)))
print(tobe)
print(np.dot(one_step_transition.T, tobe))
print()
result = steady_state_prop(one_step_transition)
print(result)
print(np.dot(one_step_transition.T, result))
print()
输出为
[0.4 0.4 0.2]
[0.37142857 0.40714286 0.22142857]
[0.38268793 0.39863326 0.21867882]
[0.38268793 0.39863326 0.21867882]
所以你的函数似乎是正确的,你期望的结果不是。
我使用了不同的方法来解决问题:
def Markov_Steady_State_Prop(p):
p = p - np.eye(p.shape[0])
for ii in range(p.shape[0]):
p[0,ii] = 1
P0 = np.zeros((p.shape[0],1))
P0[0] = 1
return np.matmul(np.linalg.inv(p),P0)
结果与你的相同,我认为你的预期结果有某种错误或者它们是近似版本。