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

Fix broadcasting scale arg of entropy #8932

Merged
merged 4 commits into from
Jun 20, 2018

Conversation

toslunar
Copy link
Contributor

Fix #8931

This PR also fixes the following error with numpy<1.12. (numpy.place does not reject >= 2-d vals)

>>> stats.norm(loc=0, scale=np.array([[1, 2], [3, 4]])).entropy()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/tos/py3sandbox/lib/python3.6/site-packages/scipy/stats/_distn_infrastructure.py", line 499, in entropy
    return self.dist.entropy(*self.args, **self.kwds)
  File "/Users/tos/py3sandbox/lib/python3.6/site-packages/scipy/stats/_distn_infrastructure.py", line 1119, in entropy
    place(output, cond0, self.vecentropy(*goodargs) + log(scale))
  File "/Users/tos/py3sandbox/lib/python3.6/site-packages/numpy/lib/function_base.py", line 2002, in place
    return _insert(arr, mask, vals)
ValueError: object too deep for desired array

@ev-br ev-br added scipy.stats enhancement A new feature or improvement labels Jun 16, 2018
@ev-br
Copy link
Member

ev-br commented Jun 16, 2018

Nice!

This also needs a test. Here's one, https://github.com/scipy/scipy/compare/master...ev-br:pr/8932?expand=1
can you take it over?

Once we go down that route, there's one more bug:

In [2]: from scipy import stats

In [3]: stats.norm.entropy(scale=2)
Out[3]: array(2.11208571)

In [4]: stats.norm.entropy(scale=[2])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-e060df9994ec> in <module>()
----> 1 stats.norm.entropy(scale=[2])

~/repos/scipy/build/testenv/lib/python3.5/site-packages/scipy/stats/_distn_infrastructure.py in entropy(self, *args, **kwds)
   1111         # NB: for discrete distributions scale=1 by construction in _parse_args
   1112         args = tuple(map(asarray, args))
-> 1113         cond0 = self._argcheck(*args) & (scale > 0) & (loc == loc)
   1114         output = zeros(shape(cond0), 'd')
   1115         place(output, (1-cond0), self.badvalue)

TypeError: unorderable types: list() > int()

This is due to a missing loc, scale = map(asarray, (loc, scale)) in the entropy method.

@ev-br ev-br added the needs-work Items that are pending response from the author label Jun 16, 2018
@toslunar
Copy link
Contributor Author

Thanks! I merged your commit, made the fix you mentioned, and added a test.

@ev-br
Copy link
Member

ev-br commented Jun 20, 2018

LGTM, merging. Thanks @toslunar!

It might make sense to do similar things for other methods of distributions, is it something you'd be interested in?

@ev-br ev-br merged commit 1686391 into scipy:master Jun 20, 2018
@ev-br ev-br removed the needs-work Items that are pending response from the author label Jun 20, 2018
@ev-br ev-br added this to the 1.2.0 milestone Jun 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement scipy.stats
Projects
None yet
Development

Successfully merging this pull request may close these issues.

scale argument of entropy doesn't broadcast
2 participants