Repository URL to install this package:
"""Test suite for statistics module, including helper NumericTestCase and
approx_equal function.
"""
import bisect
import collections
import collections.abc
import copy
import decimal
import doctest
import math
import pickle
import random
import sys
import unittest
from test import support
from decimal import Decimal
from fractions import Fraction
from test import support
# Module to be tested.
import statistics
# === Helper functions and class ===
def sign(x):
"""Return -1.0 for negatives, including -0.0, otherwise +1.0."""
return math.copysign(1, x)
def _nan_equal(a, b):
"""Return True if a and b are both the same kind of NAN.
>>> _nan_equal(Decimal('NAN'), Decimal('NAN'))
True
>>> _nan_equal(Decimal('sNAN'), Decimal('sNAN'))
True
>>> _nan_equal(Decimal('NAN'), Decimal('sNAN'))
False
>>> _nan_equal(Decimal(42), Decimal('NAN'))
False
>>> _nan_equal(float('NAN'), float('NAN'))
True
>>> _nan_equal(float('NAN'), 0.5)
False
>>> _nan_equal(float('NAN'), Decimal('NAN'))
False
NAN payloads are not compared.
"""
if type(a) is not type(b):
return False
if isinstance(a, float):
return math.isnan(a) and math.isnan(b)
aexp = a.as_tuple()[2]
bexp = b.as_tuple()[2]
return (aexp == bexp) and (aexp in ('n', 'N')) # Both NAN or both sNAN.
def _calc_errors(actual, expected):
"""Return the absolute and relative errors between two numbers.
>>> _calc_errors(100, 75)
(25, 0.25)
>>> _calc_errors(100, 100)
(0, 0.0)
Returns the (absolute error, relative error) between the two arguments.
"""
base = max(abs(actual), abs(expected))
abs_err = abs(actual - expected)
rel_err = abs_err/base if base else float('inf')
return (abs_err, rel_err)
def approx_equal(x, y, tol=1e-12, rel=1e-7):
"""approx_equal(x, y [, tol [, rel]]) => True|False
Return True if numbers x and y are approximately equal, to within some
margin of error, otherwise return False. Numbers which compare equal
will also compare approximately equal.
x is approximately equal to y if the difference between them is less than
an absolute error tol or a relative error rel, whichever is bigger.
If given, both tol and rel must be finite, non-negative numbers. If not
given, default values are tol=1e-12 and rel=1e-7.
>>> approx_equal(1.2589, 1.2587, tol=0.0003, rel=0)
True
>>> approx_equal(1.2589, 1.2587, tol=0.0001, rel=0)
False
Absolute error is defined as abs(x-y); if that is less than or equal to
tol, x and y are considered approximately equal.
Relative error is defined as abs((x-y)/x) or abs((x-y)/y), whichever is
smaller, provided x or y are not zero. If that figure is less than or
equal to rel, x and y are considered approximately equal.
Complex numbers are not directly supported. If you wish to compare to
complex numbers, extract their real and imaginary parts and compare them
individually.
NANs always compare unequal, even with themselves. Infinities compare
approximately equal if they have the same sign (both positive or both
negative). Infinities with different signs compare unequal; so do
comparisons of infinities with finite numbers.
"""
if tol < 0 or rel < 0:
raise ValueError('error tolerances must be non-negative')
# NANs are never equal to anything, approximately or otherwise.
if math.isnan(x) or math.isnan(y):
return False
# Numbers which compare equal also compare approximately equal.
if x == y:
# This includes the case of two infinities with the same sign.
return True
if math.isinf(x) or math.isinf(y):
# This includes the case of two infinities of opposite sign, or
# one infinity and one finite number.
return False
# Two finite numbers.
actual_error = abs(x - y)
allowed_error = max(tol, rel*max(abs(x), abs(y)))
return actual_error <= allowed_error
# This class exists only as somewhere to stick a docstring containing
# doctests. The following docstring and tests were originally in a separate
# module. Now that it has been merged in here, I need somewhere to hang the.
# docstring. Ultimately, this class will die, and the information below will
# either become redundant, or be moved into more appropriate places.
class _DoNothing:
"""
When doing numeric work, especially with floats, exact equality is often
not what you want. Due to round-off error, it is often a bad idea to try
to compare floats with equality. Instead the usual procedure is to test
them with some (hopefully small!) allowance for error.
The ``approx_equal`` function allows you to specify either an absolute
error tolerance, or a relative error, or both.
Absolute error tolerances are simple, but you need to know the magnitude
of the quantities being compared:
>>> approx_equal(12.345, 12.346, tol=1e-3)
True
>>> approx_equal(12.345e6, 12.346e6, tol=1e-3) # tol is too small.
False
Relative errors are more suitable when the values you are comparing can
vary in magnitude:
>>> approx_equal(12.345, 12.346, rel=1e-4)
True
>>> approx_equal(12.345e6, 12.346e6, rel=1e-4)
True
but a naive implementation of relative error testing can run into trouble
around zero.
If you supply both an absolute tolerance and a relative error, the
comparison succeeds if either individual test succeeds:
>>> approx_equal(12.345e6, 12.346e6, tol=1e-3, rel=1e-4)
True
"""
pass
# We prefer this for testing numeric values that may not be exactly equal,
# and avoid using TestCase.assertAlmostEqual, because it sucks :-)
py_statistics = support.import_fresh_module('statistics', blocked=['_statistics'])
c_statistics = support.import_fresh_module('statistics', fresh=['_statistics'])
class TestModules(unittest.TestCase):
func_names = ['_normal_dist_inv_cdf']
def test_py_functions(self):
for fname in self.func_names:
self.assertEqual(getattr(py_statistics, fname).__module__, 'statistics')
@unittest.skipUnless(c_statistics, 'requires _statistics')
def test_c_functions(self):
for fname in self.func_names:
self.assertEqual(getattr(c_statistics, fname).__module__, '_statistics')
class NumericTestCase(unittest.TestCase):
"""Unit test class for numeric work.
This subclasses TestCase. In addition to the standard method
``TestCase.assertAlmostEqual``, ``assertApproxEqual`` is provided.
"""
# By default, we expect exact equality, unless overridden.
tol = rel = 0
def assertApproxEqual(
self, first, second, tol=None, rel=None, msg=None
):
"""Test passes if ``first`` and ``second`` are approximately equal.
This test passes if ``first`` and ``second`` are equal to
within ``tol``, an absolute error, or ``rel``, a relative error.
If either ``tol`` or ``rel`` are None or not given, they default to
test attributes of the same name (by default, 0).
The objects may be either numbers, or sequences of numbers. Sequences
are tested element-by-element.
>>> class MyTest(NumericTestCase):
... def test_number(self):
... x = 1.0/6
... y = sum([x]*6)
... self.assertApproxEqual(y, 1.0, tol=1e-15)
... def test_sequence(self):
... a = [1.001, 1.001e-10, 1.001e10]
... b = [1.0, 1e-10, 1e10]
... self.assertApproxEqual(a, b, rel=1e-3)
...
>>> import unittest
>>> from io import StringIO # Suppress test runner output.
>>> suite = unittest.TestLoader().loadTestsFromTestCase(MyTest)
>>> unittest.TextTestRunner(stream=StringIO()).run(suite)
<unittest.runner.TextTestResult run=2 errors=0 failures=0>
"""
if tol is None:
tol = self.tol
if rel is None:
rel = self.rel
if (
isinstance(first, collections.abc.Sequence) and
isinstance(second, collections.abc.Sequence)
):
check = self._check_approx_seq
else:
check = self._check_approx_num
check(first, second, tol, rel, msg)
def _check_approx_seq(self, first, second, tol, rel, msg):
if len(first) != len(second):
standardMsg = (
"sequences differ in length: %d items != %d items"
% (len(first), len(second))
)
msg = self._formatMessage(msg, standardMsg)
raise self.failureException(msg)
for i, (a,e) in enumerate(zip(first, second)):
self._check_approx_num(a, e, tol, rel, msg, i)
def _check_approx_num(self, first, second, tol, rel, msg, idx=None):
if approx_equal(first, second, tol, rel):
# Test passes. Return early, we are done.
return None
# Otherwise we failed.
standardMsg = self._make_std_err_msg(first, second, tol, rel, idx)
msg = self._formatMessage(msg, standardMsg)
raise self.failureException(msg)
@staticmethod
def _make_std_err_msg(first, second, tol, rel, idx):
# Create the standard error message for approx_equal failures.
assert first != second
template = (
' %r != %r\n'
' values differ by more than tol=%r and rel=%r\n'
' -> absolute error = %r\n'
' -> relative error = %r'
)
if idx is not None:
header = 'numeric sequences first differ at index %d.\n' % idx
template = header + template
# Calculate actual errors:
abs_err, rel_err = _calc_errors(first, second)
return template % (first, second, tol, rel, abs_err, rel_err)
# ========================
# === Test the helpers ===
# ========================
class TestSign(unittest.TestCase):
"""Test that the helper function sign() works correctly."""
def testZeroes(self):
# Test that signed zeroes report their sign correctly.
self.assertEqual(sign(0.0), +1)
self.assertEqual(sign(-0.0), -1)
# --- Tests for approx_equal ---
class ApproxEqualSymmetryTest(unittest.TestCase):
# Test symmetry of approx_equal.
def test_relative_symmetry(self):
# Check that approx_equal treats relative error symmetrically.
# (a-b)/a is usually not equal to (a-b)/b. Ensure that this
# doesn't matter.
#
# Note: the reason for this test is that an early version
# of approx_equal was not symmetric. A relative error test
# would pass, or fail, depending on which value was passed
# as the first argument.
#
args1 = [2456, 37.8, -12.45, Decimal('2.54'), Fraction(17, 54)]
args2 = [2459, 37.2, -12.41, Decimal('2.59'), Fraction(15, 54)]
assert len(args1) == len(args2)
for a, b in zip(args1, args2):
self.do_relative_symmetry(a, b)
def do_relative_symmetry(self, a, b):
a, b = min(a, b), max(a, b)
assert a < b
delta = b - a # The absolute difference between the values.
rel_err1, rel_err2 = abs(delta/a), abs(delta/b)
# Choose an error margin halfway between the two.
rel = (rel_err1 + rel_err2)/2
# Now see that values a and b compare approx equal regardless of
# which is given first.
self.assertTrue(approx_equal(a, b, tol=0, rel=rel))
self.assertTrue(approx_equal(b, a, tol=0, rel=rel))
def test_symmetry(self):
# Test that approx_equal(a, b) == approx_equal(b, a)
args = [-23, -2, 5, 107, 93568]
delta = 2
for a in args:
for type_ in (int, float, Decimal, Fraction):
x = type_(a)*100
y = x + delta
r = abs(delta/max(x, y))
# There are five cases to check:
# 1) actual error <= tol, <= rel
Loading ...