-
Notifications
You must be signed in to change notification settings - Fork 75
Allow users to define custom priors #387
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
Comments
See the discussion here pymc-devs/pymc#7416 on whether to port the |
Based on the discussion, it seems that the The code is a single file so it would be easy to copy and tweak to your liking. With access to the
Based on the old API, it might look like this: from pymc_marketing.prior import Prior
from causalpy.pymc_models import WeightedSumFitter
priors = {
"beta": Prior("Dirichlet", a=[1, 2, 3, 4], dims="coeffs"),
"sigma": Prior("Gamma", mu=1.5, sigma=0.25),
}
model = WeightedSumFitter(priors=priors)
# Check the priors
print(model.priors)
model.fit(X, y) |
Note to self: Ideally we wait for pymc-devs/pymc-extras#448 (maybe done by @williambdean) then we use that :) |
Boom! #448 pymc-devs/pymc-extras#448 is now closed. Thanks @williambdean. This issue is now unblocked and we can implement custom priors in CausalPy! Well, it will be when there's a new release of I'm aiming on getting to this in June - but it someone has the desire and feels the they could do it, let us know. |
Could add in a new initialization parameter here. Say, That would make the syntax: import causalpy as cp
from pymc_extras.prior import Prior
priors = {
"beta": Prior("Normal", mu=0, sigma=5, dims="coeffs")
}
df = cp.load_data("did")
result = cp.DifferenceInDifferences(
df,
formula="y ~ 1 + group*post_treatment",
time_variable_name="t",
group_variable_name="group",
model=cp.pymc_models.LinearRegression(
sample_kwargs=sample_kwargs,
priors=priors,
),
) There would be some default priors for each of the models: default_priors = {
"beta": Prior("Normal", mu=0, sigma=50, dims="coeffs"),
# This name would have to be changed
# "sigma": Prior("HalfNormal", sigma=1),
"y_hat": Prior(
"Normal",
sigma=Prior("Normal", sigma=1),
dims="obs_ind",
),
} These would be the defaults and would be updated {**default_priors, **initialization_priors} Warning There would be some backwards compat concerns because of the auto-naming that the prior class has with sub-> parameters. This would affect the likelihood nested parameters. For example "sigma" in the example above would become "y_hat_sigma". However, this could be renamed or duplicated in the posterior Xarray |
That all looks pretty good, and aligns with what I was expecting. It could be cool to think about auto scaling the default prior to the data (similar to how bambi does it). That might negate the need to consider scaling the data? My preference is to avoid pre and post transformation steps if possible and rely on auto adjusting priors. |
I think that could be done in the experiment class. That would have both the data and the model. It could cash something like |
I'm not sure how bambi does it but it sounds like multiple steps and multiple PRs to me. Parameters can be scaled or the variables can be scaled which might be a bit more generalized. Maybe prior some helper functions that can return a dictionary of priors. |
High level goals
Implementation
We can perhaps learn from
pymc-marketing
which started off allowing users to specify priors by providingdict
s but which has moved to having a prior class (see pymc-labs/pymc-marketing#759). We may find thatdict
s are fine for our purposes, but there could be advantages of going down the path of using classes. (Tagging @wd60622)The text was updated successfully, but these errors were encountered: