Skip to content

Commit cf71558

Browse files
DOC: random: Fix a mistake in the zipf example.
There was a mistake in the code that generated the plot in the zipf docstring. The Riemann zeta function is `scipy.special.zeta`, not `scipy.special.zetac`. I also tweaked the sample parameters and the plot code so the plot is a bit more informative.
1 parent 8dbd507 commit cf71558

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

numpy/random/_generator.pyx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3153,22 +3153,29 @@ cdef class Generator:
31533153
--------
31543154
Draw samples from the distribution:
31553155
3156-
>>> a = 2. # parameter
3157-
>>> s = np.random.default_rng().zipf(a, 1000)
3156+
>>> a = 4.0
3157+
>>> n = 20000
3158+
>>> s = np.random.default_rng().zipf(a, size=n)
31583159
31593160
Display the histogram of the samples, along with
3160-
the probability density function:
3161+
the expected histogram based on the probability
3162+
density function:
31613163
31623164
>>> import matplotlib.pyplot as plt
3163-
>>> from scipy import special # doctest: +SKIP
3165+
>>> from scipy.special import zeta # doctest: +SKIP
3166+
3167+
`bincount` provides a fast histogram for small integers.
31643168
3165-
Truncate s values at 50 so plot is interesting:
3169+
>>> count = np.bincount(s)
3170+
>>> x = np.arange(1, s.max() + 1)
31663171
3167-
>>> count, bins, ignored = plt.hist(s[s<50],
3168-
... 50, density=True)
3169-
>>> x = np.arange(1., 50.)
3170-
>>> y = x**(-a) / special.zetac(a) # doctest: +SKIP
3171-
>>> plt.plot(x, y/max(y), linewidth=2, color='r') # doctest: +SKIP
3172+
>>> plt.bar(x, count[1:], alpha=0.5, label='sample count')
3173+
>>> plt.plot(x, n*(x**-a)/zeta(a), 'k.-', alpha=0.5,
3174+
... label='expected count') # doctest: +SKIP
3175+
>>> plt.semilogy()
3176+
>>> plt.grid(alpha=0.4)
3177+
>>> plt.legend()
3178+
>>> plt.title(f'Zipf sample, a={a}, size={n}')
31723179
>>> plt.show()
31733180
31743181
"""

0 commit comments

Comments
 (0)