A fast and robust implementation of the beta function

The beta function is closely related to the gamma function and is defined as B(a,b) = Γ(a) · Γ(b) / Γ(a + b) for a, b > 0. It is frequently used in statistics. From what I understand, C++ first added this function (after a fashion) as std::beta() in C++17, and it will become a first class citizen (with proper overloads) in C++23.

Based on experimenting on Compiler Explorer, platforms that use glibc already offer support for std::beta() when using the gcc, clang, and icc toolchains. But the current implementation seems to be more of a placeholder, with numerical error in the hundreds of ulps for fairly modest cases. One anecdotal test case I tried, std::betaf (0x1.0p-15f, 1e7f) returns a result off by an order of magnitude from the mathematical result of 3.27513089662328838555… ·104; the implementation shown below returns 3.27513047e+4f.

Preliminary investigation shows that accurate computation of the beta function to a reasonable error limit of 4 ulps or so is a very challenging task that seems to require computing in twice the target precision for all intermediate computation. For the single-precision implementation my_betaf() below I am observing maximum errors of around 50 ulp. This is highly dependent on the accuracy of the standard math functions used and thus may fluctuate with each CUDA version. With a standard math library that delivers results with maximum error just a tad over 0.5 ulp, the maximum error using the same code drops to around 35 ulp.

The code below was tested with the default compiler settings (ftz=false -prec-div=true -prec-sqrt=true) and choosing other settings may require adjustments to the code to maintain robustness and accuracy.

/*
  Copyright (c) 2023, Norbert Juffa

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright 
     notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* Compute scaled gamma function, Γ*(a) = sqrt(a/2*pi)*exp(a)*pow(a,-a)*Γ(a) */
__device__ float gammastarf (float x)
{
    const float MY_NAN_F = __int_as_float (0x7fffffff);
    float r, s;

    if (x <= 0.0f) {
        r = MY_NAN_F;
    } else if (x < 1.0f) {
        /* (0, 1): maximum error 4.17 ulp */
        if (x < 3.5e-9f) {
            const float oosqrt2pi = 0.39894228040143267794f; // 1/sqrt(2pi)
            r = oosqrt2pi / sqrtf (x);
        } else {
            r = 1.0f / x;
            r = gammastarf (x + 1.0f) * expf (fmaf (x, log1pf (r), -1.0f)) * 
                sqrtf (1.0f + r);
        }
    } else {
        /* [1, INF]: maximum error 0.56 ulp */
        r = 1.0f / x;
        s =              1.24335289e-4f;  //  0x1.04c000p-13
        s = fmaf (s, r, -5.94899990e-4f); // -0x1.37e620p-11
        s = fmaf (s, r,  1.07218279e-3f); //  0x1.1910f8p-10
        s = fmaf (s, r, -2.95283855e-4f); // -0x1.35a0a8p-12
        s = fmaf (s, r, -2.67404946e-3f); // -0x1.5e7e36p-9
        s = fmaf (s, r,  3.47193284e-3f); //  0x1.c712bcp-9
        s = fmaf (s, r,  8.33333358e-2f); //  0x1.555556p-4
        r = fmaf (s, r,  1.00000000e+0f); //  0x1.000000p+0
    }
    return r;
}

__device__ float my_betaf (float a, float b)
{
    const float MY_NAN_F = __int_as_float (0x7fffffff);
    const float MY_INF_F = __int_as_float (0x7f800000);
    const float sqrt_2pi = 2.506628274631000502416f;
    float sum, mn, mx, mn_over_sum, lms, phi, plo, g, p, q, r;
    if ((a < 0) || (b < 0)) return MY_NAN_F;
    sum = a + b;
    if (sum == 0) return MY_INF_F;
    mn = fminf (a, b);
    mx = fmaxf (a, b);
    if (mn < 1) {
        r = my_betaf (mn + 1.0f, mx);
        r = sum * r / mn;
        return r;
    }
    mn_over_sum = mn / sum;
    // (mx / sum) ** mx
    lms = log1pf (-mn_over_sum);
    phi = lms * mx;
    plo = fmaf (lms, mx, -phi);
    p = expf (phi);
    p = fmaf (plo, p, p);
    // (mn / sum) ** mn
    q = powf (mn_over_sum, mn);
    g = gammastarf (mn) * gammastarf (mx) / gammastarf (sum);
    r = g * sqrt_2pi * sqrtf (1.0f / mx + 1.0f / mn) * p * q; 
    return r;
}
1 Like