# Hypergeometric trials - Number of trials needed to achieve a given probability

Given a known number of white and black balls in an urn, what is the number of trials without replacement required to achieve a given x probability of drawing at least one white ball.

Which function gives me this number of trials?

Thank you very much

• I believe the replies at stats.stackexchange.com/questions/24211 contain the information needed to solve this problem. – whuber Mar 9 '12 at 14:43
• I took a look but still I cant rearrange the formula to solve for the number of trials – ECII Mar 9 '12 at 15:41
• Do you need a formula or would an R function do the trick? – whuber Mar 9 '12 at 17:19

The question asks about a hypergeometric distribution. In R, the chance of obtaining no white balls in an urn with $w$ white balls and $b$ black balls after $k$ draws is

phyper(0, w, b, k)


We need to find the smallest nonnegative integral $k$ for which this number is less than or equal to $\alpha = 1-x$. (E.g., if $x$ is 95%, $\alpha$ is $0.05$.) Most standard root-finding methods will work, but (unless the urn has a huge number of balls), a brute-force search will be reasonable:

g <- function(alpha,w,b) { # 0 < alpha < 1; 1 <= w + b; 0 <= w, b
p <- phyper(0,w,b,1:(w+b))
k <- min(which(p<=alpha))
list(count=k, probs=phyper(0,w,b,(k-1):k))
}


This incarnation of the solution returns two things: the answer $k$ (as "\$count") and a check of the answer (as "\$probs") in the form of the hypergeometric probability for $k-1$ (which should be strictly greater than $\alpha$) and the probability for $k$ (which should be less than or equal to $\alpha$).

Example:

> set.seed(17)
> table(replicate(10000, sum(sample(c(rep(1,3), rep(0,10)), 8))))
0    1    2    3
355 2815 4844 1986


In this simulation with 10,000 independent trials, 8 balls were drawn without replacement from an urn with 3 white and 10 black balls. In 355 (3.55%) of those trials no white balls were drawn. What does our previous calculation suggest?

> g(0.05, 3, 10)
$count [1] 8$probs
[1] 0.06993007 0.03496503


It says that indeed, we need to withdraw 8 balls in order to have at least $x = 1-\alpha = 95\%$ chance of obtaining a white ball, and that the chance is 100 - 3.4965% of doing so (which is at least $x$; whereas the chance when only 7 balls are withdrawn is just 100 - 6.993%, which is less than $x$). This is in close agreement with the simulated value.