[ 
https://issues.apache.org/jira/browse/NUMBERS-221?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18152417#comment-18152417
 ] 

Alex Herbert commented on NUMBERS-221:
--------------------------------------

I have ported the Boost digamma and trigamma functions. Compared to the current 
implementations the accuracy increased across all test data, and performance is 
faster with a few notable exceptions.

Note that here the 1.3 version includes fixes applied before the 1.4 release to 
the validated domain (NUMBERS-219) and to use the reflection formula 
(NUMBERS-209).

h2. Accuracy

I used the Boost test data and supplemented it with additional data using 
reference values from mpmath (1.4.1) digamma and polygamma functions.

The maximum and root mean squared (RMS) error for the digamma and trigamma 
functions expressed in ULPs is shown in the table below. The table headers are 
Boost for the new implementation, and 1.3 for the existing. This data is part 
of the test suite for the new functions.

Some parts of the domain use calls to Math functions and accuracy in some parts 
of the domain will be platform dependent. Digamma at large values of x use a 
call to Math.log, and both di and trigamma use trignomic functions to implement 
reflection formulas for negative arguments. The digamma reflection uses {{pi / 
tan(pi * x)}} and the trigamma reflection uses {{pi^2 / sin^2(pi x)}}. Both are 
sensitive to changes in pi*x. As {{x - floor\(x\)}} -> 0 this is a pole of the 
function. For the digamma function which has cancellation of terms the 
evaluation of the reflection also effects accuracy at the root if the tan term 
is of opposite sign to digamma(1 - x).

||Case||Description||n||Max||RMS||Max||RMS||
| | | |Boost| |1.3| |
|DIGAMMA_DATA|Real random x in [2, 1000]|500|0.987831|0.316799|7.93865|0.60162|
|DIGAMMA_INT_DATA|Integer x in [1, …, 
100]|100|0.916689|0.358642|12.9555|1.71244|
|DIGAMMA_HALF_INT_DATA|Half-integer s in [0.5, …, 
100.5]|101|0.92777|0.289511|117.972|11.8459|
|DIGAMMA_ROOT_DATA|Real random x in [1.39, 1.5] (root 
~1.46)|200|1.44295|0.53936|14861|1498.85|
|DIGAMMA_SMALL_DATA|Real random x in [1e-8, 
5.7]|33|0.851235|0.303128|1.02E+06|185338|
|DIGAMMA_NEG_DATA|Real random x in [-100, 0]|200|80.9962|8.1701|52911|4503.61|
|TRIGAMMA_INT_DATA|Integer x in [1, …, 
500]|500|1.37569|0.447972|20231.9|4540.32|
|TRIGAMMA_HALF_INT_DATA|Half-integer s in [0.5, …, 
100.5]|101|1.17836|0.457438|18843.4|9616.21|
|TRIGAMMA_SMALL_DATA|2^-b with b in [2, …, 100]|99|0|0|431205|44759|
|TRIGAMMA_MED_DATA|Real x in [1, 14]|200|1.41307|0.460636|5059.44|2793.63|
|TRIGAMMA_NEG_DATA|-n +/- 2^-10; n in [-20, 
0]|41|0.900578|0.411192|13354|6035.51|
|TRIGAMMA_NEG_SMALL_DATA| -(2^-b) with b in [2, …, 
100]|99|1.58877|0.344048|20.0821|2.0784|
|TRIGAMMA_NEG_HALF_DATA|Half-integer s in [-19.5, …, 
-0.5]|20|0.841433|0.469806|36.8791|36.4224|

The new Boost digamma function is very accurate for positive x. It is within 1 
ULP except very close to the root.

The 1.3 digamma version is good at large positive x. However the digamma 
function has a root around 1.46. The current implementation is poor on the root 
data, and generally increasingly bad as the magnitude of x -> 0. The 
implementation uses a recurrence relation to shift argument x to > 49. This 
uses a summation of terms of decreasing magnitude so accumulates error. When x 
is close to zero the first term 1/x is much larger than remaining terms 1/(x+k) 
and the summation cannot add the terms during the shift to (a+k) > 49. In 
addition the polynomial used for x > 49 is not accurate enough for double 
precision until x is very large.

When x is negative both digamma functions have notable error. The digamma 
function has poles at integer x = 0, -1, -2, ..., -n. As x approaches a pole 
from below or above the value tends to infinity or -infinity. There is a 
discontinuity at the pole. The function thus has a root between every negative 
integer x and large cancellation of terms in the computation close to the root. 
Without extended precision for the terms this cancellation cannot be avoided.

The new Boost trigamma function is very accurate for all x.

The 1.3 trigamma version follows the same pattern. A recurrence relation is 
used to move x to > 49. This summation is of terms of descending magnitude and 
error accumulates. This is obvious as x -> 0 (TRIGAMMA_SMALL_DATA).

Negative arguments are handled better in both implementations for trigamma. The 
function has a pole at integer x = 0, -1, -2, ..., -n. It is always infinity 
and there is no discontinuity when approaching the pole from below or above. So 
there is no cancellation and there are no roots. The identity used to map 
negative arguments to positive is similar in both implementation. The error for 
negative x then is biased towards the accuracy for trigamma(1 - x).

h2. Performance

I created a JMH benchmark to measure the performance. The following functions 
were tested:

||Method||Description||
|digamma|Boost digamma. Uses reflection when x <= -1. Uses log and a rational 
function when x >= 10. For x in (-1, 10) it shifts x to [1, 2] and uses a 
rational function.|
|digamma1.3|Version 1.3 digamma. Uses reflection when x < 0. Approximation for 
x < 1e-5. Shifts to x > 49 and uses log and a polynomial function.|
|digammaShift|Boost digamma modified to not sum 1/x when performing the shift.|
|digammaLog|Boost digamma modified to only evaluate the call to log after the 
domain checks and reflection. For -1 < x < 10 it performs no further work. |
|digamma1.3Log|Version 1.3 digamma modified to not sum 1/x when performing the 
shift and only evaluate the call to log.|
|trigamma|Boost trigamma. Uses reflection when x <= 0. Shifts to x >= 1 and 
uses rational functions for [1, 2], [2, 4], and [4, inf].|
|trigamma1.3|Version 1.3 trigamma. Uses reflection when x <= 0. Approximation 
for x < 1e-5. Shifts to x > 49 and uses a polynomial function.|

The variations for the digamma function are used to evaluate the cost of 
various parts of the function.

The timings were executed using Eclipse Temurin JDK 21.0.11 on a Mac M2 Pro.

All timings used 100,000 random points uniformly distributed in a given range. 
The following table shows ns/operation. Timings are platform dependent. The 
timings for the functions have a baseline subtracted. This was the same JMH 
loop with an identify function in place of the test method (~13500ns). The 
relative ratio indicates the performance difference of the Boost version to the 
1.3 version.

||Min||Max||digamma||digamma1.3||Relative||digammaShift||digammaLog||digamma1.3Log||trigamma||trigamma1.3||Relative||
|-100|-50|3512824.9|2959671.8|1.187|3508823.6|3145103.8|2660253.6|1394664.9|1413709.7|0.987|
|-1|0|533461.5|5314957.5|0.100|418577.9|102230.2|2249099.9|1341027.8|4357024.1|0.308|
|0|1|447648.9|3689587.2|0.121|390262.6|102547.8|686248.0|356164.3|3711661.6|0.096|
|1|2|363395.3|3606612.5|0.101|361447.7|102000.3|683637.9|311374.6|3731998.2|0.083|
|2|4|868049.4|5297133.9|0.164|765924.5|101833.0|678988.8|359857.2|4338470.8|0.083|
|4|8|1387234.0|6429112.5|0.216|1161187.0|102731.9|679219.8|399737.4|4565119.6|0.088|
|8|16|1199135.8|6590541.7|0.182|1145956.7|673896.0|684931.3|398807.4|4261040.5|0.094|
|16|32|757750.5|5691260.1|0.133|757169.2|730825.8|682285.4|398924.1|3215864.9|0.124|
|32|64|757818.4|2779272.8|0.273|758017.5|744228.2|971443.2|398845.5|1344371.2|0.297|
|64|128|760009.4|678712.3|1.120|761020.8|746035.7|609544.5|399643.9|201645.6|1.982|

The trigamma function result is simpler. Fo positive x the Boost method only 
shifts 0 < x < 1 to the [1, 2] domain. All x >= 1 use a rational function. The 
1.3 version shifts x to > 49. This has a large overhead. It is only the data in 
the range [64, 128] where no shift is required. In this case the Boost version 
is slower as the rational function is more complex than the polynomial used in 
version 1.3.

When x is negative both trigamma methods use reflection. The Boost method is 
around 4x slower when reflection is used. Version 1.3 is different. The 
reflection time is comparable to the time used to shift (1 - x) to > 49. So 
performance on small negative x is similar to its positive small x performance. 
On larger negative x [-100, -50] no shift is required for (1 - x) and the 
function has a similar speed to the Boost method. The overhead is mainly the 
reflection implementation.

Results from the trigamma function translate to the digamma function. A large 
overhead is shifting the argument x to the domain supported by 
rational/polynomial functions. Boost shifts x in (-1, 10) to [1, 2]. The 1.3 
version will shift x to > 49. When a large shift is required by the 1.3 version 
then the Boost implementation is faster as it uses a smaller shift. The method 
digammaShift is the Boost method performing the shift in the same while loop 
but not computing the sum of 1/x. This sum of divisions alone accounts for 
~10-15% of the function cost when x in [4, 8] is shifted to [1, 2]. The cost of 
a sum of 1/x is more evident for the digamma1.3Log method. This directly moves 
x to > 49 and calls the log function. This is around 50x faster when x < 10.

When no shift is required for either method (Boost x >= 10; 1.3 x > 49) then 
the computation uses a call to Math.log. Comparing the methods that only 
compute the log call and drop the rest of the rational/polynomial function 
shows relative performance of 0.98 (Boost) and 0.90 (1.3). The set-up and call 
of the log function is the major cost of the function.

Overall the Boost function is faster in the positive x domain for di and 
trigamma when x < 49. Above this limit the digamma function is around 12% 
slower and trigamma around 2x slower. The large x Boost trigamma function uses 
a rational function from two polynomials to compute p / q. The 1.3 version uses 
a simpler single polynomial. 2 -> 1 polynomials approximates the speed change. 
The large x digamma function requires a call to Math.log. Then both Boost and 
1.3 use a single polynomial. The Boost polynomial is more complex so is slower. 
Comparing the large x di and trigamma functions for version 1.3 show that the 
trigamma function without the requirement for Math.log is 3x faster. So large 
digamma cost is dominated by the call to log.

In the negative x domain the reflection identity creates slowdown in the 
functions. The Boost function is faster when |x| < 49 as the 1.3 version 
increases the work it has to perform to move (1-x) > 49. At large negative x 
the time to implement the reflection is significant compared to evaluating 
large di/trigamma(1-x). For digamma the Boost function does more work to 
compute {{pi / tan(pi * x)}} using {{x - floor\(x\)}} and the periodic domain 
of tan. This maintains accuracy at larger magnitude x. The 1.3 version uses 
tan(pi * x) directly and will lose accuracy as |x| > 1. The Boost function is 
~20% slower. The trigamma identity is {{{}pi^2 / sin^2(pi x){}}}. This is 
faster than a call to tan. Both functions are similar speed at large negative x 
and the 1.3 version has slowdown as \-x \-> 0 as it requires a larger shift of 
(1 - x) > 49.

 

> Update Digamma and Trigamma accuracy from single to double precision
> --------------------------------------------------------------------
>
>                 Key: NUMBERS-221
>                 URL: https://issues.apache.org/jira/browse/NUMBERS-221
>             Project: Commons Numbers
>          Issue Type: Improvement
>          Components: gamma
>    Affects Versions: 1.3
>            Reporter: Alex Herbert
>            Assignee: Alex Herbert
>            Priority: Minor
>
> The Digamma and Trigamma functions compute the result to 1e-8 relative or 
> 1e-8 absolute error, whichever is lower. This is single precision.
> These functions use a polynomial approximation for the asymptotic value at 
> large x after a direct sum of terms with small x. The functions can be 
> updated to use the 53-bit precision implementation from the Boost C++ library.
> This Boost implementation is a rational function with more terms. However the 
> functions require a much smaller shift of x to the range supported by its 
> rational functions than the current di/trigamma implementations which shift 
> input x to above 49. The Boost digamma function shifts x < 10 to the range 
> \[1, 2\] containing the root; any x > 10 uses a rational function. Boost 
> trigamma has rational functions for any x > 1. It is expected that runtime 
> performance will not be magnitudes different, and accuracy will change from 
> single to double precision.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to