python转移矩阵_在Python中生成马尔可夫转移矩阵

  • Post author:
  • Post category:python


Imagine I have a series of 4 possible Markovian states (A, B, C, D):

X = [A, B, B, C, B, A, D, D, A, B, A, D, ….]

How can I generate a Markov transformation matrix using Python? The matrix must be 4 by 4, showing the probability of moving from each state to the other 3 states.

I’ve been looking at many examples online but in all of them, the matrix is given, not calculated based on data.

I also looked into hmmlearn but nowhere I read on how to have it spit out the transition matrix. Is there a library that I can use for this purpose?

解决方案

This might give you some ideas:

transitions = [‘A’, ‘B’, ‘B’, ‘C’, ‘B’, ‘A’, ‘D’, ‘D’, ‘A’, ‘B’, ‘A’, ‘D’]

def rank(c):

return ord(c) – ord(‘A’)

T = [rank(c) for