Last active
October 21, 2018 08:05
-
-
Save ryu577/89e4ef0b0b7fcba33e08a549f0793c86 to your computer and use it in GitHub Desktop.
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
import numpy as np | |
start1 = np.array([1,0,0]) | |
m_3 = np.matrix([[.5,.5,0], [.5,0,.5], [0,0,1]]) | |
start2 = np.array([1,0,0,0]) | |
m_4 = np.matrix([[.5,.5,0,0], [.5,0,.5,0],[.5,0,0,.5], [0,0,0,1]]) | |
# p_n must always be one toss ahead of q_n_minus_1. So, when p_n is at toss 1, q_n_minus_1 | |
# should be at 0. When it is at 2, q_n_minus_1 should be at 1 and so on. | |
# that is why p_n starts with 1 and goes to 100 while q_n_minus_1 starts with 0 and goes to 99. | |
p_n = np.array([np.dot(start1, np.linalg.matrix_power(m_3,n))[0,2]\ | |
for n in range(1,101)]) | |
q_n_minus_1 = np.array([np.dot(start2, np.linalg.matrix_power(m_4,n))[0,2]\ | |
for n in range(100)]) | |
print("Prob(3 consec heads b4 2 consec heads):" + str(sum(q_n_minus_1*(1-p_n))/2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment