So is this right?
(compared to below)
In order to describe the algorithm, the following definitions will be used:
R = number of rows in the data set D
G = number of groups, i.e., unique value pairs, in columns A and B
If the groups are numbered from 1 to G, the following definitions will be used for
the group with index i:
Ki = number of rows belonging to group i
Mi = number of rows in D where the A value = the A value in group i
Ni = number of rows in D where the B value = the B value in group i
The p-value for the group with index i can then be calculated as follows:
Pi = P(X ≥ Ki | R, Ni, Mi) = ∑ P(X = x | R, Ni, Mi); x = Ki, ..., min(Ni, Mi)
where X is a random variable with a hypergeometric distribution. In probability theory,
this distribution describes the number of successes in a sequence of a certain number
of draws from a finite population without replacement.
This means that the probability formula can be written as follows:
P(X = x | R, Ni, Mi) = B(Ni, x) ∙ B(R - Ni, Mi - x) / B(R, Mi)
where
B(n, k) = n! / (k! ∙ (n - k)!) if n ≥ k ≥ 0
B(n, k) = 0 if k < 0 or k > n
is the binomial coefficient of n and k.