Skip to content

Commit ad0e982

Browse files
authored
Merge pull request #5 from MarcoGorelli/migrate-examples
Add examples from pymc3
2 parents ff94a12 + 3bbc6e0 commit ad0e982

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+131215
-0
lines changed

examples/GHME_2013.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
import pandas as pd
4+
5+
from pymc3 import HalfCauchy, Model, Normal, get_data, sample
6+
from pymc3.distributions.timeseries import GaussianRandomWalk
7+
8+
data = pd.read_csv(get_data("pancreatitis.csv"))
9+
countries = ["CYP", "DNK", "ESP", "FIN", "GBR", "ISL"]
10+
data = data[data.area.isin(countries)]
11+
12+
age = data["age"] = np.array(data.age_start + data.age_end) / 2
13+
rate = data.value = data.value * 1000
14+
group, countries = pd.factorize(data.area, order=countries)
15+
16+
17+
ncountries = len(countries)
18+
19+
for i, country in enumerate(countries):
20+
plt.subplot(2, 3, i + 1)
21+
plt.title(country)
22+
d = data[data.area == country]
23+
plt.plot(d.age, d.value, ".")
24+
25+
plt.ylim(0, rate.max())
26+
27+
28+
nknots = 10
29+
knots = np.linspace(data.age_start.min(), data.age_end.max(), nknots)
30+
31+
32+
def interpolate(x0, y0, x, group):
33+
x = np.array(x)
34+
group = np.array(group)
35+
36+
idx = np.searchsorted(x0, x)
37+
dl = np.array(x - x0[idx - 1])
38+
dr = np.array(x0[idx] - x)
39+
d = dl + dr
40+
wl = dr / d
41+
42+
return wl * y0[idx - 1, group] + (1 - wl) * y0[idx, group]
43+
44+
45+
with Model() as model:
46+
coeff_sd = HalfCauchy("coeff_sd", 5)
47+
48+
y = GaussianRandomWalk("y", sigma=coeff_sd, shape=(nknots, ncountries))
49+
50+
p = interpolate(knots, y, age, group)
51+
52+
sd = HalfCauchy("sd", 5)
53+
54+
vals = Normal("vals", p, sigma=sd, observed=rate)
55+
56+
57+
def run(n=3000):
58+
if n == "short":
59+
n = 150
60+
with model:
61+
trace = sample(n, tune=int(n / 2), init="advi+adapt_diag")
62+
63+
for i, country in enumerate(countries):
64+
plt.subplot(2, 3, i + 1)
65+
plt.title(country)
66+
67+
d = data[data.area == country]
68+
plt.plot(d.age, d.value, ".")
69+
plt.plot(knots, trace[y][::5, :, i].T, color="r", alpha=0.01)
70+
71+
plt.ylim(0, rate.max())
72+
73+
74+
if __name__ == "__main__":
75+
run()

examples/LKJ_correlation.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import numpy as np
2+
import theano.tensor as tt
3+
4+
from numpy.random import multivariate_normal
5+
6+
import pymc3 as pm
7+
8+
# Generate some multivariate normal data:
9+
n_obs = 1000
10+
11+
# Mean values:
12+
mu_r = np.linspace(0, 2, num=4)
13+
n_var = len(mu_r)
14+
15+
# Standard deviations:
16+
stds = np.ones(4) / 2.0
17+
18+
# Correlation matrix of 4 variables:
19+
corr_r = np.array(
20+
[
21+
[1.0, 0.75, 0.0, 0.15],
22+
[0.75, 1.0, -0.06, 0.19],
23+
[0.0, -0.06, 1.0, -0.04],
24+
[0.15, 0.19, -0.04, 1.0],
25+
]
26+
)
27+
cov_matrix = np.diag(stds).dot(corr_r.dot(np.diag(stds)))
28+
29+
dataset = multivariate_normal(mu_r, cov_matrix, size=n_obs)
30+
31+
with pm.Model() as model:
32+
33+
mu = pm.Normal("mu", mu=0, sigma=1, shape=n_var)
34+
35+
# Note that we access the distribution for the standard
36+
# deviations, and do not create a new random variable.
37+
sd_dist = pm.HalfCauchy.dist(beta=2.5)
38+
packed_chol = pm.LKJCholeskyCov("chol_cov", n=n_var, eta=1, sd_dist=sd_dist)
39+
# compute the covariance matrix
40+
chol = pm.expand_packed_triangular(n_var, packed_chol, lower=True)
41+
cov = tt.dot(chol, chol.T)
42+
43+
# Extract the standard deviations etc
44+
sd = pm.Deterministic("sd", tt.sqrt(tt.diag(cov)))
45+
corr = tt.diag(sd ** -1).dot(cov.dot(tt.diag(sd ** -1)))
46+
r = pm.Deterministic("r", corr[np.triu_indices(n_var, k=1)])
47+
48+
like = pm.MvNormal("likelihood", mu=mu, chol=chol, observed=dataset)
49+
50+
51+
def run(n=1000):
52+
if n == "short":
53+
n = 50
54+
with model:
55+
trace = pm.sample(n)
56+
pm.traceplot(
57+
trace, varnames=["mu", "r"], lines={"mu": mu_r, "r": corr_r[np.triu_indices(n_var, k=1)]}
58+
)
59+
60+
61+
if __name__ == "__main__":
62+
run()

examples/__init__.py

Whitespace-only changes.

examples/arbitrary_stochastic.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
import theano.tensor as tt
3+
4+
import pymc3 as pm
5+
6+
7+
# custom log-liklihood
8+
def logp(failure, lam, value):
9+
return tt.sum(failure * tt.log(lam) - lam * value)
10+
11+
12+
def build_model():
13+
# data
14+
failure = np.array([0.0, 1.0])
15+
value = np.array([1.0, 0.0])
16+
17+
# model
18+
with pm.Model() as model:
19+
lam = pm.Exponential("lam", 1.0)
20+
pm.DensityDist("x", logp, observed={"failure": failure, "lam": lam, "value": value})
21+
return model
22+
23+
24+
def run(n_samples=3000):
25+
model = build_model()
26+
with model:
27+
trace = pm.sample(n_samples)
28+
return trace
29+
30+
31+
if __name__ == "__main__":
32+
run()

examples/arma_example.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import numpy as np
2+
3+
from theano import scan, shared
4+
5+
import pymc3 as pm
6+
7+
"""
8+
ARMA example
9+
It is interesting to note just how much more compact this is than the original Stan example
10+
11+
The original implementation is in the Stan documentation by Gelman et al and is reproduced below
12+
13+
14+
Example from Stan- slightly altered
15+
16+
data {
17+
int<lower=1> T;
18+
real y[T];
19+
}
20+
parameters {
21+
// assume err[0] == 0
22+
}
23+
nu[t] <- mu + phi * y[t-1] + theta * err[t-1];
24+
err[t] <- y[t] - nu[t];
25+
}
26+
mu ~ normal(0,10);
27+
phi ~ normal(0,2);
28+
theta ~ normal(0,2);
29+
real mu;
30+
real phi;
31+
real theta;
32+
real<lower=0> sigma;
33+
} model {
34+
vector[T] nu;
35+
vector[T] err;
36+
nu[1] <- mu + phi * mu;
37+
err[1] <- y[1] - nu[1];
38+
for (t in 2:T) {
39+
// num observations
40+
// observed outputs
41+
// mean coeff
42+
// autoregression coeff
43+
// moving avg coeff
44+
// noise scale
45+
// prediction for time t
46+
// error for time t
47+
sigma ~ cauchy(0,5);
48+
err ~ normal(0,sigma);
49+
// priors
50+
// likelihood
51+
Ported to PyMC3 by Peadar Coyle and Chris Fonnesbeck (c) 2016.
52+
"""
53+
54+
55+
def build_model():
56+
y = shared(np.array([15, 10, 16, 11, 9, 11, 10, 18], dtype=np.float32))
57+
with pm.Model() as arma_model:
58+
sigma = pm.HalfNormal("sigma", 5.0)
59+
theta = pm.Normal("theta", 0.0, sigma=1.0)
60+
phi = pm.Normal("phi", 0.0, sigma=2.0)
61+
mu = pm.Normal("mu", 0.0, sigma=10.0)
62+
63+
err0 = y[0] - (mu + phi * mu)
64+
65+
def calc_next(last_y, this_y, err, mu, phi, theta):
66+
nu_t = mu + phi * last_y + theta * err
67+
return this_y - nu_t
68+
69+
err, _ = scan(
70+
fn=calc_next,
71+
sequences=dict(input=y, taps=[-1, 0]),
72+
outputs_info=[err0],
73+
non_sequences=[mu, phi, theta],
74+
)
75+
76+
pm.Potential("like", pm.Normal.dist(0, sigma=sigma).logp(err))
77+
return arma_model
78+
79+
80+
def run(n_samples=1000):
81+
model = build_model()
82+
with model:
83+
trace = pm.sample(draws=n_samples, tune=1000, target_accept=0.99)
84+
85+
pm.plots.traceplot(trace)
86+
pm.plots.forestplot(trace)
87+
88+
89+
if __name__ == "__main__":
90+
run()

examples/baseball.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# Demonstrates the usage of hierarchical partial pooling
3+
# See http://mc-stan.org/documentation/case-studies/pool-binary-trials.html for more details
4+
#
5+
6+
import numpy as np
7+
8+
import pymc3 as pm
9+
10+
11+
def build_model():
12+
data = np.loadtxt(
13+
pm.get_data("efron-morris-75-data.tsv"), delimiter="\t", skiprows=1, usecols=(2, 3)
14+
)
15+
16+
atbats = pm.floatX(data[:, 0])
17+
hits = pm.floatX(data[:, 1])
18+
19+
N = len(hits)
20+
21+
# we want to bound the kappa below
22+
BoundedKappa = pm.Bound(pm.Pareto, lower=1.0)
23+
24+
with pm.Model() as model:
25+
phi = pm.Uniform("phi", lower=0.0, upper=1.0)
26+
kappa = BoundedKappa("kappa", alpha=1.0001, m=1.5)
27+
thetas = pm.Beta("thetas", alpha=phi * kappa, beta=(1.0 - phi) * kappa, shape=N)
28+
ys = pm.Binomial("ys", n=atbats, p=thetas, observed=hits)
29+
return model
30+
31+
32+
def run(n=2000):
33+
model = build_model()
34+
with model:
35+
trace = pm.sample(n, target_accept=0.99)
36+
37+
pm.traceplot(trace)
38+
39+
40+
if __name__ == "__main__":
41+
run()

0 commit comments

Comments
 (0)