question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

stats.rv_discrete is not checking that xk values are integers

See original GitHub issue

scipy.stats.rv_discrete requires that the x input for the values parameters are integers:

values : tuple of two array_like, optional (xk, pk) where xk are integers and pk are the non-zero probabilities between 0 and 1 with sum(pk) = 1. xk and pk must have the same shape.

Reproducing code example:

The example code:

from scipy import stats
xk = np.arange(7)
pk = (0.1, 0.2, 0.3, 0.1, 0.1, 0.0, 0.2)
custm = stats.rv_discrete(name='custm', values=(xk, pk))

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)
ax.plot(xk, custm.pmf(xk), 'ro', ms=12, mec='r')
ax.vlines(xk, 0, custm.pmf(xk), colors='r', lw=4)
plt.show()

returns

but if I change to xk = np.linspace(1., 6.9, 7) I get:

Captura de pantalla de 2021-03-01 10-52-59

This is a user error with a silent fail. There should be a validation or at least a warning shown.

Scipy/Numpy/Python version information:

1.6.1 1.19.2 sys.version_info(major=3, minor=8, micro=8, releaselevel='final', serial=0)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
mdhabercommented, Apr 3, 2022

See gh-2414. I see this is about direct use of rv_discrete though, so maybe not as relevant. Just note that we don’t necessarily want all rv_discrete subclasses to only accept integer input. Not relevant. That was about a shape parameter, not xk.

0reactions
mdhabercommented, Apr 3, 2022

After reading this and gh-3758, it is more clear that they are duplicates. Let’s close this and continue in gh-3758.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.stats.rv_discrete — SciPy v1.9.3 Manual
(xk, pk) where xk are integers and pk are the non-zero probabilities between 0 and 1 with sum(pk) = 1 . xk and...
Read more >
Workaround for 'sum pk not equal to 1' error in scipy's stats. ...
rv_discrete function requires that the sum of the probabilities to be 1 but because of the representation of floats in memory, the sum...
Read more >
Source code for scipy.stats._distn_infrastructure
The rule is that if `size` is not None, then `size` gives the # shape of the result (integer values of `size` are...
Read more >
Statistics (scipy.stats)
In the examples above, the specific stream of random numbers is not reproducible ... normdiscrete = stats.rv_discrete(values=(gridint, ... np.round(probs, ...
Read more >
scipy.stats._distn_infrastructure
However some external (non-SciPy) distributions have not # been updated. ... 0.1, 0.1, 0.0, 0.2) >>> custm = stats.rv_discrete(name='custm', values=(xk, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found