Skip to content

Commit ed62da1

Browse files
authored
Add introduction section to Transformations API page to document role of Transforms (#7232)
1 parent 4bc8439 commit ed62da1

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

docs/source/api/distributions/transforms.rst

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,42 @@ Transformations
44

55
.. currentmodule:: pymc.distributions.transforms
66

7+
While many distributions are defined on constrained spaces (e.g. intervals), MCMC samplers typically perform best when sampling on the unconstrained real line; this is especially true of HMC samplers. PyMC balances this through the use of transforms. A transform instance can be passed to the constructor of a random variable to tell the sampler how to move between the underlying unconstrained space where the samples are actually drawn and the transformed space constituting the support of the random variable. Transforms are not currently implemented for discrete random variables.
8+
9+
All transforms have three core methods:
10+
11+
* ``forward``: The map from a constrained space to the unconstrained space.
12+
* ``backward``: The inverse map from the unconstrained space to a constrained space.
13+
* ``log_jac_det``: The log of the determinant of the Jacobian of the ``backward`` map. This is used to account for the transformed random variable correctly in the posterior log-probability.
14+
15+
.. note::
16+
Transforms are principally intended for internal use and in most cases users do not need to change them. In particular, all continuous distributions on a constrained domain that are implemented in PyMC have a ``default_transform`` that will automatically transform the random variables as required without needing any extra work from the user.
17+
18+
The main use-cases for setting custom transforms include the following:
19+
20+
#. The ``default_transform`` may need to be replaced with an alternative transform on the same constained space. For example, the ``default_transform`` for positive-valued random variables is the :class:`log` transform but in some cases it may be advantageous to use the :class:`log_exp_m1` transform instead.
21+
#. The ``default_transform`` may be removed entirely in some cases when using non-HMC samplers.
22+
#. Exceptionally, transforms can be used to *add* constraints to the model specification without modifying the ``default_transform``. This can be done by specifying the additional transform via the ``transform`` parameter. However this should not be viewed as a default use-case and, in practice, this is mostly limited to using :class:`ordered` in mixture models.
23+
24+
* NB: :class:`ordered` is not guaranteed to work correctly when used in combination with other transforms, such as :class:`simplex` and :class:`ZeroSumTransform`.
25+
26+
.. warning::
27+
Transforms are **only** applied when sampling *unobserved* random variables with :func:`pymc.sample`. In particular:
28+
29+
* Transforms are not applied during forward sampling, i.e. :func:`pymc.draw`, :func:`pymc.sample_prior_predictive` and :func:`pymc.sample_posterior_predictive`
30+
* Transforms are not applied when sampling *observed* random variables with :func:`pymc.sample`
31+
32+
Since transforms are not applied during :func:`pymc.sample_prior_predictive`, a workaround to carry out prior predictive checks is to remove observations from the likelihood and use :func:`pymc.sample` instead.
33+
34+
Transforms are not usually the correct tool to represent transformations that are part of the *generative* specification of the model. Such transformations should be included explicitly in the model, typically via :class:`pymc.Deterministic`. Doing so allows such transformed random variables to be sampled by forward samplers.
35+
36+
737
Transform Instances
838
~~~~~~~~~~~~~~~~~~~
939

1040
Transform instances are the entities that should be used in the
11-
``transform`` parameter to a random variable constructor.
41+
``default_transform`` or ``transform`` parameters to a random variable
42+
constructor.
1243

1344
.. autosummary::
1445
:toctree: generated
@@ -17,28 +48,42 @@ Transform instances are the entities that should be used in the
1748
log
1849
log_exp_m1
1950
logodds
20-
simplex
21-
sum_to_1
2251
ordered
52+
simplex
2353

2454

2555
Specific Transform Classes
2656
~~~~~~~~~~~~~~~~~~~~~~~~~~
2757

58+
An instance of these classes needs to be created before being used
59+
in the ``default_transform`` or ``transform`` parameters to a random variable
60+
constructor.
61+
2862
.. autosummary::
2963
:toctree: generated
3064

3165
CholeskyCovPacked
66+
CircularTransform
3267
Interval
3368
LogExpM1
69+
LogOddsTransform
70+
LogTransform
3471
Ordered
35-
SumTo1
72+
SimplexTransform
3673
ZeroSumTransform
3774

3875

3976
Transform Composition Classes
4077
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4178

79+
An instance of this class needs to be created from a list of transforms before
80+
being used in the ``transform`` parameter to a random variable constructor.
81+
82+
If a random variable has a ``default_transform`` and an additional transform
83+
is provided through the ``transform`` parameter, PyMC will automatically
84+
create an instance of the :class:`Chain` transform that applies the
85+
user-provided transform on top of the default one.
86+
4287
.. autosummary::
4388
:toctree: generated
4489

pymc/distributions/transforms.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ def log_jac_det(self, value, *rv_inputs):
322322
Instantiation of :class:`pymc.distributions.transforms.LogExpM1`
323323
for use in the ``transform`` argument of a random variable."""
324324

325-
# Deprecated
326325
ordered = Ordered()
327326
ordered.__doc__ = """
328327
Instantiation of :class:`pymc.distributions.transforms.Ordered`

0 commit comments

Comments
 (0)