3
$\begingroup$

I have the need to evaluate equations of the form

$$\binom{n}{x} p^a (1-p)^b$$

where the values of $p$ are between 0 and 1 and $n$, $x$, $a$, and $b$ can be very large numbers.

I attempt to use

parms = {n -> 200, x -> 103, a -> 105, b -> 98}; 
e = Exp[LogGamma[n + 1] - LogGamma[x + 1] - LogGamma[n - x + 1] + a Log[p] + b Log[1 - p]] /. parms

but this gets converted back to

82791133891761429477050485625917802548514807100408460044000 (1 - p)^98 p^105

I was thinking that the interior of Exp[...] would get evaluated first but that doesn't happen. When I need to plug in certain values for $p$ I can get underflow errors. For example,

e /. p -> 0.001

results in

Underflow error

How can I evaluate such constructions without getting underflow errors?

$\endgroup$
4
  • 3
    $\begingroup$ One way to ensure that Exp doesn't evaluate until you want it to is to use an Inactive[Exp] instead: e = Inactive[Exp][LogGamma[n + 1] - LogGamma[x + 1] - LogGamma[n - x + 1] + a Log[p] + b Log[1 - p]] /. parms. Then Activate[e /. p -> 0.001] seems to work! $\endgroup$
    – thorimur
    Nov 25 at 5:50
  • 1
    $\begingroup$ Just set the precision of p, e.g., e /. p -> 0.001`10 $\endgroup$
    – Bob Hanlon
    Nov 25 at 6:17
  • 1
    $\begingroup$ Or use exact numbers: p->1/1000. Then you can apply N. $\endgroup$
    – Bill Watts
    Nov 25 at 7:20
  • 1
    $\begingroup$ e[n_, x_, a_, b_][p_] := Exp[LogGamma[n + 1] - LogGamma[x + 1] - LogGamma[n - x + 1] + a Log[p] + b Log[1 - p]] and then e[200, 103, 105, 98][0.001] gives 7.50588e-257. $\endgroup$
    – evanb
    Nov 25 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.