Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
马尔可夫链平稳分布的Python代码解释_Python_Python 3.x_Numpy - Fatal编程技术网

马尔可夫链平稳分布的Python代码解释

马尔可夫链平稳分布的Python代码解释,python,python-3.x,numpy,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[:

我有这个密码:

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]
我得到了输出:

:  [ 0.6144763   0.57607153  0.53766676  0.03840477]
但它并没有给出相同的输出。为什么

stational=np.数组(U[:,np.其中(np.abs(S-1.)<1e-8)[0][0]]平坦)
stationary = np.array(U[:,np.where(np.abs(S-1.) < 1e-8)[0][0]].flat)
这段代码正在搜索U中的元素,其对应的特征值-1小于1e-8

stational=np.array(U[:,np.where(np.abs(s-1.)<1e-8)[0][0]]
这段代码正在搜索U中的元素,该元素对应的特征值-1小于1e-8

如何找到平稳分布。 好的,我来这篇文章是想看看是否有一个内置的方法来找到平稳分布。看起来没有。因此,对于来自谷歌的任何人来说,在这种情况下,我会这样发现固定分布:

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)
然后,您的平稳分布将匹配。

如何找到平稳分布。 好的,我来这篇文章是想看看是否有一个内置的方法来找到平稳分布。看起来没有。因此,对于来自谷歌的任何人来说,在这种情况下,我会这样发现固定分布:

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)

然后,您的固定分布将匹配。

Hm。。。它看起来像是从U中抓取元素,其中s中的对应元素的值为1。。。但是我不确定那是做什么用的。然后,它会将整个过程展平为一维数组。嗨,凯文,谢谢你的回答。你能告诉我这个代码:where(np.abs(S-1.)<1e-8)应该如何工作吗?你在一个简单的例子中试过吗?阅读任何文档?到底是什么让你困惑?@Jornsharpe,是的,我还在努力。我已经用我的代码更新了这个问题。请查收。谢谢。这里的页边空白太小,不能全部写下来。只要仔细阅读
numpy.where
的文档,并尝试一下示例!;)陛下它看起来像是从U中抓取元素,其中s中的对应元素的值为1。。。但是我不确定那是做什么用的。然后,它会将整个过程展平为一维数组。嗨,凯文,谢谢你的回答。你能告诉我这个代码:where(np.abs(S-1.)<1e-8)应该如何工作吗?你在一个简单的例子中试过吗?阅读任何文档?到底是什么让你困惑?@Jornsharpe,是的,我还在努力。我已经用我的代码更新了这个问题。请查收。谢谢。这里的页边空白太小,不能全部写下来。只要仔细阅读
numpy.where
的文档,并尝试一下示例!;)
stationary = matrix/matrix.sum()