Skip to content
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

ValueError : Zeros Dimensional Array in pm.traceplot #3505

Closed
garnier94 opened this issue Jun 4, 2019 · 2 comments
Closed

ValueError : Zeros Dimensional Array in pm.traceplot #3505

garnier94 opened this issue Jun 4, 2019 · 2 comments

Comments

@garnier94
Copy link

I'm trying to reproduce the code used in the AR(2) example. The sampling is normal but I receive a ValueError when i try to use pm.traceplot. I think that this issue is related to the way arviz handles MultiTrace.

Description of your problem

Please provide a minimal, self-contained, and reproducible example.

import pymc3 as pm
import numpy as np

np.random.seed(seed=1848)
T = 100
y = np.zeros((T,))
for i in range(1,T):
    y[i] = 0.95 * y[i-1] - 0.05 * y[i-2]+ np.random.normal()

tau = 1.0
with pm.Model() as ar2:
    beta = pm.Normal('beta', mu=0, sigma=tau, shape=2)    
    data = pm.AR('y', beta, sigma=1, observed=y)
    trace = pm.sample(1000, cores=4)

pm.traceplot(trace)

Please provide the full traceback.

<ipython-input-4-dc52240121b7> in <module>()
----> 1 pm.traceplot(trace)

*/python3.6/site-packages/pymc3/plots/__init__.py in wrapped(*args, **kwargs)
     40                 warnings.warn('Keyword argument `{old}` renamed to `{new}`, and will be removed in pymc3 3.8'.format(old=old, new=new))
     41                 kwargs[new] = kwargs.pop(old)
---> 42             return func(*args, **kwargs)
     43     return wrapped
     44 

*/python3.6/site-packages/arviz/plots/traceplot.py in plot_trace(data, var_names, coords, divergences, figsize, textsize, lines, combined, plot_kwargs, fill_kwargs, rug_kwargs, hist_kwargs, trace_kwargs)
    102             divergences = False
    103 
--> 104     data = convert_to_dataset(data, group="posterior")
    105     var_names = _var_names(var_names, data)
    106 

*/python3.6/site-packages/arviz/data/converters.py in convert_to_dataset(obj, group, coords, dims)
    156     xarray.Dataset
    157     
--> 158     inference_data = convert_to_inference_data(obj, group=group, coords=coords, dims=dims)
    159     dataset = getattr(inference_data, group, None)
    160     if dataset is None:

*/python3.6/site-packages/arviz/data/converters.py in convert_to_inference_data(obj, group, coords, dims, **kwargs)
     80             return from_pystan(**kwargs)
     81     elif obj.__class__.__name__ == "MultiTrace":  # ugly, but doesn't make PyMC3 a requirement
---> 82         return from_pymc3(trace=kwargs.pop(group), **kwargs)
     83     elif obj.__class__.__name__ == "EnsembleSampler":  # ugly, but doesn't make emcee a requirement
     84         return from_emcee(sampler=kwargs.pop(group), **kwargs)

*/python3.6/site-packages/arviz/data/io_pymc3.py in from_pymc3(trace, prior, posterior_predictive, coords, dims)
    150         posterior_predictive=posterior_predictive,
    151         coords=coords,
--> 152         dims=dims,
    153     ).to_inference_data()

*/python3.6/site-packages/arviz/data/io_pymc3.py in to_inference_data(self)
    135             **{
    136                 "posterior": self.posterior_to_xarray(),
--> 137                 "sample_stats": self.sample_stats_to_xarray(),
    138                 "posterior_predictive": self.posterior_predictive_to_xarray(),
    139                 "prior": self.prior_to_xarray(),

*/python3.6/site-packages/arviz/data/base.py in wrapped(cls, *args, **kwargs)
     23                 if getattr(cls, prop) is None:
     24                     return None
---> 25             return func(cls, *args, **kwargs)
     26 
     27         return wrapped

*/python3.6/site-packages/arviz/data/io_pymc3.py in sample_stats_to_xarray(self)
     76             name = rename_key.get(stat, stat)
     77             data[name] = np.array(self.trace.get_sampler_stats(stat, combine=False))
---> 78         log_likelihood, dims = self._extract_log_likelihood()
     79         if log_likelihood is not None:
     80             data["log_likelihood"] = log_likelihood

*/python3.6/site-packages/arviz/data/base.py in wrapped(cls, *args, **kwargs)
     23                 if getattr(cls, prop) is None:
     24                     return None
---> 25             return func(cls, *args, **kwargs)
     26 
     27         return wrapped

*/python3.6/site-packages/arviz/data/io_pymc3.py in _extract_log_likelihood(self)
     54         for chain in self.trace.chains:
     55             log_like = (log_likelihood_vals_point(point) for point in self.trace.points([chain]))
---> 56             chain_likelihoods.append(np.stack(log_like))
     57         return np.stack(chain_likelihoods), coord_name
     58 

*/python3.6/site-packages/numpy/core/shape_base.py in stack(arrays, axis, out)
    408     """
    409     _warn_for_nonsequence(arrays)
--> 410     arrays = [asanyarray(arr) for arr in arrays]
    411     if not arrays:
    412         raise ValueError('need at least one array to stack')

*/python3.6/site-packages/numpy/core/shape_base.py in <listcomp>(.0)
    408     """
    409     _warn_for_nonsequence(arrays)
--> 410     arrays = [asanyarray(arr) for arr in arrays]
    411     if not arrays:
    412         raise ValueError('need at least one array to stack')

*/python3.6/site-packages/arviz/data/io_pymc3.py in <genexpr>(.0)
     53         chain_likelihoods = []
     54         for chain in self.trace.chains:
---> 55             log_like = (log_likelihood_vals_point(point) for point in self.trace.points([chain]))
     56             chain_likelihoods.append(np.stack(log_like))
     57         return np.stack(chain_likelihoods), coord_name

*/python3.6/site-packages/arviz/data/io_pymc3.py in log_likelihood_vals_point(point)
     49                     log_like_val = log_like_val[~var.observations.mask]
     50                 log_like_vals.append(log_like_val)
---> 51             return np.concatenate(log_like_vals)
     52 
     53         chain_likelihoods = []

ValueError: zero-dimensional arrays cannot be concatenated

Please provide any additional information below.

This problem doesn't depend on the numpy version (even if i get a FutureWarning on the behavior of concatenation). I'm using numpy 1.16.4, but i still have the issue with older version of numpy.
This problem also happens in the AR(1) case, when i use shape = 1 .

Versions and main components

  • PyMC3 Version: 3.7
  • Theano Version: 1.0.4
  • Python Version: 3.6.1
  • Arviz Version : 0.4.0
  • Operating system: Debian 3.16.43
  • How did you install PyMC3: pip
@aloctavodia
Copy link
Member

This have been addressed on the ArviZ side arviz-devs/arviz#693

@lucianopaz
Copy link
Contributor

Closing thanks to the ArviZ fix. Installing the bleeding edge version of ArviZ from their github repo will fix the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants