Created
February 15, 2019 09:13
-
-
Save Saren-Arterius/d2ea461d23e97bc102fd1fce9f05e287 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
#!/usr/bin/env python3 | |
from multiprocessing import Pool | |
from random import random | |
samples = 10000000 | |
samples_mul = 8 | |
states_repr = ['sleep', 'code', 'tg'] | |
probs = [ | |
[0.8, 0.1, 0.1], | |
[0.2, 0, 0.8], | |
[0.5, 0.5, 0] | |
] | |
states_count = len(states_repr) | |
def walk(state): | |
row = probs[state] | |
r = random() | |
for i, v in enumerate(row): | |
if r <= v: | |
return i | |
r -= v | |
def mcmc(_): | |
state = 0 | |
counter = [0] * states_count | |
for _ in range(samples): | |
state = walk(state) | |
counter[state] += 1 | |
return counter | |
if __name__ == '__main__': | |
counter = [0] * states_count | |
with Pool() as p: | |
for c in p.map(mcmc, range(samples_mul)): | |
for i in range(states_count): | |
counter[i] += c[i] | |
t_samples = samples_mul * samples | |
for i, v in enumerate(counter): | |
print(f'{states_repr[i]}: {v} ({v / t_samples})') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment