[Numpy-discussion] log relative error
josef.pktd at gmail.com
josef.pktd at gmail.com
Sun Feb 26 12:46:31 EST 2012
(I got distracted by some numerical accuracy checks. np.polyfit looks
good in NIST test.)
Does numpy have something like this?
def lre(actual, desired):
'''calculate log relative error, number of correct significant digits
not an informative function name
Parameters
----------
actual : array_like
actual values
desired : array_like
desired values
Returns
-------
lre : ndarray
number of significant digits, uses relative absolute difference if
desired is not zero, and absolute difference if desired is zero.
References
----------
http://en.wikibooks.org/wiki/Statistics:Numerical_Methods/Numerical_Comparison_of_Statistical_Software#Measuring_Accuracy
http://digilander.libero.it/foxes/StRD_Benchmarks/X_NIST_StRD_Benchmarks.htm
'''
actual = np.atleast_1d(np.asarray(actual))
desired = np.atleast_1d(np.asarray(desired))
mask_zero = desired == 0
dig = np.zeros(desired.shape)
dig[mask_zero] = -np.log10(np.abs(actual[mask_zero]))
dig[~mask_zero] = -np.log10(np.abs(actual[~mask_zero]
- desired[~mask_zero])
/ np.abs(desired[~mask_zero]))
if np.size(dig) == 1:
dig = np.squeeze(dig)[()]
return dig
Josef
More information about the NumPy-Discussion
mailing list