[Python-ideas] Floating point "closeness" Proposal Outline

Neil Girdhar mistersheik at gmail.com
Tue Jan 20 11:58:31 CET 2015


On Tue, Jan 20, 2015 at 5:40 AM, Steven D'Aprano <steve at pearwood.info>
wrote:

> On Mon, Jan 19, 2015 at 08:10:35PM -0800, Neil Girdhar wrote:
>
> > If you decide to invent a relative error function,
>
> The error functions we have been talking about are hardly "invented".
> They're mathematically simple and obvious, and can be found in just
> about any undergraduate book on numerical computing:
>
> - The absolute error between two quantities a and b is the
>   absolute difference between them, abs(a-b).
>
> - The relative error is the difference relative to some
>   denominator d, abs(a-b)/abs(d), typically with d=a or d=b.
>
> If you happen to know that b is the correct value, then it is common to
> choose b as the denominator. If you have no a priori reason to think
> either a or b is correct, or if you prefer a symmetrical function, a
> common choice is to use d = min(abs(a), abs(b)).
>

I'm not against this.  What I meant by invention was that it appears that
no one defines relative error this way that I could see, which is why I
suggested you collect some examples.  The mathworld article, numpy, and
wikipedia all choose the asymmetric relative error, which is what I prefer
as well.


> See, for example:
>
> http://mathworld.wolfram.com/AbsoluteError.html
> http://mathworld.wolfram.com/RelativeError.html
>
>
> > my suggestion is:
> > (a-b)/b + log(b/a), which is nonnegative, zero only at equality, and
> > otherwise penalizes positive a for being different than some target
> > positive b.  To me, it seems like guessing b using 1.9b is better than
> > guessing it as 0.1b, and so on.  This corresponds to exponential KL
> > divergence, which has a clear statistical meaning, but only applies to
> > positive numbers.
>
> Do you have a reference or derivation for this? I'm happy to admit that
> I'm no Knuth or Kahan, but I've read a bit of numerical computing[1] and
> I've never seen anyone add a log term. I'm not even sure why you would
> do so.
>

Suppose that you make a necessarily positive real measurement such as a
length.  Your minimum assumptive distribution — the maximum entropy
distribution that is concordant with your measurement — is exponential with
appropriate mean.  Then, given a true belief distribution, you would like
to know how similar your approximated belief is to your true belief.  The
number of bits of information required to correct your approximation is the
KL divergence.

However, I should not have proposed it.  Since no one uses it and it
behaves so badly around zero, I don't think it's a good idea for the main
use of this function, which is testing.

I think I'm sold on the relative error as it's normally defined, but I'd
rather we see what all of the other packages have done and why.  I don't
think the default should be symmetrical since to me the asymmetrical case
seems more common.

How about:

relative_error(true_value, approximate_value, absolute_tolerance=...,
relative_tolerance=..., tolerance_relative_to=None)

if tolerance_relative_to is None, replace it with true_value (or min(true,
approx) if we go with the symmetrical definition).


Best,

Neil


>
>
> [1] I know just enough to know how much I don't know.
>
> --
> Steve
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
> --
>
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/VouL2DVqQmw/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150120/4a06829d/attachment.html>


More information about the Python-ideas mailing list