@@ -3609,7 +3609,7 @@ cdef class RandomState:
36093609 `a` > 1.
36103610
36113611 The Zipf distribution (also known as the zeta distribution) is a
3612- continuous probability distribution that satisfies Zipf's law: the
3612+ discrete probability distribution that satisfies Zipf's law: the
36133613 frequency of an item is inversely proportional to its rank in a
36143614 frequency table.
36153615
@@ -3642,9 +3642,10 @@ cdef class RandomState:
36423642 -----
36433643 The probability density for the Zipf distribution is
36443644
3645- .. math:: p(x ) = \\ frac{x ^{-a}}{\\ zeta(a)},
3645+ .. math:: p(k ) = \\ frac{k ^{-a}}{\\ zeta(a)},
36463646
3647- where :math:`\\ zeta` is the Riemann Zeta function.
3647+ for integers :math:`k \geq 1`, where :math:`\\ zeta` is the Riemann Zeta
3648+ function.
36483649
36493650 It is named for the American linguist George Kingsley Zipf, who noted
36503651 that the frequency of any word in a sample of a language is inversely
@@ -3660,21 +3661,29 @@ cdef class RandomState:
36603661 --------
36613662 Draw samples from the distribution:
36623663
3663- >>> a = 2. # parameter
3664- >>> s = np.random.zipf(a, 1000)
3664+ >>> a = 4.0
3665+ >>> n = 20000
3666+ >>> s = np.random.zipf(a, n)
36653667
36663668 Display the histogram of the samples, along with
3667- the probability density function:
3669+ the expected histogram based on the probability
3670+ density function:
36683671
36693672 >>> import matplotlib.pyplot as plt
3670- >>> from scipy import special # doctest: +SKIP
3673+ >>> from scipy.special import zeta # doctest: +SKIP
3674+
3675+ `bincount` provides a fast histogram for small integers.
36713676
3672- Truncate s values at 50 so plot is interesting:
3677+ >>> count = np.bincount(s)
3678+ >>> k = np.arange(1, s.max() + 1)
36733679
3674- >>> count, bins, ignored = plt.hist(s[s<50], 50, density=True)
3675- >>> x = np.arange(1., 50.)
3676- >>> y = x**(-a) / special.zetac(a) # doctest: +SKIP
3677- >>> plt.plot(x, y/max(y), linewidth=2, color='r') # doctest: +SKIP
3680+ >>> plt.bar(k, count[1:], alpha=0.5, label='sample count')
3681+ >>> plt.plot(k, n*(k**-a)/zeta(a), 'k.-', alpha=0.5,
3682+ ... label='expected count') # doctest: +SKIP
3683+ >>> plt.semilogy()
3684+ >>> plt.grid(alpha=0.4)
3685+ >>> plt.legend()
3686+ >>> plt.title(f'Zipf sample, a={a}, size={n}')
36783687 >>> plt.show()
36793688
36803689 """
0 commit comments