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 1 commit
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
4 changes: 2 additions & 2 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 @@ -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)
4 changes: 2 additions & 2 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 @@ -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)
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
4 changes: 2 additions & 2 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,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)]}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am quite sure arviz does not accept varnames, it should be var_names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, correct! Thanks for that. I updated to arviz 0.11, and got this error

y:212: UserWarning: A valid var_name should be provided, found {'m'} expected from {'mu', 'r'}
  warnings.warn(
Traceback (most recent call last):
  File "LKJ_correlation.py", line 62, in <module>
    run()
  File "LKJ_correlation.py", line 56, in run
    az.plot_trace(
  File "/Users/CloudChaoszero/opt/anaconda3/envs/pymc3-dev-py38/lib/python3.8/site-packages/arviz/plots/traceplot.py", line 238, in plot_trace
    axes = plot(**trace_plot_args)
  File "/Users/CloudChaoszero/opt/anaconda3/envs/pymc3-dev-py38/lib/python3.8/site-packages/arviz/plots/backends/matplotlib/traceplot.py", line 384, in plot_trace
    for _, _, vlines in (j for j in lines if j[0] == var_name and j[1] == selection):
  File "/Users/CloudChaoszero/opt/anaconda3/envs/pymc3-dev-py38/lib/python3.8/site-packages/arviz/plots/backends/matplotlib/traceplot.py", line 384, in <genexpr>
    for _, _, vlines in (j for j in lines if j[0] == var_name and j[1] == selection):
IndexError: string index out of range

(This error looks related arviz-devs/arviz#988)

Any thoughts why this happened?

Copy link
Contributor Author

@CloudChaoszero CloudChaoszero Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I found out why now. It's due to the lines param.

az.plot_trace(
        trace, varnames=["mu", "r"], lines={"mu": mu_r, "r": corr_r[np.triu_indices(n_var, k=1)]}
...

I removed it to make the az.plot_trac() work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try

lines=(("mu", {}, mu_r), ("r", {}, corr_r[np.triu_indices(n_var, k=1)])) 

)

Expand Down
4 changes: 2 additions & 2 deletions examples/pymc3_howto/rankdata_ordered.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion 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 @@ -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
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