Skip to content

Commit 9c4b740

Browse files
rpgoldmanColCarroll
authored andcommitted
Get the transform docs into the API pages. (#3690)
* Extend exports from transforms. * Add docs for distributions.transforms. The doc strings for the transforms were not linked into the documentation page for distributions. * Improve documentation of transform aliases. The transforms code has lower-case alternatives to the upper-cased class names. Sometimes these are instances of the classes (e.g., `log` is an instance of the `Log` transform), but sometimes they are aliases (e.g., `lowerbound` is an alias for `LowerBound`). I couldn't find a good way to document these in the source file, so they are hand-documented in transforms.rst. * Export improvements requested by @ColCarroll. * Minor edits to clarify transformations. * Added API page for random variables. * Fix typo @twiecki caught and some others. Thomas caught a typo of mine, and when fixing it, I picked off some grammar errors and improved some typesetting. * Add to release notes. * Fix typesetting of parameters. Fix typesetting of parameters in the docstrings. * Add docstring for t_stick_breaking() Thanks to @aloctavodia for help. * Slightly improved Table of Contents. I still don't know how to get the title of *this* page *not* to appear in its table of contents. Unfortunately, `autosummary` does not work at all. * Fix formatting of additional parameters. While I was at it, fixed the punctuation of parameters in the docstrings so that Sphinx will format them correctly.
1 parent 6ccc75d commit 9c4b740

File tree

10 files changed

+392
-199
lines changed

10 files changed

+392
-199
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- Wrapped `DensityDist.rand` with `generate_samples` to make it aware of the distribution's shape. Added control flow attributes to still be able to behave as in earlier versions, and to control how to interpret the `size` parameter in the `random` callable signature. Fixes [3553](https://github.com/pymc-devs/pymc3/issues/3553)
3333
- Added `theano.gof.graph.Constant` to type checks done in `_draw_value` (fixes issue [3595](https://github.com/pymc-devs/pymc3/issues/3595))
3434
- `HalfNormal` did not used to work properly in `draw_values`, `sample_prior_predictive`, or `sample_posterior_predictive` (fixes issue [3686](https://github.com/pymc-devs/pymc3/pull/3686))
35+
- Random variable transforms were inadvertently left out of the API documentation. Added them. (See PR [3690](https://github.com/pymc-devs/pymc3/pull/3690)).
3536

3637
## PyMC3 3.7 (May 29 2019)
3738

docs/source/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ API Reference
2020
api/data
2121
api/model
2222
api/model_graph
23+
api/variables
2324
api/shape_utils
2425

2526

docs/source/api/distributions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ Distributions
99
distributions/multivariate
1010
distributions/mixture
1111
distributions/timeseries
12+
distributions/transforms
1213
distributions/utilities
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
****************************************************************
2+
Transformations of a random variable from one space to another.
3+
****************************************************************
4+
5+
Note that for convenience these entities can be addressed as
6+
``pm.transforms.``\ *X* for any name *X*, although they are actually
7+
implemented as ``pm.distributions.transforms.``\*X*.
8+
9+
.. currentmodule:: pymc3.distributions.transforms
10+
11+
12+
.. contents ::
13+
14+
..
15+
.. autosummary::
16+
17+
transform
18+
stick_breaking
19+
logodds
20+
interval
21+
log_exp_m1
22+
lowerbound
23+
upperbound
24+
ordered
25+
log
26+
sum_to_1
27+
t_stick_breaking
28+
circular
29+
CholeskyCovPacked
30+
Chain
31+
32+
33+
34+
Transform Instances
35+
~~~~~~~~~~~~~~~~~~~
36+
37+
Transform instances are the entities that should be used in the
38+
``transform`` parameter to a random variable constructor. These are
39+
initialized instances of the Transform Classes, which are described
40+
below.
41+
42+
.. glossary::
43+
``stick_breaking``
44+
Instantiation of :class:`~pymc3.distributions.transforms.StickBreaking`
45+
:class:`~pymc3.distributions.transforms.Transform` class for use in the ``transform``
46+
argument of a random variable.
47+
48+
``logodds``
49+
Instantiation of
50+
:class:`~pymc3.distributions.transforms.LogOdds` :class:`~pymc3.distributions.transforms.Transform` class
51+
for use in the ``transform`` argument of a random variable.
52+
53+
``interval``
54+
Alias of
55+
:class:`~pymc3.distributions.transforms.Interval` :class:`~pymc3.distributions.transforms.Transform` class
56+
for use in the ``transform`` argument of a random variable.
57+
58+
``log_exp_m1``
59+
Instantiation of
60+
:class:`~pymc3.distributions.transforms.LogExpM1` :class:`~pymc3.distributions.transforms.Transform` class
61+
for use in the ``transform`` argument of a random variable.
62+
63+
``lowerbound``
64+
Alias of
65+
:class:`~pymc3.distributions.transforms.LowerBound` :class:`~pymc3.distributions.transforms.Transform` class
66+
for use in the ``transform`` argument of a random variable.
67+
68+
``upperbound``
69+
Alias of
70+
:class:`~pymc3.distributions.transforms.UpperBound` :class:`~pymc3.distributions.transforms.Transform` class
71+
for use in the ``transform`` argument of a random variable.
72+
73+
``ordered``
74+
Instantiation of
75+
:class:`~pymc3.distributions.transforms.Ordered` :class:`~pymc3.distributions.transforms.Transform` class
76+
for use in the ``transform`` argument of a random variable.
77+
78+
``log``
79+
Instantiation of
80+
:class:`~pymc3.distributions.transforms.Log` :class:`~pymc3.distributions.transforms.Transform` class
81+
for use in the ``transform`` argument of a random variable.
82+
83+
84+
``sum_to_1``
85+
Instantiation of
86+
:class:`~pymc3.distributions.transforms.SumTo1` :class:`~pymc3.distributions.transforms.Transform` class
87+
for use in the ``transform`` argument of a random variable.
88+
89+
90+
``circular``
91+
Instantiation of
92+
:class:`~pymc3.distributions.transforms.Circular` :class:`~pymc3.distributions.transforms.Transform` class
93+
for use in the ``transform`` argument of a random variable.
94+
95+
96+
.. autofunction:: t_stick_breaking
97+
98+
99+
Transform Base Classes
100+
~~~~~~~~~~~~~~~~~~~~~~
101+
102+
Typically the programmer will not use these directly.
103+
104+
.. autoclass:: Transform
105+
:members:
106+
.. autoclass:: transform
107+
:members:
108+
.. autoclass:: TransformedDistribution
109+
:members:
110+
111+
112+
Transform Composition Classes
113+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114+
115+
.. autoclass:: Chain
116+
:members:
117+
.. autoclass:: CholeskyCovPacked
118+
:members:
119+
120+
121+
Specific Transform Classes
122+
~~~~~~~~~~~~~~~~~~~~~~~~~~
123+
124+
.. autoclass:: Log
125+
:members:
126+
.. autoclass:: LogExpM1
127+
:members:
128+
.. autoclass:: LogOdds
129+
:members:
130+
.. autoclass:: Interval
131+
:members:
132+
.. autoclass:: LowerBound
133+
:members:
134+
.. autoclass:: UpperBound
135+
:members:
136+
.. autoclass:: Ordered
137+
:members:
138+
.. autoclass:: SumTo1
139+
:members:
140+
.. autoclass:: StickBreaking
141+
:members:
142+
.. autoclass:: Circular
143+
:members:

docs/source/api/variables.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Random Variables
2+
----------------
3+
4+
.. currentmodule:: pymc3.model
5+
6+
The normal PyMC3 programmer will typically not need to interact with these classes, except possibly when debugging. Otherwise they are primarily of interest to developers.
7+
8+
9+
.. autoclass:: PyMC3Variable
10+
:members:
11+
12+
13+
.. autoclass:: ValueGradFunction
14+
:members:
15+
16+
17+
.. autoclass:: FreeRV
18+
:members:
19+
20+
.. autoclass:: ObservedRV
21+
:members:
22+
23+
.. autoclass:: MultiObservedRV
24+
:members:
25+
26+
.. autoclass:: TransformedRV
27+
:members:
28+

docs/source/developer_guide.rst

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -378,25 +378,29 @@ https://github.com/pymc-devs/pymc3/blob/6d07591962a6c135640a3c31903eba66b34e71d8
378378
self.add_random_variable(var)
379379
return var
380380
381-
In general, if there is observed, the RV is defined as a ``ObservedRV``,
382-
otherwise if it has a transformed method, it is a ``TransformedRV``, otherwise, it returns the
383-
most elementary form: a ``FreeRV``.
381+
In general, if a variable has observations (``observed`` parameter), the RV is defined as an ``ObservedRV``,
382+
otherwise if it has a ``transformed`` (``transform`` parameter) attribute, it is a
383+
``TransformedRV``, otherwise, it will be the most elementary form: a
384+
``FreeRV``. Note that this means that random variables with
385+
observations cannot be transformed.
384386
385-
Below, I will take a deeper look into ``TransformedRV``, a normal user
387+
Below, I will take a deeper look into ``TransformedRV``. A normal user
386388
might not necessary come in contact with the concept, as
387389
``TransformedRV`` and ``TransformedDistribution`` are intentionally not
388390
user facing.
389391
390392
Because in PyMC3 there is no bijector class like in TFP or pyro, we only
391393
have a partial implementation called ``Transform``, which implements
392394
Jacobian correction for forward mapping only (there is no Jacobian
393-
correction for inverse mapping). The use case we considered are limited
395+
correction for inverse mapping). The use cases we considered are limited
394396
to the set of distributions that are bounded, and the transformation
395397
maps the bounded set to the real line - see
396-
`doc <https://docs.pymc.io/notebooks/api_quickstart.html#Automatic-transforms-of-bounded-RVs>`__.
398+
`doc
399+
<https://docs.pymc.io/notebooks/api_quickstart.html#Automatic-transforms-of-bounded-RVs>`__.
400+
However, other transformations are possible.
397401
In general, PyMC3 does not provide explicit functionality to transform
398402
one distribution to another. Instead, a dedicated distribution is
399-
usually created in consideration of optimising performance. But getting a
403+
usually created in order to optimise performance. But getting a
400404
``TransformedDistribution`` is also possible (see also in
401405
`doc <https://docs.pymc.io/notebooks/api_quickstart.html#Transformed-distributions-and-changes-of-variables>`__):
402406
@@ -422,7 +426,7 @@ usually created in consideration of optimising performance. But getting a
422426
423427
424428
425-
Now, back to ``model.RV(...)`` - things return from ``model.RV(...)``
429+
Now, back to ``model.RV(...)`` - things returned from ``model.RV(...)``
426430
are Theano tensor variables, and it is clear from looking at
427431
``TransformedRV``:
428432
@@ -431,19 +435,19 @@ are Theano tensor variables, and it is clear from looking at
431435
class TransformedRV(TensorVariable):
432436
...
433437
434-
as for ``FreeRV`` and ``ObservedRV``, they are TensorVariable with
435-
Factor:
438+
as for ``FreeRV`` and ``ObservedRV``, they are ``TensorVariable``\s with
439+
``Factor`` as mixin:
436440
437441
.. code:: python
438442
439443
class FreeRV(Factor, TensorVariable):
440444
...
441445
442-
and ``Factor`` basically `enable and assign the
446+
``Factor`` basically `enable and assign the
443447
logp <https://github.com/pymc-devs/pymc3/blob/6d07591962a6c135640a3c31903eba66b34e71d8/pymc3/model.py#L195-L276>`__
444448
(representated as a tensor also) property to a Theano tensor (thus
445-
making it a random variable). For a ``TransformedRV``, it transform the
446-
distribution into a ``TransformedDistribution``, and then model.Var is
449+
making it a random variable). For a ``TransformedRV``, it transforms the
450+
distribution into a ``TransformedDistribution``, and then ``model.Var`` is
447451
called again to added the RV associated with the
448452
``TransformedDistribution`` as a ``FreeRV``:
449453
@@ -483,11 +487,11 @@ the model logp), and also deterministic transformation (as bookkeeping):
483487
named\_vars, free\_RVs, observed\_RVs, deterministics, potentials,
484488
missing\_values. The model context then computes some simple model
485489
properties, builds a bijection mapping that transforms between
486-
dictionary and numpy/Theano ndarray, thus allowing logp/dlogp function
487-
to have two equivalent version: one take a dict as input and the other
488-
take a ndarray as input. More importantly, a pm.Model() contains methods
489-
to compile Theano function that takes Random Variables (that are also
490-
initialised within the same model) as input.
490+
dictionary and numpy/Theano ndarray, thus allowing the ``logp``/``dlogp`` functions
491+
to have two equivalent versions: one takes a ``dict`` as input and the other
492+
takes an ``ndarray`` as input. More importantly, a ``pm.Model()`` contains methods
493+
to compile Theano functions that take Random Variables (that are also
494+
initialised within the same model) as input, for example:
491495
492496
.. code:: python
493497

pymc3/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from .blocking import *
55
from .distributions import *
6+
from .distributions import transforms
67
from .glm import *
78
from . import gp
89
from .math import logaddexp, logsumexp, logit, invlogit, expand_packed_triangular, probit, invprobit

pymc3/distributions/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@
8181
from .timeseries import MvGaussianRandomWalk
8282
from .timeseries import MvStudentTRandomWalk
8383

84-
from .transforms import transform
85-
from .transforms import stick_breaking
86-
from .transforms import logodds
87-
from .transforms import log
88-
from .transforms import sum_to_1
89-
9084
from .bound import Bound
9185

9286
__all__ = ['Uniform',

0 commit comments

Comments
 (0)