问题
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