# How to make a two-tailed hypergeometric test?

I am talking about a hypergeometric test but the logic probably applies as well to the binomial test (and other similar tests).

Basic Scenario

I have a population of $n+m$ balls in an urn. There are $m$ white balls and $n$ black balls. I draw a sample of $k$ balls without replacement. I get $q$ white balls and $k-q$ black balls.

• Null hypothesis: White and black balls have the same probability of being sampled.

• Alternative hypothesis: White and black balls DO NOT have the same probability of being sampled.

One-tailed hypergeometric test

As there is no replacement, the probability of obtaining $q$ white balls is given by a hypergeometric distribution. I can calculate the probability of getting $k$ or less than $k$ balls in my sample by adding all the probability over the range k to 0. In other words, I can compute $\sum_{i=0}^q P(i)$, where $P(i)$ is the probability of of drawing $i$ balls given by the hypergeometric function. As I am using R, here is the R code for this calculation phyper(q=q, m=m, n=n, k=k).

Similarly, I could compute the probability of obtaining $q$ or more balls: $\sum_{i=q}^k P(i)$. In R it gives phyper(q=q-1, m=m, n=n, k=k, lower.tail = FALSE)

My question is...

How can I get a p.value for a two-tailed hypergeometric test?