7

I am calculating the probability of rejecting a one-tailed hypothesis test for inequality of two proportions $p_1$ and $p_2$ (i.e., power) using $H_o: p_2 \leq p_1$ vs. the alternative $H_a: p_2 > p_1$ using the exact formulation

$$ Prob=\sum_{i=0}^m \sum_{j=0}^n \binom{m}{i} \binom{n}{j} p_1^i \cdot (1 - p_1)^{m - i} \cdot p_2^j \cdot (1 - p_2)^{n - j} \cdot I(T>z_{1-\alpha}), $$

where $T = \frac{ p_j - p_i}{\sqrt{\bar{p} (1 - \bar{p})}\sqrt{\frac{1}{m}+\frac{1}{n}}}$, $p_i = i / m$, $p_j = j / n$, $\bar{p} = (i + j) / (m + n)$, and typical values of $m$ and $n$ are e.g. $80$ for my use case. $I()$ is a boolean indicator function. The binomial coefficient values $\binom{m}{i}$ take on very large integer values that are $\gg 10^{308}$ - the limit of double precision memory storage for most compilers. Analogously, the probabilities $p_1^i \cdot (1 - p_1)^{m - i} \cdot p_2^j \cdot (1 - p_2)^{n - j}$ can also be e.g. $\ll 10^{-308}$.

Are there any tricks for representing very small probabilities using large integers, or some reliable algorithm that would enable me to perform such multiplications. The limiting issue is that there doesn't seem to be one or more fast methods for memory representation of a real value with $\gg 308$ digit precision.

wjktrs
  • 207
  • 3
    This question is unclear, and probably more suited to stackoverflow since it seems to be about programming, not mathematics. That said, if you [edit] the question we might be able to help. Where to these numbers come from? In what form do you know the large number and the small probability. In the example they are exact, of the form $10^k$ so you know their product exactly. – Ethan Bolker Jun 05 '24 at 01:37
  • 2
    Do you know the formula for $\text{Prob}$ ? This a first point. Could you give one problematic case $(p=?,n=?,m=?)$ in order to check – Claude Leibovici Jun 05 '24 at 02:22
  • 1
    Isn't this just $1$, by the binomial theorem? So what is the issue? Or are you trying to evaluate the individual summands? – Jair Taylor Jun 05 '24 at 03:41
  • @Jair Taylor - No it's not one. For example, the binomial probability of $x=2$ successes (heads) in $n=3$ independent trials (flips) of a fair coin is $Prob=\binom{n}{x}p^x(1-p)^{n-x}=\binom{3}{2}0.5^2 0.5^{3-2}=3(0.25)(0.5)=0.375$ – wjktrs Jun 05 '24 at 04:22
  • 3
    @wjktrs That's one summand. The expression you gave is the sum over all possibilities, which is $1$. – Jair Taylor Jun 05 '24 at 04:35
  • 1
    @Jair Taylor - Apologies, as there's a true/false boolean indicator on the far right side of the probability equation that I just added. The equation(s) of interest are actually more detailed, and to go into detail would be beyond the scope of the question -- other variants of the equation have 4 summations, 4 binomial coefficients, and 2 different $p$'s. I'll work it out using a mixture of random draws combined with the exact equation provided. – wjktrs Jun 05 '24 at 05:13
  • FWIW a long double can store values in excess of $10^4000$ in many cases (it does depend on the compiler etc). – psmears Jun 05 '24 at 11:45
  • 1
    Can you clarify the meaning of the indicator term at the end? It seems to be independent of the summation variables (and hence irrelevant AFAICT). This whole calculation can fairly easily be done in logspace, I think (e.g. here, here, but I don't want to spend time on an example before I know about the last term ... – Ben Bolker Jun 05 '24 at 19:58
  • @Ben Bolker - I clarified the test used for the indicator function. – wjktrs Jun 05 '24 at 21:36
  • No, I subscripted the proportions with $i$ and $j$ because you get two new proportions each time $i$ and $j$ change, which are $p_i=i/m$ and $p_j = j/n$. They're not probabilities raised to the power of $i$ or $j$. – wjktrs Jun 05 '24 at 21:48
  • OK, I think I understand. (It's a little confusing having $p_1^i$, $p_2^j$, $p_i$, $p_j$ in the same equation ...) – Ben Bolker Jun 05 '24 at 23:24
  • I have a simple result if no restriction – Claude Leibovici Jun 06 '24 at 14:34
  • 2
    This is a very different question now. Please don't change the question so drastically in edits. If answers to your original question have already been given, it's better to write a new question rather than invalidate existing answers. – Jair Taylor Jun 06 '24 at 17:43

4 Answers4

14

Do you need $308$ digits of precision or just $10^{308}$ of range? If you need the precision, there is software that can do that, like Mathematica. If you just need the range, you can work with logarithms until the large and small numbers multiply to get in range. Stirling's approximation makes logs of the factorials easy. For the powers of $p$ and $(1-p)$ you can take the logs of $p$ and $1-p$. If one is small and the other is close to $1$ it may be better to use the Taylor series of $\log (1-x)$ for the one that is close to $1$.

Ross Millikan
  • 384,643
  • 3
    On a computer you can also simply add or subtract the logs of all the factors in each binomial coefficient rather than using Stirling. – David K Jun 05 '24 at 03:20
  • 5
    Many modern math libraries provide a specialised log(1+x) function (often called log1p). – PM 2Ring Jun 05 '24 at 03:28
  • If the power of 10 of a large integer-valued binomial coefficient $\binom{m}{i}$ is e.g. $10^{500}$, then you know the digit precision of the probabilities have to be smaller than $10^{-500}$. Obviously, this is because $log_{10}(ab)=log_{10}(a) + log_{10}(b)$. Thus, $ab=10^{a-b}$, so $a<b$ in order to obtain a probability. – wjktrs Jun 05 '24 at 03:36
  • Depending on what you are doing, a final answer with $20$ or even $6$ decimal digits of precision may be good enough. In that case you only need a few more digits of precision in the intermediate values. You need to keep track of the exponents, that is true, but the logs make that easy. Calculating the log of each term in your sum can be done in standard $64$ bit arithmetic. Once you multiply the big and small numbers the result will be in range of usual floats, to some terms may be too small. If you have any terms in range you can ignore the ones that are too small. – Ross Millikan Jun 05 '24 at 03:41
  • When $m\sim80$ and $n\sim80$ and e.g. $p=0.1$, use of 64-bit floating point precision precludes calculations whose values are outside of $[10^{-308},10^{308}]$, resulting in $Prob$ values that are about 0.1 to low. Blowing off (sidestepping) those calculations I believe results in the loss of probability. – wjktrs Jun 05 '24 at 03:52
11

This is an answer to the question before its edit.

Given $$\text{Prob}=\sum_{i=1}^n \sum_{j=1}^m \binom{n}{i}\binom{m}{j} p^{(i+j)}(1-p)^{(n+m-i-j)}$$ I should start rewiriting it as $$\text{Prob}=(1-p)^{(n+m)}\, \Gamma (m+1)\, \Gamma (n+1)\, \large \sum$$ with, using $\color{red}{q=\frac{p}{1-p}}$, $$\large \sum=\sum_{i=1}^n \sum_{j=1}^m \frac {q^{i+j}}{\Gamma (i+1) \Gamma (j+1) \Gamma (n-i+1) \Gamma(m-j+1) }$$ The inner sum write $$\frac{q^i \left((q+1)^m-1\right)}{\Gamma (i+1) \Gamma (m+1) \Gamma (n-i+1)}$$ making at the end $$\large \sum=\frac{\left((q+1)^m-1\right) \left((q+1)^n-1\right)}{\Gamma (m+1) \Gamma(n+1)}$$ Recombining and defining $\color{red}{t=\frac{1}{1-p}}$ $$\large\color{blue}{\text{Prob}=(1-t^{-m}) (1-t^{-n} )}$$ Computing $n\log(t)$ and $m\log(t)$ and exponentiating again is not supposed to make major problems.

Now, if $p$ is small, you could use series expansion and obtain $$\text{Prob}=mn\,p^2\Big(1+\sum_{k=1}^r a_k \, p^k \Big)$$ with $$a_1=-\frac 12 (m+n-2)$$ $$a_2=\frac 1{12}\left(2 m^2+3 m n-9 m+2 n^2-9 n+11\right)$$ $$a_3=-\frac 1{24} (m+n-4)\left(m^2+m n-4 m+n^2-4 n+5\right)$$ and so on.

  • Thanks much, I'll give this a try where $t=1/(1-p)$ and $Prob=(1-t^{-m})(1-t^{-n})$. Before I try this, if this approximation works, wouldn't it be an extremely fast way to approximate binomial probabilities? Fortunately, $0.05<p<0.95$ for this problem. – wjktrs Jun 05 '24 at 04:00
  • 2
    @wjktrs. The "blue" formula is strictly exact. – Claude Leibovici Jun 05 '24 at 04:04
  • 1
    I think this solution is overcomplicated - the sum factors as $$\left( \sum_{i=1}^n \binom{n}{i} p^i (1-p)^{n-i} \right) \cdot \left( \sum_{j=1}^m \binom{m}{j} p^j (1-p)^{m-j} \right)$$ and using $$\sum_{i=0}^n \binom{n}{i} p^i (1-p)^{n-i} = 1,$$ we can immediately write the product as $ \big( 1 - (1-p)^n \big) \big( 1 - (1-p)^m \big)$. – Adayah Jun 06 '24 at 07:16
  • @Adayah. I fully agree wih you. I just tried to show sme steps. – Claude Leibovici Jun 06 '24 at 07:18
  • 1
    Also it seems that the OP question has undergone a wild change since the time this answer was written. Which sadly makes this answer totally obsolete, even though obviously it was very much to the point at the time of publication. – Adayah Jun 06 '24 at 07:26
  • @Adayah. May I confess that I do not understand the new version ? – Claude Leibovici Jun 06 '24 at 08:51
  • The new version has $p_1$ and $p_2$ instead of a single $p$ and only some terms of the sum are considered - namely those that satisfy the inequality $T > z_{1-\alpha}$ - where $T$ is defined in terms of $i$ and $j$ and presumably $z_{1-\alpha}$ is a fixed number corresponding to the $p$-value of the test. – Adayah Jun 06 '24 at 14:18
3

You can calculate this binomial probabilities indirectly using a relationship with the beta distribution, discussed at our sister site here: https://stats.stackexchange.com/questions/4659/relationship-between-binomial-and-beta-distributions That way you avoid the problem with multiplication!

2

You can directly compute the answer only using bounded numbers. Let $f_p(m,i) := \binom{m}{i} p^i (1-p)^{m-i}$. Because it represents a probability, we have $0 \leq f_p(m,i) \leq 1$ and we have a recurrence:

$$ \begin{align} f_p(0,0) &= 1 \\ f_p(m,i) &= p f_p(m-1,i-1) + (1-p) f_p(m-1,i) & \text{if } 0 \leq i \leq m \\ f_p(m,i) &= 0 & \text{otherwise} \end{align} $$

therefore a dynamic programming algorithm computes the all values $f_p(m,i)$ given $p$ in $O(m^2)$ time.

To compute $\mathrm{Prob}$ in the question, compute the arrays $A_1, A_2$ such that $A_1[i] = f_{p_1}(m,i)$ for $0 \leq i \leq m$, and $A_2[j] = f_{p_2}(n,j)$ for $0 \leq j \leq n$, then

$$ \mathrm{Prob} = \sum_{i=0}^m \sum_{j=0}^n A_1[i] A_2[j] I(i,j) $$

for some function $I(\cdot,\cdot)$. It is total $O(n^2 + m^2 + nm)$ time, and all intermediate values are in $[0,1]$.

pcpthm
  • 211
  • You defined $A_2[i]=f_{p_2}(n,i)$, but should this be $A_2[j]=f_{p_2}(n,j)$? – wjktrs Jun 20 '24 at 00:25
  • 1
    @wjktrs I meant "$A_2[i] = f_{p_2}(n,i)$ for all $i$" so the name of the variable doesn't matter. Edited. – pcpthm Jun 20 '24 at 04:59