MCPcopy Index your code
hub / github.com/python/cpython / test_inv_cdf

Method test_inv_cdf

Lib/test/test_statistics.py:3035–3098  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

3033 @support.skip_if_pgo_task
3034 @support.requires_resource('cpu')
3035 def test_inv_cdf(self):
3036 NormalDist = self.module.NormalDist
3037
3038 # Center case should be exact.
3039 iq = NormalDist(100, 15)
3040 self.assertEqual(iq.inv_cdf(0.50), iq.mean)
3041
3042 # Test versus a published table of known percentage points.
3043 # See the second table at the bottom of the page here:
3044 # http://people.bath.ac.uk/masss/tables/normaltable.pdf
3045 Z = NormalDist()
3046 pp = {5.0: (0.000, 1.645, 2.576, 3.291, 3.891,
3047 4.417, 4.892, 5.327, 5.731, 6.109),
3048 2.5: (0.674, 1.960, 2.807, 3.481, 4.056,
3049 4.565, 5.026, 5.451, 5.847, 6.219),
3050 1.0: (1.282, 2.326, 3.090, 3.719, 4.265,
3051 4.753, 5.199, 5.612, 5.998, 6.361)}
3052 for base, row in pp.items():
3053 for exp, x in enumerate(row, start=1):
3054 p = base * 10.0 ** (-exp)
3055 self.assertAlmostEqual(-Z.inv_cdf(p), x, places=3)
3056 p = 1.0 - p
3057 self.assertAlmostEqual(Z.inv_cdf(p), x, places=3)
3058
3059 # Match published example for MS Excel
3060 # https://support.office.com/en-us/article/norm-inv-function-54b30935-fee7-493c-bedb-2278a9db7e13
3061 self.assertAlmostEqual(NormalDist(40, 1.5).inv_cdf(0.908789), 42.000002)
3062
3063 # One million equally spaced probabilities
3064 n = 2**20
3065 for p in range(1, n):
3066 p /= n
3067 self.assertAlmostEqual(iq.cdf(iq.inv_cdf(p)), p)
3068
3069 # One hundred ever smaller probabilities to test tails out to
3070 # extreme probabilities: 1 / 2**50 and (2**50-1) / 2 ** 50
3071 for e in range(1, 51):
3072 p = 2.0 ** (-e)
3073 self.assertAlmostEqual(iq.cdf(iq.inv_cdf(p)), p)
3074 p = 1.0 - p
3075 self.assertAlmostEqual(iq.cdf(iq.inv_cdf(p)), p)
3076
3077 # Now apply cdf() first. Near the tails, the round-trip loses
3078 # precision and is ill-conditioned (small changes in the inputs
3079 # give large changes in the output), so only check to 5 places.
3080 for x in range(200):
3081 self.assertAlmostEqual(iq.inv_cdf(iq.cdf(x)), x, places=5)
3082
3083 # Error cases:
3084 with self.assertRaises(self.module.StatisticsError):
3085 iq.inv_cdf(0.0) # p is zero
3086 with self.assertRaises(self.module.StatisticsError):
3087 iq.inv_cdf(-0.1) # p under zero
3088 with self.assertRaises(self.module.StatisticsError):
3089 iq.inv_cdf(1.0) # p is one
3090 with self.assertRaises(self.module.StatisticsError):
3091 iq.inv_cdf(1.1) # p over one
3092

Callers

nothing calls this directly

Calls 9

inv_cdfMethod · 0.95
cdfMethod · 0.95
NormalDistClass · 0.85
enumerateFunction · 0.85
assertTrueMethod · 0.80
assertEqualMethod · 0.45
itemsMethod · 0.45
assertAlmostEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected