Fitting a Beta Regression

I am trying to code a Beta Regression.

with pm.Model() as sample_model:
    mu_a = pm.Normal('mu_a', mu=-6, sd=3)
    sigma_a = pm.HalfNormal('sigma_a', 0.001)
    mu=pm.math.invlogit(mu_a)
    thetas = pm.Beta('thetas', mu=mu,sd=sigma_a,shape=len(df_input['Clicks']))
    cr_like=pm.Binomial('cr_like',p=thetas,n=df_input['Clicks'],observed=df_input['Resale.Orders.Total'])
with sample_model:
    bb_trace=pm.sample(500,tune=50) .

I am not able to feed observations in Beta here so I am using a Binomial but not able to use a normal directly. I have to use a normal and then a logit transform. Is there any way to do a simple beta regression ?
Any example is appreciated

Could you be a bit more specific with what you’re trying to achieve? Glancing at the code snippet, it looks like you’re implementing a Beta-Binomial model to model click-through rates (CTR). The Beta-Binomial model is actually a conjugate model, so you wouldn’t even need PyMC3 for that: there’s a closed-form expression for the posterior!

Perhaps I’m misunderstanding?

Hey, Actually I am trying to eventually do a hierarchical beta regression. But I am not able to feed my pm.Beta with observations. As soon as I fit it with Observations ( that lie between 0 and 1 ) - I get the model is misspecified error.

This might have to do with the mu/sd parametrization.
Your mu is on the correct interval, but if I see it correctly, sigma_a might occasionally get out of the interval 0 < sd < sqrt(mu * (1 - mu))

You can try two things:

  • use a bounded sigma
  • check if the model runs with alpha/beta parametrization (optionally use pm.Deterministic() to calculate \mu on the way)

The second option should be more robust.

1 Like