Skip to content

Commit 004ce61

Browse files
mcavallarotwiecki
authored andcommitted
Arbitrary deterministic function added. (#2514)
* Arbitray deterministic function added. * Typo fixed (\mu insted of \eta) * Typo fixed (nu instead of mu) * Added sensible default value for eps in stick_breaking transform * add stacking and bb-pseudo-bma to the docs (#2512) * add stacking and bb-pseudo-bma to the docs * fix typo * fix typo * Created disaster_model.py example file * Rename disaster_model_arbitrary_deterministic.py to disaster_model_theano_op.py * Update getting_started.ipynb Replace PyMC with PyMC3. Replace the hyperlink to https://github.com/pymc-devs/pymc3/blob/master/pymc3/examples/disaster_model_arbitrary_deterministic.py with https://github.com/pymc-devs/pymc3/blob/master/pymc3/examples/disaster_model_theano_op.py * Converted eps value to floatX fixes test failures of #2515 and lowered precision requirements for test_transforms on float32 machines (#2517) * Arbitray deterministic function added. * Typo fixed (\mu insted of \eta) * Typo fixed (nu instead of mu) * Created disaster_model.py example file * Rename disaster_model_arbitrary_deterministic.py to disaster_model_theano_op.py * Update getting_started.ipynb Replace PyMC with PyMC3. Replace the hyperlink to https://github.com/pymc-devs/pymc3/blob/master/pymc3/examples/disaster_model_arbitrary_deterministic.py with https://github.com/pymc-devs/pymc3/blob/master/pymc3/examples/disaster_model_theano_op.py
1 parent 46714e3 commit 004ce61

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

docs/source/notebooks/BEST.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
"cell_type": "markdown",
161161
"metadata": {},
162162
"source": [
163-
"We follow Kruschke by making the prior for $\\mu$ exponentially distributed with a mean of 30; this allocates high prior probability over the regions of the parameter that describe the range from normal to heavy-tailed data under the Student-T distribution."
163+
"We follow Kruschke by making the prior for $\\nu$ exponentially distributed with a mean of 30; this allocates high prior probability over the regions of the parameter that describe the range from normal to heavy-tailed data under the Student-T distribution."
164164
]
165165
},
166166
{

docs/source/notebooks/getting_started.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@
11131113
"cell_type": "markdown",
11141114
"metadata": {},
11151115
"source": [
1116-
"An important drawback of this approach is that it is not possible for `theano` to inspect these functions in order to compute the gradient required for the Hamiltonian-based samplers. Therefore, it is not possible to use the HMC or NUTS samplers for a model that uses such an operator. However, it is possible to add a gradient if we inherit from `theano.Op` instead of using `as_op`. The PyMC example set includes [a more elaborate example of the usage of as_op](https://github.com/pymc-devs/pymc3/blob/master/pymc3/examples/disaster_model_arbitrary_deterministic.py)."
1116+
"An important drawback of this approach is that it is not possible for `theano` to inspect these functions in order to compute the gradient required for the Hamiltonian-based samplers. Therefore, it is not possible to use the HMC or NUTS samplers for a model that uses such an operator. However, it is possible to add a gradient if we inherit from `theano.Op` instead of using `as_op`. The PyMC example set includes [a more elaborate example of the usage of as_op](https://github.com/pymc-devs/pymc3/blob/master/pymc3/examples/disaster_model_theano_op.py)."
11171117
]
11181118
},
11191119
{
@@ -1122,7 +1122,7 @@
11221122
"source": [
11231123
"## Arbitrary distributions\n",
11241124
"\n",
1125-
"Similarly, the library of statistical distributions in PyMC3 is not exhaustive, but PyMC allows for the creation of user-defined functions for an arbitrary probability distribution. For simple statistical distributions, the `DensityDist` function takes as an argument any function that calculates a log-probability $log(p(x))$. This function may employ other random variables in its calculation. Here is an example inspired by a blog post by Jake Vanderplas on which priors to use for a linear regression (Vanderplas, 2014). \n",
1125+
"Similarly, the library of statistical distributions in PyMC3 is not exhaustive, but PyMC3 allows for the creation of user-defined functions for an arbitrary probability distribution. For simple statistical distributions, the `DensityDist` function takes as an argument any function that calculates a log-probability $log(p(x))$. This function may employ other random variables in its calculation. Here is an example inspired by a blog post by Jake Vanderplas on which priors to use for a linear regression (Vanderplas, 2014). \n",
11261126
"\n",
11271127
"```python\n",
11281128
"import theano.tensor as tt\n",

pymc3/examples/disaster_model.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
A model for the disasters data with a changepoint
3+
4+
changepoint ~ U(1851, 1962)
5+
early_mean ~ Exp(1.)
6+
late_mean ~ Exp(1.)
7+
disasters[t] ~ Poi(early_mean if t <= switchpoint, late_mean otherwise)
8+
9+
"""
10+
11+
12+
import pymc3 as pm
13+
import theano.tensor as tt
14+
from numpy import arange, array
15+
16+
17+
__all__ = ['disasters_data', 'switchpoint', 'early_mean', 'late_mean', 'rate',
18+
'disasters']
19+
20+
21+
# Time series of recorded coal mining disasters in the UK from 1851 to 1962
22+
disasters_data = array([4, 5, 4, 0, 1, 4, 3, 4, 0, 6, 3, 3, 4, 0, 2, 6,
23+
3, 3, 5, 4, 5, 3, 1, 4, 4, 1, 5, 5, 3, 4, 2, 5,
24+
2, 2, 3, 4, 2, 1, 3, 2, 2, 1, 1, 1, 1, 3, 0, 0,
25+
1, 0, 1, 1, 0, 0, 3, 1, 0, 3, 2, 2, 0, 1, 1, 1,
26+
0, 1, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 1, 1, 0, 2,
27+
3, 3, 1, 1, 2, 1, 1, 1, 1, 2, 4, 2, 0, 0, 1, 4,
28+
0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1])
29+
year = arange(1851, 1962)
30+
31+
with pm.Model() as model:
32+
33+
switchpoint = pm.DiscreteUniform('switchpoint', lower=year.min(), upper=year.max())
34+
early_mean = pm.Exponential('early_mean', lam=1.)
35+
late_mean = pm.Exponential('late_mean', lam=1.)
36+
37+
# Allocate appropriate Poisson rates to years before and after current
38+
# switchpoint location
39+
rate = tt.switch(switchpoint >= year, early_mean, late_mean)
40+
41+
disasters = pm.Poisson('disasters', rate, observed=disasters_data)
42+
43+
# Initial values for stochastic nodes
44+
start = {'early_mean': 2., 'late_mean': 3.}
45+
46+
tr = pm.sample(1000, tune=500, start=start)
47+
pm.traceplot(tr)

pymc3/examples/disaster_model_arbitrary_deterministic.py renamed to pymc3/examples/disaster_model_theano_op.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pymc3 as pm
99
import theano.tensor as tt
10-
from numpy import arange, array
10+
from numpy import arange, array, empty
1111

1212
__all__ = ['disasters_data', 'switchpoint', 'early_mean', 'late_mean', 'rate',
1313
'disasters']
@@ -22,6 +22,15 @@
2222
0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1])
2323
years = len(disasters_data)
2424

25+
26+
@as_op(itypes=[tt.lscalar, tt.dscalar, tt.dscalar], otypes=[tt.dvector])
27+
def rate_(switchpoint, early_mean, late_mean):
28+
out = empty(years)
29+
out[:switchpoint] = early_mean
30+
out[switchpoint:] = late_mean
31+
return out
32+
33+
2534
with pm.Model() as model:
2635

2736
# Prior for distribution of switchpoint location
@@ -33,8 +42,8 @@
3342
# Allocate appropriate Poisson rates to years before and after current
3443
# switchpoint location
3544
idx = arange(years)
36-
rate = tt.switch(switchpoint >= idx, early_mean, late_mean)
37-
45+
rate = rate_(switchpoint, early_mean, late_mean)
46+
3847
# Data likelihood
3948
disasters = pm.Poisson('disasters', rate, observed=disasters_data)
4049

0 commit comments

Comments
 (0)