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

documentation on custom prior #656

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions docs/notebooks/getting_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,6 @@
]
Copy link
Collaborator

@tomicapretto tomicapretto Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the first paragraph I would say

Bambi's priors are a thing layer on top of PyMC distributions. If you want to ask for a prior distribution by name, it must be the name of a PyMC distribution. But sometimes we want to use more complex distributions as priors. For all those cases, Bambi's Prior class allow users to pass a distribution that returns the new prior. See the following example:

Then I would say

The example above is trival because it's just a wrapper of the pm.Normal distribution. But we can use this pattern to construct more complex distributions, such as a Truncated Laplace distribution.

And finally, close with

In summary, custom priors allow for greater flexibility by combining existing PyMC distributions in different ways. If you need to use a distribution that's not implemented in PyMC, please check the following link.

Reply via ReviewNB

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SuryaMudimi thanks a lot for the contribution! See the suggestions I left :)

},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -790,6 +789,33 @@
"It's important to note that explicitly setting priors by passing in ``Prior`` objects will disable Bambi's default behavior of scaling priors to the data in order to ensure that they remain weakly informative. This means that if you specify your own prior, you have to be sure not only to specify the distribution you want, but also any relevant scale parameters. For example, the 0.5 in ``Prior(\"Normal\", mu=0, sd=0.5)`` will be specified on the scale of the data, not the bounded partial correlation scale that Bambi uses for default priors. This means that if your outcome variable has a mean value of 10,000 and a standard deviation of, say, 1,000, you could potentially have some problems getting the model to produce reasonable estimates, since from the perspective of the data, you're specifying an extremely strong prior."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Custom Prior\n",
"\n",
"Bambi's priors are a thin layer on top of [PyMC distributions](https://www.pymc.io/projects/docs/en/latest/api/distributions.html). If you want to ask for a prior distribution by name, it must be the name of a PyMC distribution. But sometimes we want to use more complex distributions as priors. For all those cases, Bambi's `Prior` class allow users to pass a function that returns a distribution that will be used as the prior. See the following example:\n",
"\n",
"```python\n",
"def CustomPrior(name, *args, dims=None, **kwargs):\n",
" return pm.Normal(name, *args, dims=dims, **kwargs)\n",
"\n",
"priors = {\"x\": Prior(\"CustomPrior\", mu=0, sigma=5, dist=CustomPrior)}\n",
"model = Model(\"y ~ x\", data, priors=priors)\n",
"```\n",
"\n",
"The example above is trival because it's just a wrapper of the `pm.Normal` distribution. But we can use this pattern to construct more complex distributions, such as a Truncated Laplace distribution shown below.\n",
"\n",
"```python\n",
"def TruncatedLaplace(name, mu,b,lower,upper,*args, dims=None, **kwargs):\n",
" lap_dist = pm.Laplace.dist(mu=mu, b=b)\n",
" return pm.Truncated(name, lap_dist, lower=lower, upper=upper, *args, dims=dims, **kwargs)\n",
"```\n",
"\n",
"In summary, custom priors allow for greater flexibility by combining existing PyMC distributions in different ways. If you need to use a distribution that's not implemented in PyMC, please check the [link](https://www.pymc.io/projects/docs/en/latest/contributing/implementing_distribution.html) for further details."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1054,7 +1080,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -1207,7 +1232,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -1315,7 +1339,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.4 ('bambi')",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand Down