3

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?

JimB
  • 41,653
  • 3
  • 48
  • 106
  • 3
    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! – thorimur Nov 25 '21 at 05:50
  • 1
    Just set the precision of p, e.g., e /. p -> 0.001\10` – Bob Hanlon Nov 25 '21 at 06:17
  • 1
    Or use exact numbers: p->1/1000. Then you can apply N. – Bill Watts Nov 25 '21 at 07:20
  • 1
    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. – evanb Nov 25 '21 at 10:14

0 Answers0