Last active
May 13, 2024 14:03
-
-
Save danalvarez/df65adbb323e31cf6279c795ebfbe0be to your computer and use it in GitHub Desktop.
Key recursion to calculate n-step transition probabilities in Markov chains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define the transition probabilities of the Markov chain | |
TP = [ | |
[0.4, 0.6, 0, 0, 0, 0, 0], | |
[0.2, 0, 0.8, 0, 0, 0, 0], | |
[0, 0.4, 0.6, 0, 0, 0, 0], | |
[0, 0, 0.4, 0, 0.3, 0.3, 0], | |
[0, 0, 0, 0, 0, 1, 0], | |
[0, 0, 0, 0.2, 0, 0, 0.8], | |
[0, 0, 0, 0, 1, 0, 0] | |
] | |
# calculates the transition probability from state i to state j in n time steps | |
def r(i, j, n): | |
if n == 1: | |
return TP[i][j] | |
else: | |
result = 0 | |
for k in range(0, len(TP)): | |
result = result + r(i, k, n-1) * TP[k][j] | |
return result | |
# an example: what is r_{0, 2}(3) | |
# that is: the probability that, starting from state 0, after 3 time steps we end up in state 2 | |
print(r(0, 2, 3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment