Home  >  Q&A  >  body text

python - Find Markov chain state transition probability matrix

A time series is as follows:

0.19
0.19
0.13
0.01
-0.03
-0.03
0.03
0.09
-0.13
-0.13
0.05
-0.03
0.03
0.09
-0.07
0.11
0.05
0.01
-0.05
-0.01
0.07
0.01
-0.15
-0.01
······

This time series has a total of 4032 values. The value space of these values ​​has 115 ways a=[-0.49,-0.47,-0.45······1.75,1.77,1.79]. Now I want to find it out. I wrote a matlab program for this 115*115 state transition probability matrix, but there was a problem with the calculation. I would like to ask the experts what to do if I use Python. As a newbie, I only know how to use if statements to implement it, but In this case, we have to write 115 if statements. How can we do it more concisely? Thank you everyone

習慣沉默習慣沉默2710 days ago1631

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-05-18 10:59:34

    I’m not sure how the Markov chain is calculated. I guess it’s a state transfer. Let’s see if the following is useful.

    a = [1,2,3,0,0,0,3,3,2,3,3,2,3,2,1,2,3]
    
    
    l = 4
    N = [[0] * l for i in range(l)]
    
    amount = 0
    for i in data:
        print i
        amount += 1
        N[i[0]][i[1]] += 1
    
    print amount
    for i in range(l):
        for j in range(l):
            N[i][j] /= float(amount)
    print N

    reply
    0
  • Cancelreply