javaapache-commonsapache-commons-mathbernoulli-probability

Does `org.apache.commons.math3.distribution.BinomialDistribution(1,p)` come with much performance overhead?


A Bernoulli distribution is equivalent to a binomial distribution with only 1 trial i.e. BinomialDistribution(1,p) from the Apache Commons Math library. A Bernoulli distribution is obviously a much simpler thing than the general binomial distribution.

If performance matters, should I roll my own BernoulliDistribution(p) by subclassing AbstractIntegerDistribution, or would I get almost the same performance by just going with BinomialDistribution(1,p)?


Solution

  • If you are actually using the distribution methods and performance is important, you should subclass AbstractIntegerDistribution.

    BinomialDistribution implements the distribution methods using numerical approximations via special functions. These computations carry some overhead and are not necessary in the degenerate (Bernoulli) case, where constants could be returned. As of version 3.4.1, there is no check for the degenerate case in the probability method; though there is a check in cumulativeProbability.