numpy.allclose()
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sat Jan 17 15:26:26 CET 2015
Can anyone explain the rationale for numpy's allclose() semantics?
help(allclose) says:
allclose(a, b, rtol=1e-05, atol=1e-08)
Returns True if two arrays are element-wise equal within a tolerance.
The tolerance values are positive, typically very small numbers. The
relative difference (`rtol` * abs(`b`)) and the absolute difference
`atol` are added together to compare against the absolute difference
between `a` and `b`.
[...]
If the following equation is element-wise True, then allclose returns
True.
absolute(`a` - `b`) <= (`atol` + `rtol` * absolute(`b`))
I don't understand why they add the error tolerances together. I can
understand taking the minimum, or the maximum:
* taking the maximum is equivalent to the rule "numbers are close if they
differ by no more than EITHER the absolute tolerance OR the relative
tolerance";
* taking the minimum is equivalent to the rule "numbers are close if they
differ by no more than BOTH the absolute tolerance AND the relative
tolerance".
But adding them together doesn't make any sense to me. That leads to the
situation where two values are deemed "close" if you give two tolerances
even though it fails each test individually:
py> numpy.allclose([1.2], [1.0], 0.0, 0.1) # Fails absolute error test.
False
py> numpy.allclose([1.2], [1.0], 0.1, 0.0) # Fails relative error test.
False
py> numpy.allclose([1.2], [1.0], 0.1, 0.1) # Passes!
True
--
Steven
More information about the Python-list
mailing list