Skip to content

Commit 18ee50d

Browse files
rhettingermiss-islington
authored andcommitted
Add more tests for pdf() and cdf() (GH-12190)
1 parent 4fffd38 commit 18ee50d

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

Lib/test/test_statistics.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,14 +2101,28 @@ def test_pdf(self):
21012101
self.assertLess(X.pdf(99), X.pdf(100))
21022102
self.assertLess(X.pdf(101), X.pdf(100))
21032103
# Test symmetry
2104-
self.assertAlmostEqual(X.pdf(99), X.pdf(101))
2105-
self.assertAlmostEqual(X.pdf(98), X.pdf(102))
2106-
self.assertAlmostEqual(X.pdf(97), X.pdf(103))
2104+
for i in range(50):
2105+
self.assertAlmostEqual(X.pdf(100 - i), X.pdf(100 + i))
21072106
# Test vs CDF
21082107
dx = 2.0 ** -10
21092108
for x in range(90, 111):
21102109
est_pdf = (X.cdf(x + dx) - X.cdf(x)) / dx
21112110
self.assertAlmostEqual(X.pdf(x), est_pdf, places=4)
2111+
# Test vs table of known values -- CRC 26th Edition
2112+
Z = NormalDist()
2113+
for x, px in enumerate([
2114+
0.3989, 0.3989, 0.3989, 0.3988, 0.3986,
2115+
0.3984, 0.3982, 0.3980, 0.3977, 0.3973,
2116+
0.3970, 0.3965, 0.3961, 0.3956, 0.3951,
2117+
0.3945, 0.3939, 0.3932, 0.3925, 0.3918,
2118+
0.3910, 0.3902, 0.3894, 0.3885, 0.3876,
2119+
0.3867, 0.3857, 0.3847, 0.3836, 0.3825,
2120+
0.3814, 0.3802, 0.3790, 0.3778, 0.3765,
2121+
0.3752, 0.3739, 0.3725, 0.3712, 0.3697,
2122+
0.3683, 0.3668, 0.3653, 0.3637, 0.3621,
2123+
0.3605, 0.3589, 0.3572, 0.3555, 0.3538,
2124+
]):
2125+
self.assertAlmostEqual(Z.pdf(x / 100.0), px, places=4)
21122126
# Error case: variance is zero
21132127
Y = NormalDist(100, 0)
21142128
with self.assertRaises(statistics.StatisticsError):
@@ -2127,6 +2141,18 @@ def test_cdf(self):
21272141
self.assertEqual(cdfs, sorted(cdfs))
21282142
# Verify center
21292143
self.assertAlmostEqual(X.cdf(100), 0.50)
2144+
# Check against a table of known values
2145+
# https://en.wikipedia.org/wiki/Standard_normal_table#Cumulative
2146+
Z = NormalDist()
2147+
for z, cum_prob in [
2148+
(0.00, 0.50000), (0.01, 0.50399), (0.02, 0.50798),
2149+
(0.14, 0.55567), (0.29, 0.61409), (0.33, 0.62930),
2150+
(0.54, 0.70540), (0.60, 0.72575), (1.17, 0.87900),
2151+
(1.60, 0.94520), (2.05, 0.97982), (2.89, 0.99807),
2152+
(3.52, 0.99978), (3.98, 0.99997), (4.07, 0.99998),
2153+
]:
2154+
self.assertAlmostEqual(Z.cdf(z), cum_prob, places=5)
2155+
self.assertAlmostEqual(Z.cdf(-z), 1.0 - cum_prob, places=5)
21302156
# Error case: variance is zero
21312157
Y = NormalDist(100, 0)
21322158
with self.assertRaises(statistics.StatisticsError):

0 commit comments

Comments
 (0)