ad icon indicating copy to clipboard operation
ad copied to clipboard

Incorrect Results for Extra Floating Functions

Open julmb opened this issue 11 months ago • 19 comments

The library calculates incorrect results for at least some of the extra functions log1p, expm1, log1pexp, and log1mexp of the Floating class.

For example:

>>> import Numeric.AD
>>> log1pexp <$> [-1000, -100, -10, -1, 0, 1, 10, 100, 1000]
>>> fst . diff' log1pexp <$> [-1000, -100, -10, -1, 0, 1, 10, 100, 1000]
[0.0,3.720075976020836e-44,4.539889921686465e-5,0.31326168751822286,0.6931471805599453,1.3132616875182228,10.000045398899218,100.0,1000.0]
[0.0,0.0,4.5398899216870535e-5,0.31326168751822286,0.6931471805599453,1.3132616875182228,10.000045398899218,100.0,Infinity]

Near zero, there are only mild inaccuracies, but further away, the results are completely wrong.

I believe that this is due to missing definitions for these functions in the type class instances. This causes them to use the default implementations for those functions, which are pretty bad. For example, log1pexp x = log1p (exp x), which exhausts the precision of Double quite quickly.

I will do some further testing and then most likely start working on a pull request.

julmb avatar Mar 06 '24 13:03 julmb