Search code examples
c++boostbinomial-coefficients

log version of boost binomial_coefficient?


Fairly simple question but I've been having trouble finding anything online.

I'm looking for a log version of boost::math::binomial_coefficient, which can handle some of the larger values generated by my code.

In R there is lchoose which provides this, however I can't seem to find the equivalent in c++

Any help would be much appreciated


Solution

  • I think you have to calculate it yourself. However this question asks how to do that, and it turns out not to be too bad. A first approximation is:

    n log n − m log m − (n−m) log (n−m)
    

    A better approximation can be obtained by using another term of the Stirling approximation for log(n!)

    n log n − m log m − (n−m) log (n−m) + ½(log n − log m − log(n−m) − log 2π)
    

    With all of these you are subtracting two terms which are potentially of similar size, so you are vulnerable to loss of accuracy.