Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Beta

Synopsis
#include <boost/math/special_functions/beta.hpp>
namespace boost{ namespace math{

template <class T1, class T2>
calculated-result-type beta(T1 a, T2 b);

template <class T1, class T2, class Policy>
calculated-result-type beta(T1 a, T2 b, const Policy&);

}} // namespaces
Description

The beta function is defined by:

The final Policy argument is optional and can be used to control the behaviour of the function: how it handles errors, what level of precision to use etc. Refer to the policy documentation for more details.

There are effectively two versions of this function internally: a fully generic version that is slow, but reasonably accurate, and a much more efficient approximation that is used where the number of digits in the significand of T correspond to a certain Lanczos approximation. In practice any built-in floating-point type you will encounter has an appropriate Lanczos approximation defined for it. It is also possible, given enough machine time, to generate further Lanczos approximation's using the program libs/math/tools/lanczos_generator.cpp.

The return type of these functions is computed using the result type calculation rules when T1 and T2 are different types.

Accuracy

The following table shows peak errors for various domains of input arguments, along with comparisons to the GSL-1.9 and Cephes libraries. Note that only results for the widest floating point type on the system are given as narrower types have effectively zero error.

Table 6.17. Error rates for beta

Microsoft Visual C++ version 12.0
Win32
double

GNU C++ version 5.1.0
linux
long double

GNU C++ version 5.1.0
linux
double

Sun compiler version 0x5130
Sun Solaris
long double

Beta Function: Small Values

Max = 1.75ε (Mean = 0.828ε)

Max = 2.65ε (Mean = 1.22ε)

(<tr1/cmath>: Max = 11.9ε (Mean = 7.03ε))

Max = 0ε (Mean = 0ε)

(GSL 1.16: Max = 3.94ε (Mean = 1.44ε))
(Rmath 3.0.2: Max = 1.14ε (Mean = 0.601ε))
(Cephes: Max = 1.97ε (Mean = 0.934ε))

Max = 2.86ε (Mean = 1.31ε)

Beta Function: Medium Values

Max = 99.1ε (Mean = 22.4ε)

Max = 60.4ε (Mean = 19.4ε)

(<tr1/cmath>: Max = 1.07e+03ε (Mean = 264ε))

Max = 0.978ε (Mean = 0.0578ε)

(GSL 1.16: Max = 1.18e+03ε (Mean = 238ε))
(Rmath 3.0.2: Max = 1.09e+03ε (Mean = 265ε))
(Cephes: Max = 1.5e+03ε (Mean = 329ε))

Max = 107ε (Mean = 24.5ε)

Beta Function: Divergent Values

Max = 10.7ε (Mean = 2.22ε)

Max = 10.3ε (Mean = 2.41ε)

(<tr1/cmath>: Max = 128ε (Mean = 23.8ε))

Max = 0ε (Mean = 0ε)

(GSL 1.16: Max = 12.1ε (Mean = 1.99ε))
(Rmath 3.0.2: Max = 176ε (Mean = 28ε))
(Cephes: Max = 175ε (Mean = 22ε))

Max = 19.7ε (Mean = 2.75ε)


Note that the worst errors occur when a or b are large, and that when this is the case the result is very close to zero, so absolute errors will be very small.

Testing

A mixture of spot tests of exact values, and randomly generated test data are used: the test data was computed using NTL::RR at 1000-bit precision.

Implementation

Traditional methods of evaluating the beta function either involve evaluating the gamma functions directly, or taking logarithms and then exponentiating the result. However, the former is prone to overflows for even very modest arguments, while the latter is prone to cancellation errors. As an alternative, if we regard the gamma function as a white-box containing the Lanczos approximation, then we can combine the power terms:

which is almost the ideal solution, however almost all of the error occurs in evaluating the power terms when a or b are large. If we assume that a > b then the larger of the two power terms can be reduced by a factor of b, which immediately cuts the maximum error in half:

This may not be the final solution, but it is very competitive compared to other implementation methods.

The generic implementation - where no Lanczos approximation approximation is available - is implemented in a very similar way to the generic version of the gamma function. Again in order to avoid numerical overflow the power terms that prefix the series and continued fraction parts are collected together into:

where la, lb and lc are the integration limits used for a, b, and a+b.

There are a few special cases worth mentioning:

When a or b are less than one, we can use the recurrence relations:

to move to a more favorable region where they are both greater than 1.

In addition:


PrevUpHomeNext