Reputation: 67
I am trying to use Markov Chain in python by using, random number selection with particular probability. I found a code from youtube video, but I think I am missing something. I want to generate random sequence of numbers (either "0" or "1") here is the code:
n = 4 #steps
start_state = 0
prev_state = start_state
A =np.array([[0.5, 0.5], [0, 1]])
while n-1:
curr_state= np.random.choice([0, 1], p=A[prev_state])
print(state[curr_state])
prev_state = start_state
n -= 1
print("stop")
Matrix A is the transition matrix, according to this if the system goes to state "1" it cannot go to state "0" (zero probability in the matrix A).
So I must NOT get a sequence like s1=[1 0 1] or s2=[1 0 0] but a sequence like this: s3=[1 1 1]. The rest sequence I get are fine.
could you help me with that? probably I am missing something of what the code does.
thanks in advance!
Upvotes: 1
Views: 1287
Reputation: 375
Firstly, i'd like to highlight to you that in state 1, your probability matrix is [0,1], any time you land in state 1, you will be stuck there because the probability of transitioning back to 0 is 0.
Secondly, the issue lies in the line prev_state = start_state
. It should be prev_state = curr_state
instead.
Upvotes: 1