# Floating Point error when computing Binomial Distribution Probability

I have been given a binomial distribution: $$B(m+n;n,p)=\frac{(m+n)!}{m!n!}p^mq^n.$$

Here $$m = 10^3$$, $$n=10^2$$, $$p=10^{-2}$$, $$q=1-p.$$

I'm using MATLAB to compute log $$B(m+n;n,p)$$ and store the value in logB

m=10^3;
n=10^2;
p=10^(-2);
q=1-p;
logB=log(factorial(m+n)/(factorial(m)*factorial(n))*p^m*q^n)


I get logB as NaN . How can I modify the formula to avoid floating point error and get a valid answer?

• Taking those factorials is probably giving you infinities, the division of which gives you NaN. You need a factorial division function which preemptively cancels. Jun 2, 2020 at 13:53
• You have a ratio of a product of $n$ terms by a product of $n$ terms. Write this as the product of $n$ ratios, each of which should of moderate size. Jun 2, 2020 at 14:24
• @Richard I'm trying to simplify but still stuck: ((m+n)(m+n-1)...(m+1))/n!
– user36184
Jun 2, 2020 at 14:27
• @WolfgangBangerth could you elaborate a bit? Thanks.
– user36184
Jun 2, 2020 at 14:43
• You want to compute $\frac{abc}{def}$ where both enumerator and denominator end up very large numbers. So compute it as $\frac ad\cdot \frac be \cdot \frac cf$ where now each fraction is of moderate size. Jun 2, 2020 at 18:31

This is why gammaln exists
logB = gammaln(m+n+1) - gammaln(m+1) - gammaln(n+1) + m*log(p) + n*log(q);