From 18ee50d5dad81124c3fa0121c1ed7be0cd21d3b2 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 6 Mar 2019 02:31:14 -0800 Subject: [PATCH] Add more tests for pdf() and cdf() (GH-12190) --- Lib/test/test_statistics.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py index 4adc5e4cbf4..3f14e63c23f 100644 --- a/Lib/test/test_statistics.py +++ b/Lib/test/test_statistics.py @@ -2101,14 +2101,28 @@ class TestNormalDist(unittest.TestCase): self.assertLess(X.pdf(99), X.pdf(100)) self.assertLess(X.pdf(101), X.pdf(100)) # Test symmetry - self.assertAlmostEqual(X.pdf(99), X.pdf(101)) - self.assertAlmostEqual(X.pdf(98), X.pdf(102)) - self.assertAlmostEqual(X.pdf(97), X.pdf(103)) + for i in range(50): + self.assertAlmostEqual(X.pdf(100 - i), X.pdf(100 + i)) # Test vs CDF dx = 2.0 ** -10 for x in range(90, 111): est_pdf = (X.cdf(x + dx) - X.cdf(x)) / dx self.assertAlmostEqual(X.pdf(x), est_pdf, places=4) + # Test vs table of known values -- CRC 26th Edition + Z = NormalDist() + for x, px in enumerate([ + 0.3989, 0.3989, 0.3989, 0.3988, 0.3986, + 0.3984, 0.3982, 0.3980, 0.3977, 0.3973, + 0.3970, 0.3965, 0.3961, 0.3956, 0.3951, + 0.3945, 0.3939, 0.3932, 0.3925, 0.3918, + 0.3910, 0.3902, 0.3894, 0.3885, 0.3876, + 0.3867, 0.3857, 0.3847, 0.3836, 0.3825, + 0.3814, 0.3802, 0.3790, 0.3778, 0.3765, + 0.3752, 0.3739, 0.3725, 0.3712, 0.3697, + 0.3683, 0.3668, 0.3653, 0.3637, 0.3621, + 0.3605, 0.3589, 0.3572, 0.3555, 0.3538, + ]): + self.assertAlmostEqual(Z.pdf(x / 100.0), px, places=4) # Error case: variance is zero Y = NormalDist(100, 0) with self.assertRaises(statistics.StatisticsError): @@ -2127,6 +2141,18 @@ class TestNormalDist(unittest.TestCase): self.assertEqual(cdfs, sorted(cdfs)) # Verify center self.assertAlmostEqual(X.cdf(100), 0.50) + # Check against a table of known values + # https://en.wikipedia.org/wiki/Standard_normal_table#Cumulative + Z = NormalDist() + for z, cum_prob in [ + (0.00, 0.50000), (0.01, 0.50399), (0.02, 0.50798), + (0.14, 0.55567), (0.29, 0.61409), (0.33, 0.62930), + (0.54, 0.70540), (0.60, 0.72575), (1.17, 0.87900), + (1.60, 0.94520), (2.05, 0.97982), (2.89, 0.99807), + (3.52, 0.99978), (3.98, 0.99997), (4.07, 0.99998), + ]: + self.assertAlmostEqual(Z.cdf(z), cum_prob, places=5) + self.assertAlmostEqual(Z.cdf(-z), 1.0 - cum_prob, places=5) # Error case: variance is zero Y = NormalDist(100, 0) with self.assertRaises(statistics.StatisticsError):