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

Shape error in random method for HalfNormal #3147

Closed
junpenglao opened this issue Aug 10, 2018 · 7 comments
Closed

Shape error in random method for HalfNormal #3147

junpenglao opened this issue Aug 10, 2018 · 7 comments

Comments

@junpenglao
Copy link
Member

with pm.Model() as m:
    a = pm.HalfNormal('a', np.ones((3, 1)), shape=(3, 10))

a.random(point=m.test_point)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-a1da77da6c66> in <module>()
----> 1 a.random(point=m.test_point)

~/Documents/pymc3/pymc3/model.py in __call__(self, *args, **kwargs)
     40 
     41     def __call__(self, *args, **kwargs):
---> 42         return getattr(self.obj, self.method_name)(*args, **kwargs)
     43 
     44 

~/Documents/pymc3/pymc3/distributions/continuous.py in random(self, point, size)
    749         return generate_samples(stats.halfnorm.rvs, loc=0., scale=sd,
    750                                 dist_shape=self.shape,
--> 751                                 size=size)
    752 
    753     def logp(self, value):

~/Documents/pymc3/pymc3/distributions/distribution.py in generate_samples(generator, *args, **kwargs)
    522             dist_shape: {dist_shape}
    523             broadcast_shape: {broadcast_shape}
--> 524         '''.format(size=size, dist_shape=dist_shape, broadcast_shape=broadcast_shape))
    525 
    526     # reshape samples here

TypeError: Attempted to generate values with incompatible shapes:
            size: 1
            dist_shape: (3, 10)
            broadcast_shape: (3, 1)

I have a vague feeling this is something to do with broadcasting sigma parameter in Normal/HalfNormal, I think I have seen it before...

@junpenglao
Copy link
Member Author

Same error for Normal:

with pm.Model() as m:
    a = pm.Normal('a', 0, np.ones((3, 1)), shape=(3, 10))
a.random(m.test_point)

also

with pm.Model() as m:
    a = pm.Normal('a', np.zeros((3, 1)), 1, shape=(3, 10))
a.random(m.test_point)

So it is a general broadcast error

@junpenglao
Copy link
Member Author

Maybe in random method we can broadcast the numpy value that feed into generate_samples? @ColCarroll

@ColCarroll
Copy link
Member

I am pretty sure if shape is supplied, I tried to make sure it was equal to np.broadcast(*inputs).shape. In this case, the inputs were np.zeros((3, 1)) and 1 (the default standard deviation).

It sounds like you're suggesting that we set a.shape = np.broadcast(shape, *inputs).shape. I think that would work, but it is a little funny, since the result of that broadcast might be different from what the user intended.

@junpenglao
Copy link
Member Author

I meant something like:

return generate_samples(stats.halfnorm.rvs, loc=0., scale=np.broadcast(self.shape, sd),
                          dist_shape=self.shape,
                          size=size)

@springcoil
Copy link
Contributor

Has there been work on this?

@twiecki
Copy link
Member

twiecki commented Nov 1, 2018

I don't think so, although there were many shape fixes so worth checking if the behavior is still the same.

@lucianopaz
Copy link
Contributor

Closed via #3456

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

No branches or pull requests

5 participants