Skip to content

Instantly share code, notes, and snippets.

@juangesino
Created February 11, 2021 19:10
Show Gist options
  • Save juangesino/022d616c8ba1665fa6cf437910430ca7 to your computer and use it in GitHub Desktop.
Save juangesino/022d616c8ba1665fa6cf437910430ca7 to your computer and use it in GitHub Desktop.
Bayesian Weight Loss
import pymc3 as pm
with pm.Model() as model_1:
alpha = pm.Normal("alpha", mu=70, sigma=8)
beta = pm.Normal("beta", mu=0, sigma=1)
weight = pm.Deterministic("weight", alpha + beta * X)
noise = pm.HalfNormal("noise", sigma=1)
likelihood = pm.Normal("likelihood", mu=weight, sigma=noise, observed=Y)
trace_1 = pm.sample(tune=1500)
import statsmodels.api as sm
X_ols = sm.add_constant(X)
model = sm.OLS(Y, X_ols)
results = model.fit()
model.data.xnames = ["alpha", "beta"]
print(results.summary())
print("Pr(Weight > 68) =", np.mean(alpha_samples + beta_samples * max(X) > 68))
# => Pr(Weight > 68) = 0.083
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment