Last active
June 30, 2024 16:41
-
-
Save jonschoning/f31e6b992e7d40dc178bdba280598db3 to your computer and use it in GitHub Desktop.
hypergeometric calc
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
function docalc() { | |
var deck = $('#ndeck').val(); | |
var copies = $('#ncopies').val(); | |
var drawn = $('#ndrawn').val(); | |
var successes = $('#nsuccesses').val(); | |
var hyperGeoOutput = '<p>'; | |
//Chance to draw x or more cards | |
if (successes > 0) | |
pral = 1 - hyp(successes - 1, drawn, copies, deck); | |
else | |
pral = 1; | |
if (pral < 1e-6) | |
pral = 0; | |
pral = (100 * pral).toPrecision(3) + '%' | |
hyperGeoOutput += '<b>Chance to draw ' + successes + ' or more of the wanted card ' + pral + '</b><br>'; | |
//Chance to draw x cards | |
if (successes > 0) { | |
pr = hyp(successes, drawn, copies, deck) - hyp(successes - 1, drawn, copies, deck); | |
} | |
else | |
pr = hyp(0, drawn, copies, deck); | |
if (pr < 1e-6) | |
pr = 0; | |
pr = (100 * pr).toPrecision(3) + '%'; | |
hyperGeoOutput += 'Chance to draw exactly ' + successes + ' of the wanted card <b>' + pr + '</b><br>'; | |
//Chance to draw x or less cards | |
pram = hyp(successes, drawn, copies, deck); | |
if (pram < 1e-6) | |
pram = 0; | |
pram = (100 * pram).toPrecision(3) + '%'; | |
hyperGeoOutput += 'Chance to draw ' + successes + ' or less of the wanted card <b>' + pram + '</b><br>'; | |
//Chance to draw 0 cards | |
zero = hyp(0, drawn, copies, deck); | |
if (zero < 1e-6) | |
zero = 0; | |
zero = (100 * zero).toPrecision(3) + '%'; | |
hyperGeoOutput += 'Chance to draw 0 of the wanted card <b>' + zero + '</b></p>'; | |
$('#hyperGeoOutput').html(hyperGeoOutput); | |
} |
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
import numpy as np | |
from scipy.stats import hypergeom | |
# chance for 3 or more successes, pop: 99, succ in pop: 35, succ in samp: 7 | |
format(1 - hypergeom.cdf(2, 99, 35, 7), ".1%") | |
# 47.7% | |
# chance for k or more successes, pop: M, succ in pop: n, succ in samp: N | |
def hyp(k, M, n, N): return 1 - hypergeom.cdf(k-1, M, n, N) | |
hyp(3, 99, 35, 7) | |
# range succ in pop 35 to 40 | |
[[n, hyp(3, 99, n, 7)] for n in range(35, 40)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment