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

Method test_overlap

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

Source from the content-addressed store, hash-verified

3113 for e, a in zip(expected, actual)))
3114
3115 def test_overlap(self):
3116 NormalDist = self.module.NormalDist
3117
3118 # Match examples from Imman and Bradley
3119 for X1, X2, published_result in [
3120 (NormalDist(0.0, 2.0), NormalDist(1.0, 2.0), 0.80258),
3121 (NormalDist(0.0, 1.0), NormalDist(1.0, 2.0), 0.60993),
3122 ]:
3123 self.assertAlmostEqual(X1.overlap(X2), published_result, places=4)
3124 self.assertAlmostEqual(X2.overlap(X1), published_result, places=4)
3125
3126 # Check against integration of the PDF
3127 def overlap_numeric(X, Y, *, steps=8_192, z=5):
3128 'Numerical integration cross-check for overlap() '
3129 fsum = math.fsum
3130 center = (X.mean + Y.mean) / 2.0
3131 width = z * max(X.stdev, Y.stdev)
3132 start = center - width
3133 dx = 2.0 * width / steps
3134 x_arr = [start + i*dx for i in range(steps)]
3135 xp = list(map(X.pdf, x_arr))
3136 yp = list(map(Y.pdf, x_arr))
3137 total = max(fsum(xp), fsum(yp))
3138 return fsum(map(min, xp, yp)) / total
3139
3140 for X1, X2 in [
3141 # Examples from Imman and Bradley
3142 (NormalDist(0.0, 2.0), NormalDist(1.0, 2.0)),
3143 (NormalDist(0.0, 1.0), NormalDist(1.0, 2.0)),
3144 # Example from https://www.rasch.org/rmt/rmt101r.htm
3145 (NormalDist(0.0, 1.0), NormalDist(1.0, 2.0)),
3146 # Gender heights from http://www.usablestats.com/lessons/normal
3147 (NormalDist(70, 4), NormalDist(65, 3.5)),
3148 # Misc cases with equal standard deviations
3149 (NormalDist(100, 15), NormalDist(110, 15)),
3150 (NormalDist(-100, 15), NormalDist(110, 15)),
3151 (NormalDist(-100, 15), NormalDist(-110, 15)),
3152 # Misc cases with unequal standard deviations
3153 (NormalDist(100, 12), NormalDist(100, 15)),
3154 (NormalDist(100, 12), NormalDist(110, 15)),
3155 (NormalDist(100, 12), NormalDist(150, 15)),
3156 (NormalDist(100, 12), NormalDist(150, 35)),
3157 # Misc cases with small values
3158 (NormalDist(1.000, 0.002), NormalDist(1.001, 0.003)),
3159 (NormalDist(1.000, 0.002), NormalDist(1.006, 0.0003)),
3160 (NormalDist(1.000, 0.002), NormalDist(1.001, 0.099)),
3161 ]:
3162 self.assertAlmostEqual(X1.overlap(X2), overlap_numeric(X1, X2), places=5)
3163 self.assertAlmostEqual(X2.overlap(X1), overlap_numeric(X1, X2), places=5)
3164
3165 # Error cases
3166 X = NormalDist()
3167 with self.assertRaises(TypeError):
3168 X.overlap() # too few arguments
3169 with self.assertRaises(TypeError):
3170 X.overlap(X, X) # too may arguments
3171 with self.assertRaises(TypeError):
3172 X.overlap(None) # right operand not a NormalDist

Callers

nothing calls this directly

Calls 4

overlapMethod · 0.95
NormalDistClass · 0.85
assertAlmostEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected