1
$\begingroup$

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?

$\endgroup$
5
  • 1
    $\begingroup$ Taking those factorials is probably giving you infinities, the division of which gives you NaN. You need a factorial division function which preemptively cancels. $\endgroup$
    – Richard
    Jun 2, 2020 at 13:53
  • 2
    $\begingroup$ 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. $\endgroup$ Jun 2, 2020 at 14:24
  • $\begingroup$ @Richard I'm trying to simplify but still stuck: ((m+n)(m+n-1)...(m+1))/n! $\endgroup$
    – user36184
    Jun 2, 2020 at 14:27
  • $\begingroup$ @WolfgangBangerth could you elaborate a bit? Thanks. $\endgroup$
    – user36184
    Jun 2, 2020 at 14:43
  • $\begingroup$ 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. $\endgroup$ Jun 2, 2020 at 18:31

1 Answer 1

5
$\begingroup$

This is why gammaln exists

logB = gammaln(m+n+1) - gammaln(m+1) - gammaln(n+1) + m*log(p) + n*log(q);

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.