Markov Transition Probability Matrix Implementation in Python

江枫思渺然 提交于 2019-12-24 00:27:44

问题


I am trying to calculate one-step, two-step transition probability matrices for a sequence as shown below :

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))

My question, how do we calculate two step transition matrix. because when I manually calculate the matrix it is as below :

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

However. np.dot(one_step_array,one_step_arrary) gives me a result which is different and as follows :

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

Please let me know which one is correct.


回答1:


You just have to change the transitions index in your for loop:

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


来源:https://stackoverflow.com/questions/52143556/markov-transition-probability-matrix-implementation-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!