Skip to content

Instantly share code, notes, and snippets.

@Saren-Arterius
Created February 15, 2019 09:13
Show Gist options
  • Save Saren-Arterius/d2ea461d23e97bc102fd1fce9f05e287 to your computer and use it in GitHub Desktop.
Save Saren-Arterius/d2ea461d23e97bc102fd1fce9f05e287 to your computer and use it in GitHub Desktop.
#!/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