Skip to content

Instantly share code, notes, and snippets.

@aliakbars
Last active June 28, 2019 09:39
Show Gist options
  • Save aliakbars/bbb745016cc77c2ff928ddbcd5728711 to your computer and use it in GitHub Desktop.
Save aliakbars/bbb745016cc77c2ff928ddbcd5728711 to your computer and use it in GitHub Desktop.
PyMC3 approach to solve Bayesian coin flip example
import pymc3 as pm
with pm.Model() as coinflip:
which_coin = pm.Bernoulli('which_coin', 0.5)
theta_biased = pm.Uniform('theta_biased', 0, 1)
p = pm.math.switch(which_coin > 0.5, 0.5, theta_biased)
heads = pm.Binomial('heads', n=2, p=p, observed=2)
coin_trace = pm.sample(5000, tune=2500)
pm.traceplot(coin_trace);
print('p(coin=fair|HH)={}'.format(
coin_trace['which_coin'].mean()
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment