- bokeh
- micropymc
PyMC in the browser using PyScript: Fair Coin?
(c) 2022, Credits: Thomas Wiecki
This webapp runs the following PyMC model in your browser:
import pymc as pm
with pm.Model() as model:
p = pm.Beta("p", alpha=1, beta=1)
obs = pm.Binomial("obs", p=p, n=n,
observed=k)
idata = pm.sample()
This is a simple beta-binomial or coin-flip model.
Enter how many coin-flips you did (N) and how many times it came up heads (K).
Then press "Sample!" to run the NUTS sampler.
For a full explanation, see this blog post.
n=
10
k=
5
from pyodide import create_proxy
@create_proxy
def on_click(evt):
k = Element('input_box_k').element.value
if k == "":
k = 5
n = Element('input_box_n').element.value
if n == "":
n = 10
run_model(n=n, k=k)
Sampling will take a couple of seconds.
The resulting posterior belief into what
probabilities of obtaining heads are plausible will show up below:
Made using PyScript, PyMC, Arviz, Bokeh.
import warnings
warnings.filterwarnings("ignore")
import json
from js import Bokeh, JSON
from bokeh.embed import json_item
from bokeh.plotting import figure
import arviz as az
az.rcParams["plot.backend"] = "bokeh"
import os, sys
sys.stderr = open(os.devnull, "w")
import pymc as pm
def run_model(n=10, k=5):
with pm.Model() as model:
p = pm.Beta("p", alpha=1, beta=1)
obs = pm.Binomial("obs", p=p, n=n, observed=k)
idata = pm.sample(chains=1, progressbar=False)
p = figure(plot_width=500, plot_height=400, toolbar_location="below")
az.plot_posterior(idata, var_names=["p"], show=False, ax=p)
p.xaxis.axis_label = 'p'
p_json = json.dumps(json_item(p, "myplot"))
Bokeh.embed.embed_item(JSON.parse(p_json))