Created
February 21, 2024 21:12
-
-
Save romanmichaelpaolucci/10743b9b439a0f72dca69acbcdf3b2e1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# 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