Skip to content

Commit f5804e4

Browse files
committed
fixes to distribution guide and add glossary skeleton
1 parent 03acbc5 commit f5804e4

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

docs/source/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@
144144
# intersphinx configuration to ease linking arviz docs
145145
intersphinx_mapping = {
146146
"arviz": ("https://arviz-devs.github.io/arviz/", None),
147+
"aesara": ("https://aesara.readthedocs.io/en/latest/", None),
148+
"numpy": ("https://numpy.org/doc/stable/", None),
147149
}
148150

149151

docs/source/developer_guide_implementing_distribution.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This guide provides an overview on how to implement a distribution for version 4
44
It is designed for developers who wish to add a new distribution to the library.
55
Users will not be aware of all this complexity and should instead make use of helper methods such as (TODO).
66

7-
PyMC3 {`class`}`~pymc3.distributions.Distribution` build on top of Aesara's {`class`}`~aesara.tensor.random.op.RandomVariable`, and implement `logp` and `logcdf` methods as well as other initialization and validation helpers, most notably `shape/dims`, alternative parametrizations, and default `transforms`.
7+
PyMC3 {class}`~pymc3.distributions.Distribution` build on top of Aesara's {class}`~aesara.tensor.random.op.RandomVariable`, and implement `logp` and `logcdf` methods as well as other initialization and validation helpers, most notably `shape/dims`, alternative parametrizations, and default `transforms`.
88

99
Here is a summary check-list of the steps needed to implement a new distribution.
1010
Each section will be expanded below:
@@ -19,7 +19,7 @@ This guide does not attempt to explain the rationale behind the `Distributions`
1919

2020
## 1. Creating a new `RandomVariable` `Op`
2121

22-
{`class`}`~aesara.tensor.random.op.RandomVariable` are responsible for implementing the random sampling methods, which in version 3 of PyMC3 used to be one of the standard `Distribution` methods, alongside `logp` and `logcdf`.
22+
{class}`~aesara.tensor.random.op.RandomVariable` are responsible for implementing the random sampling methods, which in version 3 of PyMC3 used to be one of the standard `Distribution` methods, alongside `logp` and `logcdf`.
2323
The `RandomVariable` is also responsible for parameter broadcasting and shape inference.
2424

2525
Before creating a new `RandomVariable` make sure that it is not offered in the [Numpy library](https://numpy.org/doc/stable/reference/random/generator.html#distributions).
@@ -84,12 +84,12 @@ blah = BlahRV()
8484

8585
Some important things to keep in mind:
8686

87-
1. Everything inside the `rng_fn` method is pure Python code (as are the inputs) and should not make use of other `Aesara` symbolic ops. The random method should make use of the `rng` which is a Numpy {`class`}`~numpy.random.RandomState`, so that samples are reproducible.
87+
1. Everything inside the `rng_fn` method is pure Python code (as are the inputs) and should not make use of other `Aesara` symbolic ops. The random method should make use of the `rng` which is a Numpy {class}`~numpy.random.RandomState`, so that samples are reproducible.
8888
1. The `size` argument (together with the inputs shape) are the only way for the user to specify non-default `RandomVariable` dimensions. The `rng_fn` will have to take this into consideration for correct output. `size` is the specification used by `Numpy` and `Scipy` and works like PyMC3 `shape` for univariate distributions, but is different for multivariate distributions. Unfortunately there is no general reference documenting how `size` ought to work for multivariate distributions. This [discussion](https://github.com/numpy/numpy/issues/17669) may be helpful to get more context.
8989
1. `Aesara` tries to infer the output shape of the `RandomVariable` (given a user-specified size) by introspection of the `ndim_supp` and `ndim_params` attributes. However, the default method may not work for more complex distributions. In that case, custom `_shape_from_params` (and less probably, `_infer_shape`) should also be implemented in the new `RandomVariable` class. One simple example is seen in the {class}`~pymc3.distributions.multivariate.DirichletMultinomialRV` where it was necessary to specify the `rep_param_idx` so that the `default_shape_from_params` helper method could do its job. In more complex cases, it may not be possible to make use of the default helper, but those have not been found yet!
9090
1. It's okay to use the `rng_fn` `classmethods` of other Aesara and PyMC3 `RandomVariables` inside the new `rng_fn`. For example if you are implementing a negative HalfNormal `RandomVariable`, your `rng_fn` can simply return `- halfnormal.rng_fn(rng, scale, size)`.
9191

92-
*Note: In addition to `size`, the `PyMC3` API also provides `shape` and `dims` as alternatives to define a distribution dimensionality, but this is taken care of by {`class`}`~pymc3.distributions.Distribution`, and should not require any extra changes.*
92+
*Note: In addition to `size`, the `PyMC3` API also provides `shape` and `dims` as alternatives to define a distribution dimensionality, but this is taken care of by {class}`~pymc3.distributions.Distribution`, and should not require any extra changes.*
9393

9494
For a quick test that your new `RandomVariable` `Op` is working, you can call the `Op` with the necessary parameters and then call `eval()` on the returned object:
9595

@@ -113,11 +113,11 @@ blah([0, 0], [1, 2], size=(10, 2)).eval()
113113

114114
## 2. Inheriting from a PyMC3 base `Distribution` class
115115

116-
After implementing the new `RandomVariable` `Op`, it's time to make use of it in a new PyMC3 {`Distribution`}`pymc3.distributions.Distribution`.
117-
PyMC3 works in a very {functional}`Functional_Programming` way, and the `distribution` classes are there mostly to facilitate porting the `v3` code to the new `v4` version, add PyMC3 API features and keep related methods organized together.
116+
After implementing the new `RandomVariable` `Op`, it's time to make use of it in a new PyMC3 {class}`pymc3.distributions.Distribution`.
117+
PyMC3 works in a very {term}`functional <Functional Programming>` way, and the `distribution` classes are there mostly to facilitate porting the `v3` code to the new `v4` version, add PyMC3 API features and keep related methods organized together.
118118
In practice, they take care of:
119119

120-
1. Linking ({dispatch}`Dispatching`) a rv_op class with the corresponding logp and logcdf methods.
120+
1. Linking ({term}`Dispatching`) a rv_op class with the corresponding logp and logcdf methods.
121121
1. Defining a standard transformation (for continuous distributions) that converts a bounded variable domain (e.g., positive line) to an unbounded domain (i.e., the real line), which many samplers prefer.
122122
1. Validating the parametrization of a distribution and converting non-symbolic inputs (i.e., numeric literals or numpy arrays) to symbolic variables.
123123
1. Converting multiple alternative parametrizations to the standard parametrization that the `RandomVariable` is defined in terms of.
@@ -182,10 +182,10 @@ class Blah(PositiveContinuous):
182182

183183
Some notes:
184184

185-
1. A distribution should at the very least inherit from `Discrete` or `Continuous`. For the latter, more specific subclasses exist:`PositiveContinuous`, `UnitContinuous`, `BoundedContinuous`, `CircularContinuous`, which specify default transformations for the variables. If you need to specify a one-time custom transform you can also override the `__new__` method, as is done for the {`class`}`~pymc3.distributions.multivariate.Dirichlet`.
186-
1. If a distribution does not have a corresponding `random` implementation, a `RandomVariable` should still be created that raises a `NotImplementedError`. This is the case for the {`class`}`~pymc3.distributions.continuous.FlatRV`. In this case it will be necessary to provide a standard `initval` by
185+
1. A distribution should at the very least inherit from {class}`~pymc3.distributions.Discrete` or {class}`~pymc3.distributions.Continuous`. For the latter, more specific subclasses exist: `PositiveContinuous`, `UnitContinuous`, `BoundedContinuous`, `CircularContinuous`, which specify default transformations for the variables. If you need to specify a one-time custom transform you can also override the `__new__` method, as is done for the {class}`~pymc3.distributions.multivariate.Dirichlet`.
186+
1. If a distribution does not have a corresponding `random` implementation, a `RandomVariable` should still be created that raises a `NotImplementedError`. This is the case for the {class}`~pymc3.distributions.continuous.Flat`. In this case it will be necessary to provide a standard `initval` by
187187
overriding `__new__`.
188-
1. As mentioned above, `v4` works in a very {functional}`Functional_Programming` way, and all the information that is needed in the `logp` and `logcdf` methods is expected to be "carried" via the `RandomVariable` inputs. You may pass numerical arguments that are not strictly needed for the `rng_fn` method but are used in the `logp` and `logcdf` methods. Just keep in mind whether this affects the correct shape inference behavior of the `RandomVariable`. If specialized non-numeric information is needed you might need to define your custom`_logp` and `_logcdf` {dispatch}`Dispatching` functions, but this should be done as a last resort.
188+
1. As mentioned above, `v4` works in a very {term}`functional <Functional Programming>` way, and all the information that is needed in the `logp` and `logcdf` methods is expected to be "carried" via the `RandomVariable` inputs. You may pass numerical arguments that are not strictly needed for the `rng_fn` method but are used in the `logp` and `logcdf` methods. Just keep in mind whether this affects the correct shape inference behavior of the `RandomVariable`. If specialized non-numeric information is needed you might need to define your custom`_logp` and `_logcdf` {term}`Dispatching` functions, but this should be done as a last resort.
189189
1. The `logcdf` method is not a requirement, but it's a nice plus!
190190

191191
For a quick check that things are working you can try the following:
@@ -213,7 +213,7 @@ pm.logcdf(blah, [1.5, 1.5]).eval()
213213
## 3. Adding tests for the new `RandomVariable`
214214

215215
Tests for new `RandomVariables` are mostly located in `pymc3/tests/test_distributions_random.py`.
216-
Most tests can be accomodated by the default `BaseTestDistribution` class, which provides default tests for checking:
216+
Most tests can be accommodated by the default `BaseTestDistribution` class, which provides default tests for checking:
217217
1. Expected inputs are passed to the `rv_op` by the `dist` `classmethod`, via `check_pymc_params_match_rv_op`
218218
1. Expected (exact) draws are being returned, via `check_pymc_draws_match_reference`
219219
1. Shape variable inference is correct, via `check_rv_size`
@@ -257,7 +257,7 @@ class TestBlahAltParam2(BaseTestDistribution):
257257

258258
```
259259

260-
Custom tests can also be added to the class as is done for the {`class`}`~pymc3.tests.test_random.TestFlat`.
260+
Custom tests can also be added to the class as is done for the {class}`~pymc3.tests.test_random.TestFlat`.
261261

262262
### Note on `check_rv_size` test:
263263

@@ -270,7 +270,7 @@ tests_to_run = ["check_rv_size"]
270270
```
271271

272272
This is usually needed for Multivariate distributions.
273-
You can see an example in {`class`}`~pymc3.test.test_random.TestDirichlet`.
273+
You can see an example in {class}`~pymc3.test.test_random.TestDirichlet`.
274274

275275
### Notes on `check_pymcs_draws_match_reference` test
276276

@@ -280,7 +280,7 @@ The latter kind of test (if warranted) can be performed with the aid of `pymc3_r
280280
This kind of test only makes sense if there is a good independent generator reference (i.e., not just the same composition of numpy / scipy python calls that is done inside `rng_fn`).
281281

282282
Finally, when your `rng_fn` is doing something more than just calling a `numpy` or `scipy` method, you will need to setup an equivalent seeded function with which to compare for the exact draws (instead of relying on `seeded_[scipy|numpy]_distribution_builder`).
283-
You can find an example in {`class`}`~pymc3.tests.test_distributions_random.TestWeibull`, whose `rng_fn` returns `beta * np.random.weibull(alpha, size=size)`.
283+
You can find an example in {class}`~pymc3.tests.test_distributions_random.TestWeibull`, whose `rng_fn` returns `beta * np.random.weibull(alpha, size=size)`.
284284

285285

286286
## 4. Adding tests for the `logp` / `logcdf` methods
@@ -356,8 +356,7 @@ New distributions should have a rich docstring, following the same format as tha
356356
It generally looks something like this:
357357

358358
```python
359-
r"""
360-
Univariate blah distribution.
359+
r"""Univariate blah distribution.
361360
362361
The pdf of this distribution is
363362

docs/source/glossary.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ Second term
1212
Definitions can have as many lines as desired, and should be written in markdown. Definitions
1313
can contain any markdown formatting for MyST to parse, this includes basic markdown like **bold**
1414
as well as MyST directives and roles like {fa}`fort awesome,style=fab`
15+
Functional Programming
16+
Functional programming is writing pure functions
17+
Dispatching
18+
Choosing which implementation of an operation is used
1519
:::::

docs/source/pymc-examples

Submodule pymc-examples updated 86 files

0 commit comments

Comments
 (0)