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

Mixture distribution does not play well with size parameter #2614

Closed
ColCarroll opened this issue Oct 5, 2017 · 1 comment
Closed

Mixture distribution does not play well with size parameter #2614

ColCarroll opened this issue Oct 5, 2017 · 1 comment
Labels

Comments

@ColCarroll
Copy link
Member

Description of your problem

From
https://stackoverflow.com/questions/46581700/pymc3-sampling-multiple-numbers-from-normal-mixture-distribution-doesnt-work

See the example below

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

import pymc3 as pm

pm.NormalMixture.dist(
    w=0.5 * np.ones(2),
    mu=np.ones(2),
    sd=np.ones(2)).random(size=2)

Please provide the full traceback.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-bccf91a988a4> in <module>()
      2     w=0.5 * np.ones(2),
      3     mu=np.ones(2),
----> 4     sd=np.ones(2)).random(size=2)

~/.venv/sampled/lib/python3.6/site-packages/pymc3/distributions/mixture.py in random(self, point, size, repeat)
    133                                      dist_shape=self.shape,
    134                                      size=size).squeeze()
--> 135         comp_samples = self._comp_samples(point=point, size=size, repeat=repeat)
    136 
    137         if comp_samples.ndim > 1:

~/.venv/sampled/lib/python3.6/site-packages/pymc3/distributions/mixture.py in _comp_samples(self, point, size, repeat)
    101     def _comp_samples(self, point=None, size=None, repeat=None):
    102         try:
--> 103             samples = self.comp_dists.random(point=point, size=size, repeat=repeat)
    104         except AttributeError:
    105             samples = np.column_stack([comp_dist.random(point=point, size=size, repeat=repeat)

~/.venv/sampled/lib/python3.6/site-packages/pymc3/distributions/continuous.py in random(self, point, size, repeat)
    260         return generate_samples(stats.norm.rvs, loc=mu, scale=tau**-0.5,
    261                                 dist_shape=self.shape,
--> 262                                 size=size)
    263 
    264     def logp(self, value):

~/.venv/sampled/lib/python3.6/site-packages/pymc3/distributions/distribution.py in generate_samples(generator, *args, **kwargs)
    389                                         prefix_shape,
    390                                         *args, **kwargs)
--> 391     return reshape_sampled(samples, size, dist_shape)

~/.venv/sampled/lib/python3.6/site-packages/pymc3/distributions/distribution.py in reshape_sampled(sampled, size, dist_shape)
    305 
    306     if np.size(sampled) == 1 or repeat_shape or dist_shape:
--> 307         return np.reshape(sampled, repeat_shape + dist_shape)
    308     else:
    309         return sampled

~/.venv/sampled/lib/python3.6/site-packages/numpy/core/fromnumeric.py in reshape(a, newshape, order)
    230            [5, 6]])
    231     """
--> 232     return _wrapfunc(a, 'reshape', newshape, order=order)
    233 
    234 

~/.venv/sampled/lib/python3.6/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
     55 def _wrapfunc(obj, method, *args, **kwds):
     56     try:
---> 57         return getattr(obj, method)(*args, **kwds)
     58 
     59     # An AttributeError occurs if the object does not have

ValueError: cannot reshape array of size 4 into shape (2,)

Please provide any additional information below.
It looks like we are using the same size parameter in two places where they might mean different things around here:

 w_samples = generate_samples(random_choice,                               
                              w=w,                                         
                              broadcast_shape=w.shape[:-1] or (1,),        
                              dist_shape=self.shape,
                              size=size).squeeze()
 comp_samples = self._comp_samples(point=point, size=size, repeat=repeat)  

Versions and main components

  • PyMC3 Version: master
  • Theano Version: 0.9.0
  • Python Version: 3.6
  • Operating system: osx
  • How did you install PyMC3: (conda/pip) pip
@junpenglao
Copy link
Member

Likely related: #2478

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

No branches or pull requests

3 participants