Skip to content

Commit 6f77cc6

Browse files
HolgerPeterstwiecki
authored andcommitted
Doc Improvements: Bound API doc, HalfNormal description, markup fixes (#2888)
* Include API reference for Bound class * Include API reference for Bound class * In documentation for bounds, link to distributions * Fix mean and variance of HalfNormal in docs * Add missing sphinx markup for kronecker/math * Fix external link in docs notebook * Add encoding declaration in distribution.py * Change URL to https * Fix markup for MatrixNormal
1 parent e72b0d5 commit 6f77cc6

File tree

6 files changed

+45
-21
lines changed

6 files changed

+45
-21
lines changed

docs/source/api/bounds.rst

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
Bounded Variables
33
=================
44

5-
PyMC3 includes the construct ``Bound`` for placing constraints on existing
6-
probability distributions. It modifies a given distribution to take values
7-
only within a specified interval.
5+
PyMC3 includes the construct :class:`~pymc3.distributions.bound.Bound` for
6+
placing constraints on existing probability distributions. It modifies a given
7+
distribution to take values only within a specified interval.
88

99
Some types of variables require constraints. For instance, it doesn't make
1010
sense for a standard deviation to have a negative value, so something like a
1111
Normal prior on a parameter that represents a standard deviation would be
1212
inappropriate. PyMC3 includes distributions that have positive support, such
13-
as ``Gamma`` or ``Exponential``. PyMC3 also includes several bounded
14-
distributions, such as ``Uniform``, ``HalfNormal``, and ``HalfCauchy``, that
15-
are restricted to a specific domain.
13+
as :class:`~pymc3.distributions.continuous.Gamma` or
14+
:class:`~pymc3.distributions.continuous.Exponential`. PyMC3 also includes
15+
several bounded distributions, such as
16+
:class:`~pymc3.distributions.continuous.Uniform`,
17+
:class:`~pymc3.distributions.continuous.HalfNormal`, and
18+
:class:`~pymc3.distributions.continuous.HalfCauchy`, that are restricted to a
19+
specific domain.
1620

1721
All univariate distributions in PyMC3 can be given bounds. The distribution of
1822
a continuous variable that has been bounded is automatically transformed into
@@ -32,7 +36,7 @@ specification of a bounded distribution should go within the model block::
3236
with pm.Model() as model:
3337
BoundedNormal = pm.Bound(pm.Normal, lower=0.0)
3438
x = BoundedNormal('x', mu=1.0, sd=3.0)
35-
39+
3640
If the bound will be applied to a single variable in the model, it may be
3741
cleaner notationally to define both the bound and variable together. ::
3842

@@ -42,17 +46,24 @@ cleaner notationally to define both the bound and variable together. ::
4246
Bounds can also be applied to a vector of random variables. With the same
4347
``BoundedNormal`` object we created previously we can write::
4448

45-
with model:
49+
with model:
4650
x_vector = BoundedNormal('x_vector', mu=1.0, sd=3.0, shape=3)
4751

4852
Caveats
4953
#######
5054

5155
* Bounds cannot be given to variables that are ``observed``. To model
52-
truncated data, use a ``Potential`` in combination with a cumulative
56+
truncated data, use a :func:`~pymc3.model.Potential` in combination with a cumulative
5357
probability function. See `this example <https://github.com/pymc-devs/pymc3/blob/master/pymc3/examples/censored_data.py>`_.
5458

5559
* The automatic transformation applied to continuous distributions results in
5660
an unnormalized probability distribution. This doesn't effect inference
5761
algorithms but may complicate some model comparison procedures.
5862

63+
API
64+
###
65+
66+
67+
.. currentmodule:: pymc3.distributions.bound
68+
.. automodule:: pymc3.distributions.bound
69+
:members:

docs/source/notebooks/bayesian_neural_network_with_sgfs.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"source": [
1717
"## Stochastic Gradient Fisher Scoring\n",
1818
"\n",
19-
"Author: [__shkr__](www.github.com/shkr)\n",
19+
"Author: [__shkr__](https://www.github.com/shkr)\n",
2020
"\n",
2121
"```\n",
2222
"Can we approximately sample from a Bayesian posterior distribution if we are only allowed to touch a small mini-batch of data-items for every sample we generate?\n",

pymc3/distributions/bound.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class Bound(object):
161161
The resulting distribution is not normalized anymore. This
162162
is usually fine if the bounds are constants. If you need
163163
truncated distributions, use `Bound` in combination with
164-
a `pm.Potential` with the cumulative probability function.
164+
a :class:`~pymc3.model.Potential` with the cumulative probability function.
165165
166166
The bounds are inclusive for discrete distributions.
167167

pymc3/distributions/continuous.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,19 @@ class HalfNormal(PositiveContinuous):
336336
337337
f(x \mid \tau) =
338338
\sqrt{\frac{2\tau}{\pi}}
339-
\exp\left\{ {\frac{-x^2 \tau}{2}}\right\}
339+
\exp\left(\frac{-x^2 \tau}{2}\right)
340+
341+
f(x \mid \sigma) =\sigma
342+
\sqrt{\frac{2}{\pi}}
343+
\exp\left(\frac{-x^2}{2\sigma^2}\right)
344+
345+
.. note::
346+
347+
The parameters ``sigma``/``tau`` (:math:`\sigma`/:math:`\tau`) refer to
348+
the standard deviation/precision of the unfolded normal distribution, for
349+
the standard deviation of the half-normal distribution, see below. For
350+
the half-normal, they are just two parameterisation :math:`\sigma^2
351+
\equiv \frac{1}{\tau}` of a scale parameter
340352
341353
.. plot::
342354
@@ -355,24 +367,24 @@ class HalfNormal(PositiveContinuous):
355367
356368
======== ==========================================
357369
Support :math:`x \in [0, \infty)`
358-
Mean :math:`0`
359-
Variance :math:`\dfrac{1}{\tau}` or :math:`\sigma^2`
370+
Mean :math:`\sqrt{\dfrac{2}{\tau \pi}}` or :math:`\dfrac{\sigma \sqrt{2}}{\sqrt{\pi}}`
371+
Variance :math:`\dfrac{1}{\tau}\left(1 - \dfrac{2}{\pi}\right)` or :math:`\sigma^2\left(1 - \dfrac{2}{\pi}\right)`
360372
======== ==========================================
361373
362374
Parameters
363375
----------
364376
sd : float
365-
Standard deviation (sd > 0) (only required if tau is not specified).
377+
Scale parameter :math:`sigma` (``sd`` > 0) (only required if ``tau`` is not specified).
366378
tau : float
367-
Precision (tau > 0) (only required if sd is not specified).
368-
379+
Precision :math:`tau` (tau > 0) (only required if sd is not specified).
380+
369381
Examples
370382
--------
371383
.. code-block:: python
372384
373385
with pm.Model():
374386
x = pm.HalfNormal('x', sd=10)
375-
387+
376388
with pm.Model():
377389
x = pm.HalfNormal('x', tau=1/15)
378390
"""

pymc3/distributions/multivariate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,8 @@ class MatrixNormal(Continuous):
11511151
properties for inversion. For example, if draws from MvNormal had the same
11521152
covariance structure, but were scaled by different powers of an unknown
11531153
constant, both the covariance and scaling could be learned as follows
1154-
(see the docstring of `LKJCholeskyCov` for more information about this)::
1154+
(see the docstring of `LKJCholeskyCov` for more information about this)
1155+
11551156
.. code:: python
11561157
11571158
# Setup data

pymc3/math.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
def kronecker(*Ks):
2727
"""Return the Kronecker product of arguments:
28-
K_1 \otimes K_2 \otimes ... \otimes K_D
28+
:math:`K_1 \otimes K_2 \otimes ... \otimes K_D`
2929
3030
Parameters
3131
----------
@@ -47,7 +47,7 @@ def cartesian(*arrays):
4747

4848

4949
def kron_matrix_op(krons, m, op):
50-
"""Apply op to krons and m in a way that reproduces op(kronecker(*krons), m)
50+
"""Apply op to krons and m in a way that reproduces ``op(kronecker(*krons), m)``
5151
5252
Parameters
5353
-----------

0 commit comments

Comments
 (0)