# Avoiding underflow with equations of the form $\binom{n}{x} p^a (1-p)^b$

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 How can I evaluate such constructions without getting underflow errors?

• 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! Nov 25 at 5:50
• Just set the precision of p, e.g., e /. p -> 0.00110 Nov 25 at 6:17
• Or use exact numbers: p->1/1000. Then you can apply N. Nov 25 at 7:20
• 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. Nov 25 at 10:14