the Binomial Distribution might be one of the most important algorithms in finance and statistics, but i was surprised at how challenging it seems to be to find a good quality implementation in Swift.
since “quality” is a bit subjective, i’ll narrow it down as follows:
- performs well for very large n, including n >
Int.max / 2
, - is reasonably accurate for both p near 0.5, and very small p,
- is maintained,
- can be used in WebAssembly (does not import
Foundation
).
C++ has this in its standard library, as binomial_distribution
. it uses multiple regimes of approximations for small n, large n, p near 0.5, and very small p.
i asked Google to recommend me some Swift libraries that implement the Binomial Distribution. it gave me:
Several Swift libraries provide dedicated functions for working with binomial distributions and other statistical calculations:
Probably: This library offers Swift structures for computing probability and cumulative distributions for various probability functions, including the binomial distribution. It can be easily installed using the Swift Package Manager.
StatKit: This library provides a collection of statistical analysis tools, including distributions. It supports calculating probabilities, moments, and generating random samples from a distribution. You can install StatKit using the Swift Package Manager.
sigma: This library provides functions for various statistical calculations, written in Swift. While not explicitly listed as having a binomial distribution, it could be a useful resource for general statistical needs.
SwiftyStats: Another statistical framework written in Swift. It focuses on common statistical procedures
Probably has not been maintained in over nine years, which is almost as long as Swift has existed. so i didn’t consider it at all.
StatKit is actively maintained, but it doesn’t use a very good algorithm for large n, as it loops through the range 0 ..< n
for every sample.
SigmaSwiftStatistics has a Binomial Distribution, but has not been maintained in 6 years.
SwiftyStats is slightly fresher, it was last updated 4 years ago, but doesn’t support direct binomial sampling. it can only compute the pdf of a Binomial Distribution for a particular value of k, using an algorithm that is linear in k, which means generating a sample would take O(nk).
is there any hope, or am i better off copying source code from a popular implementation of libstd++?