Skip to content

Commit 11e99ba

Browse files
dschmitz89tylerjereddy
authored andcommitted
BUG: stats: Fix zipf.pmf and zipfian.pmf for int32 k (#20702) [wheel build]
* BUG: fix zipf.pmf for integer k * ENH: increase range for zipfian.pmf for integer k --------- Co-authored-by: Tyler Reddy <[email protected]> [wheel build]
1 parent 506cbeb commit 11e99ba

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

scipy/stats/_discrete_distns.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,8 +1305,9 @@ def _argcheck(self, a):
13051305
return a > 1
13061306

13071307
def _pmf(self, k, a):
1308+
k = k.astype(np.float64)
13081309
# zipf.pmf(k, a) = 1/(zeta(a) * k**a)
1309-
Pk = 1.0 / special.zeta(a, 1) / k**a
1310+
Pk = 1.0 / special.zeta(a, 1) * k**-a
13101311
return Pk
13111312

13121313
def _munp(self, n, a):
@@ -1404,7 +1405,8 @@ def _get_support(self, a, n):
14041405
return 1, n
14051406

14061407
def _pmf(self, k, a, n):
1407-
return 1.0 / _gen_harmonic(n, a) / k**a
1408+
k = k.astype(np.float64)
1409+
return 1.0 / _gen_harmonic(n, a) * k**-a
14081410

14091411
def _cdf(self, k, a, n):
14101412
return _gen_harmonic(k, a) / _gen_harmonic(n, a)

scipy/stats/tests/test_discrete_distns.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@ def pzip(k, a, n):
351351
assert_allclose(zipfian.stats(a, n, moments="mvsk"),
352352
[mean, var, skew, kurtosis])
353353

354+
def test_pmf_integer_k(self):
355+
k = np.arange(0, 1000)
356+
k_int32 = k.astype(np.int32)
357+
dist = zipfian(111, 22)
358+
pmf = dist.pmf(k)
359+
pmf_k_int32 = dist.pmf(k_int32)
360+
assert_equal(pmf, pmf_k_int32)
361+
354362

355363
class TestNCH:
356364
np.random.seed(2) # seeds 0 and 1 had some xl = xu; randint failed
@@ -627,3 +635,14 @@ def test_betanbinom_kurtosis(self, n, a, b, ref):
627635
# return float(fourth_moment/var**2 - 3.)
628636
assert_allclose(betanbinom.stats(n, a, b, moments="k"),
629637
ref, rtol=3e-15)
638+
639+
640+
class TestZipf:
641+
def test_gh20692(self):
642+
# test that int32 data for k generates same output as double
643+
k = np.arange(0, 1000)
644+
k_int32 = k.astype(np.int32)
645+
dist = zipf(9)
646+
pmf = dist.pmf(k)
647+
pmf_k_int32 = dist.pmf(k_int32)
648+
assert_equal(pmf, pmf_k_int32)

0 commit comments

Comments
 (0)