Skip to content

Commit 8916b31

Browse files
authored
Add plot and from_pymc functions (#697)
* Add plot and from_pymc functions * rename function * fix test * correcly expose from_pymc * fix doc error * add missing import * add docstring * polish docs * Correct code block formatting in documentation Fix formatting issues in code block and improve clarity.
1 parent 64994d7 commit 8916b31

File tree

17 files changed

+601
-71
lines changed

17 files changed

+601
-71
lines changed

docs/distributions/gallery/exponential.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ Variance :math:`\frac{1}{\lambda^2}`
3333
**Parameters:**
3434

3535
- $\lambda$ : (float) Rate parameter, $\lambda > 0$.
36-
- $\beta$ : (float) Scale parameter, $\beta > 0$.
36+
- scale : (float) Scale parameter, $\text{scale} > 0$.
3737

3838
**Alternative parametrization**
3939

40-
The exponential distribution can be parametrized in terms of the rate parameter $\lambda$ or the scale parameter $\beta$.
40+
The exponential distribution can be parametrized in terms of the rate parameter $\lambda$ or the scale parameter scale.
4141

4242
The link between the 2 alternatives is given by:
4343

4444
$$
45-
\beta = \frac{1}{\lambda}
45+
\text{scale} = \frac{1}{\lambda}
4646
$$
4747

4848
### Probability Density Function (PDF)
@@ -68,14 +68,14 @@ for lam in lambdas:
6868
```
6969
:::::
7070

71-
:::::{tab-item} Parameter $\beta$
71+
:::::{tab-item} Parameter scale
7272
:sync: scale
7373
```{jupyter-execute}
7474
:hide-code:
7575
76-
betas = [2., 1., 0.5]
77-
for beta in betas:
78-
Exponential(beta=beta).plot_pdf(support=(0, 5))
76+
scales = [2., 1., 0.5]
77+
for scale in scales:
78+
Exponential(scale=scale).plot_pdf(support=(0, 5))
7979
```
8080
:::::
8181
::::::
@@ -99,13 +99,13 @@ for lam in lambdas:
9999
```
100100
:::::
101101

102-
:::::{tab-item} Parameter $\beta$
102+
:::::{tab-item} Parameter scale
103103
:sync: scale
104104
```{jupyter-execute}
105105
:hide-code:
106106
107-
for beta in betas:
108-
Exponential(beta=beta).plot_cdf(support=(0, 5))
107+
for scale in scales:
108+
Exponential(scale=scale).plot_cdf(support=(0, 5))
109109
```
110110
:::::
111111
::::::

docs/examples/gallery/global_settings.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Some of the PreliZ default values are regulated by `preliz.rcParams`, a class si
1919

2020
## Preliz Configuration File
2121

22-
The `rcParams` class is generated and populated at import time. Preliz checks several locations for a file named `prelizrc` and, if found, prefers those settings over the library ones.
22+
The `rcParams` class is generated and populated at import time. PreliZ checks several locations for a file named `prelizrc` and, if found, prefers those settings over the library ones.
2323

2424
The locations checked are the following:
2525

@@ -32,9 +32,8 @@ The locations checked are the following:
3232
The file is a simple text file with a structure similar to the following:
3333

3434
```text
35-
36-
stats.ci_kind : hdi
37-
stats.ci_prob : 0.95
35+
stats.ci_kind : eti
36+
stats.ci_prob : 0.89
3837
```
3938

4039
All available keys are listed below. The `prelizrc` file can have any subset of the keys, it isn't necessary to include them all. For those keys without a user defined default, the library one is used.

docs/examples/gallery/working_with_distributions.md

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ kernelspec:
1111
# Working with Distributions
1212

1313
```{jupyter-execute}
14-
1514
import preliz as pz
1615
```
1716

@@ -41,22 +40,21 @@ When we define the parameters of a distribution we will get more interesting pro
4140

4241
## Properties of Distributions
4342

44-
Once we have set the parameters of a distribution we can obtain a few of its properties. The summary method, returns the mean, median, standard deviation and lower and upper values for the highest density interval.
43+
Once we have set the parameters of a distribution we can obtain a few of its properties. The summary method, returns the mean, median, standard deviation and lower and upper values for the equal-tailed interval (ETI).
4544

4645
```{jupyter-execute}
4746
4847
dist = pz.Beta(2, 5)
4948
dist.summary()
5049
```
51-
52-
The highest density interval is the shorter interval with a given mass. Following ArviZ, the default mass for these intervals is 0.94. For `az.summary()` and other functions in PreliZ, you can change it with the argument `mass`.
50+
The ETI is the interval that contains a given mass of the distribution, with equal mass in both tails.cFollowing ArviZ, the default mass for these intervals is 0.89. For `pz.summary()` and other functions in PreliZ, you can change it with the argument `mass`.
5351

5452
```{jupyter-execute}
5553
5654
dist.summary(mass=0.7)
5755
```
5856

59-
Additionally, out-of-the-box, we can compute the equal-tailed interval. This interval will define two tails with an equal mass of `(1-mass)/2`.
57+
Additionally, out-of-the-box, we can compute the highest density interval (HDI), which is the shortest interval containing a given mass of the distribution.
6058

6159
```{jupyter-execute}
6260
@@ -121,8 +119,13 @@ Many distributions in PreliZ can be defined using different sets of parameters.
121119
```{jupyter-execute}
122120
123121
dist = pz.Gamma(mu=2, sigma=1)
124-
dist.plot_pdf()
125-
pz.Gamma(dist.alpha, dist.beta).plot_pdf(); # same distribution different parametrization.
122+
dist.summary()
123+
```
124+
125+
Now we can create the same distribution using the `alpha` and `beta` parameters.
126+
127+
```{jupyter-execute}
128+
pz.Gamma(dist.alpha, dist.beta).summary()
126129
```
127130

128131
## Visualizing distributions
@@ -163,14 +166,21 @@ pz.Gamma(2, 0.5).plot_cdf()
163166
pz.Gamma(2, 1).plot_cdf()
164167
```
165168

166-
and the inverse of the cdf.
169+
Other functions that can be plotted are the inverse of the CDF, `plot_ppf` (also known as percent point function), survival function (`plot_sf`), and the inverse survival function (`plot_isf`).
170+
171+
Alternatively, we can use the top-level `pz.plot()` function to plot any of these functions.
167172

168173
```{jupyter-execute}
174+
pz.plot(pz.Gamma(2, 0.5));
175+
```
169176

170-
pz.Gamma(2, 0.5).plot_ppf()
171-
pz.Gamma(2, 1).plot_ppf()
177+
The default is to plot the PDF. But we can plot any other function as well. For instance, for the survival function:
178+
179+
```{jupyter-execute}
180+
pz.plot(pz.Gamma(2, 0.5), kind="sf");
172181
```
173182

183+
174184
If we are not very familiar with a distribution, we may want to explore how the parameters affects the “shape” of the distribution. This could be easier to do interactively.
175185

176186
```{jupyter-execute}
@@ -237,9 +247,37 @@ PreliZ supports exporting distributions. Currently, only PyMC and Bambi are supp
237247
```{jupyter-execute}
238248
239249
pz.Normal(0, 1).to_pymc()
250+
```
251+
252+
```{jupyter-execute}
253+
240254
pz.Normal(0, 1).to_bambi()
241255
```
242256

243257
For these methods to work you need to have installed PyMC and/or Bambi.
244258

245-
In other notebooks we will discuss methods more directly focus on prior elicitation, and also other ways to interact with PPLs.
259+
260+
We can also go into the opposite direction and create PreliZ distributions from PyMC distributions.
261+
262+
```{jupyter-execute}
263+
import pymc as pm
264+
pz.from_pymc(pm.Normal.dist(mu=0, sigma=1)).summary()
265+
```
266+
267+
For some functions (currently the only supported is `pz.plot`) you can directly pass a PyMC distribution, and it will work as expected.
268+
269+
```{jupyter-execute}
270+
pz.plot(pm.Normal.dist(mu=0, sigma=1));
271+
```
272+
273+
Or even
274+
275+
```{jupyter-execute}
276+
with pm.Model():
277+
x = pm.Truncated("x", pm.NegativeBinomial.dist(2.5, 3), 0, 7)
278+
pz.plot(x);
279+
```
280+
281+
282+
In other examples we will discuss methods more directly focus on prior elicitation, and also other ways to interact with PPLs.
283+

preliz/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
from matplotlib import style
1111

1212
from preliz.distributions import *
13+
from preliz.distributions.plot import plot
1314
from preliz.predictive import *
15+
from preliz.ppls import *
1416
from preliz.unidimensional import *
1517
from preliz.multidimensional import *
1618
from preliz.internal.rcparams import rc_context, rcParams

0 commit comments

Comments
 (0)