Skip to content

Commit 3dc9a9d

Browse files
rpgoldmanjunpenglao
authored andcommitted
Expand documentation (#3164)
* Clarify that backends need model context. * Rewrite Bound docstring. * Modify tutorial intro to Bound.
1 parent 27be3da commit 3dc9a9d

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

docs/source/api/bounds.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ PyMC3 includes the construct :class:`~pymc3.distributions.bound.Bound` for
66
placing constraints on existing probability distributions. It modifies a given
77
distribution to take values only within a specified interval.
88

9+
Note that the `Bound` class does *not* directly create a bounded
10+
distribution: instead it creates a Callable class that can be
11+
*invoked* to create a bounded distribution, as the example below illustrates.
12+
913
Some types of variables require constraints. For instance, it doesn't make
1014
sense for a standard deviation to have a negative value, so something like a
1115
Normal prior on a parameter that represents a standard deviation would be
@@ -43,6 +47,14 @@ cleaner notationally to define both the bound and variable together. ::
4347
with model:
4448
x = pm.Bound(pm.Normal, lower=0.0)('x', mu=1.0, sd=3.0)
4549

50+
However, it is possible to create multiple different random variables
51+
that have the same bound applied to them::
52+
53+
with model:
54+
BoundNormal = pm.Bound(pm.Normal, lower=0.0)
55+
hyper_mu = BoundNormal("hyper_mu", mu=1, sd=0.5)
56+
mu = BoundNormal("mu", mu=hyper_mu, sd=1)
57+
4658
Bounds can also be applied to a vector of random variables. With the same
4759
``BoundedNormal`` object we created previously we can write::
4860

pymc3/backends/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
in the directory 'test'.
2121
2222
>>> import pymc3 as pm
23-
>>> db = pm.backends.Text('test')
24-
>>> trace = pm.sample(..., trace=db)
23+
>>> with pm.Model():
24+
>>> db = pm.backends.Text('test')
25+
>>> trace = pm.sample(..., trace=db)
26+
27+
Note that as in the example above, one must have an active model context,
28+
or pass a `model` parameter in order to create a backend.
2529
2630
Selecting values from a backend
2731
-------------------------------

pymc3/distributions/bound.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ def __init__(self, distribution, lower, upper,
157157

158158
class Bound(object):
159159
R"""
160-
Create a new upper, lower or upper+lower bounded distribution.
160+
Create a Bound variable object that can be applied to create
161+
a new upper, lower, or upper and lower bounded distribution.
161162
162163
The resulting distribution is not normalized anymore. This
163164
is usually fine if the bounds are constants. If you need
@@ -181,9 +182,13 @@ class Bound(object):
181182
182183
with pm.Model():
183184
NegativeNormal = pm.Bound(pm.Normal, upper=0.0)
184-
par1 = NegativeNormal('par2', mu=0.0, sd=1.0, testval=1.0)
185+
par1 = NegativeNormal('par`', mu=0.0, sd=1.0, testval=-0.5)
186+
# you can use the Bound object multiple times to
187+
# create multiple bounded random variables
188+
par1_1 = NegativeNormal('par1_1', mu=-1.0, sd=1.0, testval=-1.5)
185189
186-
# or you can define it implicitly within the model context
190+
# you can also define a Bound implicitly, while applying
191+
# it to a random variable
187192
par2 = pm.Bound(pm.Normal, lower=-1.0, upper=1.0)(
188193
'par2', mu=0.0, sd=1.0, testval=1.0)
189194
"""

0 commit comments

Comments
 (0)