...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
#include <boost/math/special_functions/beta.hpp>
namespace boost{ namespace math{ template <class T1, class T2, class T3> calculatedresulttype ibeta(T1 a, T2 b, T3 x); template <class T1, class T2, class T3, class Policy> calculatedresulttype ibeta(T1 a, T2 b, T3 x, const Policy&); template <class T1, class T2, class T3> calculatedresulttype ibetac(T1 a, T2 b, T3 x); template <class T1, class T2, class T3, class Policy> calculatedresulttype ibetac(T1 a, T2 b, T3 x, const Policy&); template <class T1, class T2, class T3> calculatedresulttype beta(T1 a, T2 b, T3 x); template <class T1, class T2, class T3, class Policy> calculatedresulttype beta(T1 a, T2 b, T3 x, const Policy&); template <class T1, class T2, class T3> calculatedresulttype betac(T1 a, T2 b, T3 x); template <class T1, class T2, class T3, class Policy> calculatedresulttype betac(T1 a, T2 b, T3 x, const Policy&); }} // namespaces
There are four incomplete beta functions : two are normalised versions (also known as regularized beta functions) that return values in the range [0, 1], and two are nonnormalised and return values in the range [0, beta(a, b)]. Users interested in statistical applications should use the normalised (or regularized ) versions (ibeta and ibetac).
All of these functions require 0 <= x <= 1.
The normalized functions ibeta and ibetac require a,b >= 0, and in addition that not both a and b are zero.
The functions beta and betac require a,b > 0.
The return type of these functions is computed using the result type calculation rules when T1, T2 and T3 are different types.
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.
template <class T1, class T2, class T3> calculatedresulttype ibeta(T1 a, T2 b, T3 x); template <class T1, class T2, class T3, class Policy> calculatedresulttype ibeta(T1 a, T2 b, T3 x, const Policy&);
Returns the normalised incomplete beta function of a, b and x:
template <class T1, class T2, class T3> calculatedresulttype ibetac(T1 a, T2 b, T3 x); template <class T1, class T2, class T3, class Policy> calculatedresulttype ibetac(T1 a, T2 b, T3 x, const Policy&);
Returns the normalised complement of the incomplete beta function of a, b and x:
template <class T1, class T2, class T3> calculatedresulttype beta(T1 a, T2 b, T3 x); template <class T1, class T2, class T3, class Policy> calculatedresulttype beta(T1 a, T2 b, T3 x, const Policy&);
Returns the full (nonnormalised) incomplete beta function of a, b and x:
template <class T1, class T2, class T3> calculatedresulttype betac(T1 a, T2 b, T3 x); template <class T1, class T2, class T3, class Policy> calculatedresulttype betac(T1 a, T2 b, T3 x, const Policy&);
Returns the full (nonnormalised) complement of the incomplete beta function of a, b and x:
The following tables give peak and mean relative errors in over various domains of a, b and x, along with comparisons to the GSL1.9 and Cephes libraries. Note that only results for the widest floatingpoint type on the system are given as narrower types have effectively zero error.
Note that the results for 80 and 128bit long doubles are noticeably higher than for doubles: this is because the wider exponent range of these types allow more extreme test cases to be tested. For example expected results that are zero at double precision, may be finite but exceptionally small with the wider exponent range of the long double types.
Table 6.18. Error rates for ibeta
Microsoft Visual C++ version 12.0 
GNU C++ version 5.1.0 
GNU C++ version 5.1.0 
Sun compiler version 0x5130 


Incomplete Beta Function: Small Values 
Max = 9.42ε (Mean = 2.24ε) 
Max = 0ε (Mean = 0ε) 
Max = 6.27ε (Mean = 1.7ε) 
Max = 21.3ε (Mean = 2.74ε) 
Incomplete Beta Function: Medium Values 
Max = 108ε (Mean = 16.3ε) 
Max = 0ε (Mean = 0ε) 
Max = 51.4ε (Mean = 12ε) 
Max = 124ε (Mean = 18.4ε) 
Incomplete Beta Function: Large and Diverse Values 
Max = 1.19e+003ε (Mean = 59.5ε) 
Max = 1.26ε (Mean = 0.063ε) 
Max = 1.96e+04ε (Mean = 997ε) 
Max = 4.98e+04ε (Mean = 2.07e+03ε) 
Incomplete Beta Function: Small Integer Values 
Max = 3.69ε (Mean = 0.765ε) 
Max = 0ε (Mean = 0ε) 
Max = 4.45ε (Mean = 0.814ε) 
Max = 44.5ε (Mean = 10.1ε) 
Table 6.19. Error rates for ibetac
Microsoft Visual C++ version 12.0 
GNU C++ version 5.1.0 
GNU C++ version 5.1.0 
Sun compiler version 0x5130 


Incomplete Beta Function: Small Values 
Max = 6.56ε (Mean = 1.88ε) 
Max = 0ε (Mean = 0ε) 
Max = 8.47ε (Mean = 1.68ε) 
Max = 13.8ε (Mean = 2.69ε) 
Incomplete Beta Function: Medium Values 
Max = 56.2ε (Mean = 14.1ε) 
Max = 0ε (Mean = 0ε) 
Max = 75.3ε (Mean = 11.3ε) 
Max = 132ε (Mean = 19.8ε) 
Incomplete Beta Function: Large and Diverse Values 
Max = 1.87e+003ε (Mean = 93.8ε) 
Max = 0.981ε (Mean = 0.0638ε) 
Max = 3.45e+04ε (Mean = 1.32e+03ε) 
Max = 4.26e+04ε (Mean = 1.54e+03ε) 
Incomplete Beta Function: Small Integer Values 
Max = 6.37ε (Mean = 1.03ε) 
Max = 0ε (Mean = 0ε) 
Max = 5.34ε (Mean = 1.11ε) 
Max = 107ε (Mean = 17.1ε) 
Table 6.20. Error rates for beta (incomplete)
Microsoft Visual C++ version 12.0 
GNU C++ version 5.1.0 
GNU C++ version 5.1.0 
Sun compiler version 0x5130 


Incomplete Beta Function: Small Values 
Max = 11.1ε (Mean = 2.28ε) 
Max = 0ε (Mean = 0ε) 
Max = 12.5ε (Mean = 2.01ε) 
Max = 18.7ε (Mean = 3.19ε) 
Incomplete Beta Function: Medium Values 
Max = 91.1ε (Mean = 12.7ε) 
Max = 0.568ε (Mean = 0.0342ε) 
Max = 67.1ε (Mean = 13.3ε) 
Max = 174ε (Mean = 25ε) 
Incomplete Beta Function: Large and Diverse Values 
Max = 635ε (Mean = 29.5ε) 
Max = 0.999ε (Mean = 0.0399ε) 
Max = 6.84e+04ε (Mean = 2.76e+03ε) 
Max = 6.86e+04ε (Mean = 2.79e+03ε) 
Incomplete Beta Function: Small Integer Values 
Max = 26.8ε (Mean = 6.61ε) 
Max = 0.786ε (Mean = 0.0248ε) 
Max = 10.2ε (Mean = 3.49ε) 
Max = 51.8ε (Mean = 11ε) 
Table 6.21. Error rates for betac
Microsoft Visual C++ version 12.0 
GNU C++ version 5.1.0 
GNU C++ version 5.1.0 
Sun compiler version 0x5130 


Incomplete Beta Function: Small Values 
Max = 12ε (Mean = 2.43ε) 
Max = 0.676ε (Mean = 0.0302ε) 
Max = 7.75ε (Mean = 1.91ε) 
Max = 12.6ε (Mean = 3.02ε) 
Incomplete Beta Function: Medium Values 
Max = 91.3ε (Mean = 14.8ε) 
Max = 0.949ε (Mean = 0.0944ε) 
Max = 63.5ε (Mean = 13.4ε) 
Max = 97.6ε (Mean = 24.3ε) 
Incomplete Beta Function: Large and Diverse Values 
Max = 3.72e+003ε (Mean = 113ε) 
Max = 1.12ε (Mean = 0.0458ε) 
Max = 1.05e+05ε (Mean = 5.45e+03ε) 
Max = 1.04e+05ε (Mean = 5.46e+03ε) 
Incomplete Beta Function: Small Integer Values 
Max = 26.7ε (Mean = 6.67ε) 
Max = 0.586ε (Mean = 0.0314ε) 
Max = 10.4ε (Mean = 3.54ε) 
Max = 103ε (Mean = 17.4ε) 
There are two sets of tests: spot tests compare values taken from Mathworld's online function evaluator with this implementation: they provide a basic "sanity check" for the implementation, with one spottest in each implementationdomain (see implementation notes below).
Accuracy tests use data generated at very high precision (with NTL RR class set at 1000bit precision), using the "textbook" continued fraction representation (refer to the first continued fraction in the implementation discussion below). Note that this continued fraction is not used in the implementation, and therefore we have test data that is fully independent of the code.
This implementation is closely based upon "Algorithm 708; Significant digit computation of the incomplete beta function ratios", DiDonato and Morris, ACM, 1992.
All four of these functions share a common implementation: this is passed both x and y, and can return either p or q where these are related by:
so at any point we can swap a for b, x for y and p for q if this results in a more favourable position. Generally such swaps are performed so that we always compute a value less than 0.9: when required this can then be subtracted from 1 without undue cancellation error.
The following continued fraction representation is found in many textbooks but is not used in this implementation  it's both slower and less accurate than the alternatives  however it is used to generate test data:
The following continued fraction is due to Didonato and Morris, and is used in this implementation when a and b are both greater than 1:
For smallish b and x then a series representation can be used:
When b << a then the transition from 0 to 1 occurs very close to x = 1 and some care has to be taken over the method of computation, in that case the following series representation is used:
Where Q(a,x) is an incomplete gamma function. Note that this method relies on keeping a table of all the p_{n } previously computed, which does limit the precision of the method, depending upon the size of the table used.
When a and b are both small integers, then we can relate the incomplete beta to the binomial distribution and use the following finite sum:
Finally we can sidestep difficult areas, or move to an area with a more efficient means of computation, by using the duplication formulae:
The domains of a, b and x for which the various methods are used are identical to those described in the Didonato and Morris TOMS 708 paper.