| 1 |
x = """Test suite for statistics module, including helper NumericTestCase and |
| 2 |
approx_equal function. |
| 3 |
|
| 4 |
""" |
| 5 |
|
| 6 |
import bisect |
| 7 |
import collections |
| 8 |
import collections.abc |
| 9 |
import copy |
| 10 |
import decimal |
| 11 |
import doctest |
| 12 |
import itertools |
| 13 |
import math |
| 14 |
import pickle |
| 15 |
import random |
| 16 |
import sys |
| 17 |
import unittest |
| 18 |
from test import support |
| 19 |
from test.support import import_helper, requires_IEEE_754 |
| 20 |
|
| 21 |
from decimal import Decimal |
| 22 |
from fractions import Fraction |
| 23 |
|
| 24 |
|
| 25 |
# Module to be tested. |
| 26 |
import statistics |
| 27 |
|
| 28 |
|
| 29 |
# === Helper functions and class === |
| 30 |
|
| 31 |
# Test copied from Lib/test/test_math.py |
| 32 |
# detect evidence of double-rounding: fsum is not always correctly |
| 33 |
# rounded on machines that suffer from double rounding. |
| 34 |
x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer |
| 35 |
HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4) |
| 36 |
|
| 37 |
def sign(x): |
| 38 |
"""Return -1.0 for negatives, including -0.0, otherwise +1.0.""" |
| 39 |
return math.copysign(1, x) |
| 40 |
|
| 41 |
def _nan_equal(a, b): |
| 42 |
"""Return True if a and b are both the same kind of NAN. |
| 43 |
|
| 44 |
>>> _nan_equal(Decimal('NAN'), Decimal('NAN')) |
| 45 |
True |
| 46 |
>>> _nan_equal(Decimal('sNAN'), Decimal('sNAN')) |
| 47 |
True |
| 48 |
>>> _nan_equal(Decimal('NAN'), Decimal('sNAN')) |
| 49 |
False |
| 50 |
>>> _nan_equal(Decimal(42), Decimal('NAN')) |
| 51 |
False |
| 52 |
|
| 53 |
>>> _nan_equal(float('NAN'), float('NAN')) |
| 54 |
True |
| 55 |
>>> _nan_equal(float('NAN'), 0.5) |
| 56 |
False |
| 57 |
|
| 58 |
>>> _nan_equal(float('NAN'), Decimal('NAN')) |
| 59 |
False |
| 60 |
|
| 61 |
NAN payloads are not compared. |
| 62 |
""" |
| 63 |
if type(a) is not type(b): |
| 64 |
return False |
| 65 |
if isinstance(a, float): |
| 66 |
return math.isnan(a) and math.isnan(b) |
| 67 |
aexp = a.as_tuple()[2] |
| 68 |
bexp = b.as_tuple()[2] |
| 69 |
return (aexp == bexp) and (aexp in ('n', 'N')) # Both NAN or both sNAN. |
| 70 |
|
| 71 |
|
| 72 |
def _calc_errors(actual, expected): |
| 73 |
"""Return the absolute and relative errors between two numbers. |
| 74 |
|
| 75 |
>>> _calc_errors(100, 75) |
| 76 |
(25, 0.25) |
| 77 |
>>> _calc_errors(100, 100) |
| 78 |
(0, 0.0) |
| 79 |
|
| 80 |
Returns the (absolute error, relative error) between the two arguments. |
| 81 |
""" |
| 82 |
base = max(abs(actual), abs(expected)) |
| 83 |
abs_err = abs(actual - expected) |
| 84 |
rel_err = abs_err/base if base else float('inf') |
| 85 |
return (abs_err, rel_err) |
| 86 |
|
| 87 |
|
| 88 |
def approx_equal(x, y, tol=1e-12, rel=1e-7): |
| 89 |
"""approx_equal(x, y [, tol [, rel]]) => True|False |
| 90 |
|
| 91 |
Return True if numbers x and y are approximately equal, to within some |
| 92 |
margin of error, otherwise return False. Numbers which compare equal |
| 93 |
will also compare approximately equal. |
| 94 |
|
| 95 |
x is approximately equal to y if the difference between them is less than |
| 96 |
an absolute error tol or a relative error rel, whichever is bigger. |
| 97 |
|
| 98 |
If given, both tol and rel must be finite, non-negative numbers. If not |
| 99 |
given, default values are tol=1e-12 and rel=1e-7. |
| 100 |
|
| 101 |
>>> approx_equal(1.2589, 1.2587, tol=0.0003, rel=0) |
| 102 |
True |
| 103 |
>>> approx_equal(1.2589, 1.2587, tol=0.0001, rel=0) |
| 104 |
False |
| 105 |
|
| 106 |
Absolute error is defined as abs(x-y); if that is less than or equal to |
| 107 |
tol, x and y are considered approximately equal. |
| 108 |
|
| 109 |
Relative error is defined as abs((x-y)/x) or abs((x-y)/y), whichever is |
| 110 |
smaller, provided x or y are not zero. If that figure is less than or |
| 111 |
equal to rel, x and y are considered approximately equal. |
| 112 |
|
| 113 |
Complex numbers are not directly supported. If you wish to compare to |
| 114 |
complex numbers, extract their real and imaginary parts and compare them |
| 115 |
individually. |
| 116 |
|
| 117 |
NANs always compare unequal, even with themselves. Infinities compare |
| 118 |
approximately equal if they have the same sign (both positive or both |
| 119 |
negative). Infinities with different signs compare unequal; so do |
| 120 |
comparisons of infinities with finite numbers. |
| 121 |
""" |
| 122 |
if tol < 0 or rel < 0: |
| 123 |
raise ValueError('error tolerances must be non-negative') |
| 124 |
# NANs are never equal to anything, approximately or otherwise. |
| 125 |
if math.isnan(x) or math.isnan(y): |
| 126 |
return False |
| 127 |
# Numbers which compare equal also compare approximately equal. |
| 128 |
if x == y: |
| 129 |
# This includes the case of two infinities with the same sign. |
| 130 |
return True |
| 131 |
if math.isinf(x) or math.isinf(y): |
| 132 |
# This includes the case of two infinities of opposite sign, or |
| 133 |
# one infinity and one finite number. |
| 134 |
return False |
| 135 |
# Two finite numbers. |
| 136 |
actual_error = abs(x - y) |
| 137 |
allowed_error = max(tol, rel*max(abs(x), abs(y))) |
| 138 |
return actual_error <= allowed_error |
| 139 |
|
| 140 |
|
| 141 |
# This class exists only as somewhere to stick a docstring containing |
| 142 |
# doctests. The following docstring and tests were originally in a separate |
| 143 |
# module. Now that it has been merged in here, I need somewhere to hang the. |
| 144 |
# docstring. Ultimately, this class will die, and the information below will |
| 145 |
# either become redundant, or be moved into more appropriate places. |
| 146 |
class _DoNothing: |
| 147 |
""" |
| 148 |
When doing numeric work, especially with floats, exact equality is often |
| 149 |
not what you want. Due to round-off error, it is often a bad idea to try |
| 150 |
to compare floats with equality. Instead the usual procedure is to test |
| 151 |
them with some (hopefully small!) allowance for error. |
| 152 |
|
| 153 |
The ``approx_equal`` function allows you to specify either an absolute |
| 154 |
error tolerance, or a relative error, or both. |
| 155 |
|
| 156 |
Absolute error tolerances are simple, but you need to know the magnitude |
| 157 |
of the quantities being compared: |
| 158 |
|
| 159 |
>>> approx_equal(12.345, 12.346, tol=1e-3) |
| 160 |
True |
| 161 |
>>> approx_equal(12.345e6, 12.346e6, tol=1e-3) # tol is too small. |
| 162 |
False |
| 163 |
|
| 164 |
Relative errors are more suitable when the values you are comparing can |
| 165 |
vary in magnitude: |
| 166 |
|
| 167 |
>>> approx_equal(12.345, 12.346, rel=1e-4) |
| 168 |
True |
| 169 |
>>> approx_equal(12.345e6, 12.346e6, rel=1e-4) |
| 170 |
True |
| 171 |
|
| 172 |
but a naive implementation of relative error testing can run into trouble |
| 173 |
around zero. |
| 174 |
|
| 175 |
If you supply both an absolute tolerance and a relative error, the |
| 176 |
comparison succeeds if either individual test succeeds: |
| 177 |
|
| 178 |
>>> approx_equal(12.345e6, 12.346e6, tol=1e-3, rel=1e-4) |
| 179 |
True |
| 180 |
|
| 181 |
""" |
| 182 |
pass |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
# We prefer this for testing numeric values that may not be exactly equal, |
| 187 |
# and avoid using TestCase.assertAlmostEqual, because it sucks :-) |
| 188 |
|
| 189 |
py_statistics = import_helper.import_fresh_module('statistics', |
| 190 |
blocked=['_statistics']) |
| 191 |
c_statistics = import_helper.import_fresh_module('statistics', |
| 192 |
fresh=['_statistics']) |
| 193 |
|
| 194 |
|
| 195 |
class TestModules(unittest.TestCase): |
| 196 |
func_names = ['_normal_dist_inv_cdf'] |
| 197 |
|
| 198 |
def test_py_functions(self): |
| 199 |
for fname in self.func_names: |
| 200 |
self.assertEqual(getattr(py_statistics, fname).__module__, 'statistics') |
| 201 |
|
| 202 |
@unittest.skipUnless(c_statistics, 'requires _statistics') |
| 203 |
def test_c_functions(self): |
| 204 |
for fname in self.func_names: |
| 205 |
self.assertEqual(getattr(c_statistics, fname).__module__, '_statistics') |
| 206 |
|
| 207 |
|
| 208 |
class NumericTestCase(unittest.TestCase): |
| 209 |
"""Unit test class for numeric work. |
| 210 |
|
| 211 |
This subclasses TestCase. In addition to the standard method |
| 212 |
``TestCase.assertAlmostEqual``, ``assertApproxEqual`` is provided. |
| 213 |
""" |
| 214 |
# By default, we expect exact equality, unless overridden. |
| 215 |
tol = rel = 0 |
| 216 |
|
| 217 |
def assertApproxEqual( |
| 218 |
self, first, second, tol=None, rel=None, msg=None |
| 219 |
): |
| 220 |
"""Test passes if ``first`` and ``second`` are approximately equal. |
| 221 |
|
| 222 |
This test passes if ``first`` and ``second`` are equal to |
| 223 |
within ``tol``, an absolute error, or ``rel``, a relative error. |
| 224 |
|
| 225 |
If either ``tol`` or ``rel`` are None or not given, they default to |
| 226 |
test attributes of the same name (by default, 0). |
| 227 |
|
| 228 |
The objects may be either numbers, or sequences of numbers. Sequences |
| 229 |
are tested element-by-element. |
| 230 |
|
| 231 |
>>> class MyTest(NumericTestCase): |
| 232 |
... def test_number(self): |
| 233 |
... x = 1.0/6 |
| 234 |
... y = sum([x]*6) |
| 235 |
... self.assertApproxEqual(y, 1.0, tol=1e-15) |
| 236 |
... def test_sequence(self): |
| 237 |
... a = [1.001, 1.001e-10, 1.001e10] |
| 238 |
... b = [1.0, 1e-10, 1e10] |
| 239 |
... self.assertApproxEqual(a, b, rel=1e-3) |
| 240 |
... |
| 241 |
>>> import unittest |
| 242 |
>>> from io import StringIO # Suppress test runner output. |
| 243 |
>>> suite = unittest.TestLoader().loadTestsFromTestCase(MyTest) |
| 244 |
>>> unittest.TextTestRunner(stream=StringIO()).run(suite) |
| 245 |
<unittest.runner.TextTestResult run=2 errors=0 failures=0> |
| 246 |
|
| 247 |
""" |
| 248 |
if tol is None: |
| 249 |
tol = self.tol |
| 250 |
if rel is None: |
| 251 |
rel = self.rel |
| 252 |
if ( |
| 253 |
isinstance(first, collections.abc.Sequence) and |
| 254 |
isinstance(second, collections.abc.Sequence) |
| 255 |
): |
| 256 |
check = self._check_approx_seq |
| 257 |
else: |
| 258 |
check = self._check_approx_num |
| 259 |
check(first, second, tol, rel, msg) |
| 260 |
|
| 261 |
def _check_approx_seq(self, first, second, tol, rel, msg): |
| 262 |
if len(first) != len(second): |
| 263 |
standardMsg = ( |
| 264 |
"sequences differ in length: %d items != %d items" |
| 265 |
% (len(first), len(second)) |
| 266 |
) |
| 267 |
msg = self._formatMessage(msg, standardMsg) |
| 268 |
raise self.failureException(msg) |
| 269 |
for i, (a,e) in enumerate(zip(first, second)): |
| 270 |
self._check_approx_num(a, e, tol, rel, msg, i) |
| 271 |
|
| 272 |
def _check_approx_num(self, first, second, tol, rel, msg, idx=None): |
| 273 |
if approx_equal(first, second, tol, rel): |
| 274 |
# Test passes. Return early, we are done. |
| 275 |
return None |
| 276 |
# Otherwise we failed. |
| 277 |
standardMsg = self._make_std_err_msg(first, second, tol, rel, idx) |
| 278 |
msg = self._formatMessage(msg, standardMsg) |
| 279 |
raise self.failureException(msg) |
| 280 |
|
| 281 |
@staticmethod |
| 282 |
def _make_std_err_msg(first, second, tol, rel, idx): |
| 283 |
# Create the standard error message for approx_equal failures. |
| 284 |
assert first != second |
| 285 |
template = ( |
| 286 |
' %r != %r\n' |
| 287 |
' values differ by more than tol=%r and rel=%r\n' |
| 288 |
' -> absolute error = %r\n' |
| 289 |
' -> relative error = %r' |
| 290 |
) |
| 291 |
if idx is not None: |
| 292 |
header = 'numeric sequences first differ at index %d.\n' % idx |
| 293 |
template = header + template |
| 294 |
# Calculate actual errors: |
| 295 |
abs_err, rel_err = _calc_errors(first, second) |
| 296 |
return template % (first, second, tol, rel, abs_err, rel_err) |
| 297 |
|
| 298 |
|
| 299 |
# ======================== |
| 300 |
# === Test the helpers === |
| 301 |
# ======================== |
| 302 |
|
| 303 |
class TestSign(unittest.TestCase): |
| 304 |
"""Test that the helper function sign() works correctly.""" |
| 305 |
def testZeroes(self): |
| 306 |
# Test that signed zeroes report their sign correctly. |
| 307 |
self.assertEqual(sign(0.0), +1) |
| 308 |
self.assertEqual(sign(-0.0), -1) |
| 309 |
|
| 310 |
|
| 311 |
# --- Tests for approx_equal --- |
| 312 |
|
| 313 |
class ApproxEqualSymmetryTest(unittest.TestCase): |
| 314 |
# Test symmetry of approx_equal. |
| 315 |
|
| 316 |
def test_relative_symmetry(self): |
| 317 |
# Check that approx_equal treats relative error symmetrically. |
| 318 |
# (a-b)/a is usually not equal to (a-b)/b. Ensure that this |
| 319 |
# doesn't matter. |
| 320 |
# |
| 321 |
# Note: the reason for this test is that an early version |
| 322 |
# of approx_equal was not symmetric. A relative error test |
| 323 |
# would pass, or fail, depending on which value was passed |
| 324 |
# as the first argument. |
| 325 |
# |
| 326 |
args1 = [2456, 37.8, -12.45, Decimal('2.54'), Fraction(17, 54)] |
| 327 |
args2 = [2459, 37.2, -12.41, Decimal('2.59'), Fraction(15, 54)] |
| 328 |
assert len(args1) == len(args2) |
| 329 |
for a, b in zip(args1, args2): |
| 330 |
self.do_relative_symmetry(a, b) |
| 331 |
|
| 332 |
def do_relative_symmetry(self, a, b): |
| 333 |
a, b = min(a, b), max(a, b) |
| 334 |
assert a < b |
| 335 |
delta = b - a # The absolute difference between the values. |
| 336 |
rel_err1, rel_err2 = abs(delta/a), abs(delta/b) |
| 337 |
# Choose an error margin halfway between the two. |
| 338 |
rel = (rel_err1 + rel_err2)/2 |
| 339 |
# Now see that values a and b compare approx equal regardless of |
| 340 |
# which is given first. |
| 341 |
self.assertTrue(approx_equal(a, b, tol=0, rel=rel)) |
| 342 |
self.assertTrue(approx_equal(b, a, tol=0, rel=rel)) |
| 343 |
|
| 344 |
def test_symmetry(self): |
| 345 |
# Test that approx_equal(a, b) == approx_equal(b, a) |
| 346 |
args = [-23, -2, 5, 107, 93568] |
| 347 |
delta = 2 |
| 348 |
for a in args: |
| 349 |
for type_ in (int, float, Decimal, Fraction): |
| 350 |
x = type_(a)*100 |
| 351 |
y = x + delta |
| 352 |
r = abs(delta/max(x, y)) |
| 353 |
# There are five cases to check: |
| 354 |
# 1) actual error <= tol, <= rel |
| 355 |
self.do_symmetry_test(x, y, tol=delta, rel=r) |
| 356 |
self.do_symmetry_test(x, y, tol=delta+1, rel=2*r) |
| 357 |
# 2) actual error > tol, > rel |
| 358 |
self.do_symmetry_test(x, y, tol=delta-1, rel=r/2) |
| 359 |
# 3) actual error <= tol, > rel |
| 360 |
self.do_symmetry_test(x, y, tol=delta, rel=r/2) |
| 361 |
# 4) actual error > tol, <= rel |
| 362 |
self.do_symmetry_test(x, y, tol=delta-1, rel=r) |
| 363 |
self.do_symmetry_test(x, y, tol=delta-1, rel=2*r) |
| 364 |
# 5) exact equality test |
| 365 |
self.do_symmetry_test(x, x, tol=0, rel=0) |
| 366 |
self.do_symmetry_test(x, y, tol=0, rel=0) |
| 367 |
|
| 368 |
def do_symmetry_test(self, a, b, tol, rel): |
| 369 |
template = "approx_equal comparisons don't match for %r" |
| 370 |
flag1 = approx_equal(a, b, tol, rel) |
| 371 |
flag2 = approx_equal(b, a, tol, rel) |
| 372 |
self.assertEqual(flag1, flag2, template.format((a, b, tol, rel))) |
| 373 |
|
| 374 |
|
| 375 |
class ApproxEqualExactTest(unittest.TestCase): |
| 376 |
# Test the approx_equal function with exactly equal values. |
| 377 |
# Equal values should compare as approximately equal. |
| 378 |
# Test cases for exactly equal values, which should compare approx |
| 379 |
# equal regardless of the error tolerances given. |
| 380 |
|
| 381 |
def do_exactly_equal_test(self, x, tol, rel): |
| 382 |
result = approx_equal(x, x, tol=tol, rel=rel) |
| 383 |
self.assertTrue(result, 'equality failure for x=%r' % x) |
| 384 |
result = approx_equal(-x, -x, tol=tol, rel=rel) |
| 385 |
self.assertTrue(result, 'equality failure for x=%r' % -x) |
| 386 |
|
| 387 |
def test_exactly_equal_ints(self): |
| 388 |
# Test that equal int values are exactly equal. |
| 389 |
for n in [42, 19740, 14974, 230, 1795, 700245, 36587]: |
| 390 |
self.do_exactly_equal_test(n, 0, 0) |
| 391 |
|
| 392 |
def test_exactly_equal_floats(self): |
| 393 |
# Test that equal float values are exactly equal. |
| 394 |
for x in [0.42, 1.9740, 1497.4, 23.0, 179.5, 70.0245, 36.587]: |
| 395 |
self.do_exactly_equal_test(x, 0, 0) |
| 396 |
|
| 397 |
def test_exactly_equal_fractions(self): |
| 398 |
# Test that equal Fraction values are exactly equal. |
| 399 |
F = Fraction |
| 400 |
for f in [F(1, 2), F(0), F(5, 3), F(9, 7), F(35, 36), F(3, 7)]: |
| 401 |
self.do_exactly_equal_test(f, 0, 0) |
| 402 |
|
| 403 |
def test_exactly_equal_decimals(self): |
| 404 |
# Test that equal Decimal values are exactly equal. |
| 405 |
D = Decimal |
| 406 |
for d in map(D, "8.2 31.274 912.04 16.745 1.2047".split()): |
| 407 |
self.do_exactly_equal_test(d, 0, 0) |
| 408 |
|
| 409 |
def test_exactly_equal_absolute(self): |
| 410 |
# Test that equal values are exactly equal with an absolute error. |
| 411 |
for n in [16, 1013, 1372, 1198, 971, 4]: |
| 412 |
# Test as ints. |
| 413 |
self.do_exactly_equal_test(n, 0.01, 0) |
| 414 |
# Test as floats. |
| 415 |
self.do_exactly_equal_test(n/10, 0.01, 0) |
| 416 |
# Test as Fractions. |
| 417 |
f = Fraction(n, 1234) |
| 418 |
self.do_exactly_equal_test(f, 0.01, 0) |
| 419 |
|
| 420 |
def test_exactly_equal_absolute_decimals(self): |
| 421 |
# Test equal Decimal values are exactly equal with an absolute error. |
| 422 |
self.do_exactly_equal_test(Decimal("3.571"), Decimal("0.01"), 0) |
| 423 |
self.do_exactly_equal_test(-Decimal("81.3971"), Decimal("0.01"), 0) |
| 424 |
|
| 425 |
def test_exactly_equal_relative(self): |
| 426 |
# Test that equal values are exactly equal with a relative error. |
| 427 |
for x in [8347, 101.3, -7910.28, Fraction(5, 21)]: |
| 428 |
self.do_exactly_equal_test(x, 0, 0.01) |
| 429 |
self.do_exactly_equal_test(Decimal("11.68"), 0, Decimal("0.01")) |
| 430 |
|
| 431 |
def test_exactly_equal_both(self): |
| 432 |
# Test that equal values are equal when both tol and rel are given. |
| 433 |
for x in [41017, 16.742, -813.02, Fraction(3, 8)]: |
| 434 |
self.do_exactly_equal_test(x, 0.1, 0.01) |
| 435 |
D = Decimal |
| 436 |
self.do_exactly_equal_test(D("7.2"), D("0.1"), D("0.01")) |
| 437 |
|
| 438 |
|
| 439 |
class ApproxEqualUnequalTest(unittest.TestCase): |
| 440 |
# Unequal values should compare unequal with zero error tolerances. |
| 441 |
# Test cases for unequal values, with exact equality test. |
| 442 |
|
| 443 |
def do_exactly_unequal_test(self, x): |
| 444 |
for a in (x, -x): |
| 445 |
result = approx_equal(a, a+1, tol=0, rel=0) |
| 446 |
self.assertFalse(result, 'inequality failure for x=%r' % a) |
| 447 |
|
| 448 |
def test_exactly_unequal_ints(self): |
| 449 |
# Test unequal int values are unequal with zero error tolerance. |
| 450 |
for n in [951, 572305, 478, 917, 17240]: |
| 451 |
self.do_exactly_unequal_test(n) |
| 452 |
|
| 453 |
def test_exactly_unequal_floats(self): |
| 454 |
# Test unequal float values are unequal with zero error tolerance. |
| 455 |
for x in [9.51, 5723.05, 47.8, 9.17, 17.24]: |
| 456 |
self.do_exactly_unequal_test(x) |
| 457 |
|
| 458 |
def test_exactly_unequal_fractions(self): |
| 459 |
# Test that unequal Fractions are unequal with zero error tolerance. |
| 460 |
F = Fraction |
| 461 |
for f in [F(1, 5), F(7, 9), F(12, 11), F(101, 99023)]: |
| 462 |
self.do_exactly_unequal_test(f) |
| 463 |
|
| 464 |
def test_exactly_unequal_decimals(self): |
| 465 |
# Test that unequal Decimals are unequal with zero error tolerance. |
| 466 |
for d in map(Decimal, "3.1415 298.12 3.47 18.996 0.00245".split()): |
| 467 |
self.do_exactly_unequal_test(d) |
| 468 |
|
| 469 |
|
| 470 |
class ApproxEqualInexactTest(unittest.TestCase): |
| 471 |
# Inexact test cases for approx_error. |
| 472 |
# Test cases when comparing two values that are not exactly equal. |
| 473 |
|
| 474 |
# === Absolute error tests === |
| 475 |
|
| 476 |
def do_approx_equal_abs_test(self, x, delta): |
| 477 |
template = "Test failure for x={!r}, y={!r}" |
| 478 |
for y in (x + delta, x - delta): |
| 479 |
msg = template.format(x, y) |
| 480 |
self.assertTrue(approx_equal(x, y, tol=2*delta, rel=0), msg) |
| 481 |
self.assertFalse(approx_equal(x, y, tol=delta/2, rel=0), msg) |
| 482 |
|
| 483 |
def test_approx_equal_absolute_ints(self): |
| 484 |
# Test approximate equality of ints with an absolute error. |
| 485 |
for n in [-10737, -1975, -7, -2, 0, 1, 9, 37, 423, 9874, 23789110]: |
| 486 |
self.do_approx_equal_abs_test(n, 10) |
| 487 |
self.do_approx_equal_abs_test(n, 2) |
| 488 |
|
| 489 |
def test_approx_equal_absolute_floats(self): |
| 490 |
# Test approximate equality of floats with an absolute error. |
| 491 |
for x in [-284.126, -97.1, -3.4, -2.15, 0.5, 1.0, 7.8, 4.23, 3817.4]: |
| 492 |
self.do_approx_equal_abs_test(x, 1.5) |
| 493 |
self.do_approx_equal_abs_test(x, 0.01) |
| 494 |
self.do_approx_equal_abs_test(x, 0.0001) |
| 495 |
|
| 496 |
def test_approx_equal_absolute_fractions(self): |
| 497 |
# Test approximate equality of Fractions with an absolute error. |
| 498 |
delta = Fraction(1, 29) |
| 499 |
numerators = [-84, -15, -2, -1, 0, 1, 5, 17, 23, 34, 71] |
| 500 |
for f in (Fraction(n, 29) for n in numerators): |
| 501 |
self.do_approx_equal_abs_test(f, delta) |
| 502 |
self.do_approx_equal_abs_test(f, float(delta)) |
| 503 |
|
| 504 |
def test_approx_equal_absolute_decimals(self): |
| 505 |
# Test approximate equality of Decimals with an absolute error. |
| 506 |
delta = Decimal("0.01") |
| 507 |
for d in map(Decimal, "1.0 3.5 36.08 61.79 7912.3648".split()): |
| 508 |
self.do_approx_equal_abs_test(d, delta) |
| 509 |
self.do_approx_equal_abs_test(-d, delta) |
| 510 |
|
| 511 |
def test_cross_zero(self): |
| 512 |
# Test for the case of the two values having opposite signs. |
| 513 |
self.assertTrue(approx_equal(1e-5, -1e-5, tol=1e-4, rel=0)) |
| 514 |
|
| 515 |
# === Relative error tests === |
| 516 |
|
| 517 |
def do_approx_equal_rel_test(self, x, delta): |
| 518 |
template = "Test failure for x={!r}, y={!r}" |
| 519 |
for y in (x*(1+delta), x*(1-delta)): |
| 520 |
msg = template.format(x, y) |
| 521 |
self.assertTrue(approx_equal(x, y, tol=0, rel=2*delta), msg) |
| 522 |
self.assertFalse(approx_equal(x, y, tol=0, rel=delta/2), msg) |
| 523 |
|
| 524 |
def test_approx_equal_relative_ints(self): |
| 525 |
# Test approximate equality of ints with a relative error. |
| 526 |
self.assertTrue(approx_equal(64, 47, tol=0, rel=0.36)) |
| 527 |
self.assertTrue(approx_equal(64, 47, tol=0, rel=0.37)) |
| 528 |
# --- |
| 529 |
self.assertTrue(approx_equal(449, 512, tol=0, rel=0.125)) |
| 530 |
self.assertTrue(approx_equal(448, 512, tol=0, rel=0.125)) |
| 531 |
self.assertFalse(approx_equal(447, 512, tol=0, rel=0.125)) |
| 532 |
|
| 533 |
def test_approx_equal_relative_floats(self): |
| 534 |
# Test approximate equality of floats with a relative error. |
| 535 |
for x in [-178.34, -0.1, 0.1, 1.0, 36.97, 2847.136, 9145.074]: |
| 536 |
self.do_approx_equal_rel_test(x, 0.02) |
| 537 |
self.do_approx_equal_rel_test(x, 0.0001) |
| 538 |
|
| 539 |
def test_approx_equal_relative_fractions(self): |
| 540 |
# Test approximate equality of Fractions with a relative error. |
| 541 |
F = Fraction |
| 542 |
delta = Fraction(3, 8) |
| 543 |
for f in [F(3, 84), F(17, 30), F(49, 50), F(92, 85)]: |
| 544 |
for d in (delta, float(delta)): |
| 545 |
self.do_approx_equal_rel_test(f, d) |
| 546 |
self.do_approx_equal_rel_test(-f, d) |
| 547 |
|
| 548 |
def test_approx_equal_relative_decimals(self): |
| 549 |
# Test approximate equality of Decimals with a relative error. |
| 550 |
for d in map(Decimal, "0.02 1.0 5.7 13.67 94.138 91027.9321".split()): |
| 551 |
self.do_approx_equal_rel_test(d, Decimal("0.001")) |
| 552 |
self.do_approx_equal_rel_test(-d, Decimal("0.05")) |
| 553 |
|
| 554 |
# === Both absolute and relative error tests === |
| 555 |
|
| 556 |
# There are four cases to consider: |
| 557 |
# 1) actual error <= both absolute and relative error |
| 558 |
# 2) actual error <= absolute error but > relative error |
| 559 |
# 3) actual error <= relative error but > absolute error |
| 560 |
# 4) actual error > both absolute and relative error |
| 561 |
|
| 562 |
def do_check_both(self, a, b, tol, rel, tol_flag, rel_flag): |
| 563 |
check = self.assertTrue if tol_flag else self.assertFalse |
| 564 |
check(approx_equal(a, b, tol=tol, rel=0)) |
| 565 |
check = self.assertTrue if rel_flag else self.assertFalse |
| 566 |
check(approx_equal(a, b, tol=0, rel=rel)) |
| 567 |
check = self.assertTrue if (tol_flag or rel_flag) else self.assertFalse |
| 568 |
check(approx_equal(a, b, tol=tol, rel=rel)) |
| 569 |
|
| 570 |
def test_approx_equal_both1(self): |
| 571 |
# Test actual error <= both absolute and relative error. |
| 572 |
self.do_check_both(7.955, 7.952, 0.004, 3.8e-4, True, True) |
| 573 |
self.do_check_both(-7.387, -7.386, 0.002, 0.0002, True, True) |
| 574 |
|
| 575 |
def test_approx_equal_both2(self): |
| 576 |
# Test actual error <= absolute error but > relative error. |
| 577 |
self.do_check_both(7.955, 7.952, 0.004, 3.7e-4, True, False) |
| 578 |
|
| 579 |
def test_approx_equal_both3(self): |
| 580 |
# Test actual error <= relative error but > absolute error. |
| 581 |
self.do_check_both(7.955, 7.952, 0.001, 3.8e-4, False, True) |
| 582 |
|
| 583 |
def test_approx_equal_both4(self): |
| 584 |
# Test actual error > both absolute and relative error. |
| 585 |
self.do_check_both(2.78, 2.75, 0.01, 0.001, False, False) |
| 586 |
self.do_check_both(971.44, 971.47, 0.02, 3e-5, False, False) |
| 587 |
|
| 588 |
|
| 589 |
class ApproxEqualSpecialsTest(unittest.TestCase): |
| 590 |
# Test approx_equal with NANs and INFs and zeroes. |
| 591 |
|
| 592 |
def test_inf(self): |
| 593 |
for type_ in (float, Decimal): |
| 594 |
inf = type_('inf') |
| 595 |
self.assertTrue(approx_equal(inf, inf)) |
| 596 |
self.assertTrue(approx_equal(inf, inf, 0, 0)) |
| 597 |
self.assertTrue(approx_equal(inf, inf, 1, 0.01)) |
| 598 |
self.assertTrue(approx_equal(-inf, -inf)) |
| 599 |
self.assertFalse(approx_equal(inf, -inf)) |
| 600 |
self.assertFalse(approx_equal(inf, 1000)) |
| 601 |
|
| 602 |
def test_nan(self): |
| 603 |
for type_ in (float, Decimal): |
| 604 |
nan = type_('nan') |
| 605 |
for other in (nan, type_('inf'), 1000): |
| 606 |
self.assertFalse(approx_equal(nan, other)) |
| 607 |
|
| 608 |
def test_float_zeroes(self): |
| 609 |
nzero = math.copysign(0.0, -1) |
| 610 |
self.assertTrue(approx_equal(nzero, 0.0, tol=0.1, rel=0.1)) |
| 611 |
|
| 612 |
def test_decimal_zeroes(self): |
| 613 |
nzero = Decimal("-0.0") |
| 614 |
self.assertTrue(approx_equal(nzero, Decimal(0), tol=0.1, rel=0.1)) |
| 615 |
|
| 616 |
|
| 617 |
class TestApproxEqualErrors(unittest.TestCase): |
| 618 |
# Test error conditions of approx_equal. |
| 619 |
|
| 620 |
def test_bad_tol(self): |
| 621 |
# Test negative tol raises. |
| 622 |
self.assertRaises(ValueError, approx_equal, 100, 100, -1, 0.1) |
| 623 |
|
| 624 |
def test_bad_rel(self): |
| 625 |
# Test negative rel raises. |
| 626 |
self.assertRaises(ValueError, approx_equal, 100, 100, 1, -0.1) |
| 627 |
|
| 628 |
|
| 629 |
# --- Tests for NumericTestCase --- |
| 630 |
|
| 631 |
# The formatting routine that generates the error messages is complex enough |
| 632 |
# that it too needs testing. |
| 633 |
|
| 634 |
class TestNumericTestCase(unittest.TestCase): |
| 635 |
# The exact wording of NumericTestCase error messages is *not* guaranteed, |
| 636 |
# but we need to give them some sort of test to ensure that they are |
| 637 |
# generated correctly. As a compromise, we look for specific substrings |
| 638 |
# that are expected to be found even if the overall error message changes. |
| 639 |
|
| 640 |
def do_test(self, args): |
| 641 |
actual_msg = NumericTestCase._make_std_err_msg(*args) |
| 642 |
expected = self.generate_substrings(*args) |
| 643 |
for substring in expected: |
| 644 |
self.assertIn(substring, actual_msg) |
| 645 |
|
| 646 |
def test_numerictestcase_is_testcase(self): |
| 647 |
# Ensure that NumericTestCase actually is a TestCase. |
| 648 |
self.assertIsSubclass(NumericTestCase, unittest.TestCase) |
| 649 |
|
| 650 |
def test_error_msg_numeric(self): |
| 651 |
# Test the error message generated for numeric comparisons. |
| 652 |
args = (2.5, 4.0, 0.5, 0.25, None) |
| 653 |
self.do_test(args) |
| 654 |
|
| 655 |
def test_error_msg_sequence(self): |
| 656 |
# Test the error message generated for sequence comparisons. |
| 657 |
args = (3.75, 8.25, 1.25, 0.5, 7) |
| 658 |
self.do_test(args) |
| 659 |
|
| 660 |
def generate_substrings(self, first, second, tol, rel, idx): |
| 661 |
"""Return substrings we expect to see in error messages.""" |
| 662 |
abs_err, rel_err = _calc_errors(first, second) |
| 663 |
substrings = [ |
| 664 |
'tol=%r' % tol, |
| 665 |
'rel=%r' % rel, |
| 666 |
'absolute error = %r' % abs_err, |
| 667 |
'relative error = %r' % rel_err, |
| 668 |
] |
| 669 |
if idx is not None: |
| 670 |
substrings.append('differ at index %d' % idx) |
| 671 |
return substrings |
| 672 |
|
| 673 |
|
| 674 |
# ======================================= |
| 675 |
# === Tests for the statistics module === |
| 676 |
# ======================================= |
| 677 |
|
| 678 |
|
| 679 |
class GlobalsTest(unittest.TestCase): |
| 680 |
module = statistics |
| 681 |
expected_metadata = ["__doc__", "__all__"] |
| 682 |
|
| 683 |
def test_meta(self): |
| 684 |
# Test for the existence of metadata. |
| 685 |
for meta in self.expected_metadata: |
| 686 |
self.assertHasAttr(self.module, meta) |
| 687 |
|
| 688 |
def test_check_all(self): |
| 689 |
# Check everything in __all__ exists and is public. |
| 690 |
module = self.module |
| 691 |
for name in module.__all__: |
| 692 |
# No private names in __all__: |
| 693 |
self.assertNotStartsWith(name, "_", |
| 694 |
'private name "%s" in __all__' % name) |
| 695 |
# And anything in __all__ must exist: |
| 696 |
self.assertHasAttr(module, name) |
| 697 |
|
| 698 |
|
| 699 |
class StatisticsErrorTest(unittest.TestCase): |
| 700 |
def test_has_exception(self): |
| 701 |
self.assertHasAttr(statistics, 'StatisticsError') |
| 702 |
self.assertIsSubclass(statistics.StatisticsError, ValueError) |
| 703 |
|
| 704 |
|
| 705 |
# === Tests for private utility functions === |
| 706 |
|
| 707 |
class ExactRatioTest(unittest.TestCase): |
| 708 |
# Test _exact_ratio utility. |
| 709 |
|
| 710 |
def test_int(self): |
| 711 |
for i in (-20, -3, 0, 5, 99, 10**20): |
| 712 |
self.assertEqual(statistics._exact_ratio(i), (i, 1)) |
| 713 |
|
| 714 |
def test_fraction(self): |
| 715 |
numerators = (-5, 1, 12, 38) |
| 716 |
for n in numerators: |
| 717 |
f = Fraction(n, 37) |
| 718 |
self.assertEqual(statistics._exact_ratio(f), (n, 37)) |
| 719 |
|
| 720 |
def test_float(self): |
| 721 |
self.assertEqual(statistics._exact_ratio(0.125), (1, 8)) |
| 722 |
self.assertEqual(statistics._exact_ratio(1.125), (9, 8)) |
| 723 |
data = [random.uniform(-100, 100) for _ in range(100)] |
| 724 |
for x in data: |
| 725 |
num, den = statistics._exact_ratio(x) |
| 726 |
self.assertEqual(x, num/den) |
| 727 |
|
| 728 |
def test_decimal(self): |
| 729 |
D = Decimal |
| 730 |
_exact_ratio = statistics._exact_ratio |
| 731 |
self.assertEqual(_exact_ratio(D("0.125")), (1, 8)) |
| 732 |
self.assertEqual(_exact_ratio(D("12.345")), (2469, 200)) |
| 733 |
self.assertEqual(_exact_ratio(D("-1.98")), (-99, 50)) |
| 734 |
|
| 735 |
def test_inf(self): |
| 736 |
INF = float("INF") |
| 737 |
class MyFloat(float): |
| 738 |
pass |
| 739 |
class MyDecimal(Decimal): |
| 740 |
pass |
| 741 |
for inf in (INF, -INF): |
| 742 |
for type_ in (float, MyFloat, Decimal, MyDecimal): |
| 743 |
x = type_(inf) |
| 744 |
ratio = statistics._exact_ratio(x) |
| 745 |
self.assertEqual(ratio, (x, None)) |
| 746 |
self.assertEqual(type(ratio[0]), type_) |
| 747 |
self.assertTrue(math.isinf(ratio[0])) |
| 748 |
|
| 749 |
def test_float_nan(self): |
| 750 |
NAN = float("NAN") |
| 751 |
class MyFloat(float): |
| 752 |
pass |
| 753 |
for nan in (NAN, MyFloat(NAN)): |
| 754 |
ratio = statistics._exact_ratio(nan) |
| 755 |
self.assertTrue(math.isnan(ratio[0])) |
| 756 |
self.assertIs(ratio[1], None) |
| 757 |
self.assertEqual(type(ratio[0]), type(nan)) |
| 758 |
|
| 759 |
def test_decimal_nan(self): |
| 760 |
NAN = Decimal("NAN") |
| 761 |
sNAN = Decimal("sNAN") |
| 762 |
class MyDecimal(Decimal): |
| 763 |
pass |
| 764 |
for nan in (NAN, MyDecimal(NAN), sNAN, MyDecimal(sNAN)): |
| 765 |
ratio = statistics._exact_ratio(nan) |
| 766 |
self.assertTrue(_nan_equal(ratio[0], nan)) |
| 767 |
self.assertIs(ratio[1], None) |
| 768 |
self.assertEqual(type(ratio[0]), type(nan)) |
| 769 |
|
| 770 |
|
| 771 |
class DecimalToRatioTest(unittest.TestCase): |
| 772 |
# Test _exact_ratio private function. |
| 773 |
|
| 774 |
def test_infinity(self): |
| 775 |
# Test that INFs are handled correctly. |
| 776 |
inf = Decimal('INF') |
| 777 |
self.assertEqual(statistics._exact_ratio(inf), (inf, None)) |
| 778 |
self.assertEqual(statistics._exact_ratio(-inf), (-inf, None)) |
| 779 |
|
| 780 |
def test_nan(self): |
| 781 |
# Test that NANs are handled correctly. |
| 782 |
for nan in (Decimal('NAN'), Decimal('sNAN')): |
| 783 |
num, den = statistics._exact_ratio(nan) |
| 784 |
# Because NANs always compare non-equal, we cannot use assertEqual. |
| 785 |
# Nor can we use an identity test, as we don't guarantee anything |
| 786 |
# about the object identity. |
| 787 |
self.assertTrue(_nan_equal(num, nan)) |
| 788 |
self.assertIs(den, None) |
| 789 |
|
| 790 |
def test_sign(self): |
| 791 |
# Test sign is calculated correctly. |
| 792 |
numbers = [Decimal("9.8765e12"), Decimal("9.8765e-12")] |
| 793 |
for d in numbers: |
| 794 |
# First test positive decimals. |
| 795 |
assert d > 0 |
| 796 |
num, den = statistics._exact_ratio(d) |
| 797 |
self.assertGreaterEqual(num, 0) |
| 798 |
self.assertGreater(den, 0) |
| 799 |
# Then test negative decimals. |
| 800 |
num, den = statistics._exact_ratio(-d) |
| 801 |
self.assertLessEqual(num, 0) |
| 802 |
self.assertGreater(den, 0) |
| 803 |
|
| 804 |
def test_negative_exponent(self): |
| 805 |
# Test result when the exponent is negative. |
| 806 |
t = statistics._exact_ratio(Decimal("0.1234")) |
| 807 |
self.assertEqual(t, (617, 5000)) |
| 808 |
|
| 809 |
def test_positive_exponent(self): |
| 810 |
# Test results when the exponent is positive. |
| 811 |
t = statistics._exact_ratio(Decimal("1.234e7")) |
| 812 |
self.assertEqual(t, (12340000, 1)) |
| 813 |
|
| 814 |
def test_regression_20536(self): |
| 815 |
# Regression test for issue 20536. |
| 816 |
# See http://bugs.python.org/issue20536 |
| 817 |
t = statistics._exact_ratio(Decimal("1e2")) |
| 818 |
self.assertEqual(t, (100, 1)) |
| 819 |
t = statistics._exact_ratio(Decimal("1.47e5")) |
| 820 |
self.assertEqual(t, (147000, 1)) |
| 821 |
|
| 822 |
|
| 823 |
class IsFiniteTest(unittest.TestCase): |
| 824 |
# Test _isfinite private function. |
| 825 |
|
| 826 |
def test_finite(self): |
| 827 |
# Test that finite numbers are recognised as finite. |
| 828 |
for x in (5, Fraction(1, 3), 2.5, Decimal("5.5")): |
| 829 |
self.assertTrue(statistics._isfinite(x)) |
| 830 |
|
| 831 |
def test_infinity(self): |
| 832 |
# Test that INFs are not recognised as finite. |
| 833 |
for x in (float("inf"), Decimal("inf")): |
| 834 |
self.assertFalse(statistics._isfinite(x)) |
| 835 |
|
| 836 |
def test_nan(self): |
| 837 |
# Test that NANs are not recognised as finite. |
| 838 |
for x in (float("nan"), Decimal("NAN"), Decimal("sNAN")): |
| 839 |
self.assertFalse(statistics._isfinite(x)) |
| 840 |
|
| 841 |
|
| 842 |
class CoerceTest(unittest.TestCase): |
| 843 |
# Test that private function _coerce correctly deals with types. |
| 844 |
|
| 845 |
# The coercion rules are currently an implementation detail, although at |
| 846 |
# some point that should change. The tests and comments here define the |
| 847 |
# correct implementation. |
| 848 |
|
| 849 |
# Pre-conditions of _coerce: |
| 850 |
# |
| 851 |
# - The first time _sum calls _coerce, the |
| 852 |
# - coerce(T, S) will never be called with bool as the first argument; |
| 853 |
# this is a pre-condition, guarded with an assertion. |
| 854 |
|
| 855 |
# |
| 856 |
# - coerce(T, T) will always return T; we assume T is a valid numeric |
| 857 |
# type. Violate this assumption at your own risk. |
| 858 |
# |
| 859 |
# - Apart from as above, bool is treated as if it were actually int. |
| 860 |
# |
| 861 |
# - coerce(int, X) and coerce(X, int) return X. |
| 862 |
# - |
| 863 |
def test_bool(self): |
| 864 |
# bool is somewhat special, due to the pre-condition that it is |
| 865 |
# never given as the first argument to _coerce, and that it cannot |
| 866 |
# be subclassed. So we test it specially. |
| 867 |
for T in (int, float, Fraction, Decimal): |
| 868 |
self.assertIs(statistics._coerce(T, bool), T) |
| 869 |
class MyClass(T): pass |
| 870 |
self.assertIs(statistics._coerce(MyClass, bool), MyClass) |
| 871 |
|
| 872 |
def assertCoerceTo(self, A, B): |
| 873 |
"""Assert that type A coerces to B.""" |
| 874 |
self.assertIs(statistics._coerce(A, B), B) |
| 875 |
self.assertIs(statistics._coerce(B, A), B) |
| 876 |
|
| 877 |
def check_coerce_to(self, A, B): |
| 878 |
"""Checks that type A coerces to B, including subclasses.""" |
| 879 |
# Assert that type A is coerced to B. |
| 880 |
self.assertCoerceTo(A, B) |
| 881 |
# Subclasses of A are also coerced to B. |
| 882 |
class SubclassOfA(A): pass |
| 883 |
self.assertCoerceTo(SubclassOfA, B) |
| 884 |
# A, and subclasses of A, are coerced to subclasses of B. |
| 885 |
class SubclassOfB(B): pass |
| 886 |
self.assertCoerceTo(A, SubclassOfB) |
| 887 |
self.assertCoerceTo(SubclassOfA, SubclassOfB) |
| 888 |
|
| 889 |
def assertCoerceRaises(self, A, B): |
| 890 |
"""Assert that coercing A to B, or vice versa, raises TypeError.""" |
| 891 |
self.assertRaises(TypeError, statistics._coerce, (A, B)) |
| 892 |
self.assertRaises(TypeError, statistics._coerce, (B, A)) |
| 893 |
|
| 894 |
def check_type_coercions(self, T): |
| 895 |
"""Check that type T coerces correctly with subclasses of itself.""" |
| 896 |
assert T is not bool |
| 897 |
# Coercing a type with itself returns the same type. |
| 898 |
self.assertIs(statistics._coerce(T, T), T) |
| 899 |
# Coercing a type with a subclass of itself returns the subclass. |
| 900 |
class U(T): pass |
| 901 |
class V(T): pass |
| 902 |
class W(U): pass |
| 903 |
for typ in (U, V, W): |
| 904 |
self.assertCoerceTo(T, typ) |
| 905 |
self.assertCoerceTo(U, W) |
| 906 |
# Coercing two subclasses that aren't parent/child is an error. |
| 907 |
self.assertCoerceRaises(U, V) |
| 908 |
self.assertCoerceRaises(V, W) |
| 909 |
|
| 910 |
def test_int(self): |
| 911 |
# Check that int coerces correctly. |
| 912 |
self.check_type_coercions(int) |
| 913 |
for typ in (float, Fraction, Decimal): |
| 914 |
self.check_coerce_to(int, typ) |
| 915 |
|
| 916 |
def test_fraction(self): |
| 917 |
# Check that Fraction coerces correctly. |
| 918 |
self.check_type_coercions(Fraction) |
| 919 |
self.check_coerce_to(Fraction, float) |
| 920 |
|
| 921 |
def test_decimal(self): |
| 922 |
# Check that Decimal coerces correctly. |
| 923 |
self.check_type_coercions(Decimal) |
| 924 |
|
| 925 |
def test_float(self): |
| 926 |
# Check that float coerces correctly. |
| 927 |
self.check_type_coercions(float) |
| 928 |
|
| 929 |
def test_non_numeric_types(self): |
| 930 |
for bad_type in (str, list, type(None), tuple, dict): |
| 931 |
for good_type in (int, float, Fraction, Decimal): |
| 932 |
self.assertCoerceRaises(good_type, bad_type) |
| 933 |
|
| 934 |
def test_incompatible_types(self): |
| 935 |
# Test that incompatible types raise. |
| 936 |
for T in (float, Fraction): |
| 937 |
class MySubclass(T): pass |
| 938 |
self.assertCoerceRaises(T, Decimal) |
| 939 |
self.assertCoerceRaises(MySubclass, Decimal) |
| 940 |
|
| 941 |
|
| 942 |
class ConvertTest(unittest.TestCase): |
| 943 |
# Test private _convert function. |
| 944 |
|
| 945 |
def check_exact_equal(self, x, y): |
| 946 |
"""Check that x equals y, and has the same type as well.""" |
| 947 |
self.assertEqual(x, y) |
| 948 |
self.assertIs(type(x), type(y)) |
| 949 |
|
| 950 |
def test_int(self): |
| 951 |
# Test conversions to int. |
| 952 |
x = statistics._convert(Fraction(71), int) |
| 953 |
self.check_exact_equal(x, 71) |
| 954 |
class MyInt(int): pass |
| 955 |
x = statistics._convert(Fraction(17), MyInt) |
| 956 |
self.check_exact_equal(x, MyInt(17)) |
| 957 |
|
| 958 |
def test_fraction(self): |
| 959 |
# Test conversions to Fraction. |
| 960 |
x = statistics._convert(Fraction(95, 99), Fraction) |
| 961 |
self.check_exact_equal(x, Fraction(95, 99)) |
| 962 |
class MyFraction(Fraction): |
| 963 |
def __truediv__(self, other): |
| 964 |
return self.__class__(super().__truediv__(other)) |
| 965 |
x = statistics._convert(Fraction(71, 13), MyFraction) |
| 966 |
self.check_exact_equal(x, MyFraction(71, 13)) |
| 967 |
|
| 968 |
def test_float(self): |
| 969 |
# Test conversions to float. |
| 970 |
x = statistics._convert(Fraction(-1, 2), float) |
| 971 |
self.check_exact_equal(x, -0.5) |
| 972 |
class MyFloat(float): |
| 973 |
def __truediv__(self, other): |
| 974 |
return self.__class__(super().__truediv__(other)) |
| 975 |
x = statistics._convert(Fraction(9, 8), MyFloat) |
| 976 |
self.check_exact_equal(x, MyFloat(1.125)) |
| 977 |
|
| 978 |
def test_decimal(self): |
| 979 |
# Test conversions to Decimal. |
| 980 |
x = statistics._convert(Fraction(1, 40), Decimal) |
| 981 |
self.check_exact_equal(x, Decimal("0.025")) |
| 982 |
class MyDecimal(Decimal): |
| 983 |
def __truediv__(self, other): |
| 984 |
return self.__class__(super().__truediv__(other)) |
| 985 |
x = statistics._convert(Fraction(-15, 16), MyDecimal) |
| 986 |
self.check_exact_equal(x, MyDecimal("-0.9375")) |
| 987 |
|
| 988 |
def test_inf(self): |
| 989 |
for INF in (float('inf'), Decimal('inf')): |
| 990 |
for inf in (INF, -INF): |
| 991 |
x = statistics._convert(inf, type(inf)) |
| 992 |
self.check_exact_equal(x, inf) |
| 993 |
|
| 994 |
def test_nan(self): |
| 995 |
for nan in (float('nan'), Decimal('NAN'), Decimal('sNAN')): |
| 996 |
x = statistics._convert(nan, type(nan)) |
| 997 |
self.assertTrue(_nan_equal(x, nan)) |
| 998 |
|
| 999 |
def test_invalid_input_type(self): |
| 1000 |
with self.assertRaises(TypeError): |
| 1001 |
statistics._convert(None, float) |
| 1002 |
|
| 1003 |
|
| 1004 |
class FailNegTest(unittest.TestCase): |
| 1005 |
"""Test _fail_neg private function.""" |
| 1006 |
|
| 1007 |
def test_pass_through(self): |
| 1008 |
# Test that values are passed through unchanged. |
| 1009 |
values = [1, 2.0, Fraction(3), Decimal(4)] |
| 1010 |
new = list(statistics._fail_neg(values)) |
| 1011 |
self.assertEqual(values, new) |
| 1012 |
|
| 1013 |
def test_negatives_raise(self): |
| 1014 |
# Test that negatives raise an exception. |
| 1015 |
for x in [1, 2.0, Fraction(3), Decimal(4)]: |
| 1016 |
seq = [-x] |
| 1017 |
it = statistics._fail_neg(seq) |
| 1018 |
self.assertRaises(statistics.StatisticsError, next, it) |
| 1019 |
|
| 1020 |
def test_error_msg(self): |
| 1021 |
# Test that a given error message is used. |
| 1022 |
msg = "badness #%d" % random.randint(10000, 99999) |
| 1023 |
try: |
| 1024 |
next(statistics._fail_neg([-1], msg)) |
| 1025 |
except statistics.StatisticsError as e: |
| 1026 |
errmsg = e.args[0] |
| 1027 |
else: |
| 1028 |
self.fail("expected exception, but it didn't happen") |
| 1029 |
self.assertEqual(errmsg, msg) |
| 1030 |
|
| 1031 |
|
| 1032 |
# === Tests for public functions === |
| 1033 |
|
| 1034 |
class UnivariateCommonMixin: |
| 1035 |
# Common tests for most univariate functions that take a data argument. |
| 1036 |
|
| 1037 |
def test_no_args(self): |
| 1038 |
# Fail if given no arguments. |
| 1039 |
self.assertRaises(TypeError, self.func) |
| 1040 |
|
| 1041 |
def test_empty_data(self): |
| 1042 |
# Fail when the data argument (first argument) is empty. |
| 1043 |
for empty in ([], (), iter([])): |
| 1044 |
self.assertRaises(statistics.StatisticsError, self.func, empty) |
| 1045 |
|
| 1046 |
def prepare_data(self): |
| 1047 |
"""Return int data for various tests.""" |
| 1048 |
data = list(range(10)) |
| 1049 |
while data == sorted(data): |
| 1050 |
random.shuffle(data) |
| 1051 |
return data |
| 1052 |
|
| 1053 |
def test_no_inplace_modifications(self): |
| 1054 |
# Test that the function does not modify its input data. |
| 1055 |
data = self.prepare_data() |
| 1056 |
assert len(data) != 1 # Necessary to avoid infinite loop. |
| 1057 |
assert data != sorted(data) |
| 1058 |
saved = data[:] |
| 1059 |
assert data is not saved |
| 1060 |
_ = self.func(data) |
| 1061 |
self.assertListEqual(data, saved, "data has been modified") |
| 1062 |
|
| 1063 |
def test_order_doesnt_matter(self): |
| 1064 |
# Test that the order of data points doesn't change the result. |
| 1065 |
|
| 1066 |
# CAUTION: due to floating-point rounding errors, the result actually |
| 1067 |
# may depend on the order. Consider this test representing an ideal. |
| 1068 |
# To avoid this test failing, only test with exact values such as ints |
| 1069 |
# or Fractions. |
| 1070 |
data = [1, 2, 3, 3, 3, 4, 5, 6]*100 |
| 1071 |
expected = self.func(data) |
| 1072 |
random.shuffle(data) |
| 1073 |
actual = self.func(data) |
| 1074 |
self.assertEqual(expected, actual) |
| 1075 |
|
| 1076 |
def test_type_of_data_collection(self): |
| 1077 |
# Test that the type of iterable data doesn't effect the result. |
| 1078 |
class MyList(list): |
| 1079 |
pass |
| 1080 |
class MyTuple(tuple): |
| 1081 |
pass |
| 1082 |
def generator(data): |
| 1083 |
return (obj for obj in data) |
| 1084 |
data = self.prepare_data() |
| 1085 |
expected = self.func(data) |
| 1086 |
for kind in (list, tuple, iter, MyList, MyTuple, generator): |
| 1087 |
result = self.func(kind(data)) |
| 1088 |
self.assertEqual(result, expected) |
| 1089 |
|
| 1090 |
def test_range_data(self): |
| 1091 |
# Test that functions work with range objects. |
| 1092 |
data = range(20, 50, 3) |
| 1093 |
expected = self.func(list(data)) |
| 1094 |
self.assertEqual(self.func(data), expected) |
| 1095 |
|
| 1096 |
def test_bad_arg_types(self): |
| 1097 |
# Test that function raises when given data of the wrong type. |
| 1098 |
|
| 1099 |
# Don't roll the following into a loop like this: |
| 1100 |
# for bad in list_of_bad: |
| 1101 |
# self.check_for_type_error(bad) |
| 1102 |
# |
| 1103 |
# Since assertRaises doesn't show the arguments that caused the test |
| 1104 |
# failure, it is very difficult to debug these test failures when the |
| 1105 |
# following are in a loop. |
| 1106 |
self.check_for_type_error(None) |
| 1107 |
self.check_for_type_error(23) |
| 1108 |
self.check_for_type_error(42.0) |
| 1109 |
self.check_for_type_error(object()) |
| 1110 |
|
| 1111 |
def check_for_type_error(self, *args): |
| 1112 |
self.assertRaises(TypeError, self.func, *args) |
| 1113 |
|
| 1114 |
def test_type_of_data_element(self): |
| 1115 |
# Check the type of data elements doesn't affect the numeric result. |
| 1116 |
# This is a weaker test than UnivariateTypeMixin.testTypesConserved, |
| 1117 |
# because it checks the numeric result by equality, but not by type. |
| 1118 |
class MyFloat(float): |
| 1119 |
def __truediv__(self, other): |
| 1120 |
return type(self)(super().__truediv__(other)) |
| 1121 |
def __add__(self, other): |
| 1122 |
return type(self)(super().__add__(other)) |
| 1123 |
__radd__ = __add__ |
| 1124 |
|
| 1125 |
raw = self.prepare_data() |
| 1126 |
expected = self.func(raw) |
| 1127 |
for kind in (float, MyFloat, Decimal, Fraction): |
| 1128 |
data = [kind(x) for x in raw] |
| 1129 |
result = type(expected)(self.func(data)) |
| 1130 |
self.assertEqual(result, expected) |
| 1131 |
|
| 1132 |
|
| 1133 |
class UnivariateTypeMixin: |
| 1134 |
"""Mixin class for type-conserving functions. |
| 1135 |
|
| 1136 |
This mixin class holds test(s) for functions which conserve the type of |
| 1137 |
individual data points. E.g. the mean of a list of Fractions should itself |
| 1138 |
be a Fraction. |
| 1139 |
|
| 1140 |
Not all tests to do with types need go in this class. Only those that |
| 1141 |
rely on the function returning the same type as its input data. |
| 1142 |
""" |
| 1143 |
def prepare_types_for_conservation_test(self): |
| 1144 |
"""Return the types which are expected to be conserved.""" |
| 1145 |
class MyFloat(float): |
| 1146 |
def __truediv__(self, other): |
| 1147 |
return type(self)(super().__truediv__(other)) |
| 1148 |
def __rtruediv__(self, other): |
| 1149 |
return type(self)(super().__rtruediv__(other)) |
| 1150 |
def __sub__(self, other): |
| 1151 |
return type(self)(super().__sub__(other)) |
| 1152 |
def __rsub__(self, other): |
| 1153 |
return type(self)(super().__rsub__(other)) |
| 1154 |
def __pow__(self, other): |
| 1155 |
return type(self)(super().__pow__(other)) |
| 1156 |
def __add__(self, other): |
| 1157 |
return type(self)(super().__add__(other)) |
| 1158 |
__radd__ = __add__ |
| 1159 |
def __mul__(self, other): |
| 1160 |
return type(self)(super().__mul__(other)) |
| 1161 |
__rmul__ = __mul__ |
| 1162 |
return (float, Decimal, Fraction, MyFloat) |
| 1163 |
|
| 1164 |
def test_types_conserved(self): |
| 1165 |
# Test that functions keeps the same type as their data points. |
| 1166 |
# (Excludes mixed data types.) This only tests the type of the return |
| 1167 |
# result, not the value. |
| 1168 |
data = self.prepare_data() |
| 1169 |
for kind in self.prepare_types_for_conservation_test(): |
| 1170 |
d = [kind(x) for x in data] |
| 1171 |
result = self.func(d) |
| 1172 |
self.assertIs(type(result), kind) |
| 1173 |
|
| 1174 |
|
| 1175 |
class TestSumCommon(UnivariateCommonMixin, UnivariateTypeMixin): |
| 1176 |
# Common test cases for statistics._sum() function. |
| 1177 |
|
| 1178 |
# This test suite looks only at the numeric value returned by _sum, |
| 1179 |
# after conversion to the appropriate type. |
| 1180 |
def setUp(self): |
| 1181 |
def simplified_sum(*args): |
| 1182 |
T, value, n = statistics._sum(*args) |
| 1183 |
return statistics._coerce(value, T) |
| 1184 |
self.func = simplified_sum |
| 1185 |
|
| 1186 |
|
| 1187 |
class TestSum(NumericTestCase): |
| 1188 |
# Test cases for statistics._sum() function. |
| 1189 |
|
| 1190 |
# These tests look at the entire three value tuple returned by _sum. |
| 1191 |
|
| 1192 |
def setUp(self): |
| 1193 |
self.func = statistics._sum |
| 1194 |
|
| 1195 |
def test_empty_data(self): |
| 1196 |
# Override test for empty data. |
| 1197 |
for data in ([], (), iter([])): |
| 1198 |
self.assertEqual(self.func(data), (int, Fraction(0), 0)) |
| 1199 |
|
| 1200 |
def test_ints(self): |
| 1201 |
self.assertEqual(self.func([1, 5, 3, -4, -8, 20, 42, 1]), |
| 1202 |
(int, Fraction(60), 8)) |
| 1203 |
|
| 1204 |
def test_floats(self): |
| 1205 |
self.assertEqual(self.func([0.25]*20), |
| 1206 |
(float, Fraction(5.0), 20)) |
| 1207 |
|
| 1208 |
def test_fractions(self): |
| 1209 |
self.assertEqual(self.func([Fraction(1, 1000)]*500), |
| 1210 |
(Fraction, Fraction(1, 2), 500)) |
| 1211 |
|
| 1212 |
def test_decimals(self): |
| 1213 |
D = Decimal |
| 1214 |
data = [D("0.001"), D("5.246"), D("1.702"), D("-0.025"), |
| 1215 |
D("3.974"), D("2.328"), D("4.617"), D("2.843"), |
| 1216 |
] |
| 1217 |
self.assertEqual(self.func(data), |
| 1218 |
(Decimal, Decimal("20.686"), 8)) |
| 1219 |
|
| 1220 |
def test_compare_with_math_fsum(self): |
| 1221 |
# Compare with the math.fsum function. |
| 1222 |
# Ideally we ought to get the exact same result, but sometimes |
| 1223 |
# we differ by a very slight amount :-( |
| 1224 |
data = [random.uniform(-100, 1000) for _ in range(1000)] |
| 1225 |
self.assertApproxEqual(float(self.func(data)[1]), math.fsum(data), rel=2e-16) |
| 1226 |
|
| 1227 |
def test_strings_fail(self): |
| 1228 |
# Sum of strings should fail. |
| 1229 |
self.assertRaises(TypeError, self.func, [1, 2, 3], '999') |
| 1230 |
self.assertRaises(TypeError, self.func, [1, 2, 3, '999']) |
| 1231 |
|
| 1232 |
def test_bytes_fail(self): |
| 1233 |
# Sum of bytes should fail. |
| 1234 |
self.assertRaises(TypeError, self.func, [1, 2, 3], b'999') |
| 1235 |
self.assertRaises(TypeError, self.func, [1, 2, 3, b'999']) |
| 1236 |
|
| 1237 |
def test_mixed_sum(self): |
| 1238 |
# Mixed input types are not (currently) allowed. |
| 1239 |
# Check that mixed data types fail. |
| 1240 |
self.assertRaises(TypeError, self.func, [1, 2.0, Decimal(1)]) |
| 1241 |
# And so does mixed start argument. |
| 1242 |
self.assertRaises(TypeError, self.func, [1, 2.0], Decimal(1)) |
| 1243 |
|
| 1244 |
|
| 1245 |
class SumTortureTest(NumericTestCase): |
| 1246 |
def test_torture(self): |
| 1247 |
# Tim Peters' torture test for sum, and variants of same. |
| 1248 |
self.assertEqual(statistics._sum([1, 1e100, 1, -1e100]*10000), |
| 1249 |
(float, Fraction(20000.0), 40000)) |
| 1250 |
self.assertEqual(statistics._sum([1e100, 1, 1, -1e100]*10000), |
| 1251 |
(float, Fraction(20000.0), 40000)) |
| 1252 |
T, num, count = statistics._sum([1e-100, 1, 1e-100, -1]*10000) |
| 1253 |
self.assertIs(T, float) |
| 1254 |
self.assertEqual(count, 40000) |
| 1255 |
self.assertApproxEqual(float(num), 2.0e-96, rel=5e-16) |
| 1256 |
|
| 1257 |
|
| 1258 |
class SumSpecialValues(NumericTestCase): |
| 1259 |
# Test that sum works correctly with IEEE-754 special values. |
| 1260 |
|
| 1261 |
def test_nan(self): |
| 1262 |
for type_ in (float, Decimal): |
| 1263 |
nan = type_('nan') |
| 1264 |
result = statistics._sum([1, nan, 2])[1] |
| 1265 |
self.assertIs(type(result), type_) |
| 1266 |
self.assertTrue(math.isnan(result)) |
| 1267 |
|
| 1268 |
def check_infinity(self, x, inf): |
| 1269 |
"""Check x is an infinity of the same type and sign as inf.""" |
| 1270 |
self.assertTrue(math.isinf(x)) |
| 1271 |
self.assertIs(type(x), type(inf)) |
| 1272 |
self.assertEqual(x > 0, inf > 0) |
| 1273 |
assert x == inf |
| 1274 |
|
| 1275 |
def do_test_inf(self, inf): |
| 1276 |
# Adding a single infinity gives infinity. |
| 1277 |
result = statistics._sum([1, 2, inf, 3])[1] |
| 1278 |
self.check_infinity(result, inf) |
| 1279 |
# Adding two infinities of the same sign also gives infinity. |
| 1280 |
result = statistics._sum([1, 2, inf, 3, inf, 4])[1] |
| 1281 |
self.check_infinity(result, inf) |
| 1282 |
|
| 1283 |
def test_float_inf(self): |
| 1284 |
inf = float('inf') |
| 1285 |
for sign in (+1, -1): |
| 1286 |
self.do_test_inf(sign*inf) |
| 1287 |
|
| 1288 |
def test_decimal_inf(self): |
| 1289 |
inf = Decimal('inf') |
| 1290 |
for sign in (+1, -1): |
| 1291 |
self.do_test_inf(sign*inf) |
| 1292 |
|
| 1293 |
def test_float_mismatched_infs(self): |
| 1294 |
# Test that adding two infinities of opposite sign gives a NAN. |
| 1295 |
inf = float('inf') |
| 1296 |
result = statistics._sum([1, 2, inf, 3, -inf, 4])[1] |
| 1297 |
self.assertTrue(math.isnan(result)) |
| 1298 |
|
| 1299 |
def test_decimal_extendedcontext_mismatched_infs_to_nan(self): |
| 1300 |
# Test adding Decimal INFs with opposite sign returns NAN. |
| 1301 |
inf = Decimal('inf') |
| 1302 |
data = [1, 2, inf, 3, -inf, 4] |
| 1303 |
with decimal.localcontext(decimal.ExtendedContext): |
| 1304 |
self.assertTrue(math.isnan(statistics._sum(data)[1])) |
| 1305 |
|
| 1306 |
def test_decimal_basiccontext_mismatched_infs_to_nan(self): |
| 1307 |
# Test adding Decimal INFs with opposite sign raises InvalidOperation. |
| 1308 |
inf = Decimal('inf') |
| 1309 |
data = [1, 2, inf, 3, -inf, 4] |
| 1310 |
with decimal.localcontext(decimal.BasicContext): |
| 1311 |
self.assertRaises(decimal.InvalidOperation, statistics._sum, data) |
| 1312 |
|
| 1313 |
def test_decimal_snan_raises(self): |
| 1314 |
# Adding sNAN should raise InvalidOperation. |
| 1315 |
sNAN = Decimal('sNAN') |
| 1316 |
data = [1, sNAN, 2] |
| 1317 |
self.assertRaises(decimal.InvalidOperation, statistics._sum, data) |
| 1318 |
|
| 1319 |
|
| 1320 |
# === Tests for averages === |
| 1321 |
|
| 1322 |
class AverageMixin(UnivariateCommonMixin): |
| 1323 |
# Mixin class holding common tests for averages. |
| 1324 |
|
| 1325 |
def test_single_value(self): |
| 1326 |
# Average of a single value is the value itself. |
| 1327 |
for x in (23, 42.5, 1.3e15, Fraction(15, 19), Decimal('0.28')): |
| 1328 |
self.assertEqual(self.func([x]), x) |
| 1329 |
|
| 1330 |
def prepare_values_for_repeated_single_test(self): |
| 1331 |
return (3.5, 17, 2.5e15, Fraction(61, 67), Decimal('4.9712')) |
| 1332 |
|
| 1333 |
def test_repeated_single_value(self): |
| 1334 |
# The average of a single repeated value is the value itself. |
| 1335 |
for x in self.prepare_values_for_repeated_single_test(): |
| 1336 |
for count in (2, 5, 10, 20): |
| 1337 |
with self.subTest(x=x, count=count): |
| 1338 |
data = [x]*count |
| 1339 |
self.assertEqual(self.func(data), x) |
| 1340 |
|
| 1341 |
|
| 1342 |
class TestMean(NumericTestCase, AverageMixin, UnivariateTypeMixin): |
| 1343 |
def setUp(self): |
| 1344 |
self.func = statistics.mean |
| 1345 |
|
| 1346 |
def test_torture_pep(self): |
| 1347 |
# "Torture Test" from PEP-450. |
| 1348 |
self.assertEqual(self.func([1e100, 1, 3, -1e100]), 1) |
| 1349 |
|
| 1350 |
def test_ints(self): |
| 1351 |
# Test mean with ints. |
| 1352 |
data = [0, 1, 2, 3, 3, 3, 4, 5, 5, 6, 7, 7, 7, 7, 8, 9] |
| 1353 |
random.shuffle(data) |
| 1354 |
self.assertEqual(self.func(data), 4.8125) |
| 1355 |
|
| 1356 |
def test_floats(self): |
| 1357 |
# Test mean with floats. |
| 1358 |
data = [17.25, 19.75, 20.0, 21.5, 21.75, 23.25, 25.125, 27.5] |
| 1359 |
random.shuffle(data) |
| 1360 |
self.assertEqual(self.func(data), 22.015625) |
| 1361 |
|
| 1362 |
def test_decimals(self): |
| 1363 |
# Test mean with Decimals. |
| 1364 |
D = Decimal |
| 1365 |
data = [D("1.634"), D("2.517"), D("3.912"), D("4.072"), D("5.813")] |
| 1366 |
random.shuffle(data) |
| 1367 |
self.assertEqual(self.func(data), D("3.5896")) |
| 1368 |
|
| 1369 |
def test_fractions(self): |
| 1370 |
# Test mean with Fractions. |
| 1371 |
F = Fraction |
| 1372 |
data = [F(1, 2), F(2, 3), F(3, 4), F(4, 5), F(5, 6), F(6, 7), F(7, 8)] |
| 1373 |
random.shuffle(data) |
| 1374 |
self.assertEqual(self.func(data), F(1479, 1960)) |
| 1375 |
|
| 1376 |
def test_inf(self): |
| 1377 |
# Test mean with infinities. |
| 1378 |
raw = [1, 3, 5, 7, 9] # Use only ints, to avoid TypeError later. |
| 1379 |
for kind in (float, Decimal): |
| 1380 |
for sign in (1, -1): |
| 1381 |
inf = kind("inf")*sign |
| 1382 |
data = raw + [inf] |
| 1383 |
result = self.func(data) |
| 1384 |
self.assertTrue(math.isinf(result)) |
| 1385 |
self.assertEqual(result, inf) |
| 1386 |
|
| 1387 |
def test_mismatched_infs(self): |
| 1388 |
# Test mean with infinities of opposite sign. |
| 1389 |
data = [2, 4, 6, float('inf'), 1, 3, 5, float('-inf')] |
| 1390 |
result = self.func(data) |
| 1391 |
self.assertTrue(math.isnan(result)) |
| 1392 |
|
| 1393 |
def test_nan(self): |
| 1394 |
# Test mean with NANs. |
| 1395 |
raw = [1, 3, 5, 7, 9] # Use only ints, to avoid TypeError later. |
| 1396 |
for kind in (float, Decimal): |
| 1397 |
inf = kind("nan") |
| 1398 |
data = raw + [inf] |
| 1399 |
result = self.func(data) |
| 1400 |
self.assertTrue(math.isnan(result)) |
| 1401 |
|
| 1402 |
def test_big_data(self): |
| 1403 |
# Test adding a large constant to every data point. |
| 1404 |
c = 1e9 |
| 1405 |
data = [3.4, 4.5, 4.9, 6.7, 6.8, 7.2, 8.0, 8.1, 9.4] |
| 1406 |
expected = self.func(data) + c |
| 1407 |
assert expected != c |
| 1408 |
result = self.func([x+c for x in data]) |
| 1409 |
self.assertEqual(result, expected) |
| 1410 |
|
| 1411 |
def test_doubled_data(self): |
| 1412 |
# Mean of [a,b,c...z] should be same as for [a,a,b,b,c,c...z,z]. |
| 1413 |
data = [random.uniform(-3, 5) for _ in range(1000)] |
| 1414 |
expected = self.func(data) |
| 1415 |
actual = self.func(data*2) |
| 1416 |
self.assertApproxEqual(actual, expected) |
| 1417 |
|
| 1418 |
def test_regression_20561(self): |
| 1419 |
# Regression test for issue 20561. |
| 1420 |
# See http://bugs.python.org/issue20561 |
| 1421 |
d = Decimal('1e4') |
| 1422 |
self.assertEqual(statistics.mean([d]), d) |
| 1423 |
|
| 1424 |
def test_regression_25177(self): |
| 1425 |
# Regression test for issue 25177. |
| 1426 |
# Ensure very big and very small floats don't overflow. |
| 1427 |
# See http://bugs.python.org/issue25177. |
| 1428 |
self.assertEqual(statistics.mean( |
| 1429 |
[8.988465674311579e+307, 8.98846567431158e+307]), |
| 1430 |
8.98846567431158e+307) |
| 1431 |
big = 8.98846567431158e+307 |
| 1432 |
tiny = 5e-324 |
| 1433 |
for n in (2, 3, 5, 200): |
| 1434 |
self.assertEqual(statistics.mean([big]*n), big) |
| 1435 |
self.assertEqual(statistics.mean([tiny]*n), tiny) |
| 1436 |
|
| 1437 |
|
| 1438 |
class TestHarmonicMean(NumericTestCase, AverageMixin, UnivariateTypeMixin): |
| 1439 |
def setUp(self): |
| 1440 |
self.func = statistics.harmonic_mean |
| 1441 |
|
| 1442 |
def prepare_data(self): |
| 1443 |
# Override mixin method. |
| 1444 |
values = super().prepare_data() |
| 1445 |
values.remove(0) |
| 1446 |
return values |
| 1447 |
|
| 1448 |
def prepare_values_for_repeated_single_test(self): |
| 1449 |
# Override mixin method. |
| 1450 |
return (3.5, 17, 2.5e15, Fraction(61, 67), Decimal('4.125')) |
| 1451 |
|
| 1452 |
def test_zero(self): |
| 1453 |
# Test that harmonic mean returns zero when given zero. |
| 1454 |
values = [1, 0, 2] |
| 1455 |
self.assertEqual(self.func(values), 0) |
| 1456 |
|
| 1457 |
def test_negative_error(self): |
| 1458 |
# Test that harmonic mean raises when given a negative value. |
| 1459 |
exc = statistics.StatisticsError |
| 1460 |
for values in ([-1], [1, -2, 3]): |
| 1461 |
with self.subTest(values=values): |
| 1462 |
self.assertRaises(exc, self.func, values) |
| 1463 |
|
| 1464 |
def test_invalid_type_error(self): |
| 1465 |
# Test error is raised when input contains invalid type(s) |
| 1466 |
for data in [ |
| 1467 |
['3.14'], # single string |
| 1468 |
['1', '2', '3'], # multiple strings |
| 1469 |
[1, '2', 3, '4', 5], # mixed strings and valid integers |
| 1470 |
[2.3, 3.4, 4.5, '5.6'] # only one string and valid floats |
| 1471 |
]: |
| 1472 |
with self.subTest(data=data): |
| 1473 |
with self.assertRaises(TypeError): |
| 1474 |
self.func(data) |
| 1475 |
|
| 1476 |
def test_ints(self): |
| 1477 |
# Test harmonic mean with ints. |
| 1478 |
data = [2, 4, 4, 8, 16, 16] |
| 1479 |
random.shuffle(data) |
| 1480 |
self.assertEqual(self.func(data), 6*4/5) |
| 1481 |
|
| 1482 |
def test_floats_exact(self): |
| 1483 |
# Test harmonic mean with some carefully chosen floats. |
| 1484 |
data = [1/8, 1/4, 1/4, 1/2, 1/2] |
| 1485 |
random.shuffle(data) |
| 1486 |
self.assertEqual(self.func(data), 1/4) |
| 1487 |
self.assertEqual(self.func([0.25, 0.5, 1.0, 1.0]), 0.5) |
| 1488 |
|
| 1489 |
def test_singleton_lists(self): |
| 1490 |
# Test that harmonic mean([x]) returns (approximately) x. |
| 1491 |
for x in range(1, 101): |
| 1492 |
self.assertEqual(self.func([x]), x) |
| 1493 |
|
| 1494 |
def test_decimals_exact(self): |
| 1495 |
# Test harmonic mean with some carefully chosen Decimals. |
| 1496 |
D = Decimal |
| 1497 |
self.assertEqual(self.func([D(15), D(30), D(60), D(60)]), D(30)) |
| 1498 |
data = [D("0.05"), D("0.10"), D("0.20"), D("0.20")] |
| 1499 |
random.shuffle(data) |
| 1500 |
self.assertEqual(self.func(data), D("0.10")) |
| 1501 |
data = [D("1.68"), D("0.32"), D("5.94"), D("2.75")] |
| 1502 |
random.shuffle(data) |
| 1503 |
self.assertEqual(self.func(data), D(66528)/70723) |
| 1504 |
|
| 1505 |
def test_fractions(self): |
| 1506 |
# Test harmonic mean with Fractions. |
| 1507 |
F = Fraction |
| 1508 |
data = [F(1, 2), F(2, 3), F(3, 4), F(4, 5), F(5, 6), F(6, 7), F(7, 8)] |
| 1509 |
random.shuffle(data) |
| 1510 |
self.assertEqual(self.func(data), F(7*420, 4029)) |
| 1511 |
|
| 1512 |
def test_inf(self): |
| 1513 |
# Test harmonic mean with infinity. |
| 1514 |
values = [2.0, float('inf'), 1.0] |
| 1515 |
self.assertEqual(self.func(values), 2.0) |
| 1516 |
|
| 1517 |
def test_nan(self): |
| 1518 |
# Test harmonic mean with NANs. |
| 1519 |
values = [2.0, float('nan'), 1.0] |
| 1520 |
self.assertTrue(math.isnan(self.func(values))) |
| 1521 |
|
| 1522 |
def test_multiply_data_points(self): |
| 1523 |
# Test multiplying every data point by a constant. |
| 1524 |
c = 111 |
| 1525 |
data = [3.4, 4.5, 4.9, 6.7, 6.8, 7.2, 8.0, 8.1, 9.4] |
| 1526 |
expected = self.func(data)*c |
| 1527 |
result = self.func([x*c for x in data]) |
| 1528 |
self.assertEqual(result, expected) |
| 1529 |
|
| 1530 |
def test_doubled_data(self): |
| 1531 |
# Harmonic mean of [a,b...z] should be same as for [a,a,b,b...z,z]. |
| 1532 |
data = [random.uniform(1, 5) for _ in range(1000)] |
| 1533 |
expected = self.func(data) |
| 1534 |
actual = self.func(data*2) |
| 1535 |
self.assertApproxEqual(actual, expected) |
| 1536 |
|
| 1537 |
def test_with_weights(self): |
| 1538 |
self.assertEqual(self.func([40, 60], [5, 30]), 56.0) # common case |
| 1539 |
self.assertEqual(self.func([40, 60], |
| 1540 |
weights=[5, 30]), 56.0) # keyword argument |
| 1541 |
self.assertEqual(self.func(iter([40, 60]), |
| 1542 |
iter([5, 30])), 56.0) # iterator inputs |
| 1543 |
self.assertEqual( |
| 1544 |
self.func([Fraction(10, 3), Fraction(23, 5), Fraction(7, 2)], [5, 2, 10]), |
| 1545 |
self.func([Fraction(10, 3)] * 5 + |
| 1546 |
[Fraction(23, 5)] * 2 + |
| 1547 |
[Fraction(7, 2)] * 10)) |
| 1548 |
self.assertEqual(self.func([10], [7]), 10) # n=1 fast path |
| 1549 |
with self.assertRaises(TypeError): |
| 1550 |
self.func([1, 2, 3], [1, (), 3]) # non-numeric weight |
| 1551 |
with self.assertRaises(statistics.StatisticsError): |
| 1552 |
self.func([1, 2, 3], [1, 2]) # wrong number of weights |
| 1553 |
with self.assertRaises(statistics.StatisticsError): |
| 1554 |
self.func([10], [0]) # no non-zero weights |
| 1555 |
with self.assertRaises(statistics.StatisticsError): |
| 1556 |
self.func([10, 20], [0, 0]) # no non-zero weights |
| 1557 |
|
| 1558 |
|
| 1559 |
class TestMedian(NumericTestCase, AverageMixin): |
| 1560 |
# Common tests for median and all median.* functions. |
| 1561 |
def setUp(self): |
| 1562 |
self.func = statistics.median |
| 1563 |
|
| 1564 |
def prepare_data(self): |
| 1565 |
"""Overload method from UnivariateCommonMixin.""" |
| 1566 |
data = super().prepare_data() |
| 1567 |
if len(data)%2 != 1: |
| 1568 |
data.append(2) |
| 1569 |
return data |
| 1570 |
|
| 1571 |
def test_even_ints(self): |
| 1572 |
# Test median with an even number of int data points. |
| 1573 |
data = [1, 2, 3, 4, 5, 6] |
| 1574 |
assert len(data)%2 == 0 |
| 1575 |
self.assertEqual(self.func(data), 3.5) |
| 1576 |
|
| 1577 |
def test_odd_ints(self): |
| 1578 |
# Test median with an odd number of int data points. |
| 1579 |
data = [1, 2, 3, 4, 5, 6, 9] |
| 1580 |
assert len(data)%2 == 1 |
| 1581 |
self.assertEqual(self.func(data), 4) |
| 1582 |
|
| 1583 |
def test_odd_fractions(self): |
| 1584 |
# Test median works with an odd number of Fractions. |
| 1585 |
F = Fraction |
| 1586 |
data = [F(1, 7), F(2, 7), F(3, 7), F(4, 7), F(5, 7)] |
| 1587 |
assert len(data)%2 == 1 |
| 1588 |
random.shuffle(data) |
| 1589 |
self.assertEqual(self.func(data), F(3, 7)) |
| 1590 |
|
| 1591 |
def test_even_fractions(self): |
| 1592 |
# Test median works with an even number of Fractions. |
| 1593 |
F = Fraction |
| 1594 |
data = [F(1, 7), F(2, 7), F(3, 7), F(4, 7), F(5, 7), F(6, 7)] |
| 1595 |
assert len(data)%2 == 0 |
| 1596 |
random.shuffle(data) |
| 1597 |
self.assertEqual(self.func(data), F(1, 2)) |
| 1598 |
|
| 1599 |
def test_odd_decimals(self): |
| 1600 |
# Test median works with an odd number of Decimals. |
| 1601 |
D = Decimal |
| 1602 |
data = [D('2.5'), D('3.1'), D('4.2'), D('5.7'), D('5.8')] |
| 1603 |
assert len(data)%2 == 1 |
| 1604 |
random.shuffle(data) |
| 1605 |
self.assertEqual(self.func(data), D('4.2')) |
| 1606 |
|
| 1607 |
def test_even_decimals(self): |
| 1608 |
# Test median works with an even number of Decimals. |
| 1609 |
D = Decimal |
| 1610 |
data = [D('1.2'), D('2.5'), D('3.1'), D('4.2'), D('5.7'), D('5.8')] |
| 1611 |
assert len(data)%2 == 0 |
| 1612 |
random.shuffle(data) |
| 1613 |
self.assertEqual(self.func(data), D('3.65')) |
| 1614 |
|
| 1615 |
|
| 1616 |
class TestMedianDataType(NumericTestCase, UnivariateTypeMixin): |
| 1617 |
# Test conservation of data element type for median. |
| 1618 |
def setUp(self): |
| 1619 |
self.func = statistics.median |
| 1620 |
|
| 1621 |
def prepare_data(self): |
| 1622 |
data = list(range(15)) |
| 1623 |
assert len(data)%2 == 1 |
| 1624 |
while data == sorted(data): |
| 1625 |
random.shuffle(data) |
| 1626 |
return data |
| 1627 |
|
| 1628 |
|
| 1629 |
class TestMedianLow(TestMedian, UnivariateTypeMixin): |
| 1630 |
def setUp(self): |
| 1631 |
self.func = statistics.median_low |
| 1632 |
|
| 1633 |
def test_even_ints(self): |
| 1634 |
# Test median_low with an even number of ints. |
| 1635 |
data = [1, 2, 3, 4, 5, 6] |
| 1636 |
assert len(data)%2 == 0 |
| 1637 |
self.assertEqual(self.func(data), 3) |
| 1638 |
|
| 1639 |
def test_even_fractions(self): |
| 1640 |
# Test median_low works with an even number of Fractions. |
| 1641 |
F = Fraction |
| 1642 |
data = [F(1, 7), F(2, 7), F(3, 7), F(4, 7), F(5, 7), F(6, 7)] |
| 1643 |
assert len(data)%2 == 0 |
| 1644 |
random.shuffle(data) |
| 1645 |
self.assertEqual(self.func(data), F(3, 7)) |
| 1646 |
|
| 1647 |
def test_even_decimals(self): |
| 1648 |
# Test median_low works with an even number of Decimals. |
| 1649 |
D = Decimal |
| 1650 |
data = [D('1.1'), D('2.2'), D('3.3'), D('4.4'), D('5.5'), D('6.6')] |
| 1651 |
assert len(data)%2 == 0 |
| 1652 |
random.shuffle(data) |
| 1653 |
self.assertEqual(self.func(data), D('3.3')) |
| 1654 |
|
| 1655 |
|
| 1656 |
class TestMedianHigh(TestMedian, UnivariateTypeMixin): |
| 1657 |
def setUp(self): |
| 1658 |
self.func = statistics.median_high |
| 1659 |
|
| 1660 |
def test_even_ints(self): |
| 1661 |
# Test median_high with an even number of ints. |
| 1662 |
data = [1, 2, 3, 4, 5, 6] |
| 1663 |
assert len(data)%2 == 0 |
| 1664 |
self.assertEqual(self.func(data), 4) |
| 1665 |
|
| 1666 |
def test_even_fractions(self): |
| 1667 |
# Test median_high works with an even number of Fractions. |
| 1668 |
F = Fraction |
| 1669 |
data = [F(1, 7), F(2, 7), F(3, 7), F(4, 7), F(5, 7), F(6, 7)] |
| 1670 |
assert len(data)%2 == 0 |
| 1671 |
random.shuffle(data) |
| 1672 |
self.assertEqual(self.func(data), F(4, 7)) |
| 1673 |
|
| 1674 |
def test_even_decimals(self): |
| 1675 |
# Test median_high works with an even number of Decimals. |
| 1676 |
D = Decimal |
| 1677 |
data = [D('1.1'), D('2.2'), D('3.3'), D('4.4'), D('5.5'), D('6.6')] |
| 1678 |
assert len(data)%2 == 0 |
| 1679 |
random.shuffle(data) |
| 1680 |
self.assertEqual(self.func(data), D('4.4')) |
| 1681 |
|
| 1682 |
|
| 1683 |
class TestMedianGrouped(TestMedian): |
| 1684 |
# Test median_grouped. |
| 1685 |
# Doesn't conserve data element types, so don't use TestMedianType. |
| 1686 |
def setUp(self): |
| 1687 |
self.func = statistics.median_grouped |
| 1688 |
|
| 1689 |
def test_odd_number_repeated(self): |
| 1690 |
# Test median.grouped with repeated median values. |
| 1691 |
data = [12, 13, 14, 14, 14, 15, 15] |
| 1692 |
assert len(data)%2 == 1 |
| 1693 |
self.assertEqual(self.func(data), 14) |
| 1694 |
#--- |
| 1695 |
data = [12, 13, 14, 14, 14, 14, 15] |
| 1696 |
assert len(data)%2 == 1 |
| 1697 |
self.assertEqual(self.func(data), 13.875) |
| 1698 |
#--- |
| 1699 |
data = [5, 10, 10, 15, 20, 20, 20, 20, 25, 25, 30] |
| 1700 |
assert len(data)%2 == 1 |
| 1701 |
self.assertEqual(self.func(data, 5), 19.375) |
| 1702 |
#--- |
| 1703 |
data = [16, 18, 18, 18, 18, 20, 20, 20, 22, 22, 22, 24, 24, 26, 28] |
| 1704 |
assert len(data)%2 == 1 |
| 1705 |
self.assertApproxEqual(self.func(data, 2), 20.66666667, tol=1e-8) |
| 1706 |
|
| 1707 |
def test_even_number_repeated(self): |
| 1708 |
# Test median.grouped with repeated median values. |
| 1709 |
data = [5, 10, 10, 15, 20, 20, 20, 25, 25, 30] |
| 1710 |
assert len(data)%2 == 0 |
| 1711 |
self.assertApproxEqual(self.func(data, 5), 19.16666667, tol=1e-8) |
| 1712 |
#--- |
| 1713 |
data = [2, 3, 4, 4, 4, 5] |
| 1714 |
assert len(data)%2 == 0 |
| 1715 |
self.assertApproxEqual(self.func(data), 3.83333333, tol=1e-8) |
| 1716 |
#--- |
| 1717 |
data = [2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6] |
| 1718 |
assert len(data)%2 == 0 |
| 1719 |
self.assertEqual(self.func(data), 4.5) |
| 1720 |
#--- |
| 1721 |
data = [3, 4, 4, 4, 5, 5, 5, 5, 6, 6] |
| 1722 |
assert len(data)%2 == 0 |
| 1723 |
self.assertEqual(self.func(data), 4.75) |
| 1724 |
|
| 1725 |
def test_repeated_single_value(self): |
| 1726 |
# Override method from AverageMixin. |
| 1727 |
# Yet again, failure of median_grouped to conserve the data type |
| 1728 |
# causes me headaches :-( |
| 1729 |
for x in (5.3, 68, 4.3e17, Fraction(29, 101), Decimal('32.9714')): |
| 1730 |
for count in (2, 5, 10, 20): |
| 1731 |
data = [x]*count |
| 1732 |
self.assertEqual(self.func(data), float(x)) |
| 1733 |
|
| 1734 |
def test_single_value(self): |
| 1735 |
# Override method from AverageMixin. |
| 1736 |
# Average of a single value is the value as a float. |
| 1737 |
for x in (23, 42.5, 1.3e15, Fraction(15, 19), Decimal('0.28')): |
| 1738 |
self.assertEqual(self.func([x]), float(x)) |
| 1739 |
|
| 1740 |
def test_odd_fractions(self): |
| 1741 |
# Test median_grouped works with an odd number of Fractions. |
| 1742 |
F = Fraction |
| 1743 |
data = [F(5, 4), F(9, 4), F(13, 4), F(13, 4), F(17, 4)] |
| 1744 |
assert len(data)%2 == 1 |
| 1745 |
random.shuffle(data) |
| 1746 |
self.assertEqual(self.func(data), 3.0) |
| 1747 |
|
| 1748 |
def test_even_fractions(self): |
| 1749 |
# Test median_grouped works with an even number of Fractions. |
| 1750 |
F = Fraction |
| 1751 |
data = [F(5, 4), F(9, 4), F(13, 4), F(13, 4), F(17, 4), F(17, 4)] |
| 1752 |
assert len(data)%2 == 0 |
| 1753 |
random.shuffle(data) |
| 1754 |
self.assertEqual(self.func(data), 3.25) |
| 1755 |
|
| 1756 |
def test_odd_decimals(self): |
| 1757 |
# Test median_grouped works with an odd number of Decimals. |
| 1758 |
D = Decimal |
| 1759 |
data = [D('5.5'), D('6.5'), D('6.5'), D('7.5'), D('8.5')] |
| 1760 |
assert len(data)%2 == 1 |
| 1761 |
random.shuffle(data) |
| 1762 |
self.assertEqual(self.func(data), 6.75) |
| 1763 |
|
| 1764 |
def test_even_decimals(self): |
| 1765 |
# Test median_grouped works with an even number of Decimals. |
| 1766 |
D = Decimal |
| 1767 |
data = [D('5.5'), D('5.5'), D('6.5'), D('6.5'), D('7.5'), D('8.5')] |
| 1768 |
assert len(data)%2 == 0 |
| 1769 |
random.shuffle(data) |
| 1770 |
self.assertEqual(self.func(data), 6.5) |
| 1771 |
#--- |
| 1772 |
data = [D('5.5'), D('5.5'), D('6.5'), D('7.5'), D('7.5'), D('8.5')] |
| 1773 |
assert len(data)%2 == 0 |
| 1774 |
random.shuffle(data) |
| 1775 |
self.assertEqual(self.func(data), 7.0) |
| 1776 |
|
| 1777 |
def test_interval(self): |
| 1778 |
# Test median_grouped with interval argument. |
| 1779 |
data = [2.25, 2.5, 2.5, 2.75, 2.75, 3.0, 3.0, 3.25, 3.5, 3.75] |
| 1780 |
self.assertEqual(self.func(data, 0.25), 2.875) |
| 1781 |
data = [2.25, 2.5, 2.5, 2.75, 2.75, 2.75, 3.0, 3.0, 3.25, 3.5, 3.75] |
| 1782 |
self.assertApproxEqual(self.func(data, 0.25), 2.83333333, tol=1e-8) |
| 1783 |
data = [220, 220, 240, 260, 260, 260, 260, 280, 280, 300, 320, 340] |
| 1784 |
self.assertEqual(self.func(data, 20), 265.0) |
| 1785 |
|
| 1786 |
def test_data_type_error(self): |
| 1787 |
# Test median_grouped with str, bytes data types for data and interval |
| 1788 |
data = ["", "", ""] |
| 1789 |
self.assertRaises(TypeError, self.func, data) |
| 1790 |
#--- |
| 1791 |
data = [b"", b"", b""] |
| 1792 |
self.assertRaises(TypeError, self.func, data) |
| 1793 |
#--- |
| 1794 |
data = [1, 2, 3] |
| 1795 |
interval = "" |
| 1796 |
self.assertRaises(TypeError, self.func, data, interval) |
| 1797 |
#--- |
| 1798 |
data = [1, 2, 3] |
| 1799 |
interval = b"" |
| 1800 |
self.assertRaises(TypeError, self.func, data, interval) |
| 1801 |
|
| 1802 |
|
| 1803 |
class TestMode(NumericTestCase, AverageMixin, UnivariateTypeMixin): |
| 1804 |
# Test cases for the discrete version of mode. |
| 1805 |
def setUp(self): |
| 1806 |
self.func = statistics.mode |
| 1807 |
|
| 1808 |
def prepare_data(self): |
| 1809 |
"""Overload method from UnivariateCommonMixin.""" |
| 1810 |
# Make sure test data has exactly one mode. |
| 1811 |
return [1, 1, 1, 1, 3, 4, 7, 9, 0, 8, 2] |
| 1812 |
|
| 1813 |
def test_range_data(self): |
| 1814 |
# Override test from UnivariateCommonMixin. |
| 1815 |
data = range(20, 50, 3) |
| 1816 |
self.assertEqual(self.func(data), 20) |
| 1817 |
|
| 1818 |
def test_nominal_data(self): |
| 1819 |
# Test mode with nominal data. |
| 1820 |
data = 'abcbdb' |
| 1821 |
self.assertEqual(self.func(data), 'b') |
| 1822 |
data = 'fe fi fo fum fi fi'.split() |
| 1823 |
self.assertEqual(self.func(data), 'fi') |
| 1824 |
|
| 1825 |
def test_discrete_data(self): |
| 1826 |
# Test mode with discrete numeric data. |
| 1827 |
data = list(range(10)) |
| 1828 |
for i in range(10): |
| 1829 |
d = data + [i] |
| 1830 |
random.shuffle(d) |
| 1831 |
self.assertEqual(self.func(d), i) |
| 1832 |
|
| 1833 |
def test_bimodal_data(self): |
| 1834 |
# Test mode with bimodal data. |
| 1835 |
data = [1, 1, 2, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6, 7, 8, 9, 9] |
| 1836 |
assert data.count(2) == data.count(6) == 4 |
| 1837 |
# mode() should return 2, the first encountered mode |
| 1838 |
self.assertEqual(self.func(data), 2) |
| 1839 |
|
| 1840 |
def test_unique_data(self): |
| 1841 |
# Test mode when data points are all unique. |
| 1842 |
data = list(range(10)) |
| 1843 |
# mode() should return 0, the first encountered mode |
| 1844 |
self.assertEqual(self.func(data), 0) |
| 1845 |
|
| 1846 |
def test_none_data(self): |
| 1847 |
# Test that mode raises TypeError if given None as data. |
| 1848 |
|
| 1849 |
# This test is necessary because the implementation of mode uses |
| 1850 |
# collections.Counter, which accepts None and returns an empty dict. |
| 1851 |
self.assertRaises(TypeError, self.func, None) |
| 1852 |
|
| 1853 |
def test_counter_data(self): |
| 1854 |
# Test that a Counter is treated like any other iterable. |
| 1855 |
# We're making sure mode() first calls iter() on its input. |
| 1856 |
# The concern is that a Counter of a Counter returns the original |
| 1857 |
# unchanged rather than counting its keys. |
| 1858 |
c = collections.Counter(a=1, b=2) |
| 1859 |
# If iter() is called, mode(c) loops over the keys, ['a', 'b'], |
| 1860 |
# all the counts will be 1, and the first encountered mode is 'a'. |
| 1861 |
self.assertEqual(self.func(c), 'a') |
| 1862 |
|
| 1863 |
|
| 1864 |
class TestMultiMode(unittest.TestCase): |
| 1865 |
|
| 1866 |
def test_basics(self): |
| 1867 |
multimode = statistics.multimode |
| 1868 |
self.assertEqual(multimode('aabbbbbbbbcc'), ['b']) |
| 1869 |
self.assertEqual(multimode('aabbbbccddddeeffffgg'), ['b', 'd', 'f']) |
| 1870 |
self.assertEqual(multimode(''), []) |
| 1871 |
|
| 1872 |
|
| 1873 |
class TestFMean(unittest.TestCase): |
| 1874 |
|
| 1875 |
def test_basics(self): |
| 1876 |
fmean = statistics.fmean |
| 1877 |
D = Decimal |
| 1878 |
F = Fraction |
| 1879 |
for data, expected_mean, kind in [ |
| 1880 |
([3.5, 4.0, 5.25], 4.25, 'floats'), |
| 1881 |
([D('3.5'), D('4.0'), D('5.25')], 4.25, 'decimals'), |
| 1882 |
([F(7, 2), F(4, 1), F(21, 4)], 4.25, 'fractions'), |
| 1883 |
([True, False, True, True, False], 0.60, 'booleans'), |
| 1884 |
([3.5, 4, F(21, 4)], 4.25, 'mixed types'), |
| 1885 |
((3.5, 4.0, 5.25), 4.25, 'tuple'), |
| 1886 |
(iter([3.5, 4.0, 5.25]), 4.25, 'iterator'), |
| 1887 |
]: |
| 1888 |
actual_mean = fmean(data) |
| 1889 |
self.assertIs(type(actual_mean), float, kind) |
| 1890 |
self.assertEqual(actual_mean, expected_mean, kind) |
| 1891 |
|
| 1892 |
def test_error_cases(self): |
| 1893 |
fmean = statistics.fmean |
| 1894 |
StatisticsError = statistics.StatisticsError |
| 1895 |
with self.assertRaises(StatisticsError): |
| 1896 |
fmean([]) # empty input |
| 1897 |
with self.assertRaises(StatisticsError): |
| 1898 |
fmean(iter([])) # empty iterator |
| 1899 |
with self.assertRaises(TypeError): |
| 1900 |
fmean(None) # non-iterable input |
| 1901 |
with self.assertRaises(TypeError): |
| 1902 |
fmean([10, None, 20]) # non-numeric input |
| 1903 |
with self.assertRaises(TypeError): |
| 1904 |
fmean() # missing data argument |
| 1905 |
with self.assertRaises(TypeError): |
| 1906 |
fmean([10, 20, 60], 70) # too many arguments |
| 1907 |
|
| 1908 |
def test_special_values(self): |
| 1909 |
# Rules for special values are inherited from math.fsum() |
| 1910 |
fmean = statistics.fmean |
| 1911 |
NaN = float('Nan') |
| 1912 |
Inf = float('Inf') |
| 1913 |
self.assertTrue(math.isnan(fmean([10, NaN])), 'nan') |
| 1914 |
self.assertTrue(math.isnan(fmean([NaN, Inf])), 'nan and infinity') |
| 1915 |
self.assertTrue(math.isinf(fmean([10, Inf])), 'infinity') |
| 1916 |
with self.assertRaises(ValueError): |
| 1917 |
fmean([Inf, -Inf]) |
| 1918 |
|
| 1919 |
def test_weights(self): |
| 1920 |
fmean = statistics.fmean |
| 1921 |
StatisticsError = statistics.StatisticsError |
| 1922 |
self.assertEqual( |
| 1923 |
fmean([10, 10, 10, 50], [0.25] * 4), |
| 1924 |
fmean([10, 10, 10, 50])) |
| 1925 |
self.assertEqual( |
| 1926 |
fmean([10, 10, 20], [0.25, 0.25, 0.50]), |
| 1927 |
fmean([10, 10, 20, 20])) |
| 1928 |
self.assertEqual( # inputs are iterators |
| 1929 |
fmean(iter([10, 10, 20]), iter([0.25, 0.25, 0.50])), |
| 1930 |
fmean([10, 10, 20, 20])) |
| 1931 |
with self.assertRaises(StatisticsError): |
| 1932 |
fmean([10, 20, 30], [1, 2]) # unequal lengths |
| 1933 |
with self.assertRaises(StatisticsError): |
| 1934 |
fmean(iter([10, 20, 30]), iter([1, 2])) # unequal lengths |
| 1935 |
with self.assertRaises(StatisticsError): |
| 1936 |
fmean([10, 20], [-1, 1]) # sum of weights is zero |
| 1937 |
with self.assertRaises(StatisticsError): |
| 1938 |
fmean(iter([10, 20]), iter([-1, 1])) # sum of weights is zero |
| 1939 |
|
| 1940 |
|
| 1941 |
# === Tests for variances and standard deviations === |
| 1942 |
|
| 1943 |
class VarianceStdevMixin(UnivariateCommonMixin): |
| 1944 |
# Mixin class holding common tests for variance and std dev. |
| 1945 |
|
| 1946 |
# Subclasses should inherit from this before NumericTestClass, in order |
| 1947 |
# to see the rel attribute below. See testShiftData for an explanation. |
| 1948 |
|
| 1949 |
rel = 1e-12 |
| 1950 |
|
| 1951 |
def test_single_value(self): |
| 1952 |
# Deviation of a single value is zero. |
| 1953 |
for x in (11, 19.8, 4.6e14, Fraction(21, 34), Decimal('8.392')): |
| 1954 |
self.assertEqual(self.func([x]), 0) |
| 1955 |
|
| 1956 |
def test_repeated_single_value(self): |
| 1957 |
# The deviation of a single repeated value is zero. |
| 1958 |
for x in (7.2, 49, 8.1e15, Fraction(3, 7), Decimal('62.4802')): |
| 1959 |
for count in (2, 3, 5, 15): |
| 1960 |
data = [x]*count |
| 1961 |
self.assertEqual(self.func(data), 0) |
| 1962 |
|
| 1963 |
def test_domain_error_regression(self): |
| 1964 |
# Regression test for a domain error exception. |
| 1965 |
# (Thanks to Geremy Condra.) |
| 1966 |
data = [0.123456789012345]*10000 |
| 1967 |
# All the items are identical, so variance should be exactly zero. |
| 1968 |
# We allow some small round-off error, but not much. |
| 1969 |
result = self.func(data) |
| 1970 |
self.assertApproxEqual(result, 0.0, tol=5e-17) |
| 1971 |
self.assertGreaterEqual(result, 0) # A negative result must fail. |
| 1972 |
|
| 1973 |
def test_shift_data(self): |
| 1974 |
# Test that shifting the data by a constant amount does not affect |
| 1975 |
# the variance or stdev. Or at least not much. |
| 1976 |
|
| 1977 |
# Due to rounding, this test should be considered an ideal. We allow |
| 1978 |
# some tolerance away from "no change at all" by setting tol and/or rel |
| 1979 |
# attributes. Subclasses may set tighter or looser error tolerances. |
| 1980 |
raw = [1.03, 1.27, 1.94, 2.04, 2.58, 3.14, 4.75, 4.98, 5.42, 6.78] |
| 1981 |
expected = self.func(raw) |
| 1982 |
# Don't set shift too high, the bigger it is, the more rounding error. |
| 1983 |
shift = 1e5 |
| 1984 |
data = [x + shift for x in raw] |
| 1985 |
self.assertApproxEqual(self.func(data), expected) |
| 1986 |
|
| 1987 |
def test_shift_data_exact(self): |
| 1988 |
# Like test_shift_data, but result is always exact. |
| 1989 |
raw = [1, 3, 3, 4, 5, 7, 9, 10, 11, 16] |
| 1990 |
assert all(x==int(x) for x in raw) |
| 1991 |
expected = self.func(raw) |
| 1992 |
shift = 10**9 |
| 1993 |
data = [x + shift for x in raw] |
| 1994 |
self.assertEqual(self.func(data), expected) |
| 1995 |
|
| 1996 |
def test_iter_list_same(self): |
| 1997 |
# Test that iter data and list data give the same result. |
| 1998 |
|
| 1999 |
# This is an explicit test that iterators and lists are treated the |
| 2000 |
# same; justification for this test over and above the similar test |
| 2001 |
# in UnivariateCommonMixin is that an earlier design had variance and |
| 2002 |
# friends swap between one- and two-pass algorithms, which would |
| 2003 |
# sometimes give different results. |
| 2004 |
data = [random.uniform(-3, 8) for _ in range(1000)] |
| 2005 |
expected = self.func(data) |
| 2006 |
self.assertEqual(self.func(iter(data)), expected) |
| 2007 |
|
| 2008 |
class TestPVariance(VarianceStdevMixin, NumericTestCase, UnivariateTypeMixin): |
| 2009 |
# Tests for population variance. |
| 2010 |
def setUp(self): |
| 2011 |
self.func = statistics.pvariance |
| 2012 |
|
| 2013 |
def test_exact_uniform(self): |
| 2014 |
# Test the variance against an exact result for uniform data. |
| 2015 |
data = list(range(10000)) |
| 2016 |
random.shuffle(data) |
| 2017 |
expected = (10000**2 - 1)/12 # Exact value. |
| 2018 |
self.assertEqual(self.func(data), expected) |
| 2019 |
|
| 2020 |
def test_ints(self): |
| 2021 |
# Test population variance with int data. |
| 2022 |
data = [4, 7, 13, 16] |
| 2023 |
exact = 22.5 |
| 2024 |
self.assertEqual(self.func(data), exact) |
| 2025 |
|
| 2026 |
def test_fractions(self): |
| 2027 |
# Test population variance with Fraction data. |
| 2028 |
F = Fraction |
| 2029 |
data = [F(1, 4), F(1, 4), F(3, 4), F(7, 4)] |
| 2030 |
exact = F(3, 8) |
| 2031 |
result = self.func(data) |
| 2032 |
self.assertEqual(result, exact) |
| 2033 |
self.assertIsInstance(result, Fraction) |
| 2034 |
|
| 2035 |
def test_decimals(self): |
| 2036 |
# Test population variance with Decimal data. |
| 2037 |
D = Decimal |
| 2038 |
data = [D("12.1"), D("12.2"), D("12.5"), D("12.9")] |
| 2039 |
exact = D('0.096875') |
| 2040 |
result = self.func(data) |
| 2041 |
self.assertEqual(result, exact) |
| 2042 |
self.assertIsInstance(result, Decimal) |
| 2043 |
|
| 2044 |
def test_accuracy_bug_20499(self): |
| 2045 |
data = [0, 0, 1] |
| 2046 |
exact = 2 / 9 |
| 2047 |
result = self.func(data) |
| 2048 |
self.assertEqual(result, exact) |
| 2049 |
self.assertIsInstance(result, float) |
| 2050 |
|
| 2051 |
|
| 2052 |
class TestVariance(VarianceStdevMixin, NumericTestCase, UnivariateTypeMixin): |
| 2053 |
# Tests for sample variance. |
| 2054 |
def setUp(self): |
| 2055 |
self.func = statistics.variance |
| 2056 |
|
| 2057 |
def test_single_value(self): |
| 2058 |
# Override method from VarianceStdevMixin. |
| 2059 |
for x in (35, 24.7, 8.2e15, Fraction(19, 30), Decimal('4.2084')): |
| 2060 |
self.assertRaises(statistics.StatisticsError, self.func, [x]) |
| 2061 |
|
| 2062 |
def test_ints(self): |
| 2063 |
# Test sample variance with int data. |
| 2064 |
data = [4, 7, 13, 16] |
| 2065 |
exact = 30 |
| 2066 |
self.assertEqual(self.func(data), exact) |
| 2067 |
|
| 2068 |
def test_fractions(self): |
| 2069 |
# Test sample variance with Fraction data. |
| 2070 |
F = Fraction |
| 2071 |
data = [F(1, 4), F(1, 4), F(3, 4), F(7, 4)] |
| 2072 |
exact = F(1, 2) |
| 2073 |
result = self.func(data) |
| 2074 |
self.assertEqual(result, exact) |
| 2075 |
self.assertIsInstance(result, Fraction) |
| 2076 |
|
| 2077 |
def test_decimals(self): |
| 2078 |
# Test sample variance with Decimal data. |
| 2079 |
D = Decimal |
| 2080 |
data = [D(2), D(2), D(7), D(9)] |
| 2081 |
exact = 4*D('9.5')/D(3) |
| 2082 |
result = self.func(data) |
| 2083 |
self.assertEqual(result, exact) |
| 2084 |
self.assertIsInstance(result, Decimal) |
| 2085 |
|
| 2086 |
def test_center_not_at_mean(self): |
| 2087 |
data = (1.0, 2.0) |
| 2088 |
self.assertEqual(self.func(data), 0.5) |
| 2089 |
self.assertEqual(self.func(data, xbar=2.0), 1.0) |
| 2090 |
|
| 2091 |
def test_accuracy_bug_20499(self): |
| 2092 |
data = [0, 0, 2] |
| 2093 |
exact = 4 / 3 |
| 2094 |
result = self.func(data) |
| 2095 |
self.assertEqual(result, exact) |
| 2096 |
self.assertIsInstance(result, float) |
| 2097 |
|
| 2098 |
class TestPStdev(VarianceStdevMixin, NumericTestCase): |
| 2099 |
# Tests for population standard deviation. |
| 2100 |
def setUp(self): |
| 2101 |
self.func = statistics.pstdev |
| 2102 |
|
| 2103 |
def test_compare_to_variance(self): |
| 2104 |
# Test that stdev is, in fact, the square root of variance. |
| 2105 |
data = [random.uniform(-17, 24) for _ in range(1000)] |
| 2106 |
expected = math.sqrt(statistics.pvariance(data)) |
| 2107 |
self.assertEqual(self.func(data), expected) |
| 2108 |
|
| 2109 |
def test_center_not_at_mean(self): |
| 2110 |
# See issue: 40855 |
| 2111 |
data = (3, 6, 7, 10) |
| 2112 |
self.assertEqual(self.func(data), 2.5) |
| 2113 |
self.assertEqual(self.func(data, mu=0.5), 6.5) |
| 2114 |
|
| 2115 |
def test_gh_140938(self): |
| 2116 |
# Inputs with inf/nan should raise a ValueError |
| 2117 |
with self.assertRaises(ValueError): |
| 2118 |
self.func([1.0, math.inf]) |
| 2119 |
with self.assertRaises(ValueError): |
| 2120 |
self.func([1.0, math.nan]) |
| 2121 |
|
| 2122 |
|
| 2123 |
class TestSqrtHelpers(unittest.TestCase): |
| 2124 |
|
| 2125 |
def test_integer_sqrt_of_frac_rto(self): |
| 2126 |
for n, m in itertools.product(range(100), range(1, 1000)): |
| 2127 |
r = statistics._integer_sqrt_of_frac_rto(n, m) |
| 2128 |
self.assertIsInstance(r, int) |
| 2129 |
if r*r*m == n: |
| 2130 |
# Root is exact |
| 2131 |
continue |
| 2132 |
# Inexact, so the root should be odd |
| 2133 |
self.assertEqual(r&1, 1) |
| 2134 |
# Verify correct rounding |
| 2135 |
self.assertTrue(m * (r - 1)**2 < n < m * (r + 1)**2) |
| 2136 |
|
| 2137 |
@requires_IEEE_754 |
| 2138 |
@support.requires_resource('cpu') |
| 2139 |
def test_float_sqrt_of_frac(self): |
| 2140 |
|
| 2141 |
def is_root_correctly_rounded(x: Fraction, root: float) -> bool: |
| 2142 |
if not x: |
| 2143 |
return root == 0.0 |
| 2144 |
|
| 2145 |
# Extract adjacent representable floats |
| 2146 |
r_up: float = math.nextafter(root, math.inf) |
| 2147 |
r_down: float = math.nextafter(root, -math.inf) |
| 2148 |
assert r_down < root < r_up |
| 2149 |
|
| 2150 |
# Convert to fractions for exact arithmetic |
| 2151 |
frac_root: Fraction = Fraction(root) |
| 2152 |
half_way_up: Fraction = (frac_root + Fraction(r_up)) / 2 |
| 2153 |
half_way_down: Fraction = (frac_root + Fraction(r_down)) / 2 |
| 2154 |
|
| 2155 |
# Check a closed interval. |
| 2156 |
# Does not test for a midpoint rounding rule. |
| 2157 |
return half_way_down ** 2 <= x <= half_way_up ** 2 |
| 2158 |
|
| 2159 |
randrange = random.randrange |
| 2160 |
|
| 2161 |
for i in range(60_000): |
| 2162 |
numerator: int = randrange(10 ** randrange(50)) |
| 2163 |
denonimator: int = randrange(10 ** randrange(50)) + 1 |
| 2164 |
with self.subTest(numerator=numerator, denonimator=denonimator): |
| 2165 |
x: Fraction = Fraction(numerator, denonimator) |
| 2166 |
root: float = statistics._float_sqrt_of_frac(numerator, denonimator) |
| 2167 |
self.assertTrue(is_root_correctly_rounded(x, root)) |
| 2168 |
|
| 2169 |
# Verify that corner cases and error handling match math.sqrt() |
| 2170 |
self.assertEqual(statistics._float_sqrt_of_frac(0, 1), 0.0) |
| 2171 |
with self.assertRaises(ValueError): |
| 2172 |
statistics._float_sqrt_of_frac(-1, 1) |
| 2173 |
with self.assertRaises(ValueError): |
| 2174 |
statistics._float_sqrt_of_frac(1, -1) |
| 2175 |
|
| 2176 |
# Error handling for zero denominator matches that for Fraction(1, 0) |
| 2177 |
with self.assertRaises(ZeroDivisionError): |
| 2178 |
statistics._float_sqrt_of_frac(1, 0) |
| 2179 |
|
| 2180 |
# The result is well defined if both inputs are negative |
| 2181 |
self.assertEqual(statistics._float_sqrt_of_frac(-2, -1), statistics._float_sqrt_of_frac(2, 1)) |
| 2182 |
|
| 2183 |
def test_decimal_sqrt_of_frac(self): |
| 2184 |
root: Decimal |
| 2185 |
numerator: int |
| 2186 |
denominator: int |
| 2187 |
|
| 2188 |
for root, numerator, denominator in [ |
| 2189 |
(Decimal('0.4481904599041192673635338663'), 200874688349065940678243576378, 1000000000000000000000000000000), # No adj |
| 2190 |
(Decimal('0.7924949131383786609961759598'), 628048187350206338833590574929, 1000000000000000000000000000000), # Adj up |
| 2191 |
(Decimal('0.8500554152289934068192208727'), 722594208960136395984391238251, 1000000000000000000000000000000), # Adj down |
| 2192 |
]: |
| 2193 |
with decimal.localcontext(decimal.DefaultContext): |
| 2194 |
self.assertEqual(statistics._decimal_sqrt_of_frac(numerator, denominator), root) |
| 2195 |
|
| 2196 |
# Confirm expected root with a quad precision decimal computation |
| 2197 |
with decimal.localcontext(decimal.DefaultContext) as ctx: |
| 2198 |
ctx.prec *= 4 |
| 2199 |
high_prec_ratio = Decimal(numerator) / Decimal(denominator) |
| 2200 |
ctx.rounding = decimal.ROUND_05UP |
| 2201 |
high_prec_root = high_prec_ratio.sqrt() |
| 2202 |
with decimal.localcontext(decimal.DefaultContext): |
| 2203 |
target_root = +high_prec_root |
| 2204 |
self.assertEqual(root, target_root) |
| 2205 |
|
| 2206 |
# Verify that corner cases and error handling match Decimal.sqrt() |
| 2207 |
self.assertEqual(statistics._decimal_sqrt_of_frac(0, 1), 0.0) |
| 2208 |
with self.assertRaises(decimal.InvalidOperation): |
| 2209 |
statistics._decimal_sqrt_of_frac(-1, 1) |
| 2210 |
with self.assertRaises(decimal.InvalidOperation): |
| 2211 |
statistics._decimal_sqrt_of_frac(1, -1) |
| 2212 |
|
| 2213 |
# Error handling for zero denominator matches that for Fraction(1, 0) |
| 2214 |
with self.assertRaises(ZeroDivisionError): |
| 2215 |
statistics._decimal_sqrt_of_frac(1, 0) |
| 2216 |
|
| 2217 |
# The result is well defined if both inputs are negative |
| 2218 |
self.assertEqual(statistics._decimal_sqrt_of_frac(-2, -1), statistics._decimal_sqrt_of_frac(2, 1)) |
| 2219 |
|
| 2220 |
|
| 2221 |
class TestStdev(VarianceStdevMixin, NumericTestCase): |
| 2222 |
# Tests for sample standard deviation. |
| 2223 |
def setUp(self): |
| 2224 |
self.func = statistics.stdev |
| 2225 |
|
| 2226 |
def test_single_value(self): |
| 2227 |
# Override method from VarianceStdevMixin. |
| 2228 |
for x in (81, 203.74, 3.9e14, Fraction(5, 21), Decimal('35.719')): |
| 2229 |
self.assertRaises(statistics.StatisticsError, self.func, [x]) |
| 2230 |
|
| 2231 |
def test_compare_to_variance(self): |
| 2232 |
# Test that stdev is, in fact, the square root of variance. |
| 2233 |
data = [random.uniform(-2, 9) for _ in range(1000)] |
| 2234 |
expected = math.sqrt(statistics.variance(data)) |
| 2235 |
self.assertAlmostEqual(self.func(data), expected) |
| 2236 |
|
| 2237 |
def test_center_not_at_mean(self): |
| 2238 |
data = (1.0, 2.0) |
| 2239 |
self.assertEqual(self.func(data, xbar=2.0), 1.0) |
| 2240 |
|
| 2241 |
class TestGeometricMean(unittest.TestCase): |
| 2242 |
|
| 2243 |
def test_basics(self): |
| 2244 |
geometric_mean = statistics.geometric_mean |
| 2245 |
self.assertAlmostEqual(geometric_mean([54, 24, 36]), 36.0) |
| 2246 |
self.assertAlmostEqual(geometric_mean([4.0, 9.0]), 6.0) |
| 2247 |
self.assertAlmostEqual(geometric_mean([17.625]), 17.625) |
| 2248 |
|
| 2249 |
random.seed(86753095551212) |
| 2250 |
for rng in [ |
| 2251 |
range(1, 100), |
| 2252 |
range(1, 1_000), |
| 2253 |
range(1, 10_000), |
| 2254 |
range(500, 10_000, 3), |
| 2255 |
range(10_000, 500, -3), |
| 2256 |
[12, 17, 13, 5, 120, 7], |
| 2257 |
[random.expovariate(50.0) for i in range(1_000)], |
| 2258 |
[random.lognormvariate(20.0, 3.0) for i in range(2_000)], |
| 2259 |
[random.triangular(2000, 3000, 2200) for i in range(3_000)], |
| 2260 |
]: |
| 2261 |
gm_decimal = math.prod(map(Decimal, rng)) ** (Decimal(1) / len(rng)) |
| 2262 |
gm_float = geometric_mean(rng) |
| 2263 |
self.assertTrue(math.isclose(gm_float, float(gm_decimal))) |
| 2264 |
|
| 2265 |
def test_various_input_types(self): |
| 2266 |
geometric_mean = statistics.geometric_mean |
| 2267 |
D = Decimal |
| 2268 |
F = Fraction |
| 2269 |
# https://www.wolframalpha.com/input/?i=geometric+mean+3.5,+4.0,+5.25 |
| 2270 |
expected_mean = 4.18886 |
| 2271 |
for data, kind in [ |
| 2272 |
([3.5, 4.0, 5.25], 'floats'), |
| 2273 |
([D('3.5'), D('4.0'), D('5.25')], 'decimals'), |
| 2274 |
([F(7, 2), F(4, 1), F(21, 4)], 'fractions'), |
| 2275 |
([3.5, 4, F(21, 4)], 'mixed types'), |
| 2276 |
((3.5, 4.0, 5.25), 'tuple'), |
| 2277 |
(iter([3.5, 4.0, 5.25]), 'iterator'), |
| 2278 |
]: |
| 2279 |
actual_mean = geometric_mean(data) |
| 2280 |
self.assertIs(type(actual_mean), float, kind) |
| 2281 |
self.assertAlmostEqual(actual_mean, expected_mean, places=5) |
| 2282 |
|
| 2283 |
def test_big_and_small(self): |
| 2284 |
geometric_mean = statistics.geometric_mean |
| 2285 |
|
| 2286 |
# Avoid overflow to infinity |
| 2287 |
large = 2.0 ** 1000 |
| 2288 |
big_gm = geometric_mean([54.0 * large, 24.0 * large, 36.0 * large]) |
| 2289 |
self.assertTrue(math.isclose(big_gm, 36.0 * large)) |
| 2290 |
self.assertFalse(math.isinf(big_gm)) |
| 2291 |
|
| 2292 |
# Avoid underflow to zero |
| 2293 |
small = 2.0 ** -1000 |
| 2294 |
small_gm = geometric_mean([54.0 * small, 24.0 * small, 36.0 * small]) |
| 2295 |
self.assertTrue(math.isclose(small_gm, 36.0 * small)) |
| 2296 |
self.assertNotEqual(small_gm, 0.0) |
| 2297 |
|
| 2298 |
def test_error_cases(self): |
| 2299 |
geometric_mean = statistics.geometric_mean |
| 2300 |
StatisticsError = statistics.StatisticsError |
| 2301 |
with self.assertRaises(StatisticsError): |
| 2302 |
geometric_mean([]) # empty input |
| 2303 |
with self.assertRaises(StatisticsError): |
| 2304 |
geometric_mean([3.5, -4.0, 5.25]) # negative input |
| 2305 |
with self.assertRaises(StatisticsError): |
| 2306 |
geometric_mean([0.0, -4.0, 5.25]) # negative input with zero |
| 2307 |
with self.assertRaises(StatisticsError): |
| 2308 |
geometric_mean([3.5, -math.inf, 5.25]) # negative infinity |
| 2309 |
with self.assertRaises(StatisticsError): |
| 2310 |
geometric_mean(iter([])) # empty iterator |
| 2311 |
with self.assertRaises(TypeError): |
| 2312 |
geometric_mean(None) # non-iterable input |
| 2313 |
with self.assertRaises(TypeError): |
| 2314 |
geometric_mean([10, None, 20]) # non-numeric input |
| 2315 |
with self.assertRaises(TypeError): |
| 2316 |
geometric_mean() # missing data argument |
| 2317 |
with self.assertRaises(TypeError): |
| 2318 |
geometric_mean([10, 20, 60], 70) # too many arguments |
| 2319 |
|
| 2320 |
def test_special_values(self): |
| 2321 |
# Rules for special values are inherited from math.fsum() |
| 2322 |
geometric_mean = statistics.geometric_mean |
| 2323 |
NaN = float('Nan') |
| 2324 |
Inf = float('Inf') |
| 2325 |
self.assertTrue(math.isnan(geometric_mean([10, NaN])), 'nan') |
| 2326 |
self.assertTrue(math.isnan(geometric_mean([NaN, Inf])), 'nan and infinity') |
| 2327 |
self.assertTrue(math.isinf(geometric_mean([10, Inf])), 'infinity') |
| 2328 |
with self.assertRaises(ValueError): |
| 2329 |
geometric_mean([Inf, -Inf]) |
| 2330 |
|
| 2331 |
# Cases with zero |
| 2332 |
self.assertEqual(geometric_mean([3, 0.0, 5]), 0.0) # Any zero gives a zero |
| 2333 |
self.assertEqual(geometric_mean([3, -0.0, 5]), 0.0) # Negative zero allowed |
| 2334 |
self.assertTrue(math.isnan(geometric_mean([0, NaN]))) # NaN beats zero |
| 2335 |
self.assertTrue(math.isnan(geometric_mean([0, Inf]))) # Because 0.0 * Inf -> NaN |
| 2336 |
|
| 2337 |
def test_mixed_int_and_float(self): |
| 2338 |
# Regression test for b.p.o. issue #28327 |
| 2339 |
geometric_mean = statistics.geometric_mean |
| 2340 |
expected_mean = 3.80675409583932 |
| 2341 |
values = [ |
| 2342 |
[2, 3, 5, 7], |
| 2343 |
[2, 3, 5, 7.0], |
| 2344 |
[2, 3, 5.0, 7.0], |
| 2345 |
[2, 3.0, 5.0, 7.0], |
| 2346 |
[2.0, 3.0, 5.0, 7.0], |
| 2347 |
] |
| 2348 |
for v in values: |
| 2349 |
with self.subTest(v=v): |
| 2350 |
actual_mean = geometric_mean(v) |
| 2351 |
self.assertAlmostEqual(actual_mean, expected_mean, places=5) |
| 2352 |
|
| 2353 |
|
| 2354 |
class TestKDE(unittest.TestCase): |
| 2355 |
|
| 2356 |
@support.requires_resource('cpu') |
| 2357 |
def test_kde(self): |
| 2358 |
kde = statistics.kde |
| 2359 |
StatisticsError = statistics.StatisticsError |
| 2360 |
|
| 2361 |
kernels = ['normal', 'gauss', 'logistic', 'sigmoid', 'rectangular', |
| 2362 |
'uniform', 'triangular', 'parabolic', 'epanechnikov', |
| 2363 |
'quartic', 'biweight', 'triweight', 'cosine'] |
| 2364 |
|
| 2365 |
sample = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2] |
| 2366 |
|
| 2367 |
# The approximate integral of a PDF should be close to 1.0 |
| 2368 |
|
| 2369 |
def integrate(func, low, high, steps=10_000): |
| 2370 |
"Numeric approximation of a definite function integral." |
| 2371 |
dx = (high - low) / steps |
| 2372 |
midpoints = (low + (i + 1/2) * dx for i in range(steps)) |
| 2373 |
return sum(map(func, midpoints)) * dx |
| 2374 |
|
| 2375 |
for kernel in kernels: |
| 2376 |
with self.subTest(kernel=kernel): |
| 2377 |
f_hat = kde(sample, h=1.5, kernel=kernel) |
| 2378 |
area = integrate(f_hat, -20, 20) |
| 2379 |
self.assertAlmostEqual(area, 1.0, places=4) |
| 2380 |
|
| 2381 |
# Check CDF against an integral of the PDF |
| 2382 |
|
| 2383 |
data = [3, 5, 10, 12] |
| 2384 |
h = 2.3 |
| 2385 |
x = 10.5 |
| 2386 |
for kernel in kernels: |
| 2387 |
with self.subTest(kernel=kernel): |
| 2388 |
cdf = kde(data, h, kernel, cumulative=True) |
| 2389 |
f_hat = kde(data, h, kernel) |
| 2390 |
area = integrate(f_hat, -20, x, 100_000) |
| 2391 |
self.assertAlmostEqual(cdf(x), area, places=4) |
| 2392 |
|
| 2393 |
# Check error cases |
| 2394 |
|
| 2395 |
with self.assertRaises(StatisticsError): |
| 2396 |
kde([], h=1.0) # Empty dataset |
| 2397 |
with self.assertRaises(TypeError): |
| 2398 |
kde(['abc', 'def'], 1.5) # Non-numeric data |
| 2399 |
with self.assertRaises(TypeError): |
| 2400 |
kde(iter(sample), 1.5) # Data is not a sequence |
| 2401 |
with self.assertRaises(StatisticsError): |
| 2402 |
kde(sample, h=0.0) # Zero bandwidth |
| 2403 |
with self.assertRaises(StatisticsError): |
| 2404 |
kde(sample, h=-1.0) # Negative bandwidth |
| 2405 |
with self.assertRaises(TypeError): |
| 2406 |
kde(sample, h='str') # Wrong bandwidth type |
| 2407 |
with self.assertRaises(StatisticsError): |
| 2408 |
kde(sample, h=1.0, kernel='bogus') # Invalid kernel |
| 2409 |
with self.assertRaises(TypeError): |
| 2410 |
kde(sample, 1.0, 'gauss', True) # Positional cumulative argument |
| 2411 |
|
| 2412 |
# Test name and docstring of the generated function |
| 2413 |
|
| 2414 |
h = 1.5 |
| 2415 |
kernel = 'cosine' |
| 2416 |
f_hat = kde(sample, h, kernel) |
| 2417 |
self.assertEqual(f_hat.__name__, 'pdf') |
| 2418 |
self.assertIn(kernel, f_hat.__doc__) |
| 2419 |
self.assertIn(repr(h), f_hat.__doc__) |
| 2420 |
|
| 2421 |
# Test closed interval for the support boundaries. |
| 2422 |
# In particular, 'uniform' should non-zero at the boundaries. |
| 2423 |
|
| 2424 |
f_hat = kde([0], 1.0, 'uniform') |
| 2425 |
self.assertEqual(f_hat(-1.0), 1/2) |
| 2426 |
self.assertEqual(f_hat(1.0), 1/2) |
| 2427 |
|
| 2428 |
# Test online updates to data |
| 2429 |
|
| 2430 |
data = [1, 2] |
| 2431 |
f_hat = kde(data, 5.0, 'triangular') |
| 2432 |
self.assertEqual(f_hat(100), 0.0) |
| 2433 |
data.append(100) |
| 2434 |
self.assertGreater(f_hat(100), 0.0) |
| 2435 |
|
| 2436 |
def test_kde_kernel_specs(self): |
| 2437 |
# White-box test for the kernel formulas in isolation from |
| 2438 |
# their downstream use in kde() and kde_random() |
| 2439 |
kernel_specs = statistics._kernel_specs |
| 2440 |
|
| 2441 |
# Verify that cdf / invcdf will round trip |
| 2442 |
xarr = [i/100 for i in range(-100, 101)] |
| 2443 |
parr = [i/1000 + 5/10000 for i in range(1000)] |
| 2444 |
for kernel, spec in kernel_specs.items(): |
| 2445 |
cdf = spec['cdf'] |
| 2446 |
invcdf = spec['invcdf'] |
| 2447 |
with self.subTest(kernel=kernel): |
| 2448 |
for x in xarr: |
| 2449 |
self.assertAlmostEqual(invcdf(cdf(x)), x, places=6) |
| 2450 |
for p in parr: |
| 2451 |
self.assertAlmostEqual(cdf(invcdf(p)), p, places=11) |
| 2452 |
|
| 2453 |
@support.requires_resource('cpu') |
| 2454 |
def test_kde_random(self): |
| 2455 |
kde_random = statistics.kde_random |
| 2456 |
StatisticsError = statistics.StatisticsError |
| 2457 |
kernels = ['normal', 'gauss', 'logistic', 'sigmoid', 'rectangular', |
| 2458 |
'uniform', 'triangular', 'parabolic', 'epanechnikov', |
| 2459 |
'quartic', 'biweight', 'triweight', 'cosine'] |
| 2460 |
sample = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2] |
| 2461 |
|
| 2462 |
# Smoke test |
| 2463 |
|
| 2464 |
for kernel in kernels: |
| 2465 |
with self.subTest(kernel=kernel): |
| 2466 |
rand = kde_random(sample, h=1.5, kernel=kernel) |
| 2467 |
selections = [rand() for i in range(10)] |
| 2468 |
|
| 2469 |
# Check error cases |
| 2470 |
|
| 2471 |
with self.assertRaises(StatisticsError): |
| 2472 |
kde_random([], h=1.0) # Empty dataset |
| 2473 |
with self.assertRaises(TypeError): |
| 2474 |
kde_random(['abc', 'def'], 1.5) # Non-numeric data |
| 2475 |
with self.assertRaises(TypeError): |
| 2476 |
kde_random(iter(sample), 1.5) # Data is not a sequence |
| 2477 |
with self.assertRaises(StatisticsError): |
| 2478 |
kde_random(sample, h=-1.0) # Zero bandwidth |
| 2479 |
with self.assertRaises(StatisticsError): |
| 2480 |
kde_random(sample, h=0.0) # Negative bandwidth |
| 2481 |
with self.assertRaises(TypeError): |
| 2482 |
kde_random(sample, h='str') # Wrong bandwidth type |
| 2483 |
with self.assertRaises(StatisticsError): |
| 2484 |
kde_random(sample, h=1.0, kernel='bogus') # Invalid kernel |
| 2485 |
|
| 2486 |
# Test name and docstring of the generated function |
| 2487 |
|
| 2488 |
h = 1.5 |
| 2489 |
kernel = 'cosine' |
| 2490 |
rand = kde_random(sample, h, kernel) |
| 2491 |
self.assertEqual(rand.__name__, 'rand') |
| 2492 |
self.assertIn(kernel, rand.__doc__) |
| 2493 |
self.assertIn(repr(h), rand.__doc__) |
| 2494 |
|
| 2495 |
# Approximate distribution test: Compare a random sample to the expected distribution |
| 2496 |
|
| 2497 |
data = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2, 7.8, 14.3, 15.1, 15.3, 15.8, 17.0] |
| 2498 |
xarr = [x / 10 for x in range(-100, 250)] |
| 2499 |
n = 1_000_000 |
| 2500 |
h = 1.75 |
| 2501 |
dx = 0.1 |
| 2502 |
|
| 2503 |
def p_observed(x): |
| 2504 |
# P(x <= X < x+dx) |
| 2505 |
i = bisect.bisect_left(big_sample, x) |
| 2506 |
j = bisect.bisect_left(big_sample, x + dx) |
| 2507 |
return (j - i) / len(big_sample) |
| 2508 |
|
| 2509 |
def p_expected(x): |
| 2510 |
# P(x <= X < x+dx) |
| 2511 |
return F_hat(x + dx) - F_hat(x) |
| 2512 |
|
| 2513 |
for kernel in kernels: |
| 2514 |
with self.subTest(kernel=kernel): |
| 2515 |
|
| 2516 |
rand = kde_random(data, h, kernel, seed=8675309**2) |
| 2517 |
big_sample = sorted([rand() for i in range(n)]) |
| 2518 |
F_hat = statistics.kde(data, h, kernel, cumulative=True) |
| 2519 |
|
| 2520 |
for x in xarr: |
| 2521 |
self.assertTrue(math.isclose(p_observed(x), p_expected(x), abs_tol=0.0005)) |
| 2522 |
|
| 2523 |
# Test online updates to data |
| 2524 |
|
| 2525 |
data = [1, 2] |
| 2526 |
rand = kde_random(data, 5, 'triangular') |
| 2527 |
self.assertLess(max([rand() for i in range(5000)]), 10) |
| 2528 |
data.append(100) |
| 2529 |
self.assertGreater(max(rand() for i in range(5000)), 10) |
| 2530 |
|
| 2531 |
|
| 2532 |
class TestQuantiles(unittest.TestCase): |
| 2533 |
|
| 2534 |
def test_specific_cases(self): |
| 2535 |
# Match results computed by hand and cross-checked |
| 2536 |
# against the PERCENTILE.EXC function in MS Excel. |
| 2537 |
quantiles = statistics.quantiles |
| 2538 |
data = [120, 200, 250, 320, 350] |
| 2539 |
random.shuffle(data) |
| 2540 |
for n, expected in [ |
| 2541 |
(1, []), |
| 2542 |
(2, [250.0]), |
| 2543 |
(3, [200.0, 320.0]), |
| 2544 |
(4, [160.0, 250.0, 335.0]), |
| 2545 |
(5, [136.0, 220.0, 292.0, 344.0]), |
| 2546 |
(6, [120.0, 200.0, 250.0, 320.0, 350.0]), |
| 2547 |
(8, [100.0, 160.0, 212.5, 250.0, 302.5, 335.0, 357.5]), |
| 2548 |
(10, [88.0, 136.0, 184.0, 220.0, 250.0, 292.0, 326.0, 344.0, 362.0]), |
| 2549 |
(12, [80.0, 120.0, 160.0, 200.0, 225.0, 250.0, 285.0, 320.0, 335.0, |
| 2550 |
350.0, 365.0]), |
| 2551 |
(15, [72.0, 104.0, 136.0, 168.0, 200.0, 220.0, 240.0, 264.0, 292.0, |
| 2552 |
320.0, 332.0, 344.0, 356.0, 368.0]), |
| 2553 |
]: |
| 2554 |
self.assertEqual(expected, quantiles(data, n=n)) |
| 2555 |
self.assertEqual(len(quantiles(data, n=n)), n - 1) |
| 2556 |
# Preserve datatype when possible |
| 2557 |
for datatype in (float, Decimal, Fraction): |
| 2558 |
result = quantiles(map(datatype, data), n=n) |
| 2559 |
self.assertTrue(all(type(x) == datatype) for x in result) |
| 2560 |
self.assertEqual(result, list(map(datatype, expected))) |
| 2561 |
# Quantiles should be idempotent |
| 2562 |
if len(expected) >= 2: |
| 2563 |
self.assertEqual(quantiles(expected, n=n), expected) |
| 2564 |
# Cross-check against method='inclusive' which should give |
| 2565 |
# the same result after adding in minimum and maximum values |
| 2566 |
# extrapolated from the two lowest and two highest points. |
| 2567 |
sdata = sorted(data) |
| 2568 |
lo = 2 * sdata[0] - sdata[1] |
| 2569 |
hi = 2 * sdata[-1] - sdata[-2] |
| 2570 |
padded_data = data + [lo, hi] |
| 2571 |
self.assertEqual( |
| 2572 |
quantiles(data, n=n), |
| 2573 |
quantiles(padded_data, n=n, method='inclusive'), |
| 2574 |
(n, data), |
| 2575 |
) |
| 2576 |
# Invariant under translation and scaling |
| 2577 |
def f(x): |
| 2578 |
return 3.5 * x - 1234.675 |
| 2579 |
exp = list(map(f, expected)) |
| 2580 |
act = quantiles(map(f, data), n=n) |
| 2581 |
self.assertTrue(all(math.isclose(e, a) for e, a in zip(exp, act))) |
| 2582 |
# Q2 agrees with median() |
| 2583 |
for k in range(2, 60): |
| 2584 |
data = random.choices(range(100), k=k) |
| 2585 |
q1, q2, q3 = quantiles(data) |
| 2586 |
self.assertEqual(q2, statistics.median(data)) |
| 2587 |
|
| 2588 |
def test_specific_cases_inclusive(self): |
| 2589 |
# Match results computed by hand and cross-checked |
| 2590 |
# against the PERCENTILE.INC function in MS Excel |
| 2591 |
# and against the quantile() function in SciPy. |
| 2592 |
quantiles = statistics.quantiles |
| 2593 |
data = [100, 200, 400, 800] |
| 2594 |
random.shuffle(data) |
| 2595 |
for n, expected in [ |
| 2596 |
(1, []), |
| 2597 |
(2, [300.0]), |
| 2598 |
(3, [200.0, 400.0]), |
| 2599 |
(4, [175.0, 300.0, 500.0]), |
| 2600 |
(5, [160.0, 240.0, 360.0, 560.0]), |
| 2601 |
(6, [150.0, 200.0, 300.0, 400.0, 600.0]), |
| 2602 |
(8, [137.5, 175, 225.0, 300.0, 375.0, 500.0,650.0]), |
| 2603 |
(10, [130.0, 160.0, 190.0, 240.0, 300.0, 360.0, 440.0, 560.0, 680.0]), |
| 2604 |
(12, [125.0, 150.0, 175.0, 200.0, 250.0, 300.0, 350.0, 400.0, |
| 2605 |
500.0, 600.0, 700.0]), |
| 2606 |
(15, [120.0, 140.0, 160.0, 180.0, 200.0, 240.0, 280.0, 320.0, 360.0, |
| 2607 |
400.0, 480.0, 560.0, 640.0, 720.0]), |
| 2608 |
]: |
| 2609 |
self.assertEqual(expected, quantiles(data, n=n, method="inclusive")) |
| 2610 |
self.assertEqual(len(quantiles(data, n=n, method="inclusive")), n - 1) |
| 2611 |
# Preserve datatype when possible |
| 2612 |
for datatype in (float, Decimal, Fraction): |
| 2613 |
result = quantiles(map(datatype, data), n=n, method="inclusive") |
| 2614 |
self.assertTrue(all(type(x) == datatype) for x in result) |
| 2615 |
self.assertEqual(result, list(map(datatype, expected))) |
| 2616 |
# Invariant under translation and scaling |
| 2617 |
def f(x): |
| 2618 |
return 3.5 * x - 1234.675 |
| 2619 |
exp = list(map(f, expected)) |
| 2620 |
act = quantiles(map(f, data), n=n, method="inclusive") |
| 2621 |
self.assertTrue(all(math.isclose(e, a) for e, a in zip(exp, act))) |
| 2622 |
# Natural deciles |
| 2623 |
self.assertEqual(quantiles([0, 100], n=10, method='inclusive'), |
| 2624 |
[10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0]) |
| 2625 |
self.assertEqual(quantiles(range(0, 101), n=10, method='inclusive'), |
| 2626 |
[10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0]) |
| 2627 |
# Whenever n is smaller than the number of data points, running |
| 2628 |
# method='inclusive' should give the same result as method='exclusive' |
| 2629 |
# after the two included extreme points are removed. |
| 2630 |
data = [random.randrange(10_000) for i in range(501)] |
| 2631 |
actual = quantiles(data, n=32, method='inclusive') |
| 2632 |
data.remove(min(data)) |
| 2633 |
data.remove(max(data)) |
| 2634 |
expected = quantiles(data, n=32) |
| 2635 |
self.assertEqual(expected, actual) |
| 2636 |
# Q2 agrees with median() |
| 2637 |
for k in range(2, 60): |
| 2638 |
data = random.choices(range(100), k=k) |
| 2639 |
q1, q2, q3 = quantiles(data, method='inclusive') |
| 2640 |
self.assertEqual(q2, statistics.median(data)) |
| 2641 |
# Base case with a single data point: When estimating quantiles from |
| 2642 |
# a sample, we want to be able to add one sample point at a time, |
| 2643 |
# getting increasingly better estimates. |
| 2644 |
self.assertEqual(quantiles([10], n=4), [10.0, 10.0, 10.0]) |
| 2645 |
self.assertEqual(quantiles([10], n=4, method='exclusive'), [10.0, 10.0, 10.0]) |
| 2646 |
|
| 2647 |
def test_equal_inputs(self): |
| 2648 |
quantiles = statistics.quantiles |
| 2649 |
for n in range(2, 10): |
| 2650 |
data = [10.0] * n |
| 2651 |
self.assertEqual(quantiles(data), [10.0, 10.0, 10.0]) |
| 2652 |
self.assertEqual(quantiles(data, method='inclusive'), |
| 2653 |
[10.0, 10.0, 10.0]) |
| 2654 |
|
| 2655 |
def test_equal_sized_groups(self): |
| 2656 |
quantiles = statistics.quantiles |
| 2657 |
total = 10_000 |
| 2658 |
data = [random.expovariate(0.2) for i in range(total)] |
| 2659 |
while len(set(data)) != total: |
| 2660 |
data.append(random.expovariate(0.2)) |
| 2661 |
data.sort() |
| 2662 |
|
| 2663 |
# Cases where the group size exactly divides the total |
| 2664 |
for n in (1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000): |
| 2665 |
group_size = total // n |
| 2666 |
self.assertEqual( |
| 2667 |
[bisect.bisect(data, q) for q in quantiles(data, n=n)], |
| 2668 |
list(range(group_size, total, group_size))) |
| 2669 |
|
| 2670 |
# When the group sizes can't be exactly equal, they should |
| 2671 |
# differ by no more than one |
| 2672 |
for n in (13, 19, 59, 109, 211, 571, 1019, 1907, 5261, 9769): |
| 2673 |
group_sizes = {total // n, total // n + 1} |
| 2674 |
pos = [bisect.bisect(data, q) for q in quantiles(data, n=n)] |
| 2675 |
sizes = {q - p for p, q in zip(pos, pos[1:])} |
| 2676 |
self.assertTrue(sizes <= group_sizes) |
| 2677 |
|
| 2678 |
def test_error_cases(self): |
| 2679 |
quantiles = statistics.quantiles |
| 2680 |
StatisticsError = statistics.StatisticsError |
| 2681 |
with self.assertRaises(TypeError): |
| 2682 |
quantiles() # Missing arguments |
| 2683 |
with self.assertRaises(TypeError): |
| 2684 |
quantiles([10, 20, 30], 13, n=4) # Too many arguments |
| 2685 |
with self.assertRaises(TypeError): |
| 2686 |
quantiles([10, 20, 30], 4) # n is a positional argument |
| 2687 |
with self.assertRaises(StatisticsError): |
| 2688 |
quantiles([10, 20, 30], n=0) # n is zero |
| 2689 |
with self.assertRaises(StatisticsError): |
| 2690 |
quantiles([10, 20, 30], n=-1) # n is negative |
| 2691 |
with self.assertRaises(TypeError): |
| 2692 |
quantiles([10, 20, 30], n=1.5) # n is not an integer |
| 2693 |
with self.assertRaises(ValueError): |
| 2694 |
quantiles([10, 20, 30], method='X') # method is unknown |
| 2695 |
with self.assertRaises(StatisticsError): |
| 2696 |
quantiles([], n=4) # not enough data points |
| 2697 |
with self.assertRaises(TypeError): |
| 2698 |
quantiles([10, None, 30], n=4) # data is non-numeric |
| 2699 |
|
| 2700 |
|
| 2701 |
class TestBivariateStatistics(unittest.TestCase): |
| 2702 |
|
| 2703 |
def test_unequal_size_error(self): |
| 2704 |
for x, y in [ |
| 2705 |
([1, 2, 3], [1, 2]), |
| 2706 |
([1, 2], [1, 2, 3]), |
| 2707 |
]: |
| 2708 |
with self.assertRaises(statistics.StatisticsError): |
| 2709 |
statistics.covariance(x, y) |
| 2710 |
with self.assertRaises(statistics.StatisticsError): |
| 2711 |
statistics.correlation(x, y) |
| 2712 |
with self.assertRaises(statistics.StatisticsError): |
| 2713 |
statistics.linear_regression(x, y) |
| 2714 |
|
| 2715 |
def test_small_sample_error(self): |
| 2716 |
for x, y in [ |
| 2717 |
([], []), |
| 2718 |
([], [1, 2,]), |
| 2719 |
([1, 2,], []), |
| 2720 |
([1,], [1,]), |
| 2721 |
([1,], [1, 2,]), |
| 2722 |
([1, 2,], [1,]), |
| 2723 |
]: |
| 2724 |
with self.assertRaises(statistics.StatisticsError): |
| 2725 |
statistics.covariance(x, y) |
| 2726 |
with self.assertRaises(statistics.StatisticsError): |
| 2727 |
statistics.correlation(x, y) |
| 2728 |
with self.assertRaises(statistics.StatisticsError): |
| 2729 |
statistics.linear_regression(x, y) |
| 2730 |
|
| 2731 |
|
| 2732 |
class TestCorrelationAndCovariance(unittest.TestCase): |
| 2733 |
|
| 2734 |
def test_results(self): |
| 2735 |
for x, y, result in [ |
| 2736 |
([1, 2, 3], [1, 2, 3], 1), |
| 2737 |
([1, 2, 3], [-1, -2, -3], -1), |
| 2738 |
([1, 2, 3], [3, 2, 1], -1), |
| 2739 |
([1, 2, 3], [1, 2, 1], 0), |
| 2740 |
([1, 2, 3], [1, 3, 2], 0.5), |
| 2741 |
]: |
| 2742 |
self.assertAlmostEqual(statistics.correlation(x, y), result) |
| 2743 |
self.assertAlmostEqual(statistics.covariance(x, y), result) |
| 2744 |
|
| 2745 |
def test_different_scales(self): |
| 2746 |
x = [1, 2, 3] |
| 2747 |
y = [10, 30, 20] |
| 2748 |
self.assertAlmostEqual(statistics.correlation(x, y), 0.5) |
| 2749 |
self.assertAlmostEqual(statistics.covariance(x, y), 5) |
| 2750 |
|
| 2751 |
y = [.1, .2, .3] |
| 2752 |
self.assertAlmostEqual(statistics.correlation(x, y), 1) |
| 2753 |
self.assertAlmostEqual(statistics.covariance(x, y), 0.1) |
| 2754 |
|
| 2755 |
def test_sqrtprod_helper_function_fundamentals(self): |
| 2756 |
# Verify that results are close to sqrt(x * y) |
| 2757 |
for i in range(100): |
| 2758 |
x = random.expovariate() |
| 2759 |
y = random.expovariate() |
| 2760 |
expected = math.sqrt(x * y) |
| 2761 |
actual = statistics._sqrtprod(x, y) |
| 2762 |
with self.subTest(x=x, y=y, expected=expected, actual=actual): |
| 2763 |
self.assertAlmostEqual(expected, actual) |
| 2764 |
|
| 2765 |
x, y, target = 0.8035720646477457, 0.7957468097636939, 0.7996498651651661 |
| 2766 |
self.assertEqual(statistics._sqrtprod(x, y), target) |
| 2767 |
self.assertNotEqual(math.sqrt(x * y), target) |
| 2768 |
|
| 2769 |
# Test that range extremes avoid underflow and overflow |
| 2770 |
smallest = sys.float_info.min * sys.float_info.epsilon |
| 2771 |
self.assertEqual(statistics._sqrtprod(smallest, smallest), smallest) |
| 2772 |
biggest = sys.float_info.max |
| 2773 |
self.assertEqual(statistics._sqrtprod(biggest, biggest), biggest) |
| 2774 |
|
| 2775 |
# Check special values and the sign of the result |
| 2776 |
special_values = [0.0, -0.0, 1.0, -1.0, 4.0, -4.0, |
| 2777 |
math.nan, -math.nan, math.inf, -math.inf] |
| 2778 |
for x, y in itertools.product(special_values, repeat=2): |
| 2779 |
try: |
| 2780 |
expected = math.sqrt(x * y) |
| 2781 |
except ValueError: |
| 2782 |
expected = 'ValueError' |
| 2783 |
try: |
| 2784 |
actual = statistics._sqrtprod(x, y) |
| 2785 |
except ValueError: |
| 2786 |
actual = 'ValueError' |
| 2787 |
with self.subTest(x=x, y=y, expected=expected, actual=actual): |
| 2788 |
if isinstance(expected, str) and expected == 'ValueError': |
| 2789 |
self.assertEqual(actual, 'ValueError') |
| 2790 |
continue |
| 2791 |
self.assertIsInstance(actual, float) |
| 2792 |
if math.isnan(expected): |
| 2793 |
self.assertTrue(math.isnan(actual)) |
| 2794 |
continue |
| 2795 |
self.assertEqual(actual, expected) |
| 2796 |
self.assertEqual(sign(actual), sign(expected)) |
| 2797 |
|
| 2798 |
@requires_IEEE_754 |
| 2799 |
@unittest.skipIf(HAVE_DOUBLE_ROUNDING, |
| 2800 |
"accuracy not guaranteed on machines with double rounding") |
| 2801 |
@support.cpython_only # Allow for a weaker sumprod() implementation |
| 2802 |
def test_sqrtprod_helper_function_improved_accuracy(self): |
| 2803 |
# Test a known example where accuracy is improved |
| 2804 |
x, y, target = 0.8035720646477457, 0.7957468097636939, 0.7996498651651661 |
| 2805 |
self.assertEqual(statistics._sqrtprod(x, y), target) |
| 2806 |
self.assertNotEqual(math.sqrt(x * y), target) |
| 2807 |
|
| 2808 |
def reference_value(x: float, y: float) -> float: |
| 2809 |
x = decimal.Decimal(x) |
| 2810 |
y = decimal.Decimal(y) |
| 2811 |
with decimal.localcontext() as ctx: |
| 2812 |
ctx.prec = 200 |
| 2813 |
return float((x * y).sqrt()) |
| 2814 |
|
| 2815 |
# Verify that the new function with improved accuracy |
| 2816 |
# agrees with a reference value more often than old version. |
| 2817 |
new_agreements = 0 |
| 2818 |
old_agreements = 0 |
| 2819 |
for i in range(10_000): |
| 2820 |
x = random.expovariate() |
| 2821 |
y = random.expovariate() |
| 2822 |
new = statistics._sqrtprod(x, y) |
| 2823 |
old = math.sqrt(x * y) |
| 2824 |
ref = reference_value(x, y) |
| 2825 |
new_agreements += (new == ref) |
| 2826 |
old_agreements += (old == ref) |
| 2827 |
self.assertGreater(new_agreements, old_agreements) |
| 2828 |
|
| 2829 |
def test_correlation_spearman(self): |
| 2830 |
# https://statistics.laerd.com/statistical-guides/spearmans-rank-order-correlation-statistical-guide-2.php |
| 2831 |
# Compare with: |
| 2832 |
# >>> import scipy.stats.mstats |
| 2833 |
# >>> scipy.stats.mstats.spearmanr(reading, mathematics) |
| 2834 |
# SpearmanrResult(correlation=0.6686960980480712, pvalue=0.03450954165178532) |
| 2835 |
# And Wolfram Alpha gives: 0.668696 |
| 2836 |
# https://www.wolframalpha.com/input?i=SpearmanRho%5B%7B56%2C+75%2C+45%2C+71%2C+61%2C+64%2C+58%2C+80%2C+76%2C+61%7D%2C+%7B66%2C+70%2C+40%2C+60%2C+65%2C+56%2C+59%2C+77%2C+67%2C+63%7D%5D |
| 2837 |
reading = [56, 75, 45, 71, 61, 64, 58, 80, 76, 61] |
| 2838 |
mathematics = [66, 70, 40, 60, 65, 56, 59, 77, 67, 63] |
| 2839 |
self.assertAlmostEqual(statistics.correlation(reading, mathematics, method='ranked'), |
| 2840 |
0.6686960980480712) |
| 2841 |
|
| 2842 |
with self.assertRaises(ValueError): |
| 2843 |
statistics.correlation(reading, mathematics, method='bad_method') |
| 2844 |
|
| 2845 |
class TestLinearRegression(unittest.TestCase): |
| 2846 |
|
| 2847 |
def test_constant_input_error(self): |
| 2848 |
x = [1, 1, 1,] |
| 2849 |
y = [1, 2, 3,] |
| 2850 |
with self.assertRaises(statistics.StatisticsError): |
| 2851 |
statistics.linear_regression(x, y) |
| 2852 |
|
| 2853 |
def test_results(self): |
| 2854 |
for x, y, true_intercept, true_slope in [ |
| 2855 |
([1, 2, 3], [0, 0, 0], 0, 0), |
| 2856 |
([1, 2, 3], [1, 2, 3], 0, 1), |
| 2857 |
([1, 2, 3], [100, 100, 100], 100, 0), |
| 2858 |
([1, 2, 3], [12, 14, 16], 10, 2), |
| 2859 |
([1, 2, 3], [-1, -2, -3], 0, -1), |
| 2860 |
([1, 2, 3], [21, 22, 23], 20, 1), |
| 2861 |
([1, 2, 3], [5.1, 5.2, 5.3], 5, 0.1), |
| 2862 |
]: |
| 2863 |
slope, intercept = statistics.linear_regression(x, y) |
| 2864 |
self.assertAlmostEqual(intercept, true_intercept) |
| 2865 |
self.assertAlmostEqual(slope, true_slope) |
| 2866 |
|
| 2867 |
def test_proportional(self): |
| 2868 |
x = [10, 20, 30, 40] |
| 2869 |
y = [180, 398, 610, 799] |
| 2870 |
slope, intercept = statistics.linear_regression(x, y, proportional=True) |
| 2871 |
self.assertAlmostEqual(slope, 20 + 1/150) |
| 2872 |
self.assertEqual(intercept, 0.0) |
| 2873 |
|
| 2874 |
def test_float_output(self): |
| 2875 |
x = [Fraction(2, 3), Fraction(3, 4)] |
| 2876 |
y = [Fraction(4, 5), Fraction(5, 6)] |
| 2877 |
slope, intercept = statistics.linear_regression(x, y) |
| 2878 |
self.assertTrue(isinstance(slope, float)) |
| 2879 |
self.assertTrue(isinstance(intercept, float)) |
| 2880 |
slope, intercept = statistics.linear_regression(x, y, proportional=True) |
| 2881 |
self.assertTrue(isinstance(slope, float)) |
| 2882 |
self.assertTrue(isinstance(intercept, float)) |
| 2883 |
|
| 2884 |
class TestNormalDist: |
| 2885 |
|
| 2886 |
# General note on precision: The pdf(), cdf(), and overlap() methods |
| 2887 |
# depend on functions in the math libraries that do not make |
| 2888 |
# explicit accuracy guarantees. Accordingly, some of the accuracy |
| 2889 |
# tests below may fail if the underlying math functions are |
| 2890 |
# inaccurate. There isn't much we can do about this short of |
| 2891 |
# implementing our own implementations from scratch. |
| 2892 |
|
| 2893 |
def test_slots(self): |
| 2894 |
nd = self.module.NormalDist(300, 23) |
| 2895 |
with self.assertRaises(TypeError): |
| 2896 |
vars(nd) |
| 2897 |
self.assertEqual(tuple(nd.__slots__), ('_mu', '_sigma')) |
| 2898 |
|
| 2899 |
def test_instantiation_and_attributes(self): |
| 2900 |
nd = self.module.NormalDist(500, 17) |
| 2901 |
self.assertEqual(nd.mean, 500) |
| 2902 |
self.assertEqual(nd.stdev, 17) |
| 2903 |
self.assertEqual(nd.variance, 17**2) |
| 2904 |
|
| 2905 |
# default arguments |
| 2906 |
nd = self.module.NormalDist() |
| 2907 |
self.assertEqual(nd.mean, 0) |
| 2908 |
self.assertEqual(nd.stdev, 1) |
| 2909 |
self.assertEqual(nd.variance, 1**2) |
| 2910 |
|
| 2911 |
# error case: negative sigma |
| 2912 |
with self.assertRaises(self.module.StatisticsError): |
| 2913 |
self.module.NormalDist(500, -10) |
| 2914 |
|
| 2915 |
# verify that subclass type is honored |
| 2916 |
class NewNormalDist(self.module.NormalDist): |
| 2917 |
pass |
| 2918 |
nnd = NewNormalDist(200, 5) |
| 2919 |
self.assertEqual(type(nnd), NewNormalDist) |
| 2920 |
|
| 2921 |
def test_alternative_constructor(self): |
| 2922 |
NormalDist = self.module.NormalDist |
| 2923 |
data = [96, 107, 90, 92, 110] |
| 2924 |
# list input |
| 2925 |
self.assertEqual(NormalDist.from_samples(data), NormalDist(99, 9)) |
| 2926 |
# tuple input |
| 2927 |
self.assertEqual(NormalDist.from_samples(tuple(data)), NormalDist(99, 9)) |
| 2928 |
# iterator input |
| 2929 |
self.assertEqual(NormalDist.from_samples(iter(data)), NormalDist(99, 9)) |
| 2930 |
# error cases |
| 2931 |
with self.assertRaises(self.module.StatisticsError): |
| 2932 |
NormalDist.from_samples([]) # empty input |
| 2933 |
with self.assertRaises(self.module.StatisticsError): |
| 2934 |
NormalDist.from_samples([10]) # only one input |
| 2935 |
|
| 2936 |
# verify that subclass type is honored |
| 2937 |
class NewNormalDist(NormalDist): |
| 2938 |
pass |
| 2939 |
nnd = NewNormalDist.from_samples(data) |
| 2940 |
self.assertEqual(type(nnd), NewNormalDist) |
| 2941 |
|
| 2942 |
def test_sample_generation(self): |
| 2943 |
NormalDist = self.module.NormalDist |
| 2944 |
mu, sigma = 10_000, 3.0 |
| 2945 |
X = NormalDist(mu, sigma) |
| 2946 |
n = 1_000 |
| 2947 |
data = X.samples(n) |
| 2948 |
self.assertEqual(len(data), n) |
| 2949 |
self.assertEqual(set(map(type, data)), {float}) |
| 2950 |
# mean(data) expected to fall within 8 standard deviations |
| 2951 |
xbar = self.module.mean(data) |
| 2952 |
self.assertTrue(mu - sigma*8 <= xbar <= mu + sigma*8) |
| 2953 |
|
| 2954 |
# verify that seeding makes reproducible sequences |
| 2955 |
n = 100 |
| 2956 |
data1 = X.samples(n, seed='happiness and joy') |
| 2957 |
data2 = X.samples(n, seed='trouble and despair') |
| 2958 |
data3 = X.samples(n, seed='happiness and joy') |
| 2959 |
data4 = X.samples(n, seed='trouble and despair') |
| 2960 |
self.assertEqual(data1, data3) |
| 2961 |
self.assertEqual(data2, data4) |
| 2962 |
self.assertNotEqual(data1, data2) |
| 2963 |
|
| 2964 |
def test_pdf(self): |
| 2965 |
NormalDist = self.module.NormalDist |
| 2966 |
X = NormalDist(100, 15) |
| 2967 |
# Verify peak around center |
| 2968 |
self.assertLess(X.pdf(99), X.pdf(100)) |
| 2969 |
self.assertLess(X.pdf(101), X.pdf(100)) |
| 2970 |
# Test symmetry |
| 2971 |
for i in range(50): |
| 2972 |
self.assertAlmostEqual(X.pdf(100 - i), X.pdf(100 + i)) |
| 2973 |
# Test vs CDF |
| 2974 |
dx = 2.0 ** -10 |
| 2975 |
for x in range(90, 111): |
| 2976 |
est_pdf = (X.cdf(x + dx) - X.cdf(x)) / dx |
| 2977 |
self.assertAlmostEqual(X.pdf(x), est_pdf, places=4) |
| 2978 |
# Test vs table of known values -- CRC 26th Edition |
| 2979 |
Z = NormalDist() |
| 2980 |
for x, px in enumerate([ |
| 2981 |
0.3989, 0.3989, 0.3989, 0.3988, 0.3986, |
| 2982 |
0.3984, 0.3982, 0.3980, 0.3977, 0.3973, |
| 2983 |
0.3970, 0.3965, 0.3961, 0.3956, 0.3951, |
| 2984 |
0.3945, 0.3939, 0.3932, 0.3925, 0.3918, |
| 2985 |
0.3910, 0.3902, 0.3894, 0.3885, 0.3876, |
| 2986 |
0.3867, 0.3857, 0.3847, 0.3836, 0.3825, |
| 2987 |
0.3814, 0.3802, 0.3790, 0.3778, 0.3765, |
| 2988 |
0.3752, 0.3739, 0.3725, 0.3712, 0.3697, |
| 2989 |
0.3683, 0.3668, 0.3653, 0.3637, 0.3621, |
| 2990 |
0.3605, 0.3589, 0.3572, 0.3555, 0.3538, |
| 2991 |
]): |
| 2992 |
self.assertAlmostEqual(Z.pdf(x / 100.0), px, places=4) |
| 2993 |
self.assertAlmostEqual(Z.pdf(-x / 100.0), px, places=4) |
| 2994 |
# Error case: variance is zero |
| 2995 |
Y = NormalDist(100, 0) |
| 2996 |
with self.assertRaises(self.module.StatisticsError): |
| 2997 |
Y.pdf(90) |
| 2998 |
# Special values |
| 2999 |
self.assertEqual(X.pdf(float('-Inf')), 0.0) |
| 3000 |
self.assertEqual(X.pdf(float('Inf')), 0.0) |
| 3001 |
self.assertTrue(math.isnan(X.pdf(float('NaN')))) |
| 3002 |
|
| 3003 |
def test_cdf(self): |
| 3004 |
NormalDist = self.module.NormalDist |
| 3005 |
X = NormalDist(100, 15) |
| 3006 |
cdfs = [X.cdf(x) for x in range(1, 200)] |
| 3007 |
self.assertEqual(set(map(type, cdfs)), {float}) |
| 3008 |
# Verify montonic |
| 3009 |
self.assertEqual(cdfs, sorted(cdfs)) |
| 3010 |
# Verify center (should be exact) |
| 3011 |
self.assertEqual(X.cdf(100), 0.50) |
| 3012 |
# Check against a table of known values |
| 3013 |
# https://en.wikipedia.org/wiki/Standard_normal_table#Cumulative |
| 3014 |
Z = NormalDist() |
| 3015 |
for z, cum_prob in [ |
| 3016 |
(0.00, 0.50000), (0.01, 0.50399), (0.02, 0.50798), |
| 3017 |
(0.14, 0.55567), (0.29, 0.61409), (0.33, 0.62930), |
| 3018 |
(0.54, 0.70540), (0.60, 0.72575), (1.17, 0.87900), |
| 3019 |
(1.60, 0.94520), (2.05, 0.97982), (2.89, 0.99807), |
| 3020 |
(3.52, 0.99978), (3.98, 0.99997), (4.07, 0.99998), |
| 3021 |
]: |
| 3022 |
self.assertAlmostEqual(Z.cdf(z), cum_prob, places=5) |
| 3023 |
self.assertAlmostEqual(Z.cdf(-z), 1.0 - cum_prob, places=5) |
| 3024 |
# Error case: variance is zero |
| 3025 |
Y = NormalDist(100, 0) |
| 3026 |
with self.assertRaises(self.module.StatisticsError): |
| 3027 |
Y.cdf(90) |
| 3028 |
# Special values |
| 3029 |
self.assertEqual(X.cdf(float('-Inf')), 0.0) |
| 3030 |
self.assertEqual(X.cdf(float('Inf')), 1.0) |
| 3031 |
self.assertTrue(math.isnan(X.cdf(float('NaN')))) |
| 3032 |
|
| 3033 |
@support.skip_if_pgo_task |
| 3034 |
@support.requires_resource('cpu') |
| 3035 |
def test_inv_cdf(self): |
| 3036 |
NormalDist = self.module.NormalDist |
| 3037 |
|
| 3038 |
# Center case should be exact. |
| 3039 |
iq = NormalDist(100, 15) |
| 3040 |
self.assertEqual(iq.inv_cdf(0.50), iq.mean) |
| 3041 |
|
| 3042 |
# Test versus a published table of known percentage points. |
| 3043 |
# See the second table at the bottom of the page here: |
| 3044 |
# http://people.bath.ac.uk/masss/tables/normaltable.pdf |
| 3045 |
Z = NormalDist() |
| 3046 |
pp = {5.0: (0.000, 1.645, 2.576, 3.291, 3.891, |
| 3047 |
4.417, 4.892, 5.327, 5.731, 6.109), |
| 3048 |
2.5: (0.674, 1.960, 2.807, 3.481, 4.056, |
| 3049 |
4.565, 5.026, 5.451, 5.847, 6.219), |
| 3050 |
1.0: (1.282, 2.326, 3.090, 3.719, 4.265, |
| 3051 |
4.753, 5.199, 5.612, 5.998, 6.361)} |
| 3052 |
for base, row in pp.items(): |
| 3053 |
for exp, x in enumerate(row, start=1): |
| 3054 |
p = base * 10.0 ** (-exp) |
| 3055 |
self.assertAlmostEqual(-Z.inv_cdf(p), x, places=3) |
| 3056 |
p = 1.0 - p |
| 3057 |
self.assertAlmostEqual(Z.inv_cdf(p), x, places=3) |
| 3058 |
|
| 3059 |
# Match published example for MS Excel |
| 3060 |
# https://support.office.com/en-us/article/norm-inv-function-54b30935-fee7-493c-bedb-2278a9db7e13 |
| 3061 |
self.assertAlmostEqual(NormalDist(40, 1.5).inv_cdf(0.908789), 42.000002) |
| 3062 |
|
| 3063 |
# One million equally spaced probabilities |
| 3064 |
n = 2**20 |
| 3065 |
for p in range(1, n): |
| 3066 |
p /= n |
| 3067 |
self.assertAlmostEqual(iq.cdf(iq.inv_cdf(p)), p) |
| 3068 |
|
| 3069 |
# One hundred ever smaller probabilities to test tails out to |
| 3070 |
# extreme probabilities: 1 / 2**50 and (2**50-1) / 2 ** 50 |
| 3071 |
for e in range(1, 51): |
| 3072 |
p = 2.0 ** (-e) |
| 3073 |
self.assertAlmostEqual(iq.cdf(iq.inv_cdf(p)), p) |
| 3074 |
p = 1.0 - p |
| 3075 |
self.assertAlmostEqual(iq.cdf(iq.inv_cdf(p)), p) |
| 3076 |
|
| 3077 |
# Now apply cdf() first. Near the tails, the round-trip loses |
| 3078 |
# precision and is ill-conditioned (small changes in the inputs |
| 3079 |
# give large changes in the output), so only check to 5 places. |
| 3080 |
for x in range(200): |
| 3081 |
self.assertAlmostEqual(iq.inv_cdf(iq.cdf(x)), x, places=5) |
| 3082 |
|
| 3083 |
# Error cases: |
| 3084 |
with self.assertRaises(self.module.StatisticsError): |
| 3085 |
iq.inv_cdf(0.0) # p is zero |
| 3086 |
with self.assertRaises(self.module.StatisticsError): |
| 3087 |
iq.inv_cdf(-0.1) # p under zero |
| 3088 |
with self.assertRaises(self.module.StatisticsError): |
| 3089 |
iq.inv_cdf(1.0) # p is one |
| 3090 |
with self.assertRaises(self.module.StatisticsError): |
| 3091 |
iq.inv_cdf(1.1) # p over one |
| 3092 |
|
| 3093 |
# Supported case: |
| 3094 |
iq = NormalDist(100, 0) # sigma is zero |
| 3095 |
self.assertEqual(iq.inv_cdf(0.5), 100) |
| 3096 |
|
| 3097 |
# Special values |
| 3098 |
self.assertTrue(math.isnan(Z.inv_cdf(float('NaN')))) |
| 3099 |
|
| 3100 |
def test_quantiles(self): |
| 3101 |
# Quartiles of a standard normal distribution |
| 3102 |
Z = self.module.NormalDist() |
| 3103 |
for n, expected in [ |
| 3104 |
(1, []), |
| 3105 |
(2, [0.0]), |
| 3106 |
(3, [-0.4307, 0.4307]), |
| 3107 |
(4 ,[-0.6745, 0.0, 0.6745]), |
| 3108 |
]: |
| 3109 |
actual = Z.quantiles(n=n) |
| 3110 |
self.assertTrue(all(math.isclose(e, a, abs_tol=0.0001) |
| 3111 |
for e, a in zip(expected, actual))) |
| 3112 |
|
| 3113 |
def test_overlap(self): |
| 3114 |
NormalDist = self.module.NormalDist |
| 3115 |
|
| 3116 |
# Match examples from Imman and Bradley |
| 3117 |
for X1, X2, published_result in [ |
| 3118 |
(NormalDist(0.0, 2.0), NormalDist(1.0, 2.0), 0.80258), |
| 3119 |
(NormalDist(0.0, 1.0), NormalDist(1.0, 2.0), 0.60993), |
| 3120 |
]: |
| 3121 |
self.assertAlmostEqual(X1.overlap(X2), published_result, places=4) |
| 3122 |
self.assertAlmostEqual(X2.overlap(X1), published_result, places=4) |
| 3123 |
|
| 3124 |
# Check against integration of the PDF |
| 3125 |
def overlap_numeric(X, Y, *, steps=8_192, z=5): |
| 3126 |
'Numerical integration cross-check for overlap() ' |
| 3127 |
fsum = math.fsum |
| 3128 |
center = (X.mean + Y.mean) / 2.0 |
| 3129 |
width = z * max(X.stdev, Y.stdev) |
| 3130 |
start = center - width |
| 3131 |
dx = 2.0 * width / steps |
| 3132 |
x_arr = [start + i*dx for i in range(steps)] |
| 3133 |
xp = list(map(X.pdf, x_arr)) |
| 3134 |
yp = list(map(Y.pdf, x_arr)) |
| 3135 |
total = max(fsum(xp), fsum(yp)) |
| 3136 |
return fsum(map(min, xp, yp)) / total |
| 3137 |
|
| 3138 |
for X1, X2 in [ |
| 3139 |
# Examples from Imman and Bradley |
| 3140 |
(NormalDist(0.0, 2.0), NormalDist(1.0, 2.0)), |
| 3141 |
(NormalDist(0.0, 1.0), NormalDist(1.0, 2.0)), |
| 3142 |
# Example from https://www.rasch.org/rmt/rmt101r.htm |
| 3143 |
(NormalDist(0.0, 1.0), NormalDist(1.0, 2.0)), |
| 3144 |
# Gender heights from http://www.usablestats.com/lessons/normal |
| 3145 |
(NormalDist(70, 4), NormalDist(65, 3.5)), |
| 3146 |
# Misc cases with equal standard deviations |
| 3147 |
(NormalDist(100, 15), NormalDist(110, 15)), |
| 3148 |
(NormalDist(-100, 15), NormalDist(110, 15)), |
| 3149 |
(NormalDist(-100, 15), NormalDist(-110, 15)), |
| 3150 |
# Misc cases with unequal standard deviations |
| 3151 |
(NormalDist(100, 12), NormalDist(100, 15)), |
| 3152 |
(NormalDist(100, 12), NormalDist(110, 15)), |
| 3153 |
(NormalDist(100, 12), NormalDist(150, 15)), |
| 3154 |
(NormalDist(100, 12), NormalDist(150, 35)), |
| 3155 |
# Misc cases with small values |
| 3156 |
(NormalDist(1.000, 0.002), NormalDist(1.001, 0.003)), |
| 3157 |
(NormalDist(1.000, 0.002), NormalDist(1.006, 0.0003)), |
| 3158 |
(NormalDist(1.000, 0.002), NormalDist(1.001, 0.099)), |
| 3159 |
]: |
| 3160 |
self.assertAlmostEqual(X1.overlap(X2), overlap_numeric(X1, X2), places=5) |
| 3161 |
self.assertAlmostEqual(X2.overlap(X1), overlap_numeric(X1, X2), places=5) |
| 3162 |
|
| 3163 |
# Error cases |
| 3164 |
X = NormalDist() |
| 3165 |
with self.assertRaises(TypeError): |
| 3166 |
X.overlap() # too few arguments |
| 3167 |
with self.assertRaises(TypeError): |
| 3168 |
X.overlap(X, X) # too may arguments |
| 3169 |
with self.assertRaises(TypeError): |
| 3170 |
X.overlap(None) # right operand not a NormalDist |
| 3171 |
with self.assertRaises(self.module.StatisticsError): |
| 3172 |
X.overlap(NormalDist(1, 0)) # right operand sigma is zero |
| 3173 |
with self.assertRaises(self.module.StatisticsError): |
| 3174 |
NormalDist(1, 0).overlap(X) # left operand sigma is zero |
| 3175 |
|
| 3176 |
def test_zscore(self): |
| 3177 |
NormalDist = self.module.NormalDist |
| 3178 |
X = NormalDist(100, 15) |
| 3179 |
self.assertEqual(X.zscore(142), 2.8) |
| 3180 |
self.assertEqual(X.zscore(58), -2.8) |
| 3181 |
self.assertEqual(X.zscore(100), 0.0) |
| 3182 |
with self.assertRaises(TypeError): |
| 3183 |
X.zscore() # too few arguments |
| 3184 |
with self.assertRaises(TypeError): |
| 3185 |
X.zscore(1, 1) # too may arguments |
| 3186 |
with self.assertRaises(TypeError): |
| 3187 |
X.zscore(None) # non-numeric type |
| 3188 |
with self.assertRaises(self.module.StatisticsError): |
| 3189 |
NormalDist(1, 0).zscore(100) # sigma is zero |
| 3190 |
|
| 3191 |
def test_properties(self): |
| 3192 |
X = self.module.NormalDist(100, 15) |
| 3193 |
self.assertEqual(X.mean, 100) |
| 3194 |
self.assertEqual(X.median, 100) |
| 3195 |
self.assertEqual(X.mode, 100) |
| 3196 |
self.assertEqual(X.stdev, 15) |
| 3197 |
self.assertEqual(X.variance, 225) |
| 3198 |
|
| 3199 |
def test_same_type_addition_and_subtraction(self): |
| 3200 |
NormalDist = self.module.NormalDist |
| 3201 |
X = NormalDist(100, 12) |
| 3202 |
Y = NormalDist(40, 5) |
| 3203 |
self.assertEqual(X + Y, NormalDist(140, 13)) # __add__ |
| 3204 |
self.assertEqual(X - Y, NormalDist(60, 13)) # __sub__ |
| 3205 |
|
| 3206 |
def test_translation_and_scaling(self): |
| 3207 |
NormalDist = self.module.NormalDist |
| 3208 |
X = NormalDist(100, 15) |
| 3209 |
y = 10 |
| 3210 |
self.assertEqual(+X, NormalDist(100, 15)) # __pos__ |
| 3211 |
self.assertEqual(-X, NormalDist(-100, 15)) # __neg__ |
| 3212 |
self.assertEqual(X + y, NormalDist(110, 15)) # __add__ |
| 3213 |
self.assertEqual(y + X, NormalDist(110, 15)) # __radd__ |
| 3214 |
self.assertEqual(X - y, NormalDist(90, 15)) # __sub__ |
| 3215 |
self.assertEqual(y - X, NormalDist(-90, 15)) # __rsub__ |
| 3216 |
self.assertEqual(X * y, NormalDist(1000, 150)) # __mul__ |
| 3217 |
self.assertEqual(y * X, NormalDist(1000, 150)) # __rmul__ |
| 3218 |
self.assertEqual(X / y, NormalDist(10, 1.5)) # __truediv__ |
| 3219 |
with self.assertRaises(TypeError): # __rtruediv__ |
| 3220 |
y / X |
| 3221 |
|
| 3222 |
def test_unary_operations(self): |
| 3223 |
NormalDist = self.module.NormalDist |
| 3224 |
X = NormalDist(100, 12) |
| 3225 |
Y = +X |
| 3226 |
self.assertIsNot(X, Y) |
| 3227 |
self.assertEqual(X.mean, Y.mean) |
| 3228 |
self.assertEqual(X.stdev, Y.stdev) |
| 3229 |
Y = -X |
| 3230 |
self.assertIsNot(X, Y) |
| 3231 |
self.assertEqual(X.mean, -Y.mean) |
| 3232 |
self.assertEqual(X.stdev, Y.stdev) |
| 3233 |
|
| 3234 |
def test_equality(self): |
| 3235 |
NormalDist = self.module.NormalDist |
| 3236 |
nd1 = NormalDist() |
| 3237 |
nd2 = NormalDist(2, 4) |
| 3238 |
nd3 = NormalDist() |
| 3239 |
nd4 = NormalDist(2, 4) |
| 3240 |
nd5 = NormalDist(2, 8) |
| 3241 |
nd6 = NormalDist(8, 4) |
| 3242 |
self.assertNotEqual(nd1, nd2) |
| 3243 |
self.assertEqual(nd1, nd3) |
| 3244 |
self.assertEqual(nd2, nd4) |
| 3245 |
self.assertNotEqual(nd2, nd5) |
| 3246 |
self.assertNotEqual(nd2, nd6) |
| 3247 |
|
| 3248 |
# Test NotImplemented when types are different |
| 3249 |
class A: |
| 3250 |
def __eq__(self, other): |
| 3251 |
return 10 |
| 3252 |
a = A() |
| 3253 |
self.assertEqual(nd1.__eq__(a), NotImplemented) |
| 3254 |
self.assertEqual(nd1 == a, 10) |
| 3255 |
self.assertEqual(a == nd1, 10) |
| 3256 |
|
| 3257 |
# All subclasses to compare equal giving the same behavior |
| 3258 |
# as list, tuple, int, float, complex, str, dict, set, etc. |
| 3259 |
class SizedNormalDist(NormalDist): |
| 3260 |
def __init__(self, mu, sigma, n): |
| 3261 |
super().__init__(mu, sigma) |
| 3262 |
self.n = n |
| 3263 |
s = SizedNormalDist(100, 15, 57) |
| 3264 |
nd4 = NormalDist(100, 15) |
| 3265 |
self.assertEqual(s, nd4) |
| 3266 |
|
| 3267 |
# Don't allow duck type equality because we wouldn't |
| 3268 |
# want a lognormal distribution to compare equal |
| 3269 |
# to a normal distribution with the same parameters |
| 3270 |
class LognormalDist: |
| 3271 |
def __init__(self, mu, sigma): |
| 3272 |
self.mu = mu |
| 3273 |
self.sigma = sigma |
| 3274 |
lnd = LognormalDist(100, 15) |
| 3275 |
nd = NormalDist(100, 15) |
| 3276 |
self.assertNotEqual(nd, lnd) |
| 3277 |
|
| 3278 |
def test_copy(self): |
| 3279 |
nd = self.module.NormalDist(37.5, 5.625) |
| 3280 |
nd1 = copy.copy(nd) |
| 3281 |
self.assertEqual(nd, nd1) |
| 3282 |
nd2 = copy.deepcopy(nd) |
| 3283 |
self.assertEqual(nd, nd2) |
| 3284 |
|
| 3285 |
def test_pickle(self): |
| 3286 |
nd = self.module.NormalDist(37.5, 5.625) |
| 3287 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 3288 |
with self.subTest(proto=proto): |
| 3289 |
pickled = pickle.loads(pickle.dumps(nd, protocol=proto)) |
| 3290 |
self.assertEqual(nd, pickled) |
| 3291 |
|
| 3292 |
def test_hashability(self): |
| 3293 |
ND = self.module.NormalDist |
| 3294 |
s = {ND(100, 15), ND(100.0, 15.0), ND(100, 10), ND(95, 15), ND(100, 15)} |
| 3295 |
self.assertEqual(len(s), 3) |
| 3296 |
|
| 3297 |
def test_repr(self): |
| 3298 |
nd = self.module.NormalDist(37.5, 5.625) |
| 3299 |
self.assertEqual(repr(nd), 'NormalDist(mu=37.5, sigma=5.625)') |
| 3300 |
|
| 3301 |
# Swapping the sys.modules['statistics'] is to solving the |
| 3302 |
# _pickle.PicklingError: |
| 3303 |
# Can't pickle <class 'statistics.NormalDist'>: |
| 3304 |
# it's not the same object as statistics.NormalDist |
| 3305 |
class TestNormalDistPython(unittest.TestCase, TestNormalDist): |
| 3306 |
module = py_statistics |
| 3307 |
def setUp(self): |
| 3308 |
sys.modules['statistics'] = self.module |
| 3309 |
|
| 3310 |
def tearDown(self): |
| 3311 |
sys.modules['statistics'] = statistics |
| 3312 |
|
| 3313 |
|
| 3314 |
@unittest.skipUnless(c_statistics, 'requires _statistics') |
| 3315 |
class TestNormalDistC(unittest.TestCase, TestNormalDist): |
| 3316 |
module = c_statistics |
| 3317 |
def setUp(self): |
| 3318 |
sys.modules['statistics'] = self.module |
| 3319 |
|
| 3320 |
def tearDown(self): |
| 3321 |
sys.modules['statistics'] = statistics |
| 3322 |
|
| 3323 |
|
| 3324 |
# === Run tests === |
| 3325 |
|
| 3326 |
def load_tests(loader, tests, ignore): |
| 3327 |
"""Used for doctest/unittest integration.""" |
| 3328 |
tests.addTests(doctest.DocTestSuite()) |
| 3329 |
if sys.float_repr_style == 'short': |
| 3330 |
tests.addTests(doctest.DocTestSuite(statistics)) |
| 3331 |
return tests |
| 3332 |
|
| 3333 |
|
| 3334 |
if __name__ == "__main__": |
| 3335 |
unittest.main() |