马尔可夫链静态分布的Python代码解释

时间:2015-08-03 15:56:08

标签: python python-3.x numpy

我有这段代码:

import numpy as np
from scipy.linalg import eig 
transition_mat = np.matrix([
    [.95, .05, 0., 0.],\
    [0., 0.9, 0.09, 0.01],\
    [0., 0.05, 0.9, 0.05],\
    [0.8, 0., 0.05, 0.15]])

S, U = eig(transition_mat.T)
stationary = np.array(U[:, np.where(np.abs(S - 1.) < 1e-8)[0][0]].flat)
stationary = stationary / np.sum(stationary)

>>> print stationary
[ 0.34782609  0.32608696  0.30434783  0.02173913]

但我无法理解这句话:

stationary = np.array(U[:, np.where(np.abs(S - 1.) < 1e-8)[0][0]].flat)

任何人都可以解释这部分:U[:, np.where(np.abs(S - 1.) < 1e-8)[0][0]].flat

我知道例程返回S:特征值,U:特征向量。我需要找到对应于特征值1的特征向量。我已经编写了下面的代码:

for i in range(len(S)):
    if S[i] == 1.0:
        j = i

 matrix = np.array(U[:, j].flat)

我得到了输出:

:  [ 0.6144763   0.57607153  0.53766676  0.03840477]

但它没有提供相同的输出。为什么?!

2 个答案:

答案 0 :(得分:1)

如何找到平稳分布。

好吧,我来到这篇文章,看看是否有内置的方法来找到固定分布。好像没有。因此,对于来自Google的任何人,在这种情况下,这就是我要找到固定分布的方式:

import numpy as np

#note: the matrix is row stochastic.
#A markov chain transition will correspond to left multiplying by a row vector.
Q = np.array([
    [.95, .05, 0., 0.],
    [0., 0.9, 0.09, 0.01],
    [0., 0.05, 0.9, 0.05],
    [0.8, 0., 0.05, 0.15]])

#We have to transpose so that Markov transitions correspond to right multiplying by a column vector.  np.linalg.eig finds right eigenvectors.
evals, evecs = np.linalg.eig(Q.T)
evec1 = evecs[:,np.isclose(evals, 1)]

#Since np.isclose will return an array, we've indexed with an array
#so we still have our 2nd axis.  Get rid of it, since it's only size 1.
evec1 = evec1[:,0]

stationary = evec1 / evec1.sum()

#eigs finds complex eigenvalues and eigenvectors, so you'll want the real part.
stationary = stationary.real

那条怪异的线在做什么。

让我们把这行分成几部分:

#Find the eigenvalues that are really close to 1.
eval_close_to_1 = np.abs(S-1.) < 1e-8

#Find the indices of the eigenvalues that are close to 1.
indices = np.where(eval_close_to_1)

#np.where acts weirdly.  In this case it returns a 1-tuple with an array of size 1 in it.
the_array = indices[0]
index = the_array[0]

#Now we have the index of the eigenvector with eigenvalue 1.
stationary = U[:, index]

#For some really weird reason, the person that wrote the code
#also does this step, which is completely redundant.
#It just flattens the array, but the array is already 1-d.
stationary = np.array(stationary.flat)

如果将所有这些代码行压缩为一行,则会得到stationary = np.array(U[:, np.where(np.abs(S-1.)<1e-8)[0][0]].flat)

如果您删除多余的内容,则会得到stationary = U[:, np.where(np.abs(S - 1.) < 1e-8)[0][0]]

为什么您的代码给出了不同的平稳向量。

正如@Forzaa所指出的,您的向量不能表示概率向量,因为它的总和不等于1。如果将其除以它的总和,您将获得原始代码段具有的向量。

只需添加以下行:

stationary = matrix/matrix.sum()

您的固定分布将匹配。

答案 1 :(得分:0)

stationary = np.array(U[:,np.where(np.abs(S-1.) < 1e-8)[0][0]].flat)

这段代码正在搜索 U 中相应特征值的元素 - 1小于1e-8