python - Python 中的马尔可夫转移概率矩阵实现

标签 python numpy matrix scipy markov

我正在尝试计算序列的一步、两步转移概率矩阵,如下所示:

sample = [1,1,2,2,1,3,2,1,2,3,1,2,3,1,2,3,1,2,1,2]
import numpy as np

def onestep_transition_matrix(transitions):
    n = 3 #number of states

    M = [[0]*n for _ in range(n)]

    for (i,j) in zip(transitions,transitions[1:]):
        M[i-1][j-1] += 1

    #now convert to probabilities:
    for row in M:
        s = sum(row)
        if s > 0:
            row[:] = [f/s for f in row]
    return M

one_step_array = np.array(onestep_transition_matrix(sample))

我的问题是,我们如何计算两步转移矩阵。因为当我手动计算矩阵时,如下所示:

two_step_array = array([[1/7,3/7,3/7],
                       [4/7,2/7,1/7],
                       [1/4,3/4,0]])

但是。 np.dot(one_step_array,one_step_arrary) 给我一个不同的结果,如下所示:

array([[0.43080357, 0.23214286, 0.33705357],
   [0.43622449, 0.44897959, 0.11479592],
   [0.20089286, 0.59821429, 0.20089286]])

请告诉我哪一个是正确的。

最佳答案

您只需更改 for 循环中的转换索引即可:

def twostep_transition_matrix(transitions):
    n = 3 #number of states

    M = [[0]*n for _ in range(n)]

    for (i,j) in zip(transitions,transitions[2:]):
        M[i-1][j-1] += 1

    #now convert to probabilities:
    for row in M:
        s = sum(row)
        if s > 0:
            row[:] = [f/s for f in row]
    return M

关于python - Python 中的马尔可夫转移概率矩阵实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52143556/

相关文章:

Python 正则表达式 - 保持字母字符连续相邻/在数字序列内

java - 为什么我使用 java post-request 来响应状态代码 500 的内部服务器错误?

r - 计算时间序列矩阵的近似熵

java - hadoop上的矩阵乘法

python - 如何根据已知的html id编写输入数据处理器?

python - 如何使用不同行上的每个键保存到 JSON 文件

python - Tensorflow (GPU) 与 Numpy

python - 将带有索引的 numpy 数组转换为 pandas 数据框

python - 非连续拥有 numpy 数组 : do they exist & when to expect them

android - 为什么 Matrix.MutiplyMV 顺时针旋转向量?