MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_inv_cdf

Method test_inv_cdf

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

Source from the content-addressed store, hash-verified

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

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