Skip to content

Commit ccb03d6

Browse files
ColCarrolltwiecki
authored andcommitted
TST Refactor some examples to run as tests, delete outdated (#1360)
1 parent 33ec4c8 commit ccb03d6

31 files changed

+461
-1778
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ install:
1818

1919
env:
2020
- TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 -e test_examples -e test_distributions" PYTHON_VERSION=${TRAVIS_PYTHON_VERSION}
21-
- TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 pymc3.tests.test_distributions" PYTHON_VERSION=${TRAVIS_PYTHON_VERSION}
21+
- TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 pymc3.tests.test_distributions pymc3.tests.test_examples" PYTHON_VERSION=${TRAVIS_PYTHON_VERSION}
2222
- TESTCMD=" -vv --with-timer --with-coverage --cover-package=pymc3 pymc3.tests.test_distributions_random" PYTHON_VERSION=${TRAVIS_PYTHON_VERSION}
2323

2424
script:

pymc3/examples/ARM12_6.py

Lines changed: 0 additions & 66 deletions
This file was deleted.

pymc3/examples/ARM12_6uranium.py

Lines changed: 0 additions & 73 deletions
This file was deleted.

pymc3/examples/ARM5_4.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

pymc3/examples/ATMIP_2gaussians.py

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
from pymc3 import *
21
import numpy as np
3-
with Model() as model:
4-
lam = Exponential('lam', 1)
2+
import pymc3 as pm
3+
import theano.tensor as tt
54

6-
failure = np.array([0, 1])
7-
value = np.array([1, 0])
85

9-
def logp(failure, value):
10-
return sum(failure * log(lam) - lam * value)
6+
def build_model():
7+
with pm.Model() as model:
8+
lam = pm.Exponential('lam', 1)
9+
failure = np.array([0, 1])
10+
value = np.array([1, 0])
1111

12-
x = DensityDist('x', logp, observed={'failure': failure, 'value': value})
12+
def logp(failure, value):
13+
return tt.sum(failure * np.log(lam) - lam * value)
14+
pm.DensityDist('x', logp, observed={'failure': failure, 'value': value})
15+
return model
1316

1417

15-
def run(n=3000):
16-
if n == "short":
17-
n = 50
18-
with model:
19-
20-
start = model.test_point
21-
h = find_hessian(start)
22-
step = Metropolis(model.vars, h, blocked=True)
23-
trace = sample(n, step, start)
18+
def run(n_samples=3000):
19+
model = build_model()
20+
start = model.test_point
21+
h = pm.find_hessian(start, model=model)
22+
step = pm.Metropolis(model.vars, h, blocked=True, model=model)
23+
trace = pm.sample(n_samples, step, start, model=model)
24+
return trace
2425

2526
if __name__ == "__main__":
2627
run()

pymc3/examples/arma_example.py

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
2-
from pymc3 import Normal, sample, Model, traceplot, plots, NUTS, Potential, variational, Cauchy, find_MAP, Slice, HalfCauchy
1+
from pymc3 import Normal, sample, Model, plots, Potential, variational, HalfCauchy
32
from theano import scan, shared
4-
from scipy import optimize
5-
import theano.tensor as tt
6-
73

84
import numpy as np
95
"""
@@ -53,46 +49,39 @@
5349
Ported to PyMC3 by Peadar Coyle and Chris Fonnesbeck (c) 2016.
5450
"""
5551

56-
t = np.array([1, 2, 4, 5, 6, 8, 19, 18, 12])
57-
58-
y = shared(np.array([15, 10, 16, 11, 9, 11, 10, 18], dtype=np.float32))
59-
60-
61-
with Model() as arma_model:
62-
63-
sigma = HalfCauchy('sigma', 5)
64-
theta = Normal('theta', 0, sd=2)
65-
phi = Normal('phi', 0, sd=2)
66-
mu = Normal('mu', 0, sd=10)
67-
68-
err0 = y[0] - (mu + phi * mu)
69-
70-
def calc_next(last_y, this_y, err, mu, phi, theta):
71-
nu_t = mu + phi * last_y + theta * err
72-
return this_y - nu_t
7352

74-
err, _ = scan(fn=calc_next,
75-
sequences=dict(input=y, taps=[-1, 0]),
76-
outputs_info=[err0],
77-
non_sequences=[mu, phi, theta])
53+
def build_model():
54+
y = shared(np.array([15, 10, 16, 11, 9, 11, 10, 18], dtype=np.float32))
55+
with Model() as arma_model:
56+
sigma = HalfCauchy('sigma', 5)
57+
theta = Normal('theta', 0, sd=2)
58+
phi = Normal('phi', 0, sd=2)
59+
mu = Normal('mu', 0, sd=10)
7860

79-
like = Potential('like', Normal.dist(0, sd=sigma).logp(err))
61+
err0 = y[0] - (mu + phi * mu)
8062

81-
with arma_model:
82-
mu, sds, elbo = variational.advi(n=2000)
63+
def calc_next(last_y, this_y, err, mu, phi, theta):
64+
nu_t = mu + phi * last_y + theta * err
65+
return this_y - nu_t
8366

67+
err, _ = scan(fn=calc_next,
68+
sequences=dict(input=y, taps=[-1, 0]),
69+
outputs_info=[err0],
70+
non_sequences=[mu, phi, theta])
8471

85-
def run(n=1000):
86-
if n == "short":
87-
n = 50
88-
with arma_model:
72+
Potential('like', Normal.dist(0, sd=sigma).logp(err))
73+
mu, sds, elbo = variational.advi(n=2000)
74+
return arma_model
8975

90-
trace = sample(1000)
9176

92-
burn = n / 10
77+
def run(n_samples=1000):
78+
model = build_model()
79+
with model:
80+
trace = sample(draws=n_samples)
9381

94-
traceplot(trace[burn:])
95-
plots.summary(trace[burn:])
82+
burn = n_samples // 10
83+
plots.traceplot(trace[burn:])
84+
plots.forestplot(trace[burn:])
9685

9786

9887
if __name__ == '__main__':

0 commit comments

Comments
 (0)