New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using custom distributions in bmb.Prior
for group-specific terms
#763
Comments
Do you have an example I can follow? It's OK if you don't have a full-working example, but I would like how you're passing the priors when creating the model. |
Sure! Here's what I understand what the issue is: suppose I have a regression def CustomPrior(name, mu, sigma, lower, upper):
dist = pm.Normal.dist(mu=mu, sigma=sigma) # assume mu and/or sigma here is a hyperprior
return pm.Truncated(name=name, dist=dist, lower=lower, upper=upper) Then I create a Here's the code that raises the error in Bambi: bambi/bambi/terms/group_specific.py Lines 89 to 91 in 2d4b260
|
Is this what you're trying to do? import bambi as bmb
import numpy as np
import pandas as pd
import pymc as pm
n = 20
g = 8
rng = np.random.default_rng(1234)
outcomes = rng.normal(rng.normal(size=g), scale=0.5, size=(n, g))
data = pd.DataFrame(dict(y = outcomes.T.flatten(), g = np.repeat(list("abcdefgh"), 20)))
data
def custom_prior(name, mu, sigma, lower, upper, dims=None):
print("is this called?")
dist = pm.Normal.dist(mu=mu, sigma=sigma)
return pm.Truncated(name=name, dist=dist, lower=lower, upper=upper, dims=dims)
priors = {
"1|g": bmb.Prior(
"CustomPrior",
mu=0,
sigma=bmb.Prior("Exponential", lam=1),
lower=-2,
upper=2,
dist=custom_prior
)
}
model = bmb.Model("y ~ 1 + (1|g)", data, priors=priors)
model.build() It's still problematic because it's not using the customprior so I think there's a bug with custom priors and group-specific effects. |
Yeah the latter is what I was trying. There does seem to be a bug. Is there a way to make |
@digicosmos86 I found the problem is here: Lines 145 to 148 in 2d4b260
When we use a noncentered parametrization, Bambi is assuming you have a normal prior. If you pass Edit I'm not saying Bambi should allow other distributions for a noncentered pamatrization as it would be non-trivial to determine how to use a noncentered parametrization for any distribution. But I do think there should be at least a warning when you pass a non-normal prior and |
@tomicapretto Thank you for looking into this! I think this is a slightly different issue. Sorry I should have included the error message so it's a bit more clear. Here's the bami code and the error message when I run a test: @prior.setter
def prior(self, value):
# This does not check which argument has hyperprior (must be dispersion?)
assert isinstance(value, VALID_PRIORS), f"Prior must be one of {VALID_PRIORS}"
if isinstance(value, Prior):
any_hyperprior = any(isinstance(x, Prior) for x in value.args.values())
if not any_hyperprior:
> raise ValueError("Prior for group-specific terms must have hyperpriors")
E ValueError: Prior for group-specific terms must have hyperpriors I think what happens is when the prior of a group-specific terms is specified like this: def custom_prior(name, mu, sigma, lower, upper, dims=None):
print("is this called?")
dist = pm.Normal.dist(mu=mu, sigma=sigma)
return pm.Truncated(name=name, dist=dist, lower=lower, upper=upper, dims=dims)
priors = {
"1|g": bmb.Prior(
"CustomPrior",
mu=0,
sigma=bmb.Prior("Exponential", lam=1),
lower=-2,
upper=2,
dist=custom_prior
)
}
model = bmb.Model("y ~ 1 + (1|g)", data, priors=priors)
model.build()
|
I'm sorry but I don't see the difference between the final block of code you shared def custom_prior(name, mu, sigma, lower, upper, dims=None):
print("is this called?")
dist = pm.Normal.dist(mu=mu, sigma=sigma)
return pm.Truncated(name=name, dist=dist, lower=lower, upper=upper, dims=dims)
priors = {
"1|g": bmb.Prior(
"CustomPrior",
mu=0,
sigma=bmb.Prior("Exponential", lam=1),
lower=-2,
upper=2,
dist=custom_prior
)
}
model = bmb.Model("y ~ 1 + (1|g)", data, priors=priors)
model.build() and the code that I shared in my previous comment. Is there anything I'm missing or you pasted the wrong thing? |
No I am just using your code to show that a different error can be caused. Please see the first block of code for the error. |
Hi @digicosmos86 I'm reviewing issues and realized I never replied here. I see there could be a different error raised (i.e. |
Thanks @tomicapretto! Do you have a moment for a quick call? Please feel free to pm me on slack :) |
Hi,
I know that
bambi
supports arbitrary distributions as priors through thedist
argument. However, when I wanted to use a custom distribution as the prior for a group-specific term, an error is thrown at model creation time, because bambi checksPrior.args
for at least one hyperprior. However, forbmb.Prior
objects created withdist
argument,args
property needs to be empty (I remember that keeping a non-emptyargs
will result in errors). Any idea how I can get around the check?The text was updated successfully, but these errors were encountered: