Plot arviz.plot_loo_pit for pystan

I want to plot arviz.plot_loo_pit()
for eight-schools model as follow:

import pystan

schools_code = """

data {

  int<lower=0> J;

  real y[J];

  real<lower=0> sigma[J];

}

parameters {

  real mu;

  real<lower=0> tau;

  real theta[J];

}

model {

  mu ~ normal(0, 5);

  tau ~ cauchy(0, 5);

  theta ~ normal(mu, tau);

  y ~ normal(theta, sigma);

}

generated quantities {

    vector[J] log_lik;

    vector[J] y_hat;

    for (j in 1:J) {

        log_lik[j] = normal_lpdf(y[j] | theta[j], sigma[j]);

        y_hat[j] = normal_rng(theta[j], sigma[j]);

    }

}

"""

schools_dat = {

    "J": 8,

    "y": [28, 8, -3, 7, -1, 1, 18, 12],

    "sigma": [15, 10, 16, 11, 9, 11, 10, 18],

}

sm = pystan.StanModel(model_code=schools_code, verbose=False)

fit = sm.sampling(data=schools_dat, iter=2000, chains=4)

import arviz as az

import numpy as np

schools = np.array(['Choate', 'Deerfield', 'Phillips Andover', 'Phillips Exeter',

                    'Hotchkiss', 'Lawrenceville', "St. Paul's", 'Mt. Hermon'])

coord={"school": schools}

data = az.from_pystan(

    posterior=fit,

    posterior_predictive="y_hat",

    observed_data=["y"],

    log_likelihood={"y": "log_lik"},

    coords=coord,

    dims={

        "theta": ["school"],

        "y": ["school"],

        "log_lik": ["school"],

        "y_hat": ["school"],

        "theta_tilde": ["school"],

    },

)

As I run

az.plot_loo_pit(data, y="y", ecdf=True, color="maroon")

I get following error:

KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/xarray/core/dataset.py in _construct_dataarray(self, name)
   1258         try:
-> 1259             variable = self._variables[name]
   1260         except KeyError:

KeyError: 'y'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
5 frames
/usr/local/lib/python3.7/dist-packages/xarray/core/dataset.py in _get_virtual_variable(variables, key, level_vars, dim_sizes)
    169         ref_var = dim_var.to_index_variable().get_level_variable(ref_name)
    170     else:
--> 171         ref_var = variables[ref_name]
    172 
    173     if var_name is None:

KeyError: 'y'

I run program on Google colab. As I run az.plot_mcse(data, var_names=["tau", "mu"], rug=True, extra_methods=True) it run correctly.
Thank a lot for any help.

1 Like

Hi, when you call plot_loo_pit using this arguments:

ArviZ tries to retrieve the observed data, posterior predictive samples and pointwise log likelihood values for the variable called y. However, the posterior predictive samples are stored as y_hat like in the Stan code. You should use

az.plot_loo_pit(data, y="y", y_hat="y_hat", ecdf=True, color="maroon")
3 Likes

Thanks.It work.

1 Like