Advertisement
Guest User

Multivariable Hypergeometric Calculator

a guest
Oct 28th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 6.27 KB | None | 0 0
  1. <html>
  2.     <head>
  3.         <title>
  4.             Multivariate Hypergeometric Calculator
  5.         </title>
  6.     </head>
  7.     <body style="text-align: center; padding-top: 50px;">
  8.         <p id="parameters">
  9.             Population Size: <input id="populationSize" type="text" />
  10.             Sample Size: <input id="sampleSize" type="text" />
  11.         </p>
  12.         <p id="var1">
  13.             Quantity: <input id="quantity1" type="text" />
  14.             Min: <input id="min1" type="text" />
  15.             Max: <input id="max1" type="text" />
  16.         </p>
  17.         <p id="var2">
  18.             Quantity: <input id="quantity2" type="text" />
  19.             Min: <input id="min2" type="text" />
  20.             Max: <input id="max2" type="text" />
  21.         </p>
  22.         <p id="var3">
  23.             Quantity: <input id="quantity3" type="text" />
  24.             Min: <input id="min3" type="text" />
  25.             Max: <input id="max3" type="text" />
  26.         </p>
  27.         <p id="var4">
  28.             Quantity: <input id="quantity4" type="text" />
  29.             Min: <input id="min4" type="text" />
  30.             Max: <input id="max4" type="text" />
  31.         </p>
  32.         <p id="var5">
  33.             Quantity: <input id="quantity5" type="text" />
  34.             Min: <input id="min5" type="text" />
  35.             Max: <input id="max5" type="text" />
  36.         </p>
  37.         <p id="var6">
  38.             Quantity: <input id="quantity6" type="text" />
  39.             Min: <input id="min6" type="text" />
  40.             Max: <input id="max6" type="text" />
  41.         </p>
  42.         <p id="var7">
  43.             Quantity: <input id="quantity7" type="text" />
  44.             Min: <input id="min7" type="text" />
  45.             Max: <input id="max7" type="text" />
  46.         </p>
  47.         <p id="var8">
  48.             Quantity: <input id="quantity8" type="text" />
  49.             Min: <input id="min8" type="text" />
  50.             Max: <input id="max8" type="text" />
  51.         </p>
  52.         <p id="var9">
  53.             Quantity: <input id="quantity9" type="text" />
  54.             Min: <input id="min9" type="text" />
  55.             Max: <input id="max9" type="text" />
  56.         </p>
  57.         <p id="var10">
  58.             Quantity: <input id="quantity10" type="text" />
  59.             Min: <input id="min10" type="text" />
  60.             Max: <input id="max10" type="text" />
  61.         </p>
  62.         <p>
  63.             <button type="button" onclick="calculateOdds()">Calculate Odds</button>
  64.         </p>
  65.         <p>
  66.             <span id="result"></span>
  67.         </p>
  68.  
  69.         <script>
  70.             const factorial = n => {
  71.                 let acc = n;
  72.                 if (n === 0 || n === 1)
  73.                     return 1;
  74.                 for (var i = n - 1; i >= 1; i--) {
  75.                     acc *= i;
  76.                 }
  77.                 return acc;
  78.             }
  79.  
  80.             const binomialCoefficientValue = (n, k) => Math.round(factorial(n) / (factorial(k) * factorial(n - k)));
  81.  
  82.             const getBinomialCoefficient = (n, k) => {
  83.                 return {
  84.                     n : n,
  85.                     k : k,
  86.                     value : binomialCoefficientValue(n, k)
  87.                 }
  88.             }
  89.  
  90.             const getBinomialCoefficientsInRange = (n, minK, maxK) => {
  91.                 let coeffs = [];
  92.                 for (let k = minK; k <= maxK; k++) {
  93.                    coeffs.push(getBinomialCoefficient(n, k));
  94.                }
  95.                return coeffs;
  96.            }
  97.  
  98.            const f = (a, b) => [].concat(...a.map(d => b.map(e => [].concat(d, e))));
  99.             const cartesian = (a, b, ...c) => (b ? cartesian(f(a, b), ...c) : a);
  100.             const cartesianProduct = a => a.length > 1 ? cartesian(...a) : a[0].map(c => [c]);
  101.  
  102.             const sumN = binomialCoefficients => binomialCoefficients.reduce((acc, coefficient) => acc + coefficient.n, 0);
  103.             const sumK = binomialCoefficients => binomialCoefficients.reduce((acc, coefficient) => acc + coefficient.k, 0);
  104.  
  105.             const getAllNumerators = (coefficientsGroupedByRanges, populationSize, sampleSize) => cartesianProduct(coefficientsGroupedByRanges).map(coefficients => coefficients.concat([getBinomialCoefficient(populationSize - sumN(coefficients), sampleSize - sumK(coefficients))]));
  106.             const getDenominator = (populationSize, sampleSize) => getBinomialCoefficient(populationSize, sampleSize);
  107.  
  108.             const getAllNumeratorValues = (coefficientsGroupedByRanges, populationSize, sampleSize) => getAllNumerators(coefficientsGroupedByRanges, populationSize, sampleSize).map(numerator => numerator.reduce((acc, coefficient) => acc * coefficient.value, 1));
  109.             const getDenominatorValue = (populationSize, sampleSize) => getDenominator(populationSize, sampleSize).value;
  110.  
  111.             const hypergeo = (coefficientsGroupedByRanges, populationSize, sampleSize) => getAllNumeratorValues(coefficientsGroupedByRanges, populationSize, sampleSize).map(numerator => numerator / getDenominatorValue(populationSize, sampleSize)).reduce((acc, result) => acc + result, 0);
  112.             const prettyPrint = result => `${(result * 100).toFixed(2)}%`;
  113.  
  114.             const getCoefficientsGroupedByRanges = () => {
  115.                 let coefficientsGroupedByRanges = [];
  116.                 for (let i = 1; i <= 10; i++) {
  117.                    var quantity = parseInt(document.getElementById(`quantity${i}`).value);
  118.                    var min = parseInt(document.getElementById(`min${i}`).value);
  119.                    var max = parseInt(document.getElementById(`max${i}`).value);
  120.  
  121.                    if (quantity > 0 && min >= 0 && max >= 0) {
  122.                        coefficientsGroupedByRanges.push(getBinomialCoefficientsInRange(quantity, min, max));
  123.                     }
  124.                 }
  125.                 return coefficientsGroupedByRanges;
  126.             }
  127.  
  128.             const calculateOdds = () => {
  129.                 const coefficientsGroupedByRanges = getCoefficientsGroupedByRanges();
  130.                 const populationSize = parseInt(document.getElementById(`populationSize`).value);
  131.                 const sampleSize = parseInt(document.getElementById(`sampleSize`).value);
  132.  
  133.                 const result = hypergeo(coefficientsGroupedByRanges, populationSize, sampleSize);
  134.                 document.getElementById(`result`).innerHTML = prettyPrint(result);
  135.             }
  136.         </script>
  137.     </body>
  138. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement