Skip to content

Replacing PyMC3 plots w/ Arviz plots & sigma Param change [Part 1] #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/case_studies/disaster_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

"""


import arviz as az
import theano.tensor as tt

from numpy import arange, array
Expand Down Expand Up @@ -151,5 +151,5 @@
# Initial values for stochastic nodes
start = {"early_mean": 2.0, "late_mean": 3.0}

tr = pm.sample(1000, tune=500, start=start)
pm.traceplot(tr)
tr = pm.sample(1000, tune=500, start=start, cores=1)
az.plot_trace(tr)
6 changes: 3 additions & 3 deletions examples/case_studies/disaster_model_theano_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Note that gradient based samplers will not work.
"""


import arviz as az
import theano.tensor as tt

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

tr = pm.sample(1000, tune=500, start=start, step=[step1, step2], cores=2)
pm.traceplot(tr)
tr = pm.sample(1000, tune=500, start=start, step=[step1, step2], cores=1)
az.plot_trace(tr)
3 changes: 2 additions & 1 deletion examples/case_studies/garch_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
import theano.tensor as tt

from pymc3 import Model, Normal, Uniform, sample, summary
from arviz import summary
from pymc3 import Model, Normal, Uniform, sample

"""
Example from Stan - slightly altered
Expand Down
3 changes: 2 additions & 1 deletion examples/case_studies/gelman_schools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np

from pymc3 import HalfCauchy, Model, Normal, loo, sample
from arviz import loo
from pymc3 import HalfCauchy, Model, Normal, sample

"""Original Stan model

Expand Down
4 changes: 2 additions & 2 deletions examples/case_studies/lightspeed_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np

import arviz as az
import pymc3 as pm

light_speed = np.array(
Expand Down Expand Up @@ -92,7 +92,7 @@ def run(n=5000):
with model_1:
trace = pm.sample(n)

pm.summary(trace)
az.summary(trace)


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions examples/pymc3_howto/LKJ_correlation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import theano.tensor as tt

import arviz as az
from numpy.random import multivariate_normal

import pymc3 as pm
Expand Down Expand Up @@ -53,8 +53,8 @@ def run(n=1000):
n = 50
with model:
trace = pm.sample(n)
pm.traceplot(
trace, varnames=["mu", "r"], lines={"mu": mu_r, "r": corr_r[np.triu_indices(n_var, k=1)]}
az.plot_trace(
trace, var_names=["mu", "r"]
)


Expand Down
5 changes: 3 additions & 2 deletions examples/pymc3_howto/rankdata_ordered.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import arviz as az
import numpy as np
import theano.tensor as tt

Expand Down Expand Up @@ -46,14 +47,14 @@ def run(n=1500):
with m:
trace = pm.sample(n)

pm.traceplot(trace, varnames=["mu_hat"])
az.plot_trace(trace, var_names=["mu_hat"])

print("Example observed data: ")
print(y[:30, :].T)
print("The true ranking is: ")
print(yreal.flatten())
print("The Latent mean is: ")
latentmu = np.hstack(([0], pm.summary(trace, varnames=["mu_hat"])["mean"].values))
latentmu = np.hstack(([0], az.summary(trace, var_names=["mu_hat"])["mean"].values))
print(np.round(latentmu, 2))
print("The estimated ranking is: ")
print(np.argsort(latentmu))
Expand Down
11 changes: 6 additions & 5 deletions examples/samplers/samplers_mvnormal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import time

import arviz as az
import numpy as np
import pandas as pd
import theano.tensor as tt
Expand Down Expand Up @@ -46,11 +47,11 @@ def run(steppers, p):
for step_cls in steppers:
name = step_cls.__name__
t_start = time.time()
mt = pm.sample(draws=10000, chains=16, parallelize=False, step=step_cls(), start=start)
mt = pm.sample(draws=10000, chains=16, step=step_cls(), start=start)
runtimes[name] = time.time() - t_start
print("{} samples across {} chains".format(len(mt) * mt.nchains, mt.nchains))
traces[name] = mt
en = pm.ess(mt)
en = az.ess(mt)
print(f"effective: {en}\r\n")
if USE_XY:
effn[name] = np.mean(en["x"]) / len(mt) / mt.nchains
Expand All @@ -74,9 +75,9 @@ def run(steppers, p):
for p in df_effectiven.index:
trace, rate, runtime = run(methods, p)
for name in names:
df_effectiven.set_value(p, name, rate[name])
df_runtime.set_value(p, name, runtime[name])
df_performance.set_value(p, name, rate[name] / runtime[name])
df_effectiven.at[p, name] = rate[name]
df_runtime.at[p, name] = runtime[name]
df_performance.at[p, name] = rate[name] / runtime[name]

print("\r\nEffective sample size [0...1]")
print(df_effectiven.T.to_string(float_format="{:.3f}".format))
Expand Down
5 changes: 3 additions & 2 deletions examples/time_series/arma_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from theano import scan, shared

import arviz as az
import pymc3 as pm

"""
Expand Down Expand Up @@ -82,8 +83,8 @@ def run(n_samples=1000):
with model:
trace = pm.sample(draws=n_samples, tune=1000, target_accept=0.99)

pm.plots.traceplot(trace)
pm.plots.forestplot(trace)
az.plot_trace(trace)
az.plot_forest(trace)


if __name__ == "__main__":
Expand Down