blob: 104718ed7db6c32914a8ec64f0b642627a9527a3 [file] [log] [blame]
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001"""Test suite for statistics module, including helper NumericTestCase and
2approx_equal function.
3
4"""
5
Raymond Hettinger9013ccf2019-04-23 00:06:35 -07006import bisect
Larry Hastingsf5e987b2013-10-19 11:50:09 -07007import collections
Serhiy Storchaka2e576f52017-04-24 09:05:00 +03008import collections.abc
Raymond Hettinger11c79532019-02-23 14:44:07 -08009import copy
Larry Hastingsf5e987b2013-10-19 11:50:09 -070010import decimal
11import doctest
12import math
Raymond Hettinger11c79532019-02-23 14:44:07 -080013import pickle
Larry Hastingsf5e987b2013-10-19 11:50:09 -070014import random
Serhiy Storchakab12cb6a2013-12-08 18:16:18 +020015import sys
Larry Hastingsf5e987b2013-10-19 11:50:09 -070016import unittest
Neil Schemenauer52a48e62019-07-30 11:08:18 -070017from test import support
Larry Hastingsf5e987b2013-10-19 11:50:09 -070018
19from decimal import Decimal
20from fractions import Fraction
21
22
23# Module to be tested.
24import statistics
25
26
27# === Helper functions and class ===
28
Steven D'Apranoa474afd2016-08-09 12:49:01 +100029def sign(x):
30 """Return -1.0 for negatives, including -0.0, otherwise +1.0."""
31 return math.copysign(1, x)
32
Steven D'Apranob28c3272015-12-01 19:59:53 +110033def _nan_equal(a, b):
34 """Return True if a and b are both the same kind of NAN.
35
36 >>> _nan_equal(Decimal('NAN'), Decimal('NAN'))
37 True
38 >>> _nan_equal(Decimal('sNAN'), Decimal('sNAN'))
39 True
40 >>> _nan_equal(Decimal('NAN'), Decimal('sNAN'))
41 False
42 >>> _nan_equal(Decimal(42), Decimal('NAN'))
43 False
44
45 >>> _nan_equal(float('NAN'), float('NAN'))
46 True
47 >>> _nan_equal(float('NAN'), 0.5)
48 False
49
50 >>> _nan_equal(float('NAN'), Decimal('NAN'))
51 False
52
53 NAN payloads are not compared.
54 """
55 if type(a) is not type(b):
56 return False
57 if isinstance(a, float):
58 return math.isnan(a) and math.isnan(b)
59 aexp = a.as_tuple()[2]
60 bexp = b.as_tuple()[2]
61 return (aexp == bexp) and (aexp in ('n', 'N')) # Both NAN or both sNAN.
62
63
Larry Hastingsf5e987b2013-10-19 11:50:09 -070064def _calc_errors(actual, expected):
65 """Return the absolute and relative errors between two numbers.
66
67 >>> _calc_errors(100, 75)
68 (25, 0.25)
69 >>> _calc_errors(100, 100)
70 (0, 0.0)
71
72 Returns the (absolute error, relative error) between the two arguments.
73 """
74 base = max(abs(actual), abs(expected))
75 abs_err = abs(actual - expected)
76 rel_err = abs_err/base if base else float('inf')
77 return (abs_err, rel_err)
78
79
80def approx_equal(x, y, tol=1e-12, rel=1e-7):
81 """approx_equal(x, y [, tol [, rel]]) => True|False
82
83 Return True if numbers x and y are approximately equal, to within some
84 margin of error, otherwise return False. Numbers which compare equal
85 will also compare approximately equal.
86
87 x is approximately equal to y if the difference between them is less than
88 an absolute error tol or a relative error rel, whichever is bigger.
89
90 If given, both tol and rel must be finite, non-negative numbers. If not
91 given, default values are tol=1e-12 and rel=1e-7.
92
93 >>> approx_equal(1.2589, 1.2587, tol=0.0003, rel=0)
94 True
95 >>> approx_equal(1.2589, 1.2587, tol=0.0001, rel=0)
96 False
97
98 Absolute error is defined as abs(x-y); if that is less than or equal to
99 tol, x and y are considered approximately equal.
100
101 Relative error is defined as abs((x-y)/x) or abs((x-y)/y), whichever is
102 smaller, provided x or y are not zero. If that figure is less than or
103 equal to rel, x and y are considered approximately equal.
104
105 Complex numbers are not directly supported. If you wish to compare to
106 complex numbers, extract their real and imaginary parts and compare them
107 individually.
108
109 NANs always compare unequal, even with themselves. Infinities compare
110 approximately equal if they have the same sign (both positive or both
111 negative). Infinities with different signs compare unequal; so do
112 comparisons of infinities with finite numbers.
113 """
114 if tol < 0 or rel < 0:
115 raise ValueError('error tolerances must be non-negative')
116 # NANs are never equal to anything, approximately or otherwise.
117 if math.isnan(x) or math.isnan(y):
118 return False
119 # Numbers which compare equal also compare approximately equal.
120 if x == y:
121 # This includes the case of two infinities with the same sign.
122 return True
123 if math.isinf(x) or math.isinf(y):
124 # This includes the case of two infinities of opposite sign, or
125 # one infinity and one finite number.
126 return False
127 # Two finite numbers.
128 actual_error = abs(x - y)
129 allowed_error = max(tol, rel*max(abs(x), abs(y)))
130 return actual_error <= allowed_error
131
132
133# This class exists only as somewhere to stick a docstring containing
134# doctests. The following docstring and tests were originally in a separate
135# module. Now that it has been merged in here, I need somewhere to hang the.
136# docstring. Ultimately, this class will die, and the information below will
137# either become redundant, or be moved into more appropriate places.
138class _DoNothing:
139 """
140 When doing numeric work, especially with floats, exact equality is often
141 not what you want. Due to round-off error, it is often a bad idea to try
142 to compare floats with equality. Instead the usual procedure is to test
143 them with some (hopefully small!) allowance for error.
144
145 The ``approx_equal`` function allows you to specify either an absolute
146 error tolerance, or a relative error, or both.
147
148 Absolute error tolerances are simple, but you need to know the magnitude
149 of the quantities being compared:
150
151 >>> approx_equal(12.345, 12.346, tol=1e-3)
152 True
153 >>> approx_equal(12.345e6, 12.346e6, tol=1e-3) # tol is too small.
154 False
155
156 Relative errors are more suitable when the values you are comparing can
157 vary in magnitude:
158
159 >>> approx_equal(12.345, 12.346, rel=1e-4)
160 True
161 >>> approx_equal(12.345e6, 12.346e6, rel=1e-4)
162 True
163
164 but a naive implementation of relative error testing can run into trouble
165 around zero.
166
167 If you supply both an absolute tolerance and a relative error, the
168 comparison succeeds if either individual test succeeds:
169
170 >>> approx_equal(12.345e6, 12.346e6, tol=1e-3, rel=1e-4)
171 True
172
173 """
174 pass
175
176
177
178# We prefer this for testing numeric values that may not be exactly equal,
179# and avoid using TestCase.assertAlmostEqual, because it sucks :-)
180
181class NumericTestCase(unittest.TestCase):
182 """Unit test class for numeric work.
183
184 This subclasses TestCase. In addition to the standard method
185 ``TestCase.assertAlmostEqual``, ``assertApproxEqual`` is provided.
186 """
187 # By default, we expect exact equality, unless overridden.
188 tol = rel = 0
189
190 def assertApproxEqual(
191 self, first, second, tol=None, rel=None, msg=None
192 ):
193 """Test passes if ``first`` and ``second`` are approximately equal.
194
195 This test passes if ``first`` and ``second`` are equal to
196 within ``tol``, an absolute error, or ``rel``, a relative error.
197
198 If either ``tol`` or ``rel`` are None or not given, they default to
199 test attributes of the same name (by default, 0).
200
201 The objects may be either numbers, or sequences of numbers. Sequences
202 are tested element-by-element.
203
204 >>> class MyTest(NumericTestCase):
205 ... def test_number(self):
206 ... x = 1.0/6
207 ... y = sum([x]*6)
208 ... self.assertApproxEqual(y, 1.0, tol=1e-15)
209 ... def test_sequence(self):
210 ... a = [1.001, 1.001e-10, 1.001e10]
211 ... b = [1.0, 1e-10, 1e10]
212 ... self.assertApproxEqual(a, b, rel=1e-3)
213 ...
214 >>> import unittest
215 >>> from io import StringIO # Suppress test runner output.
216 >>> suite = unittest.TestLoader().loadTestsFromTestCase(MyTest)
217 >>> unittest.TextTestRunner(stream=StringIO()).run(suite)
218 <unittest.runner.TextTestResult run=2 errors=0 failures=0>
219
220 """
221 if tol is None:
222 tol = self.tol
223 if rel is None:
224 rel = self.rel
225 if (
Serhiy Storchaka2e576f52017-04-24 09:05:00 +0300226 isinstance(first, collections.abc.Sequence) and
227 isinstance(second, collections.abc.Sequence)
Larry Hastingsf5e987b2013-10-19 11:50:09 -0700228 ):
229 check = self._check_approx_seq
230 else:
231 check = self._check_approx_num
232 check(first, second, tol, rel, msg)
233
234 def _check_approx_seq(self, first, second, tol, rel, msg):
235 if len(first) != len(second):
236 standardMsg = (
237 "sequences differ in length: %d items != %d items"
238 % (len(first), len(second))
239 )
240 msg = self._formatMessage(msg, standardMsg)
241 raise self.failureException(msg)
242 for i, (a,e) in enumerate(zip(first, second)):
243 self._check_approx_num(a, e, tol, rel, msg, i)
244
245 def _check_approx_num(self, first, second, tol, rel, msg, idx=None):
246 if approx_equal(first, second, tol, rel):
247 # Test passes. Return early, we are done.
248 return None
249 # Otherwise we failed.
250 standardMsg = self._make_std_err_msg(first, second, tol, rel, idx)
251 msg = self._formatMessage(msg, standardMsg)
252 raise self.failureException(msg)
253
254 @staticmethod
255 def _make_std_err_msg(first, second, tol, rel, idx):
256 # Create the standard error message for approx_equal failures.
257 assert first != second
258 template = (
259 ' %r != %r\n'
260 ' values differ by more than tol=%r and rel=%r\n'
261 ' -> absolute error = %r\n'
262 ' -> relative error = %r'
263 )
264 if idx is not None:
265 header = 'numeric sequences first differ at index %d.\n' % idx
266 template = header + template
267 # Calculate actual errors:
268 abs_err, rel_err = _calc_errors(first, second)
269 return template % (first, second, tol, rel, abs_err, rel_err)
270
271
272# ========================
273# === Test the helpers ===
274# ========================
275
Steven D'Apranoa474afd2016-08-09 12:49:01 +1000276class TestSign(unittest.TestCase):
277 """Test that the helper function sign() works correctly."""
278 def testZeroes(self):
279 # Test that signed zeroes report their sign correctly.
280 self.assertEqual(sign(0.0), +1)
281 self.assertEqual(sign(-0.0), -1)
282
Larry Hastingsf5e987b2013-10-19 11:50:09 -0700283
284# --- Tests for approx_equal ---
285
286class ApproxEqualSymmetryTest(unittest.TestCase):
287 # Test symmetry of approx_equal.
288
289 def test_relative_symmetry(self):
290 # Check that approx_equal treats relative error symmetrically.
291 # (a-b)/a is usually not equal to (a-b)/b. Ensure that this
292 # doesn't matter.
293 #
294 # Note: the reason for this test is that an early version
295 # of approx_equal was not symmetric. A relative error test
296 # would pass, or fail, depending on which value was passed
297 # as the first argument.
298 #
299 args1 = [2456, 37.8, -12.45, Decimal('2.54'), Fraction(17, 54)]
300 args2 = [2459, 37.2, -12.41, Decimal('2.59'), Fraction(15, 54)]
301 assert len(args1) == len(args2)
302 for a, b in zip(args1, args2):
303 self.do_relative_symmetry(a, b)
304
305 def do_relative_symmetry(self, a, b):
306 a, b = min(a, b), max(a, b)
307 assert a < b
308 delta = b - a # The absolute difference between the values.
309 rel_err1, rel_err2 = abs(delta/a), abs(delta/b)
310 # Choose an error margin halfway between the two.
311 rel = (rel_err1 + rel_err2)/2
312 # Now see that values a and b compare approx equal regardless of
313 # which is given first.
314 self.assertTrue(approx_equal(a, b, tol=0, rel=rel))
315 self.assertTrue(approx_equal(b, a, tol=0, rel=rel))
316
317 def test_symmetry(self):
318 # Test that approx_equal(a, b) == approx_equal(b, a)
319 args = [-23, -2, 5, 107, 93568]
320 delta = 2
Christian Heimesad393602013-11-26 01:32:15 +0100321 for a in args:
Larry Hastingsf5e987b2013-10-19 11:50:09 -0700322 for type_ in (int, float, Decimal, Fraction):
Christian Heimesad393602013-11-26 01:32:15 +0100323 x = type_(a)*100
Larry Hastingsf5e987b2013-10-19 11:50:09 -0700324 y = x + delta
325 r = abs(delta/max(x, y))
326 # There are five cases to check:
327 # 1) actual error <= tol, <= rel
328 self.do_symmetry_test(x, y, tol=delta, rel=r)
329 self.do_symmetry_test(x, y, tol=delta+1, rel=2*r)
330 # 2) actual error > tol, > rel
331 self.do_symmetry_test(x, y, tol=delta-1, rel=r/2)
332 # 3) actual error <= tol, > rel
333 self.do_symmetry_test(x, y, tol=delta, rel=r/2)
334 # 4) actual error > tol, <= rel
335 self.do_symmetry_test(x, y, tol=delta-1, rel=r)
336 self.do_symmetry_test(x, y, tol=delta-1, rel=2*r)
337 # 5) exact equality test
338 self.do_symmetry_test(x, x, tol=0, rel=0)
339 self.do_symmetry_test(x, y, tol=0, rel=0)
340
341 def do_symmetry_test(self, a, b, tol, rel):
342 template = "approx_equal comparisons don't match for %r"
343 flag1 = approx_equal(a, b, tol, rel)
344 flag2 = approx_equal(b, a, tol, rel)
345 self.assertEqual(flag1, flag2, template.format((a, b, tol, rel)))
346
347
348class ApproxEqualExactTest(unittest.TestCase):
349 # Test the approx_equal function with exactly equal values.
350 # Equal values should compare as approximately equal.
351 # Test cases for exactly equal values, which should compare approx
352 # equal regardless of the error tolerances given.
353
354 def do_exactly_equal_test(self, x, tol, rel):
355 result = approx_equal(x, x, tol=tol, rel=rel)
356 self.assertTrue(result, 'equality failure for x=%r' % x)
357 result = approx_equal(-x, -x, tol=tol, rel=rel)
358 self.assertTrue(result, 'equality failure for x=%r' % -x)
359
360 def test_exactly_equal_ints(self):
361 # Test that equal int values are exactly equal.
362 for n in [42, 19740, 14974, 230, 1795, 700245, 36587]:
363 self.do_exactly_equal_test(n, 0, 0)
364
365 def test_exactly_equal_floats(self):
366 # Test that equal float values are exactly equal.
367 for x in [0.42, 1.9740, 1497.4, 23.0, 179.5, 70.0245, 36.587]:
368 self.do_exactly_equal_test(x, 0, 0)
369
370 def test_exactly_equal_fractions(self):
371 # Test that equal Fraction values are exactly equal.
372 F = Fraction
373 for f in [F(1, 2), F(0), F(5, 3), F(9, 7), F(35, 36), F(3, 7)]:
374 self.do_exactly_equal_test(f, 0, 0)
375
376 def test_exactly_equal_decimals(self):
377 # Test that equal Decimal values are exactly equal.
378 D = Decimal
379 for d in map(D, "8.2 31.274 912.04 16.745 1.2047".split()):
380 self.do_exactly_equal_test(d, 0, 0)
381
382 def test_exactly_equal_absolute(self):
383 # Test that equal values are exactly equal with an absolute error.
384 for n in [16, 1013, 1372, 1198, 971, 4]:
385 # Test as ints.
386 self.do_exactly_equal_test(n, 0.01, 0)
387 # Test as floats.
388 self.do_exactly_equal_test(n/10, 0.01, 0)
389 # Test as Fractions.
390 f = Fraction(n, 1234)
391 self.do_exactly_equal_test(f, 0.01, 0)
392
393 def test_exactly_equal_absolute_decimals(self):
394 # Test equal Decimal values are exactly equal with an absolute error.
395 self.do_exactly_equal_test(Decimal("3.571"), Decimal("0.01"), 0)
396 self.do_exactly_equal_test(-Decimal("81.3971"), Decimal("0.01"), 0)
397
398 def test_exactly_equal_relative(self):
399 # Test that equal values are exactly equal with a relative error.
400 for x in [8347, 101.3, -7910.28, Fraction(5, 21)]:
401 self.do_exactly_equal_test(x, 0, 0.01)
402 self.do_exactly_equal_test(Decimal("11.68"), 0, Decimal("0.01"))
403
404 def test_exactly_equal_both(self):
405 # Test that equal values are equal when both tol and rel are given.
406 for x in [41017, 16.742, -813.02, Fraction(3, 8)]:
407 self.do_exactly_equal_test(x, 0.1, 0.01)
408 D = Decimal
409 self.do_exactly_equal_test(D("7.2"), D("0.1"), D("0.01"))
410
411
412class ApproxEqualUnequalTest(unittest.TestCase):
413 # Unequal values should compare unequal with zero error tolerances.
414 # Test cases for unequal values, with exact equality test.
415
416 def do_exactly_unequal_test(self, x):
417 for a in (x, -x):
418 result = approx_equal(a, a+1, tol=0, rel=0)
419 self.assertFalse(result, 'inequality failure for x=%r' % a)
420
421 def test_exactly_unequal_ints(self):
422 # Test unequal int values are unequal with zero error tolerance.
423 for n in [951, 572305, 478, 917, 17240]:
424 self.do_exactly_unequal_test(n)
425
426 def test_exactly_unequal_floats(self):
427 # Test unequal float values are unequal with zero error tolerance.
428 for x in [9.51, 5723.05, 47.8, 9.17, 17.24]:
429 self.do_exactly_unequal_test(x)
430
431 def test_exactly_unequal_fractions(self):
432 # Test that unequal Fractions are unequal with zero error tolerance.
433 F = Fraction
434 for f in [F(1, 5), F(7, 9), F(12, 11), F(101, 99023)]:
435 self.do_exactly_unequal_test(f)
436
437 def test_exactly_unequal_decimals(self):
438 # Test that unequal Decimals are unequal with zero error tolerance.
439 for d in map(Decimal, "3.1415 298.12 3.47 18.996 0.00245".split()):
440 self.do_exactly_unequal_test(d)
441
442
443class ApproxEqualInexactTest(unittest.TestCase):
444 # Inexact test cases for approx_error.
445 # Test cases when comparing two values that are not exactly equal.
446
447 # === Absolute error tests ===
448
449 def do_approx_equal_abs_test(self, x, delta):
450 template = "Test failure for x={!r}, y={!r}"
451 for y in (x + delta, x - delta):
452 msg = template.format(x, y)
453 self.assertTrue(approx_equal(x, y, tol=2*delta, rel=0), msg)
454 self.assertFalse(approx_equal(x, y, tol=delta/2, rel=0), msg)
455
456 def test_approx_equal_absolute_ints(self):
457 # Test approximate equality of ints with an absolute error.
458 for n in [-10737, -1975, -7, -2, 0, 1, 9, 37, 423, 9874, 23789110]:
459 self.do_approx_equal_abs_test(n, 10)
460 self.do_approx_equal_abs_test(n, 2)
461
462 def test_approx_equal_absolute_floats(self):
463 # Test approximate equality of floats with an absolute error.
464 for x in [-284.126, -97.1, -3.4, -2.15, 0.5, 1.0, 7.8, 4.23, 3817.4]:
465 self.do_approx_equal_abs_test(x, 1.5)
466 self.do_approx_equal_abs_test(x, 0.01)
467 self.do_approx_equal_abs_test(x, 0.0001)
468
469 def test_approx_equal_absolute_fractions(self):
470 # Test approximate equality of Fractions with an absolute error.
471 delta = Fraction(1, 29)
472 numerators = [-84, -15, -2, -1, 0, 1, 5, 17, 23, 34, 71]
473 for f in (Fraction(n, 29) for n in numerators):
474 self.do_approx_equal_abs_test(f, delta)
475 self.do_approx_equal_abs_test(f, float(delta))
476
477 def test_approx_equal_absolute_decimals(self):
478 # Test approximate equality of Decimals with an absolute error.
479 delta = Decimal("0.01")
480 for d in map(Decimal, "1.0 3.5 36.08 61.79 7912.3648".split()):
481 self.do_approx_equal_abs_test(d, delta)
482 self.do_approx_equal_abs_test(-d, delta)
483
484 def test_cross_zero(self):
485 # Test for the case of the two values having opposite signs.
486 self.assertTrue(approx_equal(1e-5, -1e-5, tol=1e-4, rel=0))
487
488 # === Relative error tests ===
489
490 def do_approx_equal_rel_test(self, x, delta):
491 template = "Test failure for x={!r}, y={!r}"
492 for y in (x*(1+delta), x*(1-delta)):
493 msg = template.format(x, y)
494 self.assertTrue(approx_equal(x, y, tol=0, rel=2*delta), msg)
495 self.assertFalse(approx_equal(x, y, tol=0, rel=delta/2), msg)
496
497 def test_approx_equal_relative_ints(self):
498 # Test approximate equality of ints with a relative error.
499 self.assertTrue(approx_equal(64, 47, tol=0, rel=0.36))
500 self.assertTrue(approx_equal(64, 47, tol=0, rel=0.37))
501 # ---
502 self.assertTrue(approx_equal(449, 512, tol=0, rel=0.125))
503 self.assertTrue(approx_equal(448, 512, tol=0, rel=0.125))
504 self.assertFalse(approx_equal(447, 512, tol=0, rel=0.125))
505
506 def test_approx_equal_relative_floats(self):
507 # Test approximate equality of floats with a relative error.
508 for x in [-178.34, -0.1, 0.1, 1.0, 36.97, 2847.136, 9145.074]:
509 self.do_approx_equal_rel_test(x, 0.02)
510 self.do_approx_equal_rel_test(x, 0.0001)
511
512 def test_approx_equal_relative_fractions(self):
513 # Test approximate equality of Fractions with a relative error.
514 F = Fraction
515 delta = Fraction(3, 8)
516 for f in [F(3, 84), F(17, 30), F(49, 50), F(92, 85)]:
517 for d in (delta, float(delta)):
518 self.do_approx_equal_rel_test(f, d)
519 self.do_approx_equal_rel_test(-f, d)
520
521 def test_approx_equal_relative_decimals(self):
522 # Test approximate equality of Decimals with a relative error.
523 for d in map(Decimal, "0.02 1.0 5.7 13.67 94.138 91027.9321".split()):
524 self.do_approx_equal_rel_test(d, Decimal("0.001"))
525 self.do_approx_equal_rel_test(-d, Decimal("0.05"))
526
527 # === Both absolute and relative error tests ===
528
529 # There are four cases to consider:
530 # 1) actual error <= both absolute and relative error
531 # 2) actual error <= absolute error but > relative error
532 # 3) actual error <= relative error but > absolute error
533 # 4) actual error > both absolute and relative error
534
535 def do_check_both(self, a, b, tol, rel, tol_flag, rel_flag):
536 check = self.assertTrue if tol_flag else self.assertFalse
537 check(approx_equal(a, b, tol=tol, rel=0))
538 check = self.assertTrue if rel_flag else self.assertFalse
539 check(approx_equal(a, b, tol=0, rel=rel))
540 check = self.assertTrue if (tol_flag or rel_flag) else self.assertFalse
541 check(approx_equal(a, b, tol=tol, rel=rel))
542
543 def test_approx_equal_both1(self):
544 # Test actual error <= both absolute and relative error.
545 self.do_check_both(7.955, 7.952, 0.004, 3.8e-4, True, True)
546 self.do_check_both(-7.387, -7.386, 0.002, 0.0002, True, True)
547
548 def test_approx_equal_both2(self):
549 # Test actual error <= absolute error but > relative error.
550 self.do_check_both(7.955, 7.952, 0.004, 3.7e-4, True, False)
551
552 def test_approx_equal_both3(self):
553 # Test actual error <= relative error but > absolute error.
554 self.do_check_both(7.955, 7.952, 0.001, 3.8e-4, False, True)
555
556 def test_approx_equal_both4(self):
557 # Test actual error > both absolute and relative error.
558 self.do_check_both(2.78, 2.75, 0.01, 0.001, False, False)
559 self.do_check_both(971.44, 971.47, 0.02, 3e-5, False, False)
560
561
562class ApproxEqualSpecialsTest(unittest.TestCase):
563 # Test approx_equal with NANs and INFs and zeroes.
564
565 def test_inf(self):
566 for type_ in (float, Decimal):
567 inf = type_('inf')
568 self.assertTrue(approx_equal(inf, inf))
569 self.assertTrue(approx_equal(inf, inf, 0, 0))
570 self.assertTrue(approx_equal(inf, inf, 1, 0.01))
571 self.assertTrue(approx_equal(-inf, -inf))
572 self.assertFalse(approx_equal(inf, -inf))
573 self.assertFalse(approx_equal(inf, 1000))
574
575 def test_nan(self):
576 for type_ in (float, Decimal):
577 nan = type_('nan')
578 for other in (nan, type_('inf'), 1000):
579 self.assertFalse(approx_equal(nan, other))
580
581 def test_float_zeroes(self):
582 nzero = math.copysign(0.0, -1)
583 self.assertTrue(approx_equal(nzero, 0.0, tol=0.1, rel=0.1))
584
585 def test_decimal_zeroes(self):
586 nzero = Decimal("-0.0")
587 self.assertTrue(approx_equal(nzero, Decimal(0), tol=0.1, rel=0.1))
588
589
590class TestApproxEqualErrors(unittest.TestCase):
591 # Test error conditions of approx_equal.
592
593 def test_bad_tol(self):
594 # Test negative tol raises.
595 self.assertRaises(ValueError, approx_equal, 100, 100, -1, 0.1)
596
597 def test_bad_rel(self):
598 # Test negative rel raises.
599 self.assertRaises(ValueError, approx_equal, 100, 100, 1, -0.1)
600
601
602# --- Tests for NumericTestCase ---
603
604# The formatting routine that generates the error messages is complex enough
605# that it too needs testing.
606
607class TestNumericTestCase(unittest.TestCase):
608 # The exact wording of NumericTestCase error messages is *not* guaranteed,
609 # but we need to give them some sort of test to ensure that they are
610 # generated correctly. As a compromise, we look for specific substrings
611 # that are expected to be found even if the overall error message changes.
612
613 def do_test(self, args):
614 actual_msg = NumericTestCase._make_std_err_msg(*args)
615 expected = self.generate_substrings(*args)
616 for substring in expected:
617 self.assertIn(substring, actual_msg)
618
619 def test_numerictestcase_is_testcase(self):
620 # Ensure that NumericTestCase actually is a TestCase.
621 self.assertTrue(issubclass(NumericTestCase, unittest.TestCase))
622
623 def test_error_msg_numeric(self):
624 # Test the error message generated for numeric comparisons.
625 args = (2.5, 4.0, 0.5, 0.25, None)
626 self.do_test(args)
627
628 def test_error_msg_sequence(self):
629 # Test the error message generated for sequence comparisons.
630 args = (3.75, 8.25, 1.25, 0.5, 7)
631 self.do_test(args)
632
633 def generate_substrings(self, first, second, tol, rel, idx):
634 """Return substrings we expect to see in error messages."""
635 abs_err, rel_err = _calc_errors(first, second)
636 substrings = [
637 'tol=%r' % tol,
638 'rel=%r' % rel,
639 'absolute error = %r' % abs_err,
640 'relative error = %r' % rel_err,
641 ]
642 if idx is not None:
643 substrings.append('differ at index %d' % idx)
644 return substrings
645
646
647# =======================================
648# === Tests for the statistics module ===
649# =======================================
650
651
652class GlobalsTest(unittest.TestCase):
653 module = statistics
654 expected_metadata = ["__doc__", "__all__"]
655
656 def test_meta(self):
657 # Test for the existence of metadata.
658 for meta in self.expected_metadata:
659 self.assertTrue(hasattr(self.module, meta),
660 "%s not present" % meta)
661
662 def test_check_all(self):
663 # Check everything in __all__ exists and is public.
664 module = self.module
665 for name in module.__all__:
666 # No private names in __all__:
667 self.assertFalse(name.startswith("_"),
668 'private name "%s" in __all__' % name)
669 # And anything in __all__ must exist:
670 self.assertTrue(hasattr(module, name),
671 'missing name "%s" in __all__' % name)
672
673
674class DocTests(unittest.TestCase):
Serhiy Storchakab12cb6a2013-12-08 18:16:18 +0200675 @unittest.skipIf(sys.flags.optimize >= 2,
676 "Docstrings are omitted with -OO and above")
Larry Hastingsf5e987b2013-10-19 11:50:09 -0700677 def test_doc_tests(self):
Steven D'Apranoa474afd2016-08-09 12:49:01 +1000678 failed, tried = doctest.testmod(statistics, optionflags=doctest.ELLIPSIS)
Larry Hastingsf5e987b2013-10-19 11:50:09 -0700679 self.assertGreater(tried, 0)
680 self.assertEqual(failed, 0)
681
682class StatisticsErrorTest(unittest.TestCase):
683 def test_has_exception(self):
684 errmsg = (
685 "Expected StatisticsError to be a ValueError, but got a"
686 " subclass of %r instead."
687 )
688 self.assertTrue(hasattr(statistics, 'StatisticsError'))
689 self.assertTrue(
690 issubclass(statistics.StatisticsError, ValueError),
691 errmsg % statistics.StatisticsError.__base__
692 )
693
694
695# === Tests for private utility functions ===
696
697class ExactRatioTest(unittest.TestCase):
698 # Test _exact_ratio utility.
699
700 def test_int(self):
701 for i in (-20, -3, 0, 5, 99, 10**20):
702 self.assertEqual(statistics._exact_ratio(i), (i, 1))
703
704 def test_fraction(self):
705 numerators = (-5, 1, 12, 38)
706 for n in numerators:
707 f = Fraction(n, 37)
708 self.assertEqual(statistics._exact_ratio(f), (n, 37))
709
710 def test_float(self):
711 self.assertEqual(statistics._exact_ratio(0.125), (1, 8))
712 self.assertEqual(statistics._exact_ratio(1.125), (9, 8))
713 data = [random.uniform(-100, 100) for _ in range(100)]
714 for x in data:
715 num, den = statistics._exact_ratio(x)
716 self.assertEqual(x, num/den)
717
718 def test_decimal(self):
719 D = Decimal
720 _exact_ratio = statistics._exact_ratio
Steven D'Aprano3b06e242016-05-05 03:54:29 +1000721 self.assertEqual(_exact_ratio(D("0.125")), (1, 8))
722 self.assertEqual(_exact_ratio(D("12.345")), (2469, 200))
723 self.assertEqual(_exact_ratio(D("-1.98")), (-99, 50))
Larry Hastingsf5e987b2013-10-19 11:50:09 -0700724
Steven D'Apranob28c3272015-12-01 19:59:53 +1100725 def test_inf(self):
726 INF = float("INF")
727 class MyFloat(float):
728 pass
729 class MyDecimal(Decimal):
730 pass
731 for inf in (INF, -INF):
732 for type_ in (float, MyFloat, Decimal, MyDecimal):
733 x = type_(inf)
734 ratio = statistics._exact_ratio(x)
735 self.assertEqual(ratio, (x, None))
736 self.assertEqual(type(ratio[0]), type_)
737 self.assertTrue(math.isinf(ratio[0]))
738
739 def test_float_nan(self):
740 NAN = float("NAN")
741 class MyFloat(float):
742 pass
743 for nan in (NAN, MyFloat(NAN)):
744 ratio = statistics._exact_ratio(nan)
745 self.assertTrue(math.isnan(ratio[0]))
746 self.assertIs(ratio[1], None)
747 self.assertEqual(type(ratio[0]), type(nan))
748
749 def test_decimal_nan(self):
750 NAN = Decimal("NAN")
751 sNAN = Decimal("sNAN")
752 class MyDecimal(Decimal):
753 pass
754 for nan in (NAN, MyDecimal(NAN), sNAN, MyDecimal(sNAN)):
755 ratio = statistics._exact_ratio(nan)
756 self.assertTrue(_nan_equal(ratio[0], nan))
757 self.assertIs(ratio[1], None)
758 self.assertEqual(type(ratio[0]), type(nan))
759
Larry Hastingsf5e987b2013-10-19 11:50:09 -0700760
761class DecimalToRatioTest(unittest.TestCase):
Steven D'Aprano3b06e242016-05-05 03:54:29 +1000762 # Test _exact_ratio private function.
Larry Hastingsf5e987b2013-10-19 11:50:09 -0700763
Steven D'Apranob28c3272015-12-01 19:59:53 +1100764 def test_infinity(self):
765 # Test that INFs are handled correctly.
766 inf = Decimal('INF')
Steven D'Aprano3b06e242016-05-05 03:54:29 +1000767 self.assertEqual(statistics._exact_ratio(inf), (inf, None))
768 self.assertEqual(statistics._exact_ratio(-inf), (-inf, None))
Steven D'Apranob28c3272015-12-01 19:59:53 +1100769
770 def test_nan(self):
771 # Test that NANs are handled correctly.
772 for nan in (Decimal('NAN'), Decimal('sNAN')):
Steven D'Aprano3b06e242016-05-05 03:54:29 +1000773 num, den = statistics._exact_ratio(nan)
Steven D'Apranob28c3272015-12-01 19:59:53 +1100774 # Because NANs always compare non-equal, we cannot use assertEqual.
775 # Nor can we use an identity test, as we don't guarantee anything
776 # about the object identity.
777 self.assertTrue(_nan_equal(num, nan))
778 self.assertIs(den, None)
Larry Hastingsf5e987b2013-10-19 11:50:09 -0700779
Nick Coghlan4a7668a2014-02-08 23:55:14 +1000780 def test_sign(self):
781 # Test sign is calculated correctly.
782 numbers = [Decimal("9.8765e12"), Decimal("9.8765e-12")]
783 for d in numbers:
784 # First test positive decimals.
785 assert d > 0
Steven D'Aprano3b06e242016-05-05 03:54:29 +1000786 num, den = statistics._exact_ratio(d)
Nick Coghlan4a7668a2014-02-08 23:55:14 +1000787 self.assertGreaterEqual(num, 0)
788 self.assertGreater(den, 0)
789 # Then test negative decimals.
Steven D'Aprano3b06e242016-05-05 03:54:29 +1000790 num, den = statistics._exact_ratio(-d)
Nick Coghlan4a7668a2014-02-08 23:55:14 +1000791 self.assertLessEqual(num, 0)
792 self.assertGreater(den, 0)
793
794 def test_negative_exponent(self):
795 # Test result when the exponent is negative.
Steven D'Aprano3b06e242016-05-05 03:54:29 +1000796 t = statistics._exact_ratio(Decimal("0.1234"))
797 self.assertEqual(t, (617, 5000))
Nick Coghlan4a7668a2014-02-08 23:55:14 +1000798
799 def test_positive_exponent(self):
800 # Test results when the exponent is positive.
Steven D'Aprano3b06e242016-05-05 03:54:29 +1000801 t = statistics._exact_ratio(Decimal("1.234e7"))
Nick Coghlan4a7668a2014-02-08 23:55:14 +1000802 self.assertEqual(t, (12340000, 1))
803
804 def test_regression_20536(self):
805 # Regression test for issue 20536.
806 # See http://bugs.python.org/issue20536
Steven D'Aprano3b06e242016-05-05 03:54:29 +1000807 t = statistics._exact_ratio(Decimal("1e2"))
Nick Coghlan4a7668a2014-02-08 23:55:14 +1000808 self.assertEqual(t, (100, 1))
Steven D'Aprano3b06e242016-05-05 03:54:29 +1000809 t = statistics._exact_ratio(Decimal("1.47e5"))
Nick Coghlan4a7668a2014-02-08 23:55:14 +1000810 self.assertEqual(t, (147000, 1))
811
Larry Hastingsf5e987b2013-10-19 11:50:09 -0700812
Steven D'Apranob28c3272015-12-01 19:59:53 +1100813class IsFiniteTest(unittest.TestCase):
814 # Test _isfinite private function.
Nick Coghlan73afe2a2014-02-08 19:58:04 +1000815
Steven D'Apranob28c3272015-12-01 19:59:53 +1100816 def test_finite(self):
817 # Test that finite numbers are recognised as finite.
818 for x in (5, Fraction(1, 3), 2.5, Decimal("5.5")):
819 self.assertTrue(statistics._isfinite(x))
Nick Coghlan73afe2a2014-02-08 19:58:04 +1000820
Steven D'Apranob28c3272015-12-01 19:59:53 +1100821 def test_infinity(self):
822 # Test that INFs are not recognised as finite.
823 for x in (float("inf"), Decimal("inf")):
824 self.assertFalse(statistics._isfinite(x))
Nick Coghlan73afe2a2014-02-08 19:58:04 +1000825
Steven D'Apranob28c3272015-12-01 19:59:53 +1100826 def test_nan(self):
827 # Test that NANs are not recognised as finite.
828 for x in (float("nan"), Decimal("NAN"), Decimal("sNAN")):
829 self.assertFalse(statistics._isfinite(x))
830
831
832class CoerceTest(unittest.TestCase):
833 # Test that private function _coerce correctly deals with types.
834
835 # The coercion rules are currently an implementation detail, although at
836 # some point that should change. The tests and comments here define the
837 # correct implementation.
838
839 # Pre-conditions of _coerce:
840 #
841 # - The first time _sum calls _coerce, the
842 # - coerce(T, S) will never be called with bool as the first argument;
843 # this is a pre-condition, guarded with an assertion.
844
845 #
846 # - coerce(T, T) will always return T; we assume T is a valid numeric
847 # type. Violate this assumption at your own risk.
848 #
849 # - Apart from as above, bool is treated as if it were actually int.
850 #
851 # - coerce(int, X) and coerce(X, int) return X.
852 # -
853 def test_bool(self):
854 # bool is somewhat special, due to the pre-condition that it is
855 # never given as the first argument to _coerce, and that it cannot
856 # be subclassed. So we test it specially.
857 for T in (int, float, Fraction, Decimal):
858 self.assertIs(statistics._coerce(T, bool), T)
859 class MyClass(T): pass
860 self.assertIs(statistics._coerce(MyClass, bool), MyClass)
861
862 def assertCoerceTo(self, A, B):
863 """Assert that type A coerces to B."""
864 self.assertIs(statistics._coerce(A, B), B)
865 self.assertIs(statistics._coerce(B, A), B)
866
867 def check_coerce_to(self, A, B):
868 """Checks that type A coerces to B, including subclasses."""
869 # Assert that type A is coerced to B.
870 self.assertCoerceTo(A, B)
871 # Subclasses of A are also coerced to B.
872 class SubclassOfA(A): pass
873 self.assertCoerceTo(SubclassOfA, B)
874 # A, and subclasses of A, are coerced to subclasses of B.
875 class SubclassOfB(B): pass
876 self.assertCoerceTo(A, SubclassOfB)
877 self.assertCoerceTo(SubclassOfA, SubclassOfB)
878
879 def assertCoerceRaises(self, A, B):
880 """Assert that coercing A to B, or vice versa, raises TypeError."""
881 self.assertRaises(TypeError, statistics._coerce, (A, B))
882 self.assertRaises(TypeError, statistics._coerce, (B, A))
883
884 def check_type_coercions(self, T):
885 """Check that type T coerces correctly with subclasses of itself."""
886 assert T is not bool
887 # Coercing a type with itself returns the same type.
888 self.assertIs(statistics._coerce(T, T), T)
889 # Coercing a type with a subclass of itself returns the subclass.
890 class U(T): pass
891 class V(T): pass
892 class W(U): pass
893 for typ in (U, V, W):
894 self.assertCoerceTo(T, typ)
895 self.assertCoerceTo(U, W)
896 # Coercing two subclasses that aren't parent/child is an error.
897 self.assertCoerceRaises(U, V)
898 self.assertCoerceRaises(V, W)
899
900 def test_int(self):
901 # Check that int coerces correctly.
902 self.check_type_coercions(int)
903 for typ in (float, Fraction, Decimal):
904 self.check_coerce_to(int, typ)
905
906 def test_fraction(self):
907 # Check that Fraction coerces correctly.
908 self.check_type_coercions(Fraction)
909 self.check_coerce_to(Fraction, float)
910
911 def test_decimal(self):
912 # Check that Decimal coerces correctly.
913 self.check_type_coercions(Decimal)
914
915 def test_float(self):
916 # Check that float coerces correctly.
917 self.check_type_coercions(float)
918
919 def test_non_numeric_types(self):
920 for bad_type in (str, list, type(None), tuple, dict):
921 for good_type in (int, float, Fraction, Decimal):
922 self.assertCoerceRaises(good_type, bad_type)
923
924 def test_incompatible_types(self):
925 # Test that incompatible types raise.
926 for T in (float, Fraction):
927 class MySubclass(T): pass
928 self.assertCoerceRaises(T, Decimal)
929 self.assertCoerceRaises(MySubclass, Decimal)
930
931
932class ConvertTest(unittest.TestCase):
933 # Test private _convert function.
934
935 def check_exact_equal(self, x, y):
936 """Check that x equals y, and has the same type as well."""
937 self.assertEqual(x, y)
938 self.assertIs(type(x), type(y))
939
940 def test_int(self):
941 # Test conversions to int.
942 x = statistics._convert(Fraction(71), int)
943 self.check_exact_equal(x, 71)
944 class MyInt(int): pass
945 x = statistics._convert(Fraction(17), MyInt)
946 self.check_exact_equal(x, MyInt(17))
947
948 def test_fraction(self):
949 # Test conversions to Fraction.
950 x = statistics._convert(Fraction(95, 99), Fraction)
951 self.check_exact_equal(x, Fraction(95, 99))
952 class MyFraction(Fraction):
953 def __truediv__(self, other):
954 return self.__class__(super().__truediv__(other))
955 x = statistics._convert(Fraction(71, 13), MyFraction)
956 self.check_exact_equal(x, MyFraction(71, 13))
957
958 def test_float(self):
959 # Test conversions to float.
960 x = statistics._convert(Fraction(-1, 2), float)
961 self.check_exact_equal(x, -0.5)
962 class MyFloat(float):
963 def __truediv__(self, other):
964 return self.__class__(super().__truediv__(other))
965 x = statistics._convert(Fraction(9, 8), MyFloat)
966 self.check_exact_equal(x, MyFloat(1.125))
967
968 def test_decimal(self):
969 # Test conversions to Decimal.
970 x = statistics._convert(Fraction(1, 40), Decimal)
971 self.check_exact_equal(x, Decimal("0.025"))
972 class MyDecimal(Decimal):
973 def __truediv__(self, other):
974 return self.__class__(super().__truediv__(other))
975 x = statistics._convert(Fraction(-15, 16), MyDecimal)
976 self.check_exact_equal(x, MyDecimal("-0.9375"))
977
978 def test_inf(self):
979 for INF in (float('inf'), Decimal('inf')):
980 for inf in (INF, -INF):
981 x = statistics._convert(inf, type(inf))
982 self.check_exact_equal(x, inf)
983
984 def test_nan(self):
985 for nan in (float('nan'), Decimal('NAN'), Decimal('sNAN')):
986 x = statistics._convert(nan, type(nan))
987 self.assertTrue(_nan_equal(x, nan))
Nick Coghlan73afe2a2014-02-08 19:58:04 +1000988
Larry Hastingsf5e987b2013-10-19 11:50:09 -0700989
Steven D'Apranoa474afd2016-08-09 12:49:01 +1000990class FailNegTest(unittest.TestCase):
991 """Test _fail_neg private function."""
992
993 def test_pass_through(self):
994 # Test that values are passed through unchanged.
995 values = [1, 2.0, Fraction(3), Decimal(4)]
996 new = list(statistics._fail_neg(values))
997 self.assertEqual(values, new)
998
999 def test_negatives_raise(self):
1000 # Test that negatives raise an exception.
1001 for x in [1, 2.0, Fraction(3), Decimal(4)]:
1002 seq = [-x]
1003 it = statistics._fail_neg(seq)
1004 self.assertRaises(statistics.StatisticsError, next, it)
1005
1006 def test_error_msg(self):
1007 # Test that a given error message is used.
1008 msg = "badness #%d" % random.randint(10000, 99999)
1009 try:
1010 next(statistics._fail_neg([-1], msg))
1011 except statistics.StatisticsError as e:
1012 errmsg = e.args[0]
1013 else:
1014 self.fail("expected exception, but it didn't happen")
1015 self.assertEqual(errmsg, msg)
1016
1017
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001018# === Tests for public functions ===
1019
1020class UnivariateCommonMixin:
1021 # Common tests for most univariate functions that take a data argument.
1022
1023 def test_no_args(self):
1024 # Fail if given no arguments.
1025 self.assertRaises(TypeError, self.func)
1026
1027 def test_empty_data(self):
1028 # Fail when the data argument (first argument) is empty.
1029 for empty in ([], (), iter([])):
1030 self.assertRaises(statistics.StatisticsError, self.func, empty)
1031
1032 def prepare_data(self):
1033 """Return int data for various tests."""
1034 data = list(range(10))
1035 while data == sorted(data):
1036 random.shuffle(data)
1037 return data
1038
1039 def test_no_inplace_modifications(self):
1040 # Test that the function does not modify its input data.
1041 data = self.prepare_data()
1042 assert len(data) != 1 # Necessary to avoid infinite loop.
1043 assert data != sorted(data)
1044 saved = data[:]
1045 assert data is not saved
1046 _ = self.func(data)
1047 self.assertListEqual(data, saved, "data has been modified")
1048
1049 def test_order_doesnt_matter(self):
1050 # Test that the order of data points doesn't change the result.
1051
1052 # CAUTION: due to floating point rounding errors, the result actually
1053 # may depend on the order. Consider this test representing an ideal.
1054 # To avoid this test failing, only test with exact values such as ints
1055 # or Fractions.
1056 data = [1, 2, 3, 3, 3, 4, 5, 6]*100
1057 expected = self.func(data)
1058 random.shuffle(data)
1059 actual = self.func(data)
1060 self.assertEqual(expected, actual)
1061
1062 def test_type_of_data_collection(self):
1063 # Test that the type of iterable data doesn't effect the result.
1064 class MyList(list):
1065 pass
1066 class MyTuple(tuple):
1067 pass
1068 def generator(data):
1069 return (obj for obj in data)
1070 data = self.prepare_data()
1071 expected = self.func(data)
1072 for kind in (list, tuple, iter, MyList, MyTuple, generator):
1073 result = self.func(kind(data))
1074 self.assertEqual(result, expected)
1075
1076 def test_range_data(self):
1077 # Test that functions work with range objects.
1078 data = range(20, 50, 3)
1079 expected = self.func(list(data))
1080 self.assertEqual(self.func(data), expected)
1081
1082 def test_bad_arg_types(self):
1083 # Test that function raises when given data of the wrong type.
1084
1085 # Don't roll the following into a loop like this:
1086 # for bad in list_of_bad:
1087 # self.check_for_type_error(bad)
1088 #
1089 # Since assertRaises doesn't show the arguments that caused the test
1090 # failure, it is very difficult to debug these test failures when the
1091 # following are in a loop.
1092 self.check_for_type_error(None)
1093 self.check_for_type_error(23)
1094 self.check_for_type_error(42.0)
1095 self.check_for_type_error(object())
1096
1097 def check_for_type_error(self, *args):
1098 self.assertRaises(TypeError, self.func, *args)
1099
1100 def test_type_of_data_element(self):
1101 # Check the type of data elements doesn't affect the numeric result.
1102 # This is a weaker test than UnivariateTypeMixin.testTypesConserved,
1103 # because it checks the numeric result by equality, but not by type.
1104 class MyFloat(float):
1105 def __truediv__(self, other):
1106 return type(self)(super().__truediv__(other))
1107 def __add__(self, other):
1108 return type(self)(super().__add__(other))
1109 __radd__ = __add__
1110
1111 raw = self.prepare_data()
1112 expected = self.func(raw)
1113 for kind in (float, MyFloat, Decimal, Fraction):
1114 data = [kind(x) for x in raw]
1115 result = type(expected)(self.func(data))
1116 self.assertEqual(result, expected)
1117
1118
1119class UnivariateTypeMixin:
1120 """Mixin class for type-conserving functions.
1121
1122 This mixin class holds test(s) for functions which conserve the type of
1123 individual data points. E.g. the mean of a list of Fractions should itself
1124 be a Fraction.
1125
1126 Not all tests to do with types need go in this class. Only those that
1127 rely on the function returning the same type as its input data.
1128 """
Steven D'Apranoa474afd2016-08-09 12:49:01 +10001129 def prepare_types_for_conservation_test(self):
1130 """Return the types which are expected to be conserved."""
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001131 class MyFloat(float):
1132 def __truediv__(self, other):
1133 return type(self)(super().__truediv__(other))
Steven D'Apranoa474afd2016-08-09 12:49:01 +10001134 def __rtruediv__(self, other):
1135 return type(self)(super().__rtruediv__(other))
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001136 def __sub__(self, other):
1137 return type(self)(super().__sub__(other))
1138 def __rsub__(self, other):
1139 return type(self)(super().__rsub__(other))
1140 def __pow__(self, other):
1141 return type(self)(super().__pow__(other))
1142 def __add__(self, other):
1143 return type(self)(super().__add__(other))
1144 __radd__ = __add__
Steven D'Apranoa474afd2016-08-09 12:49:01 +10001145 return (float, Decimal, Fraction, MyFloat)
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001146
Steven D'Apranoa474afd2016-08-09 12:49:01 +10001147 def test_types_conserved(self):
1148 # Test that functions keeps the same type as their data points.
1149 # (Excludes mixed data types.) This only tests the type of the return
1150 # result, not the value.
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001151 data = self.prepare_data()
Steven D'Apranoa474afd2016-08-09 12:49:01 +10001152 for kind in self.prepare_types_for_conservation_test():
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001153 d = [kind(x) for x in data]
1154 result = self.func(d)
1155 self.assertIs(type(result), kind)
1156
1157
Steven D'Apranob28c3272015-12-01 19:59:53 +11001158class TestSumCommon(UnivariateCommonMixin, UnivariateTypeMixin):
1159 # Common test cases for statistics._sum() function.
1160
1161 # This test suite looks only at the numeric value returned by _sum,
1162 # after conversion to the appropriate type.
1163 def setUp(self):
1164 def simplified_sum(*args):
1165 T, value, n = statistics._sum(*args)
1166 return statistics._coerce(value, T)
1167 self.func = simplified_sum
1168
1169
1170class TestSum(NumericTestCase):
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001171 # Test cases for statistics._sum() function.
1172
Steven D'Apranob28c3272015-12-01 19:59:53 +11001173 # These tests look at the entire three value tuple returned by _sum.
1174
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001175 def setUp(self):
1176 self.func = statistics._sum
1177
1178 def test_empty_data(self):
1179 # Override test for empty data.
1180 for data in ([], (), iter([])):
Steven D'Apranob28c3272015-12-01 19:59:53 +11001181 self.assertEqual(self.func(data), (int, Fraction(0), 0))
1182 self.assertEqual(self.func(data, 23), (int, Fraction(23), 0))
1183 self.assertEqual(self.func(data, 2.3), (float, Fraction(2.3), 0))
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001184
1185 def test_ints(self):
Steven D'Apranob28c3272015-12-01 19:59:53 +11001186 self.assertEqual(self.func([1, 5, 3, -4, -8, 20, 42, 1]),
1187 (int, Fraction(60), 8))
1188 self.assertEqual(self.func([4, 2, 3, -8, 7], 1000),
1189 (int, Fraction(1008), 5))
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001190
1191 def test_floats(self):
Steven D'Apranob28c3272015-12-01 19:59:53 +11001192 self.assertEqual(self.func([0.25]*20),
1193 (float, Fraction(5.0), 20))
1194 self.assertEqual(self.func([0.125, 0.25, 0.5, 0.75], 1.5),
1195 (float, Fraction(3.125), 4))
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001196
1197 def test_fractions(self):
Steven D'Apranob28c3272015-12-01 19:59:53 +11001198 self.assertEqual(self.func([Fraction(1, 1000)]*500),
1199 (Fraction, Fraction(1, 2), 500))
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001200
1201 def test_decimals(self):
1202 D = Decimal
1203 data = [D("0.001"), D("5.246"), D("1.702"), D("-0.025"),
1204 D("3.974"), D("2.328"), D("4.617"), D("2.843"),
1205 ]
Steven D'Apranob28c3272015-12-01 19:59:53 +11001206 self.assertEqual(self.func(data),
1207 (Decimal, Decimal("20.686"), 8))
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001208
1209 def test_compare_with_math_fsum(self):
1210 # Compare with the math.fsum function.
1211 # Ideally we ought to get the exact same result, but sometimes
1212 # we differ by a very slight amount :-(
1213 data = [random.uniform(-100, 1000) for _ in range(1000)]
Steven D'Apranob28c3272015-12-01 19:59:53 +11001214 self.assertApproxEqual(float(self.func(data)[1]), math.fsum(data), rel=2e-16)
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001215
1216 def test_start_argument(self):
1217 # Test that the optional start argument works correctly.
1218 data = [random.uniform(1, 1000) for _ in range(100)]
Steven D'Apranob28c3272015-12-01 19:59:53 +11001219 t = self.func(data)[1]
1220 self.assertEqual(t+42, self.func(data, 42)[1])
1221 self.assertEqual(t-23, self.func(data, -23)[1])
1222 self.assertEqual(t+Fraction(1e20), self.func(data, 1e20)[1])
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001223
1224 def test_strings_fail(self):
1225 # Sum of strings should fail.
1226 self.assertRaises(TypeError, self.func, [1, 2, 3], '999')
1227 self.assertRaises(TypeError, self.func, [1, 2, 3, '999'])
1228
1229 def test_bytes_fail(self):
1230 # Sum of bytes should fail.
1231 self.assertRaises(TypeError, self.func, [1, 2, 3], b'999')
1232 self.assertRaises(TypeError, self.func, [1, 2, 3, b'999'])
1233
1234 def test_mixed_sum(self):
Nick Coghlan73afe2a2014-02-08 19:58:04 +10001235 # Mixed input types are not (currently) allowed.
1236 # Check that mixed data types fail.
Steven D'Apranob28c3272015-12-01 19:59:53 +11001237 self.assertRaises(TypeError, self.func, [1, 2.0, Decimal(1)])
Nick Coghlan73afe2a2014-02-08 19:58:04 +10001238 # And so does mixed start argument.
1239 self.assertRaises(TypeError, self.func, [1, 2.0], Decimal(1))
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001240
1241
1242class SumTortureTest(NumericTestCase):
1243 def test_torture(self):
1244 # Tim Peters' torture test for sum, and variants of same.
Steven D'Apranob28c3272015-12-01 19:59:53 +11001245 self.assertEqual(statistics._sum([1, 1e100, 1, -1e100]*10000),
1246 (float, Fraction(20000.0), 40000))
1247 self.assertEqual(statistics._sum([1e100, 1, 1, -1e100]*10000),
1248 (float, Fraction(20000.0), 40000))
1249 T, num, count = statistics._sum([1e-100, 1, 1e-100, -1]*10000)
1250 self.assertIs(T, float)
1251 self.assertEqual(count, 40000)
1252 self.assertApproxEqual(float(num), 2.0e-96, rel=5e-16)
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001253
1254
1255class SumSpecialValues(NumericTestCase):
1256 # Test that sum works correctly with IEEE-754 special values.
1257
1258 def test_nan(self):
1259 for type_ in (float, Decimal):
1260 nan = type_('nan')
Steven D'Apranob28c3272015-12-01 19:59:53 +11001261 result = statistics._sum([1, nan, 2])[1]
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001262 self.assertIs(type(result), type_)
1263 self.assertTrue(math.isnan(result))
1264
1265 def check_infinity(self, x, inf):
1266 """Check x is an infinity of the same type and sign as inf."""
1267 self.assertTrue(math.isinf(x))
1268 self.assertIs(type(x), type(inf))
1269 self.assertEqual(x > 0, inf > 0)
1270 assert x == inf
1271
1272 def do_test_inf(self, inf):
1273 # Adding a single infinity gives infinity.
Steven D'Apranob28c3272015-12-01 19:59:53 +11001274 result = statistics._sum([1, 2, inf, 3])[1]
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001275 self.check_infinity(result, inf)
1276 # Adding two infinities of the same sign also gives infinity.
Steven D'Apranob28c3272015-12-01 19:59:53 +11001277 result = statistics._sum([1, 2, inf, 3, inf, 4])[1]
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001278 self.check_infinity(result, inf)
1279
1280 def test_float_inf(self):
1281 inf = float('inf')
1282 for sign in (+1, -1):
1283 self.do_test_inf(sign*inf)
1284
1285 def test_decimal_inf(self):
1286 inf = Decimal('inf')
1287 for sign in (+1, -1):
1288 self.do_test_inf(sign*inf)
1289
1290 def test_float_mismatched_infs(self):
1291 # Test that adding two infinities of opposite sign gives a NAN.
1292 inf = float('inf')
Steven D'Apranob28c3272015-12-01 19:59:53 +11001293 result = statistics._sum([1, 2, inf, 3, -inf, 4])[1]
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001294 self.assertTrue(math.isnan(result))
1295
Berker Peksagf8c111d2014-09-24 15:03:25 +03001296 def test_decimal_extendedcontext_mismatched_infs_to_nan(self):
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001297 # Test adding Decimal INFs with opposite sign returns NAN.
1298 inf = Decimal('inf')
1299 data = [1, 2, inf, 3, -inf, 4]
1300 with decimal.localcontext(decimal.ExtendedContext):
Steven D'Apranob28c3272015-12-01 19:59:53 +11001301 self.assertTrue(math.isnan(statistics._sum(data)[1]))
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001302
Berker Peksagf8c111d2014-09-24 15:03:25 +03001303 def test_decimal_basiccontext_mismatched_infs_to_nan(self):
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001304 # Test adding Decimal INFs with opposite sign raises InvalidOperation.
1305 inf = Decimal('inf')
1306 data = [1, 2, inf, 3, -inf, 4]
1307 with decimal.localcontext(decimal.BasicContext):
1308 self.assertRaises(decimal.InvalidOperation, statistics._sum, data)
1309
1310 def test_decimal_snan_raises(self):
1311 # Adding sNAN should raise InvalidOperation.
1312 sNAN = Decimal('sNAN')
1313 data = [1, sNAN, 2]
1314 self.assertRaises(decimal.InvalidOperation, statistics._sum, data)
1315
1316
1317# === Tests for averages ===
1318
1319class AverageMixin(UnivariateCommonMixin):
1320 # Mixin class holding common tests for averages.
1321
1322 def test_single_value(self):
1323 # Average of a single value is the value itself.
1324 for x in (23, 42.5, 1.3e15, Fraction(15, 19), Decimal('0.28')):
1325 self.assertEqual(self.func([x]), x)
1326
Steven D'Apranoa474afd2016-08-09 12:49:01 +10001327 def prepare_values_for_repeated_single_test(self):
1328 return (3.5, 17, 2.5e15, Fraction(61, 67), Decimal('4.9712'))
1329
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001330 def test_repeated_single_value(self):
1331 # The average of a single repeated value is the value itself.
Steven D'Apranoa474afd2016-08-09 12:49:01 +10001332 for x in self.prepare_values_for_repeated_single_test():
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001333 for count in (2, 5, 10, 20):
Steven D'Apranoa474afd2016-08-09 12:49:01 +10001334 with self.subTest(x=x, count=count):
1335 data = [x]*count
1336 self.assertEqual(self.func(data), x)
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001337
1338
1339class TestMean(NumericTestCase, AverageMixin, UnivariateTypeMixin):
1340 def setUp(self):
1341 self.func = statistics.mean
1342
1343 def test_torture_pep(self):
1344 # "Torture Test" from PEP-450.
1345 self.assertEqual(self.func([1e100, 1, 3, -1e100]), 1)
1346
1347 def test_ints(self):
1348 # Test mean with ints.
1349 data = [0, 1, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 8, 9]
1350 random.shuffle(data)
1351 self.assertEqual(self.func(data), 4.8125)
1352
1353 def test_floats(self):
1354 # Test mean with floats.
1355 data = [17.25, 19.75, 20.0, 21.5, 21.75, 23.25, 25.125, 27.5]
1356 random.shuffle(data)
1357 self.assertEqual(self.func(data), 22.015625)
1358
1359 def test_decimals(self):
Steven D'Apranoa474afd2016-08-09 12:49:01 +10001360 # Test mean with Decimals.
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001361 D = Decimal
1362 data = [D("1.634"), D("2.517"), D("3.912"), D("4.072"), D("5.813")]
1363 random.shuffle(data)
1364 self.assertEqual(self.func(data), D("3.5896"))
1365
1366 def test_fractions(self):
1367 # Test mean with Fractions.
1368 F = Fraction
1369 data = [F(1, 2), F(2, 3), F(3, 4), F(4, 5), F(5, 6), F(6, 7), F(7, 8)]
1370 random.shuffle(data)
1371 self.assertEqual(self.func(data), F(1479, 1960))
1372
1373 def test_inf(self):
1374 # Test mean with infinities.
1375 raw = [1, 3, 5, 7, 9] # Use only ints, to avoid TypeError later.
1376 for kind in (float, Decimal):
1377 for sign in (1, -1):
1378 inf = kind("inf")*sign
1379 data = raw + [inf]
1380 result = self.func(data)
1381 self.assertTrue(math.isinf(result))
1382 self.assertEqual(result, inf)
1383
1384 def test_mismatched_infs(self):
1385 # Test mean with infinities of opposite sign.
1386 data = [2, 4, 6, float('inf'), 1, 3, 5, float('-inf')]
1387 result = self.func(data)
1388 self.assertTrue(math.isnan(result))
1389
1390 def test_nan(self):
1391 # Test mean with NANs.
1392 raw = [1, 3, 5, 7, 9] # Use only ints, to avoid TypeError later.
1393 for kind in (float, Decimal):
1394 inf = kind("nan")
1395 data = raw + [inf]
1396 result = self.func(data)
1397 self.assertTrue(math.isnan(result))
1398
1399 def test_big_data(self):
1400 # Test adding a large constant to every data point.
1401 c = 1e9
1402 data = [3.4, 4.5, 4.9, 6.7, 6.8, 7.2, 8.0, 8.1, 9.4]
1403 expected = self.func(data) + c
1404 assert expected != c
1405 result = self.func([x+c for x in data])
1406 self.assertEqual(result, expected)
1407
1408 def test_doubled_data(self):
1409 # Mean of [a,b,c...z] should be same as for [a,a,b,b,c,c...z,z].
1410 data = [random.uniform(-3, 5) for _ in range(1000)]
1411 expected = self.func(data)
1412 actual = self.func(data*2)
1413 self.assertApproxEqual(actual, expected)
1414
Nick Coghlan4a7668a2014-02-08 23:55:14 +10001415 def test_regression_20561(self):
1416 # Regression test for issue 20561.
1417 # See http://bugs.python.org/issue20561
1418 d = Decimal('1e4')
1419 self.assertEqual(statistics.mean([d]), d)
1420
Steven D'Apranob28c3272015-12-01 19:59:53 +11001421 def test_regression_25177(self):
1422 # Regression test for issue 25177.
1423 # Ensure very big and very small floats don't overflow.
1424 # See http://bugs.python.org/issue25177.
1425 self.assertEqual(statistics.mean(
1426 [8.988465674311579e+307, 8.98846567431158e+307]),
1427 8.98846567431158e+307)
1428 big = 8.98846567431158e+307
1429 tiny = 5e-324
1430 for n in (2, 3, 5, 200):
1431 self.assertEqual(statistics.mean([big]*n), big)
1432 self.assertEqual(statistics.mean([tiny]*n), tiny)
1433
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001434
Steven D'Apranoa474afd2016-08-09 12:49:01 +10001435class TestHarmonicMean(NumericTestCase, AverageMixin, UnivariateTypeMixin):
1436 def setUp(self):
1437 self.func = statistics.harmonic_mean
1438
1439 def prepare_data(self):
1440 # Override mixin method.
1441 values = super().prepare_data()
1442 values.remove(0)
1443 return values
1444
1445 def prepare_values_for_repeated_single_test(self):
1446 # Override mixin method.
1447 return (3.5, 17, 2.5e15, Fraction(61, 67), Decimal('4.125'))
1448
1449 def test_zero(self):
1450 # Test that harmonic mean returns zero when given zero.
1451 values = [1, 0, 2]
1452 self.assertEqual(self.func(values), 0)
1453
1454 def test_negative_error(self):
1455 # Test that harmonic mean raises when given a negative value.
1456 exc = statistics.StatisticsError
1457 for values in ([-1], [1, -2, 3]):
1458 with self.subTest(values=values):
1459 self.assertRaises(exc, self.func, values)
1460
1461 def test_ints(self):
1462 # Test harmonic mean with ints.
1463 data = [2, 4, 4, 8, 16, 16]
1464 random.shuffle(data)
1465 self.assertEqual(self.func(data), 6*4/5)
1466
1467 def test_floats_exact(self):
1468 # Test harmonic mean with some carefully chosen floats.
1469 data = [1/8, 1/4, 1/4, 1/2, 1/2]
1470 random.shuffle(data)
1471 self.assertEqual(self.func(data), 1/4)
1472 self.assertEqual(self.func([0.25, 0.5, 1.0, 1.0]), 0.5)
1473
1474 def test_singleton_lists(self):
1475 # Test that harmonic mean([x]) returns (approximately) x.
1476 for x in range(1, 101):
Steven D'Apranoe7fef522016-08-09 13:19:48 +10001477 self.assertEqual(self.func([x]), x)
Steven D'Apranoa474afd2016-08-09 12:49:01 +10001478
1479 def test_decimals_exact(self):
1480 # Test harmonic mean with some carefully chosen Decimals.
1481 D = Decimal
1482 self.assertEqual(self.func([D(15), D(30), D(60), D(60)]), D(30))
1483 data = [D("0.05"), D("0.10"), D("0.20"), D("0.20")]
1484 random.shuffle(data)
1485 self.assertEqual(self.func(data), D("0.10"))
1486 data = [D("1.68"), D("0.32"), D("5.94"), D("2.75")]
1487 random.shuffle(data)
1488 self.assertEqual(self.func(data), D(66528)/70723)
1489
1490 def test_fractions(self):
1491 # Test harmonic mean with Fractions.
1492 F = Fraction
1493 data = [F(1, 2), F(2, 3), F(3, 4), F(4, 5), F(5, 6), F(6, 7), F(7, 8)]
1494 random.shuffle(data)
1495 self.assertEqual(self.func(data), F(7*420, 4029))
1496
1497 def test_inf(self):
1498 # Test harmonic mean with infinity.
1499 values = [2.0, float('inf'), 1.0]
1500 self.assertEqual(self.func(values), 2.0)
1501
1502 def test_nan(self):
1503 # Test harmonic mean with NANs.
1504 values = [2.0, float('nan'), 1.0]
1505 self.assertTrue(math.isnan(self.func(values)))
1506
1507 def test_multiply_data_points(self):
1508 # Test multiplying every data point by a constant.
1509 c = 111
1510 data = [3.4, 4.5, 4.9, 6.7, 6.8, 7.2, 8.0, 8.1, 9.4]
1511 expected = self.func(data)*c
1512 result = self.func([x*c for x in data])
1513 self.assertEqual(result, expected)
1514
1515 def test_doubled_data(self):
1516 # Harmonic mean of [a,b...z] should be same as for [a,a,b,b...z,z].
1517 data = [random.uniform(1, 5) for _ in range(1000)]
1518 expected = self.func(data)
1519 actual = self.func(data*2)
1520 self.assertApproxEqual(actual, expected)
1521
1522
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001523class TestMedian(NumericTestCase, AverageMixin):
1524 # Common tests for median and all median.* functions.
1525 def setUp(self):
1526 self.func = statistics.median
1527
1528 def prepare_data(self):
1529 """Overload method from UnivariateCommonMixin."""
1530 data = super().prepare_data()
1531 if len(data)%2 != 1:
1532 data.append(2)
1533 return data
1534
1535 def test_even_ints(self):
1536 # Test median with an even number of int data points.
1537 data = [1, 2, 3, 4, 5, 6]
1538 assert len(data)%2 == 0
1539 self.assertEqual(self.func(data), 3.5)
1540
1541 def test_odd_ints(self):
1542 # Test median with an odd number of int data points.
1543 data = [1, 2, 3, 4, 5, 6, 9]
1544 assert len(data)%2 == 1
1545 self.assertEqual(self.func(data), 4)
1546
1547 def test_odd_fractions(self):
1548 # Test median works with an odd number of Fractions.
1549 F = Fraction
1550 data = [F(1, 7), F(2, 7), F(3, 7), F(4, 7), F(5, 7)]
1551 assert len(data)%2 == 1
1552 random.shuffle(data)
1553 self.assertEqual(self.func(data), F(3, 7))
1554
1555 def test_even_fractions(self):
1556 # Test median works with an even number of Fractions.
1557 F = Fraction
1558 data = [F(1, 7), F(2, 7), F(3, 7), F(4, 7), F(5, 7), F(6, 7)]
1559 assert len(data)%2 == 0
1560 random.shuffle(data)
1561 self.assertEqual(self.func(data), F(1, 2))
1562
1563 def test_odd_decimals(self):
1564 # Test median works with an odd number of Decimals.
1565 D = Decimal
1566 data = [D('2.5'), D('3.1'), D('4.2'), D('5.7'), D('5.8')]
1567 assert len(data)%2 == 1
1568 random.shuffle(data)
1569 self.assertEqual(self.func(data), D('4.2'))
1570
1571 def test_even_decimals(self):
1572 # Test median works with an even number of Decimals.
1573 D = Decimal
1574 data = [D('1.2'), D('2.5'), D('3.1'), D('4.2'), D('5.7'), D('5.8')]
1575 assert len(data)%2 == 0
1576 random.shuffle(data)
1577 self.assertEqual(self.func(data), D('3.65'))
1578
1579
1580class TestMedianDataType(NumericTestCase, UnivariateTypeMixin):
1581 # Test conservation of data element type for median.
1582 def setUp(self):
1583 self.func = statistics.median
1584
1585 def prepare_data(self):
1586 data = list(range(15))
1587 assert len(data)%2 == 1
1588 while data == sorted(data):
1589 random.shuffle(data)
1590 return data
1591
1592
1593class TestMedianLow(TestMedian, UnivariateTypeMixin):
1594 def setUp(self):
1595 self.func = statistics.median_low
1596
1597 def test_even_ints(self):
1598 # Test median_low with an even number of ints.
1599 data = [1, 2, 3, 4, 5, 6]
1600 assert len(data)%2 == 0
1601 self.assertEqual(self.func(data), 3)
1602
1603 def test_even_fractions(self):
1604 # Test median_low works with an even number of Fractions.
1605 F = Fraction
1606 data = [F(1, 7), F(2, 7), F(3, 7), F(4, 7), F(5, 7), F(6, 7)]
1607 assert len(data)%2 == 0
1608 random.shuffle(data)
1609 self.assertEqual(self.func(data), F(3, 7))
1610
1611 def test_even_decimals(self):
1612 # Test median_low works with an even number of Decimals.
1613 D = Decimal
1614 data = [D('1.1'), D('2.2'), D('3.3'), D('4.4'), D('5.5'), D('6.6')]
1615 assert len(data)%2 == 0
1616 random.shuffle(data)
1617 self.assertEqual(self.func(data), D('3.3'))
1618
1619
1620class TestMedianHigh(TestMedian, UnivariateTypeMixin):
1621 def setUp(self):
1622 self.func = statistics.median_high
1623
1624 def test_even_ints(self):
1625 # Test median_high with an even number of ints.
1626 data = [1, 2, 3, 4, 5, 6]
1627 assert len(data)%2 == 0
1628 self.assertEqual(self.func(data), 4)
1629
1630 def test_even_fractions(self):
1631 # Test median_high works with an even number of Fractions.
1632 F = Fraction
1633 data = [F(1, 7), F(2, 7), F(3, 7), F(4, 7), F(5, 7), F(6, 7)]
1634 assert len(data)%2 == 0
1635 random.shuffle(data)
1636 self.assertEqual(self.func(data), F(4, 7))
1637
1638 def test_even_decimals(self):
1639 # Test median_high works with an even number of Decimals.
1640 D = Decimal
1641 data = [D('1.1'), D('2.2'), D('3.3'), D('4.4'), D('5.5'), D('6.6')]
1642 assert len(data)%2 == 0
1643 random.shuffle(data)
1644 self.assertEqual(self.func(data), D('4.4'))
1645
1646
1647class TestMedianGrouped(TestMedian):
1648 # Test median_grouped.
1649 # Doesn't conserve data element types, so don't use TestMedianType.
1650 def setUp(self):
1651 self.func = statistics.median_grouped
1652
1653 def test_odd_number_repeated(self):
1654 # Test median.grouped with repeated median values.
1655 data = [12, 13, 14, 14, 14, 15, 15]
1656 assert len(data)%2 == 1
1657 self.assertEqual(self.func(data), 14)
1658 #---
1659 data = [12, 13, 14, 14, 14, 14, 15]
1660 assert len(data)%2 == 1
1661 self.assertEqual(self.func(data), 13.875)
1662 #---
1663 data = [5, 10, 10, 15, 20, 20, 20, 20, 25, 25, 30]
1664 assert len(data)%2 == 1
1665 self.assertEqual(self.func(data, 5), 19.375)
1666 #---
1667 data = [16, 18, 18, 18, 18, 20, 20, 20, 22, 22, 22, 24, 24, 26, 28]
1668 assert len(data)%2 == 1
1669 self.assertApproxEqual(self.func(data, 2), 20.66666667, tol=1e-8)
1670
1671 def test_even_number_repeated(self):
1672 # Test median.grouped with repeated median values.
1673 data = [5, 10, 10, 15, 20, 20, 20, 25, 25, 30]
1674 assert len(data)%2 == 0
1675 self.assertApproxEqual(self.func(data, 5), 19.16666667, tol=1e-8)
1676 #---
1677 data = [2, 3, 4, 4, 4, 5]
1678 assert len(data)%2 == 0
1679 self.assertApproxEqual(self.func(data), 3.83333333, tol=1e-8)
1680 #---
1681 data = [2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6]
1682 assert len(data)%2 == 0
1683 self.assertEqual(self.func(data), 4.5)
1684 #---
1685 data = [3, 4, 4, 4, 5, 5, 5, 5, 6, 6]
1686 assert len(data)%2 == 0
1687 self.assertEqual(self.func(data), 4.75)
1688
1689 def test_repeated_single_value(self):
1690 # Override method from AverageMixin.
1691 # Yet again, failure of median_grouped to conserve the data type
1692 # causes me headaches :-(
1693 for x in (5.3, 68, 4.3e17, Fraction(29, 101), Decimal('32.9714')):
1694 for count in (2, 5, 10, 20):
1695 data = [x]*count
1696 self.assertEqual(self.func(data), float(x))
1697
1698 def test_odd_fractions(self):
1699 # Test median_grouped works with an odd number of Fractions.
1700 F = Fraction
1701 data = [F(5, 4), F(9, 4), F(13, 4), F(13, 4), F(17, 4)]
1702 assert len(data)%2 == 1
1703 random.shuffle(data)
1704 self.assertEqual(self.func(data), 3.0)
1705
1706 def test_even_fractions(self):
1707 # Test median_grouped works with an even number of Fractions.
1708 F = Fraction
1709 data = [F(5, 4), F(9, 4), F(13, 4), F(13, 4), F(17, 4), F(17, 4)]
1710 assert len(data)%2 == 0
1711 random.shuffle(data)
1712 self.assertEqual(self.func(data), 3.25)
1713
1714 def test_odd_decimals(self):
1715 # Test median_grouped works with an odd number of Decimals.
1716 D = Decimal
1717 data = [D('5.5'), D('6.5'), D('6.5'), D('7.5'), D('8.5')]
1718 assert len(data)%2 == 1
1719 random.shuffle(data)
1720 self.assertEqual(self.func(data), 6.75)
1721
1722 def test_even_decimals(self):
1723 # Test median_grouped works with an even number of Decimals.
1724 D = Decimal
1725 data = [D('5.5'), D('5.5'), D('6.5'), D('6.5'), D('7.5'), D('8.5')]
1726 assert len(data)%2 == 0
1727 random.shuffle(data)
1728 self.assertEqual(self.func(data), 6.5)
1729 #---
1730 data = [D('5.5'), D('5.5'), D('6.5'), D('7.5'), D('7.5'), D('8.5')]
1731 assert len(data)%2 == 0
1732 random.shuffle(data)
1733 self.assertEqual(self.func(data), 7.0)
1734
1735 def test_interval(self):
1736 # Test median_grouped with interval argument.
1737 data = [2.25, 2.5, 2.5, 2.75, 2.75, 3.0, 3.0, 3.25, 3.5, 3.75]
1738 self.assertEqual(self.func(data, 0.25), 2.875)
1739 data = [2.25, 2.5, 2.5, 2.75, 2.75, 2.75, 3.0, 3.0, 3.25, 3.5, 3.75]
1740 self.assertApproxEqual(self.func(data, 0.25), 2.83333333, tol=1e-8)
1741 data = [220, 220, 240, 260, 260, 260, 260, 280, 280, 300, 320, 340]
1742 self.assertEqual(self.func(data, 20), 265.0)
1743
Steven D'Aprano8c115a42016-07-08 02:38:45 +10001744 def test_data_type_error(self):
1745 # Test median_grouped with str, bytes data types for data and interval
1746 data = ["", "", ""]
1747 self.assertRaises(TypeError, self.func, data)
1748 #---
1749 data = [b"", b"", b""]
1750 self.assertRaises(TypeError, self.func, data)
1751 #---
1752 data = [1, 2, 3]
1753 interval = ""
1754 self.assertRaises(TypeError, self.func, data, interval)
1755 #---
1756 data = [1, 2, 3]
1757 interval = b""
1758 self.assertRaises(TypeError, self.func, data, interval)
1759
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001760
1761class TestMode(NumericTestCase, AverageMixin, UnivariateTypeMixin):
1762 # Test cases for the discrete version of mode.
1763 def setUp(self):
1764 self.func = statistics.mode
1765
1766 def prepare_data(self):
1767 """Overload method from UnivariateCommonMixin."""
1768 # Make sure test data has exactly one mode.
1769 return [1, 1, 1, 1, 3, 4, 7, 9, 0, 8, 2]
1770
1771 def test_range_data(self):
1772 # Override test from UnivariateCommonMixin.
1773 data = range(20, 50, 3)
Raymond Hettingerfc06a192019-03-12 00:43:27 -07001774 self.assertEqual(self.func(data), 20)
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001775
1776 def test_nominal_data(self):
1777 # Test mode with nominal data.
1778 data = 'abcbdb'
1779 self.assertEqual(self.func(data), 'b')
1780 data = 'fe fi fo fum fi fi'.split()
1781 self.assertEqual(self.func(data), 'fi')
1782
1783 def test_discrete_data(self):
1784 # Test mode with discrete numeric data.
1785 data = list(range(10))
1786 for i in range(10):
1787 d = data + [i]
1788 random.shuffle(d)
1789 self.assertEqual(self.func(d), i)
1790
1791 def test_bimodal_data(self):
1792 # Test mode with bimodal data.
1793 data = [1, 1, 2, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6, 7, 8, 9, 9]
1794 assert data.count(2) == data.count(6) == 4
Raymond Hettingerfc06a192019-03-12 00:43:27 -07001795 # mode() should return 2, the first encounted mode
1796 self.assertEqual(self.func(data), 2)
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001797
Raymond Hettingerfc06a192019-03-12 00:43:27 -07001798 def test_unique_data(self):
1799 # Test mode when data points are all unique.
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001800 data = list(range(10))
Raymond Hettingerfc06a192019-03-12 00:43:27 -07001801 # mode() should return 0, the first encounted mode
1802 self.assertEqual(self.func(data), 0)
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001803
1804 def test_none_data(self):
1805 # Test that mode raises TypeError if given None as data.
1806
1807 # This test is necessary because the implementation of mode uses
1808 # collections.Counter, which accepts None and returns an empty dict.
1809 self.assertRaises(TypeError, self.func, None)
1810
Nick Coghlanbfd68bf2014-02-08 19:44:16 +10001811 def test_counter_data(self):
1812 # Test that a Counter is treated like any other iterable.
1813 data = collections.Counter([1, 1, 1, 2])
1814 # Since the keys of the counter are treated as data points, not the
Raymond Hettingerfc06a192019-03-12 00:43:27 -07001815 # counts, this should return the first mode encountered, 1
1816 self.assertEqual(self.func(data), 1)
1817
1818
1819class TestMultiMode(unittest.TestCase):
1820
1821 def test_basics(self):
1822 multimode = statistics.multimode
1823 self.assertEqual(multimode('aabbbbbbbbcc'), ['b'])
1824 self.assertEqual(multimode('aabbbbccddddeeffffgg'), ['b', 'd', 'f'])
1825 self.assertEqual(multimode(''), [])
1826
Nick Coghlanbfd68bf2014-02-08 19:44:16 +10001827
Raymond Hettinger47d99872019-02-21 15:06:29 -08001828class TestFMean(unittest.TestCase):
1829
1830 def test_basics(self):
1831 fmean = statistics.fmean
1832 D = Decimal
1833 F = Fraction
1834 for data, expected_mean, kind in [
1835 ([3.5, 4.0, 5.25], 4.25, 'floats'),
1836 ([D('3.5'), D('4.0'), D('5.25')], 4.25, 'decimals'),
1837 ([F(7, 2), F(4, 1), F(21, 4)], 4.25, 'fractions'),
1838 ([True, False, True, True, False], 0.60, 'booleans'),
1839 ([3.5, 4, F(21, 4)], 4.25, 'mixed types'),
1840 ((3.5, 4.0, 5.25), 4.25, 'tuple'),
1841 (iter([3.5, 4.0, 5.25]), 4.25, 'iterator'),
1842 ]:
1843 actual_mean = fmean(data)
1844 self.assertIs(type(actual_mean), float, kind)
1845 self.assertEqual(actual_mean, expected_mean, kind)
1846
1847 def test_error_cases(self):
1848 fmean = statistics.fmean
1849 StatisticsError = statistics.StatisticsError
1850 with self.assertRaises(StatisticsError):
1851 fmean([]) # empty input
1852 with self.assertRaises(StatisticsError):
1853 fmean(iter([])) # empty iterator
1854 with self.assertRaises(TypeError):
1855 fmean(None) # non-iterable input
1856 with self.assertRaises(TypeError):
1857 fmean([10, None, 20]) # non-numeric input
1858 with self.assertRaises(TypeError):
1859 fmean() # missing data argument
1860 with self.assertRaises(TypeError):
1861 fmean([10, 20, 60], 70) # too many arguments
1862
1863 def test_special_values(self):
1864 # Rules for special values are inherited from math.fsum()
1865 fmean = statistics.fmean
1866 NaN = float('Nan')
1867 Inf = float('Inf')
1868 self.assertTrue(math.isnan(fmean([10, NaN])), 'nan')
1869 self.assertTrue(math.isnan(fmean([NaN, Inf])), 'nan and infinity')
1870 self.assertTrue(math.isinf(fmean([10, Inf])), 'infinity')
1871 with self.assertRaises(ValueError):
1872 fmean([Inf, -Inf])
Nick Coghlanbfd68bf2014-02-08 19:44:16 +10001873
Larry Hastingsf5e987b2013-10-19 11:50:09 -07001874
1875# === Tests for variances and standard deviations ===
1876
1877class VarianceStdevMixin(UnivariateCommonMixin):
1878 # Mixin class holding common tests for variance and std dev.
1879
1880 # Subclasses should inherit from this before NumericTestClass, in order
1881 # to see the rel attribute below. See testShiftData for an explanation.
1882
1883 rel = 1e-12
1884
1885 def test_single_value(self):
1886 # Deviation of a single value is zero.
1887 for x in (11, 19.8, 4.6e14, Fraction(21, 34), Decimal('8.392')):
1888 self.assertEqual(self.func([x]), 0)
1889
1890 def test_repeated_single_value(self):
1891 # The deviation of a single repeated value is zero.
1892 for x in (7.2, 49, 8.1e15, Fraction(3, 7), Decimal('62.4802')):
1893 for count in (2, 3, 5, 15):
1894 data = [x]*count
1895 self.assertEqual(self.func(data), 0)
1896
1897 def test_domain_error_regression(self):
1898 # Regression test for a domain error exception.
1899 # (Thanks to Geremy Condra.)
1900 data = [0.123456789012345]*10000
1901 # All the items are identical, so variance should be exactly zero.
1902 # We allow some small round-off error, but not much.
1903 result = self.func(data)
1904 self.assertApproxEqual(result, 0.0, tol=5e-17)
1905 self.assertGreaterEqual(result, 0) # A negative result must fail.
1906
1907 def test_shift_data(self):
1908 # Test that shifting the data by a constant amount does not affect
1909 # the variance or stdev. Or at least not much.
1910
1911 # Due to rounding, this test should be considered an ideal. We allow
1912 # some tolerance away from "no change at all" by setting tol and/or rel
1913 # attributes. Subclasses may set tighter or looser error tolerances.
1914 raw = [1.03, 1.27, 1.94, 2.04, 2.58, 3.14, 4.75, 4.98, 5.42, 6.78]
1915 expected = self.func(raw)
1916 # Don't set shift too high, the bigger it is, the more rounding error.
1917 shift = 1e5
1918 data = [x + shift for x in raw]
1919 self.assertApproxEqual(self.func(data), expected)
1920
1921 def test_shift_data_exact(self):
1922 # Like test_shift_data, but result is always exact.
1923 raw = [1, 3, 3, 4, 5, 7, 9, 10, 11, 16]
1924 assert all(x==int(x) for x in raw)
1925 expected = self.func(raw)
1926 shift = 10**9
1927 data = [x + shift for x in raw]
1928 self.assertEqual(self.func(data), expected)
1929
1930 def test_iter_list_same(self):
1931 # Test that iter data and list data give the same result.
1932
1933 # This is an explicit test that iterators and lists are treated the
1934 # same; justification for this test over and above the similar test
1935 # in UnivariateCommonMixin is that an earlier design had variance and
1936 # friends swap between one- and two-pass algorithms, which would
1937 # sometimes give different results.
1938 data = [random.uniform(-3, 8) for _ in range(1000)]
1939 expected = self.func(data)
1940 self.assertEqual(self.func(iter(data)), expected)
1941
1942
1943class TestPVariance(VarianceStdevMixin, NumericTestCase, UnivariateTypeMixin):
1944 # Tests for population variance.
1945 def setUp(self):
1946 self.func = statistics.pvariance
1947
1948 def test_exact_uniform(self):
1949 # Test the variance against an exact result for uniform data.
1950 data = list(range(10000))
1951 random.shuffle(data)
1952 expected = (10000**2 - 1)/12 # Exact value.
1953 self.assertEqual(self.func(data), expected)
1954
1955 def test_ints(self):
1956 # Test population variance with int data.
1957 data = [4, 7, 13, 16]
1958 exact = 22.5
1959 self.assertEqual(self.func(data), exact)
1960
1961 def test_fractions(self):
1962 # Test population variance with Fraction data.
1963 F = Fraction
1964 data = [F(1, 4), F(1, 4), F(3, 4), F(7, 4)]
1965 exact = F(3, 8)
1966 result = self.func(data)
1967 self.assertEqual(result, exact)
1968 self.assertIsInstance(result, Fraction)
1969
1970 def test_decimals(self):
1971 # Test population variance with Decimal data.
1972 D = Decimal
1973 data = [D("12.1"), D("12.2"), D("12.5"), D("12.9")]
1974 exact = D('0.096875')
1975 result = self.func(data)
1976 self.assertEqual(result, exact)
1977 self.assertIsInstance(result, Decimal)
1978
1979
1980class TestVariance(VarianceStdevMixin, NumericTestCase, UnivariateTypeMixin):
1981 # Tests for sample variance.
1982 def setUp(self):
1983 self.func = statistics.variance
1984
1985 def test_single_value(self):
1986 # Override method from VarianceStdevMixin.
1987 for x in (35, 24.7, 8.2e15, Fraction(19, 30), Decimal('4.2084')):
1988 self.assertRaises(statistics.StatisticsError, self.func, [x])
1989
1990 def test_ints(self):
1991 # Test sample variance with int data.
1992 data = [4, 7, 13, 16]
1993 exact = 30
1994 self.assertEqual(self.func(data), exact)
1995
1996 def test_fractions(self):
1997 # Test sample variance with Fraction data.
1998 F = Fraction
1999 data = [F(1, 4), F(1, 4), F(3, 4), F(7, 4)]
2000 exact = F(1, 2)
2001 result = self.func(data)
2002 self.assertEqual(result, exact)
2003 self.assertIsInstance(result, Fraction)
2004
2005 def test_decimals(self):
2006 # Test sample variance with Decimal data.
2007 D = Decimal
2008 data = [D(2), D(2), D(7), D(9)]
2009 exact = 4*D('9.5')/D(3)
2010 result = self.func(data)
2011 self.assertEqual(result, exact)
2012 self.assertIsInstance(result, Decimal)
2013
2014
2015class TestPStdev(VarianceStdevMixin, NumericTestCase):
2016 # Tests for population standard deviation.
2017 def setUp(self):
2018 self.func = statistics.pstdev
2019
2020 def test_compare_to_variance(self):
2021 # Test that stdev is, in fact, the square root of variance.
2022 data = [random.uniform(-17, 24) for _ in range(1000)]
2023 expected = math.sqrt(statistics.pvariance(data))
2024 self.assertEqual(self.func(data), expected)
2025
2026
2027class TestStdev(VarianceStdevMixin, NumericTestCase):
2028 # Tests for sample standard deviation.
2029 def setUp(self):
2030 self.func = statistics.stdev
2031
2032 def test_single_value(self):
2033 # Override method from VarianceStdevMixin.
2034 for x in (81, 203.74, 3.9e14, Fraction(5, 21), Decimal('35.719')):
2035 self.assertRaises(statistics.StatisticsError, self.func, [x])
2036
2037 def test_compare_to_variance(self):
2038 # Test that stdev is, in fact, the square root of variance.
2039 data = [random.uniform(-2, 9) for _ in range(1000)]
2040 expected = math.sqrt(statistics.variance(data))
2041 self.assertEqual(self.func(data), expected)
2042
Raymond Hettinger9013ccf2019-04-23 00:06:35 -07002043
Raymond Hettinger6463ba32019-04-07 09:20:03 -07002044class TestGeometricMean(unittest.TestCase):
2045
2046 def test_basics(self):
2047 geometric_mean = statistics.geometric_mean
2048 self.assertAlmostEqual(geometric_mean([54, 24, 36]), 36.0)
2049 self.assertAlmostEqual(geometric_mean([4.0, 9.0]), 6.0)
2050 self.assertAlmostEqual(geometric_mean([17.625]), 17.625)
2051
2052 random.seed(86753095551212)
2053 for rng in [
2054 range(1, 100),
2055 range(1, 1_000),
2056 range(1, 10_000),
2057 range(500, 10_000, 3),
2058 range(10_000, 500, -3),
2059 [12, 17, 13, 5, 120, 7],
2060 [random.expovariate(50.0) for i in range(1_000)],
2061 [random.lognormvariate(20.0, 3.0) for i in range(2_000)],
2062 [random.triangular(2000, 3000, 2200) for i in range(3_000)],
2063 ]:
2064 gm_decimal = math.prod(map(Decimal, rng)) ** (Decimal(1) / len(rng))
2065 gm_float = geometric_mean(rng)
2066 self.assertTrue(math.isclose(gm_float, float(gm_decimal)))
2067
2068 def test_various_input_types(self):
2069 geometric_mean = statistics.geometric_mean
2070 D = Decimal
2071 F = Fraction
2072 # https://www.wolframalpha.com/input/?i=geometric+mean+3.5,+4.0,+5.25
2073 expected_mean = 4.18886
2074 for data, kind in [
2075 ([3.5, 4.0, 5.25], 'floats'),
2076 ([D('3.5'), D('4.0'), D('5.25')], 'decimals'),
2077 ([F(7, 2), F(4, 1), F(21, 4)], 'fractions'),
2078 ([3.5, 4, F(21, 4)], 'mixed types'),
2079 ((3.5, 4.0, 5.25), 'tuple'),
2080 (iter([3.5, 4.0, 5.25]), 'iterator'),
2081 ]:
2082 actual_mean = geometric_mean(data)
2083 self.assertIs(type(actual_mean), float, kind)
2084 self.assertAlmostEqual(actual_mean, expected_mean, places=5)
2085
2086 def test_big_and_small(self):
2087 geometric_mean = statistics.geometric_mean
2088
2089 # Avoid overflow to infinity
2090 large = 2.0 ** 1000
2091 big_gm = geometric_mean([54.0 * large, 24.0 * large, 36.0 * large])
2092 self.assertTrue(math.isclose(big_gm, 36.0 * large))
2093 self.assertFalse(math.isinf(big_gm))
2094
2095 # Avoid underflow to zero
2096 small = 2.0 ** -1000
2097 small_gm = geometric_mean([54.0 * small, 24.0 * small, 36.0 * small])
2098 self.assertTrue(math.isclose(small_gm, 36.0 * small))
2099 self.assertNotEqual(small_gm, 0.0)
2100
2101 def test_error_cases(self):
2102 geometric_mean = statistics.geometric_mean
2103 StatisticsError = statistics.StatisticsError
2104 with self.assertRaises(StatisticsError):
2105 geometric_mean([]) # empty input
2106 with self.assertRaises(StatisticsError):
2107 geometric_mean([3.5, 0.0, 5.25]) # zero input
2108 with self.assertRaises(StatisticsError):
2109 geometric_mean([3.5, -4.0, 5.25]) # negative input
2110 with self.assertRaises(StatisticsError):
2111 geometric_mean(iter([])) # empty iterator
2112 with self.assertRaises(TypeError):
2113 geometric_mean(None) # non-iterable input
2114 with self.assertRaises(TypeError):
2115 geometric_mean([10, None, 20]) # non-numeric input
2116 with self.assertRaises(TypeError):
2117 geometric_mean() # missing data argument
2118 with self.assertRaises(TypeError):
2119 geometric_mean([10, 20, 60], 70) # too many arguments
2120
2121 def test_special_values(self):
2122 # Rules for special values are inherited from math.fsum()
2123 geometric_mean = statistics.geometric_mean
2124 NaN = float('Nan')
2125 Inf = float('Inf')
2126 self.assertTrue(math.isnan(geometric_mean([10, NaN])), 'nan')
2127 self.assertTrue(math.isnan(geometric_mean([NaN, Inf])), 'nan and infinity')
2128 self.assertTrue(math.isinf(geometric_mean([10, Inf])), 'infinity')
2129 with self.assertRaises(ValueError):
2130 geometric_mean([Inf, -Inf])
2131
Raymond Hettinger9013ccf2019-04-23 00:06:35 -07002132
2133class TestQuantiles(unittest.TestCase):
2134
2135 def test_specific_cases(self):
2136 # Match results computed by hand and cross-checked
2137 # against the PERCENTILE.EXC function in MS Excel.
2138 quantiles = statistics.quantiles
2139 data = [120, 200, 250, 320, 350]
2140 random.shuffle(data)
2141 for n, expected in [
2142 (1, []),
2143 (2, [250.0]),
2144 (3, [200.0, 320.0]),
2145 (4, [160.0, 250.0, 335.0]),
2146 (5, [136.0, 220.0, 292.0, 344.0]),
2147 (6, [120.0, 200.0, 250.0, 320.0, 350.0]),
2148 (8, [100.0, 160.0, 212.5, 250.0, 302.5, 335.0, 357.5]),
2149 (10, [88.0, 136.0, 184.0, 220.0, 250.0, 292.0, 326.0, 344.0, 362.0]),
2150 (12, [80.0, 120.0, 160.0, 200.0, 225.0, 250.0, 285.0, 320.0, 335.0,
2151 350.0, 365.0]),
2152 (15, [72.0, 104.0, 136.0, 168.0, 200.0, 220.0, 240.0, 264.0, 292.0,
2153 320.0, 332.0, 344.0, 356.0, 368.0]),
2154 ]:
2155 self.assertEqual(expected, quantiles(data, n=n))
2156 self.assertEqual(len(quantiles(data, n=n)), n - 1)
Raymond Hettingerdb81ba12019-04-28 21:31:55 -07002157 # Preserve datatype when possible
2158 for datatype in (float, Decimal, Fraction):
2159 result = quantiles(map(datatype, data), n=n)
2160 self.assertTrue(all(type(x) == datatype) for x in result)
2161 self.assertEqual(result, list(map(datatype, expected)))
Raymond Hettingerb0a2c0f2019-04-29 23:47:33 -07002162 # Quantiles should be idempotent
2163 if len(expected) >= 2:
2164 self.assertEqual(quantiles(expected, n=n), expected)
Raymond Hettingere917f2e2019-05-18 10:18:29 -07002165 # Cross-check against method='inclusive' which should give
2166 # the same result after adding in minimum and maximum values
2167 # extrapolated from the two lowest and two highest points.
2168 sdata = sorted(data)
2169 lo = 2 * sdata[0] - sdata[1]
2170 hi = 2 * sdata[-1] - sdata[-2]
2171 padded_data = data + [lo, hi]
2172 self.assertEqual(
2173 quantiles(data, n=n),
2174 quantiles(padded_data, n=n, method='inclusive'),
2175 (n, data),
2176 )
Raymond Hettinger9013ccf2019-04-23 00:06:35 -07002177 # Invariant under tranlation and scaling
2178 def f(x):
2179 return 3.5 * x - 1234.675
2180 exp = list(map(f, expected))
2181 act = quantiles(map(f, data), n=n)
2182 self.assertTrue(all(math.isclose(e, a) for e, a in zip(exp, act)))
2183 # Quartiles of a standard normal distribution
2184 for n, expected in [
2185 (1, []),
2186 (2, [0.0]),
2187 (3, [-0.4307, 0.4307]),
2188 (4 ,[-0.6745, 0.0, 0.6745]),
2189 ]:
2190 actual = quantiles(statistics.NormalDist(), n=n)
2191 self.assertTrue(all(math.isclose(e, a, abs_tol=0.0001)
2192 for e, a in zip(expected, actual)))
Raymond Hettingere917f2e2019-05-18 10:18:29 -07002193 # Q2 agrees with median()
2194 for k in range(2, 60):
2195 data = random.choices(range(100), k=k)
2196 q1, q2, q3 = quantiles(data)
2197 self.assertEqual(q2, statistics.median(data))
Raymond Hettinger9013ccf2019-04-23 00:06:35 -07002198
2199 def test_specific_cases_inclusive(self):
2200 # Match results computed by hand and cross-checked
2201 # against the PERCENTILE.INC function in MS Excel
Xtreak874ad1b2019-05-02 23:50:59 +05302202 # and against the quantile() function in SciPy.
Raymond Hettinger9013ccf2019-04-23 00:06:35 -07002203 quantiles = statistics.quantiles
2204 data = [100, 200, 400, 800]
2205 random.shuffle(data)
2206 for n, expected in [
2207 (1, []),
2208 (2, [300.0]),
2209 (3, [200.0, 400.0]),
2210 (4, [175.0, 300.0, 500.0]),
2211 (5, [160.0, 240.0, 360.0, 560.0]),
2212 (6, [150.0, 200.0, 300.0, 400.0, 600.0]),
2213 (8, [137.5, 175, 225.0, 300.0, 375.0, 500.0,650.0]),
2214 (10, [130.0, 160.0, 190.0, 240.0, 300.0, 360.0, 440.0, 560.0, 680.0]),
2215 (12, [125.0, 150.0, 175.0, 200.0, 250.0, 300.0, 350.0, 400.0,
2216 500.0, 600.0, 700.0]),
2217 (15, [120.0, 140.0, 160.0, 180.0, 200.0, 240.0, 280.0, 320.0, 360.0,
2218 400.0, 480.0, 560.0, 640.0, 720.0]),
2219 ]:
2220 self.assertEqual(expected, quantiles(data, n=n, method="inclusive"))
2221 self.assertEqual(len(quantiles(data, n=n, method="inclusive")), n - 1)
Raymond Hettingerdb81ba12019-04-28 21:31:55 -07002222 # Preserve datatype when possible
2223 for datatype in (float, Decimal, Fraction):
2224 result = quantiles(map(datatype, data), n=n, method="inclusive")
2225 self.assertTrue(all(type(x) == datatype) for x in result)
2226 self.assertEqual(result, list(map(datatype, expected)))
Raymond Hettinger9013ccf2019-04-23 00:06:35 -07002227 # Invariant under tranlation and scaling
2228 def f(x):
2229 return 3.5 * x - 1234.675
2230 exp = list(map(f, expected))
2231 act = quantiles(map(f, data), n=n, method="inclusive")
2232 self.assertTrue(all(math.isclose(e, a) for e, a in zip(exp, act)))
2233 # Quartiles of a standard normal distribution
2234 for n, expected in [
2235 (1, []),
2236 (2, [0.0]),
2237 (3, [-0.4307, 0.4307]),
2238 (4 ,[-0.6745, 0.0, 0.6745]),
2239 ]:
2240 actual = quantiles(statistics.NormalDist(), n=n, method="inclusive")
2241 self.assertTrue(all(math.isclose(e, a, abs_tol=0.0001)
2242 for e, a in zip(expected, actual)))
Raymond Hettingere917f2e2019-05-18 10:18:29 -07002243 # Natural deciles
2244 self.assertEqual(quantiles([0, 100], n=10, method='inclusive'),
2245 [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0])
2246 self.assertEqual(quantiles(range(0, 101), n=10, method='inclusive'),
2247 [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0])
Raymond Hettingerb0a2c0f2019-04-29 23:47:33 -07002248 # Whenever n is smaller than the number of data points, running
2249 # method='inclusive' should give the same result as method='exclusive'
2250 # after the two included extreme points are removed.
2251 data = [random.randrange(10_000) for i in range(501)]
2252 actual = quantiles(data, n=32, method='inclusive')
2253 data.remove(min(data))
2254 data.remove(max(data))
2255 expected = quantiles(data, n=32)
2256 self.assertEqual(expected, actual)
Raymond Hettingere917f2e2019-05-18 10:18:29 -07002257 # Q2 agrees with median()
2258 for k in range(2, 60):
2259 data = random.choices(range(100), k=k)
2260 q1, q2, q3 = quantiles(data, method='inclusive')
2261 self.assertEqual(q2, statistics.median(data))
Raymond Hettinger9013ccf2019-04-23 00:06:35 -07002262
Raymond Hettingerdb81ba12019-04-28 21:31:55 -07002263 def test_equal_inputs(self):
2264 quantiles = statistics.quantiles
2265 for n in range(2, 10):
2266 data = [10.0] * n
2267 self.assertEqual(quantiles(data), [10.0, 10.0, 10.0])
2268 self.assertEqual(quantiles(data, method='inclusive'),
2269 [10.0, 10.0, 10.0])
2270
Raymond Hettinger9013ccf2019-04-23 00:06:35 -07002271 def test_equal_sized_groups(self):
2272 quantiles = statistics.quantiles
2273 total = 10_000
2274 data = [random.expovariate(0.2) for i in range(total)]
2275 while len(set(data)) != total:
2276 data.append(random.expovariate(0.2))
2277 data.sort()
2278
2279 # Cases where the group size exactly divides the total
2280 for n in (1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000):
2281 group_size = total // n
2282 self.assertEqual(
2283 [bisect.bisect(data, q) for q in quantiles(data, n=n)],
2284 list(range(group_size, total, group_size)))
2285
2286 # When the group sizes can't be exactly equal, they should
2287 # differ by no more than one
2288 for n in (13, 19, 59, 109, 211, 571, 1019, 1907, 5261, 9769):
2289 group_sizes = {total // n, total // n + 1}
2290 pos = [bisect.bisect(data, q) for q in quantiles(data, n=n)]
2291 sizes = {q - p for p, q in zip(pos, pos[1:])}
2292 self.assertTrue(sizes <= group_sizes)
2293
2294 def test_error_cases(self):
2295 quantiles = statistics.quantiles
2296 StatisticsError = statistics.StatisticsError
2297 with self.assertRaises(TypeError):
2298 quantiles() # Missing arguments
2299 with self.assertRaises(TypeError):
2300 quantiles([10, 20, 30], 13, n=4) # Too many arguments
2301 with self.assertRaises(TypeError):
2302 quantiles([10, 20, 30], 4) # n is a positional argument
2303 with self.assertRaises(StatisticsError):
2304 quantiles([10, 20, 30], n=0) # n is zero
2305 with self.assertRaises(StatisticsError):
2306 quantiles([10, 20, 30], n=-1) # n is negative
2307 with self.assertRaises(TypeError):
2308 quantiles([10, 20, 30], n=1.5) # n is not an integer
2309 with self.assertRaises(ValueError):
2310 quantiles([10, 20, 30], method='X') # method is unknown
2311 with self.assertRaises(StatisticsError):
2312 quantiles([10], n=4) # not enough data points
2313 with self.assertRaises(TypeError):
2314 quantiles([10, None, 30], n=4) # data is non-numeric
2315
2316
Raymond Hettinger11c79532019-02-23 14:44:07 -08002317class TestNormalDist(unittest.TestCase):
2318
Raymond Hettinger2afb5982019-03-20 13:28:59 -07002319 # General note on precision: The pdf(), cdf(), and overlap() methods
2320 # depend on functions in the math libraries that do not make
2321 # explicit accuracy guarantees. Accordingly, some of the accuracy
2322 # tests below may fail if the underlying math functions are
2323 # inaccurate. There isn't much we can do about this short of
2324 # implementing our own implementations from scratch.
2325
Raymond Hettinger11c79532019-02-23 14:44:07 -08002326 def test_slots(self):
2327 nd = statistics.NormalDist(300, 23)
2328 with self.assertRaises(TypeError):
2329 vars(nd)
Raymond Hettinger02c91f52019-07-21 00:34:47 -07002330 self.assertEqual(tuple(nd.__slots__), ('_mu', '_sigma'))
Raymond Hettinger11c79532019-02-23 14:44:07 -08002331
2332 def test_instantiation_and_attributes(self):
2333 nd = statistics.NormalDist(500, 17)
Raymond Hettinger02c91f52019-07-21 00:34:47 -07002334 self.assertEqual(nd.mean, 500)
2335 self.assertEqual(nd.stdev, 17)
Raymond Hettinger11c79532019-02-23 14:44:07 -08002336 self.assertEqual(nd.variance, 17**2)
2337
2338 # default arguments
2339 nd = statistics.NormalDist()
Raymond Hettinger02c91f52019-07-21 00:34:47 -07002340 self.assertEqual(nd.mean, 0)
2341 self.assertEqual(nd.stdev, 1)
Raymond Hettinger11c79532019-02-23 14:44:07 -08002342 self.assertEqual(nd.variance, 1**2)
2343
2344 # error case: negative sigma
2345 with self.assertRaises(statistics.StatisticsError):
2346 statistics.NormalDist(500, -10)
2347
Raymond Hettinger2afb5982019-03-20 13:28:59 -07002348 # verify that subclass type is honored
2349 class NewNormalDist(statistics.NormalDist):
2350 pass
2351 nnd = NewNormalDist(200, 5)
2352 self.assertEqual(type(nnd), NewNormalDist)
2353
Raymond Hettinger11c79532019-02-23 14:44:07 -08002354 def test_alternative_constructor(self):
2355 NormalDist = statistics.NormalDist
2356 data = [96, 107, 90, 92, 110]
2357 # list input
2358 self.assertEqual(NormalDist.from_samples(data), NormalDist(99, 9))
2359 # tuple input
2360 self.assertEqual(NormalDist.from_samples(tuple(data)), NormalDist(99, 9))
2361 # iterator input
2362 self.assertEqual(NormalDist.from_samples(iter(data)), NormalDist(99, 9))
2363 # error cases
2364 with self.assertRaises(statistics.StatisticsError):
2365 NormalDist.from_samples([]) # empty input
2366 with self.assertRaises(statistics.StatisticsError):
2367 NormalDist.from_samples([10]) # only one input
2368
Raymond Hettinger2afb5982019-03-20 13:28:59 -07002369 # verify that subclass type is honored
2370 class NewNormalDist(NormalDist):
2371 pass
2372 nnd = NewNormalDist.from_samples(data)
2373 self.assertEqual(type(nnd), NewNormalDist)
2374
Raymond Hettinger11c79532019-02-23 14:44:07 -08002375 def test_sample_generation(self):
2376 NormalDist = statistics.NormalDist
2377 mu, sigma = 10_000, 3.0
2378 X = NormalDist(mu, sigma)
2379 n = 1_000
2380 data = X.samples(n)
2381 self.assertEqual(len(data), n)
2382 self.assertEqual(set(map(type, data)), {float})
2383 # mean(data) expected to fall within 8 standard deviations
2384 xbar = statistics.mean(data)
2385 self.assertTrue(mu - sigma*8 <= xbar <= mu + sigma*8)
2386
2387 # verify that seeding makes reproducible sequences
2388 n = 100
2389 data1 = X.samples(n, seed='happiness and joy')
2390 data2 = X.samples(n, seed='trouble and despair')
2391 data3 = X.samples(n, seed='happiness and joy')
2392 data4 = X.samples(n, seed='trouble and despair')
2393 self.assertEqual(data1, data3)
2394 self.assertEqual(data2, data4)
2395 self.assertNotEqual(data1, data2)
2396
Raymond Hettinger11c79532019-02-23 14:44:07 -08002397 def test_pdf(self):
2398 NormalDist = statistics.NormalDist
2399 X = NormalDist(100, 15)
2400 # Verify peak around center
2401 self.assertLess(X.pdf(99), X.pdf(100))
2402 self.assertLess(X.pdf(101), X.pdf(100))
2403 # Test symmetry
Raymond Hettinger18ee50d2019-03-06 02:31:14 -08002404 for i in range(50):
2405 self.assertAlmostEqual(X.pdf(100 - i), X.pdf(100 + i))
Raymond Hettinger11c79532019-02-23 14:44:07 -08002406 # Test vs CDF
2407 dx = 2.0 ** -10
2408 for x in range(90, 111):
2409 est_pdf = (X.cdf(x + dx) - X.cdf(x)) / dx
2410 self.assertAlmostEqual(X.pdf(x), est_pdf, places=4)
Raymond Hettinger18ee50d2019-03-06 02:31:14 -08002411 # Test vs table of known values -- CRC 26th Edition
2412 Z = NormalDist()
2413 for x, px in enumerate([
2414 0.3989, 0.3989, 0.3989, 0.3988, 0.3986,
2415 0.3984, 0.3982, 0.3980, 0.3977, 0.3973,
2416 0.3970, 0.3965, 0.3961, 0.3956, 0.3951,
2417 0.3945, 0.3939, 0.3932, 0.3925, 0.3918,
2418 0.3910, 0.3902, 0.3894, 0.3885, 0.3876,
2419 0.3867, 0.3857, 0.3847, 0.3836, 0.3825,
2420 0.3814, 0.3802, 0.3790, 0.3778, 0.3765,
2421 0.3752, 0.3739, 0.3725, 0.3712, 0.3697,
2422 0.3683, 0.3668, 0.3653, 0.3637, 0.3621,
2423 0.3605, 0.3589, 0.3572, 0.3555, 0.3538,
2424 ]):
2425 self.assertAlmostEqual(Z.pdf(x / 100.0), px, places=4)
Raymond Hettinger1f58f4f2019-03-06 23:23:55 -08002426 self.assertAlmostEqual(Z.pdf(-x / 100.0), px, places=4)
Raymond Hettinger11c79532019-02-23 14:44:07 -08002427 # Error case: variance is zero
2428 Y = NormalDist(100, 0)
2429 with self.assertRaises(statistics.StatisticsError):
2430 Y.pdf(90)
Raymond Hettingeref17fdb2019-02-28 09:16:25 -08002431 # Special values
2432 self.assertEqual(X.pdf(float('-Inf')), 0.0)
2433 self.assertEqual(X.pdf(float('Inf')), 0.0)
2434 self.assertTrue(math.isnan(X.pdf(float('NaN'))))
Raymond Hettinger11c79532019-02-23 14:44:07 -08002435
2436 def test_cdf(self):
2437 NormalDist = statistics.NormalDist
2438 X = NormalDist(100, 15)
2439 cdfs = [X.cdf(x) for x in range(1, 200)]
2440 self.assertEqual(set(map(type, cdfs)), {float})
2441 # Verify montonic
2442 self.assertEqual(cdfs, sorted(cdfs))
Raymond Hettinger2afb5982019-03-20 13:28:59 -07002443 # Verify center (should be exact)
2444 self.assertEqual(X.cdf(100), 0.50)
Raymond Hettinger18ee50d2019-03-06 02:31:14 -08002445 # Check against a table of known values
2446 # https://en.wikipedia.org/wiki/Standard_normal_table#Cumulative
2447 Z = NormalDist()
2448 for z, cum_prob in [
2449 (0.00, 0.50000), (0.01, 0.50399), (0.02, 0.50798),
2450 (0.14, 0.55567), (0.29, 0.61409), (0.33, 0.62930),
2451 (0.54, 0.70540), (0.60, 0.72575), (1.17, 0.87900),
2452 (1.60, 0.94520), (2.05, 0.97982), (2.89, 0.99807),
2453 (3.52, 0.99978), (3.98, 0.99997), (4.07, 0.99998),
2454 ]:
2455 self.assertAlmostEqual(Z.cdf(z), cum_prob, places=5)
2456 self.assertAlmostEqual(Z.cdf(-z), 1.0 - cum_prob, places=5)
Raymond Hettinger11c79532019-02-23 14:44:07 -08002457 # Error case: variance is zero
2458 Y = NormalDist(100, 0)
2459 with self.assertRaises(statistics.StatisticsError):
2460 Y.cdf(90)
Raymond Hettingeref17fdb2019-02-28 09:16:25 -08002461 # Special values
2462 self.assertEqual(X.cdf(float('-Inf')), 0.0)
2463 self.assertEqual(X.cdf(float('Inf')), 1.0)
2464 self.assertTrue(math.isnan(X.cdf(float('NaN'))))
Raymond Hettinger11c79532019-02-23 14:44:07 -08002465
Neil Schemenauer52a48e62019-07-30 11:08:18 -07002466 @support.skip_if_pgo_task
Raymond Hettinger714c60d2019-03-18 20:17:14 -07002467 def test_inv_cdf(self):
2468 NormalDist = statistics.NormalDist
2469
2470 # Center case should be exact.
2471 iq = NormalDist(100, 15)
2472 self.assertEqual(iq.inv_cdf(0.50), iq.mean)
2473
2474 # Test versus a published table of known percentage points.
2475 # See the second table at the bottom of the page here:
2476 # http://people.bath.ac.uk/masss/tables/normaltable.pdf
2477 Z = NormalDist()
2478 pp = {5.0: (0.000, 1.645, 2.576, 3.291, 3.891,
2479 4.417, 4.892, 5.327, 5.731, 6.109),
2480 2.5: (0.674, 1.960, 2.807, 3.481, 4.056,
2481 4.565, 5.026, 5.451, 5.847, 6.219),
2482 1.0: (1.282, 2.326, 3.090, 3.719, 4.265,
2483 4.753, 5.199, 5.612, 5.998, 6.361)}
2484 for base, row in pp.items():
2485 for exp, x in enumerate(row, start=1):
2486 p = base * 10.0 ** (-exp)
2487 self.assertAlmostEqual(-Z.inv_cdf(p), x, places=3)
2488 p = 1.0 - p
2489 self.assertAlmostEqual(Z.inv_cdf(p), x, places=3)
2490
2491 # Match published example for MS Excel
2492 # https://support.office.com/en-us/article/norm-inv-function-54b30935-fee7-493c-bedb-2278a9db7e13
2493 self.assertAlmostEqual(NormalDist(40, 1.5).inv_cdf(0.908789), 42.000002)
2494
2495 # One million equally spaced probabilities
2496 n = 2**20
2497 for p in range(1, n):
2498 p /= n
2499 self.assertAlmostEqual(iq.cdf(iq.inv_cdf(p)), p)
2500
2501 # One hundred ever smaller probabilities to test tails out to
2502 # extreme probabilities: 1 / 2**50 and (2**50-1) / 2 ** 50
2503 for e in range(1, 51):
2504 p = 2.0 ** (-e)
2505 self.assertAlmostEqual(iq.cdf(iq.inv_cdf(p)), p)
2506 p = 1.0 - p
2507 self.assertAlmostEqual(iq.cdf(iq.inv_cdf(p)), p)
2508
Raymond Hettinger2afb5982019-03-20 13:28:59 -07002509 # Now apply cdf() first. Near the tails, the round-trip loses
2510 # precision and is ill-conditioned (small changes in the inputs
2511 # give large changes in the output), so only check to 5 places.
2512 for x in range(200):
2513 self.assertAlmostEqual(iq.inv_cdf(iq.cdf(x)), x, places=5)
Raymond Hettinger714c60d2019-03-18 20:17:14 -07002514
2515 # Error cases:
2516 with self.assertRaises(statistics.StatisticsError):
2517 iq.inv_cdf(0.0) # p is zero
2518 with self.assertRaises(statistics.StatisticsError):
2519 iq.inv_cdf(-0.1) # p under zero
2520 with self.assertRaises(statistics.StatisticsError):
2521 iq.inv_cdf(1.0) # p is one
2522 with self.assertRaises(statistics.StatisticsError):
2523 iq.inv_cdf(1.1) # p over one
2524 with self.assertRaises(statistics.StatisticsError):
Raymond Hettinger02c91f52019-07-21 00:34:47 -07002525 iq = NormalDist(100, 0) # sigma is zero
Raymond Hettinger714c60d2019-03-18 20:17:14 -07002526 iq.inv_cdf(0.5)
2527
Raymond Hettinger2afb5982019-03-20 13:28:59 -07002528 # Special values
2529 self.assertTrue(math.isnan(Z.inv_cdf(float('NaN'))))
2530
Raymond Hettinger318d5372019-03-06 22:59:40 -08002531 def test_overlap(self):
2532 NormalDist = statistics.NormalDist
2533
2534 # Match examples from Imman and Bradley
2535 for X1, X2, published_result in [
2536 (NormalDist(0.0, 2.0), NormalDist(1.0, 2.0), 0.80258),
2537 (NormalDist(0.0, 1.0), NormalDist(1.0, 2.0), 0.60993),
2538 ]:
2539 self.assertAlmostEqual(X1.overlap(X2), published_result, places=4)
2540 self.assertAlmostEqual(X2.overlap(X1), published_result, places=4)
2541
2542 # Check against integration of the PDF
2543 def overlap_numeric(X, Y, *, steps=8_192, z=5):
2544 'Numerical integration cross-check for overlap() '
2545 fsum = math.fsum
Raymond Hettinger02c91f52019-07-21 00:34:47 -07002546 center = (X.mean + Y.mean) / 2.0
2547 width = z * max(X.stdev, Y.stdev)
Raymond Hettinger318d5372019-03-06 22:59:40 -08002548 start = center - width
2549 dx = 2.0 * width / steps
2550 x_arr = [start + i*dx for i in range(steps)]
2551 xp = list(map(X.pdf, x_arr))
2552 yp = list(map(Y.pdf, x_arr))
2553 total = max(fsum(xp), fsum(yp))
2554 return fsum(map(min, xp, yp)) / total
2555
2556 for X1, X2 in [
2557 # Examples from Imman and Bradley
2558 (NormalDist(0.0, 2.0), NormalDist(1.0, 2.0)),
2559 (NormalDist(0.0, 1.0), NormalDist(1.0, 2.0)),
2560 # Example from https://www.rasch.org/rmt/rmt101r.htm
2561 (NormalDist(0.0, 1.0), NormalDist(1.0, 2.0)),
2562 # Gender heights from http://www.usablestats.com/lessons/normal
2563 (NormalDist(70, 4), NormalDist(65, 3.5)),
2564 # Misc cases with equal standard deviations
2565 (NormalDist(100, 15), NormalDist(110, 15)),
2566 (NormalDist(-100, 15), NormalDist(110, 15)),
2567 (NormalDist(-100, 15), NormalDist(-110, 15)),
2568 # Misc cases with unequal standard deviations
Raymond Hettinger2afb5982019-03-20 13:28:59 -07002569 (NormalDist(100, 12), NormalDist(100, 15)),
Raymond Hettinger318d5372019-03-06 22:59:40 -08002570 (NormalDist(100, 12), NormalDist(110, 15)),
2571 (NormalDist(100, 12), NormalDist(150, 15)),
2572 (NormalDist(100, 12), NormalDist(150, 35)),
2573 # Misc cases with small values
2574 (NormalDist(1.000, 0.002), NormalDist(1.001, 0.003)),
2575 (NormalDist(1.000, 0.002), NormalDist(1.006, 0.0003)),
2576 (NormalDist(1.000, 0.002), NormalDist(1.001, 0.099)),
2577 ]:
2578 self.assertAlmostEqual(X1.overlap(X2), overlap_numeric(X1, X2), places=5)
2579 self.assertAlmostEqual(X2.overlap(X1), overlap_numeric(X1, X2), places=5)
2580
2581 # Error cases
2582 X = NormalDist()
2583 with self.assertRaises(TypeError):
2584 X.overlap() # too few arguments
2585 with self.assertRaises(TypeError):
2586 X.overlap(X, X) # too may arguments
2587 with self.assertRaises(TypeError):
2588 X.overlap(None) # right operand not a NormalDist
2589 with self.assertRaises(statistics.StatisticsError):
2590 X.overlap(NormalDist(1, 0)) # right operand sigma is zero
2591 with self.assertRaises(statistics.StatisticsError):
2592 NormalDist(1, 0).overlap(X) # left operand sigma is zero
2593
Raymond Hettinger9e456bc2019-02-24 11:44:55 -08002594 def test_properties(self):
2595 X = statistics.NormalDist(100, 15)
2596 self.assertEqual(X.mean, 100)
2597 self.assertEqual(X.stdev, 15)
2598 self.assertEqual(X.variance, 225)
2599
Raymond Hettinger11c79532019-02-23 14:44:07 -08002600 def test_same_type_addition_and_subtraction(self):
2601 NormalDist = statistics.NormalDist
2602 X = NormalDist(100, 12)
2603 Y = NormalDist(40, 5)
2604 self.assertEqual(X + Y, NormalDist(140, 13)) # __add__
2605 self.assertEqual(X - Y, NormalDist(60, 13)) # __sub__
2606
2607 def test_translation_and_scaling(self):
2608 NormalDist = statistics.NormalDist
2609 X = NormalDist(100, 15)
2610 y = 10
2611 self.assertEqual(+X, NormalDist(100, 15)) # __pos__
2612 self.assertEqual(-X, NormalDist(-100, 15)) # __neg__
2613 self.assertEqual(X + y, NormalDist(110, 15)) # __add__
2614 self.assertEqual(y + X, NormalDist(110, 15)) # __radd__
2615 self.assertEqual(X - y, NormalDist(90, 15)) # __sub__
2616 self.assertEqual(y - X, NormalDist(-90, 15)) # __rsub__
2617 self.assertEqual(X * y, NormalDist(1000, 150)) # __mul__
2618 self.assertEqual(y * X, NormalDist(1000, 150)) # __rmul__
2619 self.assertEqual(X / y, NormalDist(10, 1.5)) # __truediv__
Raymond Hettinger1f58f4f2019-03-06 23:23:55 -08002620 with self.assertRaises(TypeError): # __rtruediv__
Raymond Hettinger11c79532019-02-23 14:44:07 -08002621 y / X
2622
Raymond Hettinger2afb5982019-03-20 13:28:59 -07002623 def test_unary_operations(self):
2624 NormalDist = statistics.NormalDist
2625 X = NormalDist(100, 12)
2626 Y = +X
2627 self.assertIsNot(X, Y)
Raymond Hettinger02c91f52019-07-21 00:34:47 -07002628 self.assertEqual(X.mean, Y.mean)
2629 self.assertEqual(X.stdev, Y.stdev)
Raymond Hettinger2afb5982019-03-20 13:28:59 -07002630 Y = -X
2631 self.assertIsNot(X, Y)
Raymond Hettinger02c91f52019-07-21 00:34:47 -07002632 self.assertEqual(X.mean, -Y.mean)
2633 self.assertEqual(X.stdev, Y.stdev)
Raymond Hettinger2afb5982019-03-20 13:28:59 -07002634
Raymond Hettinger11c79532019-02-23 14:44:07 -08002635 def test_equality(self):
2636 NormalDist = statistics.NormalDist
2637 nd1 = NormalDist()
2638 nd2 = NormalDist(2, 4)
2639 nd3 = NormalDist()
Raymond Hettinger2afb5982019-03-20 13:28:59 -07002640 nd4 = NormalDist(2, 4)
Raymond Hettinger11c79532019-02-23 14:44:07 -08002641 self.assertNotEqual(nd1, nd2)
2642 self.assertEqual(nd1, nd3)
Raymond Hettinger2afb5982019-03-20 13:28:59 -07002643 self.assertEqual(nd2, nd4)
Raymond Hettinger11c79532019-02-23 14:44:07 -08002644
2645 # Test NotImplemented when types are different
2646 class A:
2647 def __eq__(self, other):
2648 return 10
2649 a = A()
2650 self.assertEqual(nd1.__eq__(a), NotImplemented)
2651 self.assertEqual(nd1 == a, 10)
2652 self.assertEqual(a == nd1, 10)
2653
2654 # All subclasses to compare equal giving the same behavior
2655 # as list, tuple, int, float, complex, str, dict, set, etc.
2656 class SizedNormalDist(NormalDist):
2657 def __init__(self, mu, sigma, n):
2658 super().__init__(mu, sigma)
2659 self.n = n
2660 s = SizedNormalDist(100, 15, 57)
2661 nd4 = NormalDist(100, 15)
2662 self.assertEqual(s, nd4)
2663
2664 # Don't allow duck type equality because we wouldn't
2665 # want a lognormal distribution to compare equal
2666 # to a normal distribution with the same parameters
2667 class LognormalDist:
2668 def __init__(self, mu, sigma):
2669 self.mu = mu
2670 self.sigma = sigma
2671 lnd = LognormalDist(100, 15)
2672 nd = NormalDist(100, 15)
2673 self.assertNotEqual(nd, lnd)
2674
2675 def test_pickle_and_copy(self):
2676 nd = statistics.NormalDist(37.5, 5.625)
2677 nd1 = copy.copy(nd)
2678 self.assertEqual(nd, nd1)
2679 nd2 = copy.deepcopy(nd)
2680 self.assertEqual(nd, nd2)
2681 nd3 = pickle.loads(pickle.dumps(nd))
2682 self.assertEqual(nd, nd3)
2683
Raymond Hettinger02c91f52019-07-21 00:34:47 -07002684 def test_hashability(self):
2685 ND = statistics.NormalDist
2686 s = {ND(100, 15), ND(100.0, 15.0), ND(100, 10), ND(95, 15), ND(100, 15)}
2687 self.assertEqual(len(s), 3)
2688
Raymond Hettinger11c79532019-02-23 14:44:07 -08002689 def test_repr(self):
2690 nd = statistics.NormalDist(37.5, 5.625)
2691 self.assertEqual(repr(nd), 'NormalDist(mu=37.5, sigma=5.625)')
2692
Larry Hastingsf5e987b2013-10-19 11:50:09 -07002693
2694# === Run tests ===
2695
2696def load_tests(loader, tests, ignore):
2697 """Used for doctest/unittest integration."""
2698 tests.addTests(doctest.DocTestSuite())
2699 return tests
2700
2701
2702if __name__ == "__main__":
2703 unittest.main()