I have a Markov chain given as a large sparse scipy
matrix A
. (I've constructed the matrix in scipy.sparse.dok_matrix
format, but converting to other ones or constructing it as csc_matrix
are fine.)
I'd like to know any stationary distribution p
of this matrix, which is an eigenvector to the eigenvalue 1
. All entries in this eigenvector should be positive and add up to 1, in order to represent a probability distribution.
This means I want any solution for the system
(A-I) p = 0
, p.sum()=1
(where I=scipy.sparse.eye(*A.shape)
is the idententy matrix), but (A-I)
will not be of full rank, and even the whole system may be under-determined. In addition, eigenvectors with negative entries can be generated, which cannot be normalized to be valid probability distributions. Preventing negative entries in p
would be nice.
Using
scipy.sparse.linalg.eigen.eigs
is not a solution: It does not permit specifying the additive constraint. (Normalizing does not help if the eigenvectors contain negative entries.) Furthermore, it deviates quite a bit from the true results, and sometimes has problems converging, behaving worse thanscipy.linalg.eig
. (Also, I used shift-invert mode, which improves finding the types of eigenvalues I want, but not their quality. If I don't use it, it's even more overkill, because I am only interested in one particular eigenvalue,1
.)Converting to dense matrices and using
scipy.linalg.eig
is not a solution: In addition to the negative entry problem, the matrix is too large.Using
scipy.sparse.spsolve
is not an obvious solution: The matrix is either not square (when combining the additive constraint and the eigenvector condition) or not of full rank (when trying to specify them separately in some way), sometimes neither.
Is there a good way to numerically get a stationary state of a Markov chain given as sparse matrix using python? If there is a way to get an exhaustive list (and possibly also nearly stationary states), that's appreciated, but not necessary.