From bae7f980094d0a987e2d008669ed78704f4f37bb Mon Sep 17 00:00:00 2001 From: CloudChaoszero Date: Mon, 18 Jan 2021 18:45:19 -0800 Subject: [PATCH 1/3] :art: Update plots from pymc to arviz dependencies & replace param sd with sigma --- examples/case_studies/disaster_model.py | 4 ++-- examples/case_studies/disaster_model_theano_op.py | 4 ++-- examples/case_studies/garch_example.py | 3 ++- examples/case_studies/gelman_schools.py | 3 ++- examples/case_studies/lightspeed_example.py | 4 ++-- examples/pymc3_howto/LKJ_correlation.py | 4 ++-- examples/pymc3_howto/rankdata_ordered.py | 4 ++-- examples/samplers/samplers_mvnormal.py | 3 ++- examples/time_series/arma_example.py | 5 +++-- 9 files changed, 19 insertions(+), 15 deletions(-) diff --git a/examples/case_studies/disaster_model.py b/examples/case_studies/disaster_model.py index 103206a6d..17ad28921 100644 --- a/examples/case_studies/disaster_model.py +++ b/examples/case_studies/disaster_model.py @@ -8,7 +8,7 @@ """ - +import arviz as az import theano.tensor as tt from numpy import arange, array @@ -152,4 +152,4 @@ start = {"early_mean": 2.0, "late_mean": 3.0} tr = pm.sample(1000, tune=500, start=start) - pm.traceplot(tr) + pm.plot_trace(tr) diff --git a/examples/case_studies/disaster_model_theano_op.py b/examples/case_studies/disaster_model_theano_op.py index 3b3597a28..75f25fe6f 100644 --- a/examples/case_studies/disaster_model_theano_op.py +++ b/examples/case_studies/disaster_model_theano_op.py @@ -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 @@ -166,4 +166,4 @@ def rate_(switchpoint, early_mean, late_mean): 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) + az.plot_trace(tr) diff --git a/examples/case_studies/garch_example.py b/examples/case_studies/garch_example.py index bf2295551..02a878067 100644 --- a/examples/case_studies/garch_example.py +++ b/examples/case_studies/garch_example.py @@ -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 diff --git a/examples/case_studies/gelman_schools.py b/examples/case_studies/gelman_schools.py index 984f30587..36e72a91c 100644 --- a/examples/case_studies/gelman_schools.py +++ b/examples/case_studies/gelman_schools.py @@ -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 diff --git a/examples/case_studies/lightspeed_example.py b/examples/case_studies/lightspeed_example.py index 8edfea596..061a28617 100644 --- a/examples/case_studies/lightspeed_example.py +++ b/examples/case_studies/lightspeed_example.py @@ -1,5 +1,5 @@ import numpy as np - +import arviz as az import pymc3 as pm light_speed = np.array( @@ -92,7 +92,7 @@ def run(n=5000): with model_1: trace = pm.sample(n) - pm.summary(trace) + az.summary(trace) if __name__ == "__main__": diff --git a/examples/pymc3_howto/LKJ_correlation.py b/examples/pymc3_howto/LKJ_correlation.py index 8a70c38a6..01befe398 100644 --- a/examples/pymc3_howto/LKJ_correlation.py +++ b/examples/pymc3_howto/LKJ_correlation.py @@ -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 @@ -53,7 +53,7 @@ def run(n=1000): n = 50 with model: trace = pm.sample(n) - pm.traceplot( + az.plot_trace( trace, varnames=["mu", "r"], lines={"mu": mu_r, "r": corr_r[np.triu_indices(n_var, k=1)]} ) diff --git a/examples/pymc3_howto/rankdata_ordered.py b/examples/pymc3_howto/rankdata_ordered.py index ef5a0f5fa..8fd43a6ad 100644 --- a/examples/pymc3_howto/rankdata_ordered.py +++ b/examples/pymc3_howto/rankdata_ordered.py @@ -46,14 +46,14 @@ def run(n=1500): with m: trace = pm.sample(n) - pm.traceplot(trace, varnames=["mu_hat"]) + az.plot_trace(trace, varnames=["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, varnames=["mu_hat"])["mean"].values)) print(np.round(latentmu, 2)) print("The estimated ranking is: ") print(np.argsort(latentmu)) diff --git a/examples/samplers/samplers_mvnormal.py b/examples/samplers/samplers_mvnormal.py index 2d5fb1a8a..91817691d 100644 --- a/examples/samplers/samplers_mvnormal.py +++ b/examples/samplers/samplers_mvnormal.py @@ -9,6 +9,7 @@ import time +import arviz as az import numpy as np import pandas as pd import theano.tensor as tt @@ -50,7 +51,7 @@ def run(steppers, p): 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 diff --git a/examples/time_series/arma_example.py b/examples/time_series/arma_example.py index c18305225..e8ee01d35 100644 --- a/examples/time_series/arma_example.py +++ b/examples/time_series/arma_example.py @@ -2,6 +2,7 @@ from theano import scan, shared +import arviz as az import pymc3 as pm """ @@ -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__": From 39ddaf00e9bd01f6008a41536023ea8d479f82f2 Mon Sep 17 00:00:00 2001 From: CloudChaoszero Date: Thu, 4 Feb 2021 03:53:48 -0800 Subject: [PATCH 2/3] :bug: Update varname param to var_names for arviz dep --- examples/case_studies/disaster_model.py | 2 +- examples/pymc3_howto/LKJ_correlation.py | 2 +- examples/pymc3_howto/rankdata_ordered.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/case_studies/disaster_model.py b/examples/case_studies/disaster_model.py index 17ad28921..392dd9ab9 100644 --- a/examples/case_studies/disaster_model.py +++ b/examples/case_studies/disaster_model.py @@ -152,4 +152,4 @@ start = {"early_mean": 2.0, "late_mean": 3.0} tr = pm.sample(1000, tune=500, start=start) - pm.plot_trace(tr) + az.plot_trace(tr) diff --git a/examples/pymc3_howto/LKJ_correlation.py b/examples/pymc3_howto/LKJ_correlation.py index 01befe398..80979581c 100644 --- a/examples/pymc3_howto/LKJ_correlation.py +++ b/examples/pymc3_howto/LKJ_correlation.py @@ -54,7 +54,7 @@ def run(n=1000): with model: trace = pm.sample(n) az.plot_trace( - trace, varnames=["mu", "r"], lines={"mu": mu_r, "r": corr_r[np.triu_indices(n_var, k=1)]} + trace, var_names=["mu", "r"], lines={"mu": mu_r, "r": corr_r[np.triu_indices(n_var, k=1)]} ) diff --git a/examples/pymc3_howto/rankdata_ordered.py b/examples/pymc3_howto/rankdata_ordered.py index 8fd43a6ad..460fc49eb 100644 --- a/examples/pymc3_howto/rankdata_ordered.py +++ b/examples/pymc3_howto/rankdata_ordered.py @@ -46,14 +46,14 @@ def run(n=1500): with m: trace = pm.sample(n) - az.plot_trace(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], az.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)) From 6ffdc0f42ef325a05b3655d1f8590f04623bac03 Mon Sep 17 00:00:00 2001 From: CloudChaoszero Date: Thu, 4 Feb 2021 23:21:46 -0800 Subject: [PATCH 3/3] :bug: Resolve outdated PyMC3 or Pandas functionality to current form --- examples/case_studies/disaster_model.py | 2 +- examples/case_studies/disaster_model_theano_op.py | 2 +- examples/pymc3_howto/LKJ_correlation.py | 2 +- examples/pymc3_howto/rankdata_ordered.py | 1 + examples/samplers/samplers_mvnormal.py | 8 ++++---- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/examples/case_studies/disaster_model.py b/examples/case_studies/disaster_model.py index 392dd9ab9..1a77a8812 100644 --- a/examples/case_studies/disaster_model.py +++ b/examples/case_studies/disaster_model.py @@ -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) + tr = pm.sample(1000, tune=500, start=start, cores=1) az.plot_trace(tr) diff --git a/examples/case_studies/disaster_model_theano_op.py b/examples/case_studies/disaster_model_theano_op.py index 75f25fe6f..5ef54faa1 100644 --- a/examples/case_studies/disaster_model_theano_op.py +++ b/examples/case_studies/disaster_model_theano_op.py @@ -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) + tr = pm.sample(1000, tune=500, start=start, step=[step1, step2], cores=1) az.plot_trace(tr) diff --git a/examples/pymc3_howto/LKJ_correlation.py b/examples/pymc3_howto/LKJ_correlation.py index 80979581c..94226066a 100644 --- a/examples/pymc3_howto/LKJ_correlation.py +++ b/examples/pymc3_howto/LKJ_correlation.py @@ -54,7 +54,7 @@ def run(n=1000): with model: trace = pm.sample(n) az.plot_trace( - trace, var_names=["mu", "r"], lines={"mu": mu_r, "r": corr_r[np.triu_indices(n_var, k=1)]} + trace, var_names=["mu", "r"] ) diff --git a/examples/pymc3_howto/rankdata_ordered.py b/examples/pymc3_howto/rankdata_ordered.py index 460fc49eb..707c0381a 100644 --- a/examples/pymc3_howto/rankdata_ordered.py +++ b/examples/pymc3_howto/rankdata_ordered.py @@ -1,3 +1,4 @@ +import arviz as az import numpy as np import theano.tensor as tt diff --git a/examples/samplers/samplers_mvnormal.py b/examples/samplers/samplers_mvnormal.py index 91817691d..464bb8322 100644 --- a/examples/samplers/samplers_mvnormal.py +++ b/examples/samplers/samplers_mvnormal.py @@ -47,7 +47,7 @@ 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 @@ -75,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))