Created
June 30, 2014 05:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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