我需要计算与马尔可夫链相关的平稳分布。我尝试了三种不同的方法: 1)基于线性系统的解 2)基于固定分布的迭代 3)基于最大特征值(lambda = 1)的评估
我如何改进其中一种方法来处理大型稀疏矩阵(5000-7500个节点)。现在,对于完整矩阵,方法(2)是最快的。
import time
import numpy as np
from scipy.linalg import norm
def solveStationaryUsingSystem( A ):
t0 = time.time()
n = A.shape[0]
a = np.eye( n ) - A
a = np.vstack( (a.T, np.ones( n )) )
b = np.matrix( [0] * n + [ 1 ] ).T
print ("System time: ", time.time() - t0)
return np.linalg.lstsq( a, b )[0]
def solveStationaryByIterating( A ):
t0 = time.time()
nStates=np.shape(A)[0]
p = np.random.rand(nStates,1)
p /= p.sum()
n=50
k=0
pOld=np.zeros([nStates,1])
epsilon=1e-8
theNorm=norm(p-pOld)
pOld=p
while k < n or theNorm>epsilon:
p = A.T.dot(p)
theNorm=norm(p-pOld)
pOld=p
k=k+1
print ("Iterating time: ", time.time() - t0)
return p
def solveStationaryByEigenvalue( A ):
t0 = time.time()
nStates=np.shape(A)[0]
p = np.random.rand(nStates,1)
p /= p.sum()
w, v = np.linalg.eig(A.T)
j_stationary = np.argmin(abs(w - 1.0))
p_stationary = v[:,j_stationary].real
p_stationary /= p_stationary.sum()
print ("Eigenvalue time: ", time.time() - t0)
return(p_stationary)
if __name__=="__main__":
nStates=5000
transitionMatrix=np.random.rand(nStates, nStates)
transitionMatrix /= transitionMatrix.sum(axis=1)[:,np.newaxis]
print('system')
pS=solveStationaryUsingSystem(transitionMatrix)
print('iteratiion')
pI=solveStationaryByIterating(transitionMatrix)
print('eigenvalue')
pE=solveStationaryByEigenvalue(transitionMatrix)