Created
November 8, 2023 09:59
-
-
Save rusco/6d093604f1b7d6cbd07fff2f78a3a64d to your computer and use it in GitHub Desktop.
hipergeometric function in javascript (with some testcases)
This file contains hidden or 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
//07.11.2023 | |
// Factorial function | |
const fact = (x) => (x == 0 ? 1 : x * fact(x - 1)); | |
// binomial coefficient where a is the total set of posibbilites and b is the number of combinatios we're interested in | |
const bincoeff = (a, b) => fact(a) / (fact(a - b) * fact(b)); | |
//hipergeometric function | |
const hipergeo = (M, N, n, k) => (bincoeff(M, k) * bincoeff(N - M, n - k)) / bincoeff(N, n); | |
//example : calc prob >= 3 | |
let N = 164, M = 18, n = 25, probabilidadeSum = 0.0; | |
for (let k = 3; k < 18; k++) { | |
probabilidadeSum += hipergeo(M, N, n, k); | |
} | |
console.log("Probabilidade = ", probabilidadeSum); | |
//use case, german lotto: | |
console.log(hipergeo(6, 49, 6, 0)); //lotto DE, 0 ok | |
console.log(hipergeo(6, 49, 6, 1)); //lotto DE, 1 ok | |
console.log(hipergeo(6, 49, 6, 2)); //lotto DE, 2 ok | |
console.log(hipergeo(6, 49, 6, 3)); //lotto DE, 3 ok | |
console.log(hipergeo(6, 49, 6, 4)); //lotto DE, 4 ok | |
console.log(hipergeo(6, 49, 6, 5)); //lotto DE, 5 ok | |
console.log(hipergeo(6, 49, 6, 6)); //lotto DE, 6 ok | |
//with layback: https://de.wikipedia.org/wiki/Binomialverteilung | |
//without layback: https://de.wikipedia.org/wiki/Hypergeometrische_Verteilung |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment