Skip to content

Instantly share code, notes, and snippets.

@romanmichaelpaolucci
Created February 21, 2024 21:12
Show Gist options
  • Save romanmichaelpaolucci/10743b9b439a0f72dca69acbcdf3b2e1 to your computer and use it in GitHub Desktop.
Save romanmichaelpaolucci/10743b9b439a0f72dca69acbcdf3b2e1 to your computer and use it in GitHub Desktop.
import numpy as np
# transition matrix
P = np.array([[.5, .5, 0, 0],
[.5, 0, .5, 0],
[.5, 0, 0, .5],
[.5, 0, 0, .5]])
# Simulation
hits = []
for i in range(10000):
chain = []
for j in range(10):
chain.append(np.random.choice([0, 1], p=[.5, .5]))
if chain[-1] == 1 and chain[-2] == 1 and chain[-3] == 1:
hits.append(1)
else:
hits.append(0)
print('Simulated Probability: ', np.mean(hits))
print('Analytical Probability: ', np.linalg.matrix_power(P, 10)[0][3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment