Skip to content

Instantly share code, notes, and snippets.

@ngopal
Created June 30, 2014 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngopal/82e618f74934a06f8658 to your computer and use it in GitHub Desktop.
Save ngopal/82e618f74934a06f8658 to your computer and use it in GitHub Desktop.
A set of functions to calculate the probability of intersection between sets being a chance event in a hypergeometric distribution
function GetBinCoeff(N, K) {
// http://stackoverflow.com/questions/14556266/how-to-calculate-combination-of-large-numbers
if ( K > N) {
return 0;
}
else {
var r = 1;
var d;
for (d = 1; d <= K; d++)
{
r *= N--;
r /= d;
}
return r;
}
}
function hypergeometric(N, K, n, k) {
// https://www.biostars.org/p/15548/
// http://en.wikipedia.org/wiki/Hypergeometric_distribution
return GetBinCoeff(K, k) * GetBinCoeff(N-K, n-k) / GetBinCoeff(N, n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment