Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>
- Multivariate Hypergeometric Calculator
- </title>
- </head>
- <body style="text-align: center; padding-top: 50px;">
- <p id="parameters">
- Population Size: <input id="populationSize" type="text" />
- Sample Size: <input id="sampleSize" type="text" />
- </p>
- <p id="var1">
- Quantity: <input id="quantity1" type="text" />
- Min: <input id="min1" type="text" />
- Max: <input id="max1" type="text" />
- </p>
- <p id="var2">
- Quantity: <input id="quantity2" type="text" />
- Min: <input id="min2" type="text" />
- Max: <input id="max2" type="text" />
- </p>
- <p id="var3">
- Quantity: <input id="quantity3" type="text" />
- Min: <input id="min3" type="text" />
- Max: <input id="max3" type="text" />
- </p>
- <p id="var4">
- Quantity: <input id="quantity4" type="text" />
- Min: <input id="min4" type="text" />
- Max: <input id="max4" type="text" />
- </p>
- <p id="var5">
- Quantity: <input id="quantity5" type="text" />
- Min: <input id="min5" type="text" />
- Max: <input id="max5" type="text" />
- </p>
- <p id="var6">
- Quantity: <input id="quantity6" type="text" />
- Min: <input id="min6" type="text" />
- Max: <input id="max6" type="text" />
- </p>
- <p id="var7">
- Quantity: <input id="quantity7" type="text" />
- Min: <input id="min7" type="text" />
- Max: <input id="max7" type="text" />
- </p>
- <p id="var8">
- Quantity: <input id="quantity8" type="text" />
- Min: <input id="min8" type="text" />
- Max: <input id="max8" type="text" />
- </p>
- <p id="var9">
- Quantity: <input id="quantity9" type="text" />
- Min: <input id="min9" type="text" />
- Max: <input id="max9" type="text" />
- </p>
- <p id="var10">
- Quantity: <input id="quantity10" type="text" />
- Min: <input id="min10" type="text" />
- Max: <input id="max10" type="text" />
- </p>
- <p>
- <button type="button" onclick="calculateOdds()">Calculate Odds</button>
- </p>
- <p>
- <span id="result"></span>
- </p>
- <script>
- const factorial = n => {
- let acc = n;
- if (n === 0 || n === 1)
- return 1;
- for (var i = n - 1; i >= 1; i--) {
- acc *= i;
- }
- return acc;
- }
- const binomialCoefficientValue = (n, k) => Math.round(factorial(n) / (factorial(k) * factorial(n - k)));
- const getBinomialCoefficient = (n, k) => {
- return {
- n : n,
- k : k,
- value : binomialCoefficientValue(n, k)
- }
- }
- const getBinomialCoefficientsInRange = (n, minK, maxK) => {
- let coeffs = [];
- for (let k = minK; k <= maxK; k++) {
- coeffs.push(getBinomialCoefficient(n, k));
- }
- return coeffs;
- }
- const f = (a, b) => [].concat(...a.map(d => b.map(e => [].concat(d, e))));
- const cartesian = (a, b, ...c) => (b ? cartesian(f(a, b), ...c) : a);
- const cartesianProduct = a => a.length > 1 ? cartesian(...a) : a[0].map(c => [c]);
- const sumN = binomialCoefficients => binomialCoefficients.reduce((acc, coefficient) => acc + coefficient.n, 0);
- const sumK = binomialCoefficients => binomialCoefficients.reduce((acc, coefficient) => acc + coefficient.k, 0);
- const getAllNumerators = (coefficientsGroupedByRanges, populationSize, sampleSize) => cartesianProduct(coefficientsGroupedByRanges).map(coefficients => coefficients.concat([getBinomialCoefficient(populationSize - sumN(coefficients), sampleSize - sumK(coefficients))]));
- const getDenominator = (populationSize, sampleSize) => getBinomialCoefficient(populationSize, sampleSize);
- const getAllNumeratorValues = (coefficientsGroupedByRanges, populationSize, sampleSize) => getAllNumerators(coefficientsGroupedByRanges, populationSize, sampleSize).map(numerator => numerator.reduce((acc, coefficient) => acc * coefficient.value, 1));
- const getDenominatorValue = (populationSize, sampleSize) => getDenominator(populationSize, sampleSize).value;
- const hypergeo = (coefficientsGroupedByRanges, populationSize, sampleSize) => getAllNumeratorValues(coefficientsGroupedByRanges, populationSize, sampleSize).map(numerator => numerator / getDenominatorValue(populationSize, sampleSize)).reduce((acc, result) => acc + result, 0);
- const prettyPrint = result => `${(result * 100).toFixed(2)}%`;
- const getCoefficientsGroupedByRanges = () => {
- let coefficientsGroupedByRanges = [];
- for (let i = 1; i <= 10; i++) {
- var quantity = parseInt(document.getElementById(`quantity${i}`).value);
- var min = parseInt(document.getElementById(`min${i}`).value);
- var max = parseInt(document.getElementById(`max${i}`).value);
- if (quantity > 0 && min >= 0 && max >= 0) {
- coefficientsGroupedByRanges.push(getBinomialCoefficientsInRange(quantity, min, max));
- }
- }
- return coefficientsGroupedByRanges;
- }
- const calculateOdds = () => {
- const coefficientsGroupedByRanges = getCoefficientsGroupedByRanges();
- const populationSize = parseInt(document.getElementById(`populationSize`).value);
- const sampleSize = parseInt(document.getElementById(`sampleSize`).value);
- const result = hypergeo(coefficientsGroupedByRanges, populationSize, sampleSize);
- document.getElementById(`result`).innerHTML = prettyPrint(result);
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement