Skip to content
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

Add function that returns the prior as a dictionary #829

Open
aloctavodia opened this issue Jul 31, 2024 · 3 comments
Open

Add function that returns the prior as a dictionary #829

aloctavodia opened this issue Jul 31, 2024 · 3 comments

Comments

@aloctavodia
Copy link
Collaborator

Now we can "see" the prior being used by printing the model. But it will be nice to have direct access to the dictionary of priors. This dictionary should be the one expected by priors argument in .Model(), or a string representing it. This will make it easier to modify priors and use functions like predictive_explorer from PreliZ

@tanishy7777
Copy link
Contributor

Hey I would like to work on this, could you please elaborate more about this.

@tomicapretto
Copy link
Collaborator

Hi @tanishy7777, I imagine a method called .get_priors() that would return a dictionary where the keys are likelihood parameters and values are also dictionaries, mapping parameter names to their prior objects.

See the following examples

# Example 1
priors = {
    "mu": {
        "Intercept": bmb.Prior("Normal", mu=0, sigma=1),
        "x": bmb.Prior("Normal", mu=0, sigma=5),
    },
    "sigma": bmb.Prior("HalfNormal", sigma=5)
}

model = bmb.Model("y ~ 1 + x", data, priors=priors)
model.get_priors()
# {
#     "mu": {
#         "Intercept": Prior("Normal", mu=0, sigma=1), 
#         "x": Prior("Normal", mu=0, sigma=5),
#     },
#     "sigma": Prior("HalfNormal", sigma=5)
# }

# Example 2
formula = bmb.Formula(
    "y ~ 1 + x",
    "sigma ~ 1 + x",
)
priors = {
    "mu": {
        "Intercept": bmb.Prior("Normal", mu=0, sigma=1),
        "x": bmb.Prior("Normal", mu=0, sigma=5),
    },
    "sigma": {
        "Intercept": bmb.Prior("Normal", mu=0, sigma=0.25),
        "x": bmb.Prior("Normal", mu=0, sigma=1),
    }
}
model = bmb.Model(formula, data, priors=priors)
model.get_priors()
# {
#     "mu": {
#         "Intercept": Prior("Normal", mu=0, sigma=1), 
#         "x": Prior("Normal", mu=0, sigma=5),
#     },
#     "sigma": {
#         "Intercept": Prior("Normal", mu=0, sigma=0.25),
#         "x": Prior("Normal", mu=0, sigma=1),
#     },
# }

Note that those examples are simple as the priors were explicitly passed by the user. This is a bit more complicated when priors are not explicitly set. And I can imagine working on this issue would require familiarization with how things are structured in Bambi.

@speco29
Copy link

speco29 commented Feb 6, 2025

Will something like this work?

`import bambi as bmb

class Model:
def init(self, formula, data, priors=None):
self.formula = formula
self.data = data
self.priors = priors or self._set_default_priors()

def _set_default_priors(self):
    return {
        "mu": {
            "Intercept": bmb.Prior("Normal", mu=0, sigma=1),
            "x": bmb.Prior("Normal", mu=0, sigma=5),
        },
        "sigma": bmb.Prior("HalfNormal", sigma=5)
    }

def get_priors(self):
    return self.priors

Example 1

priors_example_1 = {
"mu": {
"Intercept": bmb.Prior("Normal", mu=0, sigma=1),
"x": bmb.Prior("Normal", mu=0, sigma=5),
},
"sigma": bmb.Prior("HalfNormal", sigma=5)
}

model_1 = Model("y ~ 1 + x", data=None, priors=priors_example_1)
print(model_1.get_priors())

Example 2

formula_example_2 = bmb.Formula(
"y ~ 1 + x",
"sigma ~ 1 + x",
)

priors_example_2 = {
"mu": {
"Intercept": bmb.Prior("Normal", mu=0, sigma=1),
"x": bmb.Prior("Normal", mu=0, sigma=5),
},
"sigma": {
"Intercept": bmb.Prior("Normal", mu=0, sigma=0.25),
"x": bmb.Prior("Normal", mu=0, sigma=1),
}
}

model_2 = Model(formula_example_2, data=None, priors=priors_example_2)
print(model_2.get_priors())
`

It introduces the .get_priors() method to the Model class in Bambi. The new method provides a way to access the dictionary of priors directly, making it easier to modify priors and use functions like predictive_explorer from PreliZ. The implementation handles both explicit and default priors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants