I've generated some data from the Markov model, which represents the process of transitioning from one state to the another, in this case I've 4 states. I want to store the recurrence of this four states and then plot them together using Matplotlib. Here is my code
import numpy as np
T = np.array([ [ 0.40, 0.56, 0.03, 0.01],
[0.45, 0.51, 0.04, 0.00],
[0.25, 0.25, 0.25, 0.25 ],
[0.00, 0.00, 0.01, 0.99 ]])
xk = np.arange(len(T))
def gen_sample(state):
return np.random.choice(xk, 1, p=T[state, :])
initial_state = 0
sample_len = 100
output = [-1 for i in range(sample_len)]
output[0] = initial_state
for i in range(1, sample_len):
output[i] = gen_sample(output[i-1])[0]
print(output)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…