I am playing around with Markov-Chains and depending on the number of states I need the same amount of ifs.
So if I happen to have a chain with 4 (from 0 to 3) states, my logic is as follows:
startingstate = <is a variable>
if state == 0:
change = <some code that tells me what the state has changed to if it has>
if change == 0:
<do somethin01>
elif change == 1:
<do something1>
elif change == 2:
<do something2>
elif change == 3:
<do something3>
elif state == 1:
<same stuff as above>
and so on until we get to the last else of state == 3
Usually when I had such problems that required weird loop fiddling, there was some easier logic to get it done, but I cannot wrap my head around this. Do I have to go with a variable amount of elifs? If yes how do I do it? Or is there a better way?
Thanks!:)
My current attempt looks like this:
b = 3 #amount of possible states
prob = 1
s= 0 #helper variable
init_state = 0
state_list = []
forecast = 6
f = 0
while f != forecast:
while s < b:
o=0 #helper variable
if init_state == s:
change = np.random.choice(transitionName[s],replace=True,p=transitionMatrix[s])
print(change)
while o < b:
if change == transitionName[s][o]:
prob = prob * transitionMatrix[s][o]
init_state = o
print(prob)
state_list.append(s)
o+=1
s+=1
f+=1
with:
transitionName
Out[83]:
[['0 to 0', '0 to 1', '0 to 2'],
['1 to 0', '1 to 1', '1 to 2'],
['2 to 0', '2 to 1', '2 to 2']]
transitionMatrix
Out[84]:
[[0.8333333333333334, 0.1111111111111111, 0.05555555555555555],
[1.0, 0.0, 0.0],
[0.3333333333333333, 0.0, 0.6666666666666666]]
this code is supposed to fill the state_list with 6 states, as forecast = 6. Somewhere something is wonky though and it only works if the state following the current state is different than the current state. How do I have to rearrange the loops so that I will always get forecast amount of items in state_list ?