Skip to content

Instantly share code, notes, and snippets.

@ryu577
Last active October 21, 2018 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryu577/89e4ef0b0b7fcba33e08a549f0793c86 to your computer and use it in GitHub Desktop.
Save ryu577/89e4ef0b0b7fcba33e08a549f0793c86 to your computer and use it in GitHub Desktop.
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