bambinos / bambi

BAyesian Model-Building Interface (Bambi) in Python.
https://bambinos.github.io/bambi/
MIT License
1.08k stars 124 forks source link

Bambi Returning wrong values for simple binomial model #756

Closed canyon289 closed 1 year ago

canyon289 commented 1 year ago

Bambi seems to be returning values that are incorrect from a simplistic dataset and model. Are folks seeing the same issue that I am?

# Generate two random groups
p_a, samples_a = .6, 10000
p_b, samples_b = .7, 10000

df_a = pd.DataFrame({"samples": stats.bernoulli(p=p_a).rvs(samples_a)}).assign(group="A")
df_b = pd.DataFrame({"samples": stats.bernoulli(p=p_b).rvs(samples_b)}).assign(group="B")

df = pd.concat([df_a, df_b])#.groupby("group").agg({len, sum})

model = bmb.Model("samples ~ 0 + group", data=df, family="bernoulli")
ab_test = model.fit()
az.plot_posterior(ab_test);
image image image
canyon289 commented 1 year ago

PyMC gets the right values

with pm.Model() as model:
    p = pm.Beta("p", 2, 2, shape=2)
    obs = pm.Bernoulli("obs", p=p[idx.codes], observed = df["samples"])
    idata = pm.sample()
image
tomicapretto commented 1 year ago

This is because you're visualizing the regression coefficients, while what you want are the success probabilities. You need the inverse link function:

from scipy.special import expit
az.plot_posterior(expit(ab_test.posterior))

and you get the plot you want

canyon289 commented 1 year ago

:facepalm: