Skip to content

Commit f51ce8c

Browse files
authored
Merge pull request #27 from CloudChaoszero/replace-pymc3-arviz-plots_part1
Replacing PyMC3 plots w/ Arviz plots & sigma Param change [Part 1]
2 parents f624891 + 6ffdc0f commit f51ce8c

9 files changed

+27
-22
lines changed

Diff for: examples/case_studies/disaster_model.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
"""
1010

11-
11+
import arviz as az
1212
import theano.tensor as tt
1313

1414
from numpy import arange, array
@@ -151,5 +151,5 @@
151151
# Initial values for stochastic nodes
152152
start = {"early_mean": 2.0, "late_mean": 3.0}
153153

154-
tr = pm.sample(1000, tune=500, start=start)
155-
pm.traceplot(tr)
154+
tr = pm.sample(1000, tune=500, start=start, cores=1)
155+
az.plot_trace(tr)

Diff for: examples/case_studies/disaster_model_theano_op.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Note that gradient based samplers will not work.
55
"""
66

7-
7+
import arviz as az
88
import theano.tensor as tt
99

1010
from numpy import arange, array, empty
@@ -165,5 +165,5 @@ def rate_(switchpoint, early_mean, late_mean):
165165
# Initial values for stochastic nodes
166166
start = {"early_mean": 2.0, "late_mean": 3.0}
167167

168-
tr = pm.sample(1000, tune=500, start=start, step=[step1, step2], cores=2)
169-
pm.traceplot(tr)
168+
tr = pm.sample(1000, tune=500, start=start, step=[step1, step2], cores=1)
169+
az.plot_trace(tr)

Diff for: examples/case_studies/garch_example.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import numpy as np
22
import theano.tensor as tt
33

4-
from pymc3 import Model, Normal, Uniform, sample, summary
4+
from arviz import summary
5+
from pymc3 import Model, Normal, Uniform, sample
56

67
"""
78
Example from Stan - slightly altered

Diff for: examples/case_studies/gelman_schools.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22

3-
from pymc3 import HalfCauchy, Model, Normal, loo, sample
3+
from arviz import loo
4+
from pymc3 import HalfCauchy, Model, Normal, sample
45

56
"""Original Stan model
67

Diff for: examples/case_studies/lightspeed_example.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import numpy as np
2-
2+
import arviz as az
33
import pymc3 as pm
44

55
light_speed = np.array(
@@ -92,7 +92,7 @@ def run(n=5000):
9292
with model_1:
9393
trace = pm.sample(n)
9494

95-
pm.summary(trace)
95+
az.summary(trace)
9696

9797

9898
if __name__ == "__main__":

Diff for: examples/pymc3_howto/LKJ_correlation.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22
import theano.tensor as tt
3-
3+
import arviz as az
44
from numpy.random import multivariate_normal
55

66
import pymc3 as pm
@@ -53,8 +53,8 @@ def run(n=1000):
5353
n = 50
5454
with model:
5555
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)]}
56+
az.plot_trace(
57+
trace, var_names=["mu", "r"]
5858
)
5959

6060

Diff for: examples/pymc3_howto/rankdata_ordered.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import arviz as az
12
import numpy as np
23
import theano.tensor as tt
34

@@ -46,14 +47,14 @@ def run(n=1500):
4647
with m:
4748
trace = pm.sample(n)
4849

49-
pm.traceplot(trace, varnames=["mu_hat"])
50+
az.plot_trace(trace, var_names=["mu_hat"])
5051

5152
print("Example observed data: ")
5253
print(y[:30, :].T)
5354
print("The true ranking is: ")
5455
print(yreal.flatten())
5556
print("The Latent mean is: ")
56-
latentmu = np.hstack(([0], pm.summary(trace, varnames=["mu_hat"])["mean"].values))
57+
latentmu = np.hstack(([0], az.summary(trace, var_names=["mu_hat"])["mean"].values))
5758
print(np.round(latentmu, 2))
5859
print("The estimated ranking is: ")
5960
print(np.argsort(latentmu))

Diff for: examples/samplers/samplers_mvnormal.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import time
1111

12+
import arviz as az
1213
import numpy as np
1314
import pandas as pd
1415
import theano.tensor as tt
@@ -46,11 +47,11 @@ def run(steppers, p):
4647
for step_cls in steppers:
4748
name = step_cls.__name__
4849
t_start = time.time()
49-
mt = pm.sample(draws=10000, chains=16, parallelize=False, step=step_cls(), start=start)
50+
mt = pm.sample(draws=10000, chains=16, step=step_cls(), start=start)
5051
runtimes[name] = time.time() - t_start
5152
print("{} samples across {} chains".format(len(mt) * mt.nchains, mt.nchains))
5253
traces[name] = mt
53-
en = pm.ess(mt)
54+
en = az.ess(mt)
5455
print(f"effective: {en}\r\n")
5556
if USE_XY:
5657
effn[name] = np.mean(en["x"]) / len(mt) / mt.nchains
@@ -74,9 +75,9 @@ def run(steppers, p):
7475
for p in df_effectiven.index:
7576
trace, rate, runtime = run(methods, p)
7677
for name in names:
77-
df_effectiven.set_value(p, name, rate[name])
78-
df_runtime.set_value(p, name, runtime[name])
79-
df_performance.set_value(p, name, rate[name] / runtime[name])
78+
df_effectiven.at[p, name] = rate[name]
79+
df_runtime.at[p, name] = runtime[name]
80+
df_performance.at[p, name] = rate[name] / runtime[name]
8081

8182
print("\r\nEffective sample size [0...1]")
8283
print(df_effectiven.T.to_string(float_format="{:.3f}".format))

Diff for: examples/time_series/arma_example.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from theano import scan, shared
44

5+
import arviz as az
56
import pymc3 as pm
67

78
"""
@@ -82,8 +83,8 @@ def run(n_samples=1000):
8283
with model:
8384
trace = pm.sample(draws=n_samples, tune=1000, target_accept=0.99)
8485

85-
pm.plots.traceplot(trace)
86-
pm.plots.forestplot(trace)
86+
az.plot_trace(trace)
87+
az.plot_forest(trace)
8788

8889

8990
if __name__ == "__main__":

0 commit comments

Comments
 (0)