Skip to content

Mixture distribution does not play well with size parameter  #2614

Closed
@ColCarroll

Description

@ColCarroll

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions