{"record":{"id":"17f84e19c5ed3868","repo":"stanfordnlp/CoreNLP","slug":"invalid-hypergeometric","errorCode":null,"errorMessage":"Invalid hypergeometric","messagePattern":"Invalid hypergeometric","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/SloppyMath.java","lineNumber":421,"sourceCode":"  }\n\n  /**\n   * Find a hypergeometric distribution.  This uses exact math, trying\n   * fairly hard to avoid numeric overflow by interleaving\n   * multiplications and divisions.\n   * (To do: make it even better at avoiding overflow, by using loops\n   * that will do either a multiple or divide based on the size of the\n   * intermediate result.)\n   *\n   * @param k The number of black balls drawn\n   * @param n The total number of balls\n   * @param r The number of black balls\n   * @param m The number of balls drawn\n   * @return The hypergeometric value\n   */\n  public static double hypergeometric(int k, int n, int r, int m) {\n    if (k < 0 || r > n || m > n || n <= 0 || m < 0 || r < 0) {\n      throw new IllegalArgumentException(\"Invalid hypergeometric\");\n    }\n\n    // exploit symmetry of problem\n    if (m > n / 2) {\n      m = n - m;\n      k = r - k;\n    }\n    if (r > n / 2) {\n      r = n - r;\n      k = m - k;\n    }\n    if (m > r) {\n      int temp = m;\n      m = r;\n      r = temp;\n    }\n    // now we have that k <= m <= r <= n/2\n","sourceCodeStart":403,"sourceCodeEnd":439,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/SloppyMath.java#L403-L439","documentation":"SloppyMath.hypergeometric(k, n, r, m) computes a hypergeometric probability; the parameters only form a valid problem when 0 <= k, 0 <= r <= n, 0 <= m <= n, and n > 0. Any violation makes the combinatorial terms undefined, so an IllegalArgumentException(\"Invalid hypergeometric\") is thrown.","triggerScenarios":"Calling hypergeometric with n <= 0, negative k/r/m, or r > n / m > n — e.g. counts drawn from mis-parsed contingency tables or off-by-one index math.","commonSituations":"Population size computed as 0 from an empty dataset, swapped argument order (r and m transposed), or negative counts from a diff of counters.","solutions":["Validate arguments before the call: n > 0, 0 <= k, 0 <= r <= n, 0 <= m <= n","Check argument ordering (k, n, r, m) — swapped r/m is a common cause","Fix upstream count extraction so counts are non-negative and bounded by n"],"exampleFix":"// before\ndouble p = SloppyMath.hypergeometric(k, n, r, m); // r > n\n// after\nif (n <= 0 || k < 0 || r < 0 || m < 0 || r > n || m > n) {\n  throw new IllegalArgumentException(\"bad hypergeometric args\");\n}\ndouble p = SloppyMath.hypergeometric(k, n, r, m);","handlingStrategy":"validation","validationCode":"if (n <= 0 || k < 0 || r < 0 || m < 0 || r > n || m > n) throw new IllegalArgumentException(\"invalid hypergeometric args: k=\" + k + \" n=\" + n + \" r=\" + r + \" m=\" + m);","typeGuard":"static boolean validHypergeometric(int k, int n, int r, int m) {\n  return n > 0 && k >= 0 && r >= 0 && m >= 0 && r <= n && m <= n;\n}","tryCatchPattern":"try {\n  double p = SloppyMath.hypergeometric(k, n, r, m);\n} catch (IllegalArgumentException e) {\n  log.warn(\"invalid table, returning NaN\");\n  double p = Double.NaN;\n}","preventionTips":["Verify the (k, n, r, m) argument order from the Javadoc before each use","Validate counts are non-negative and bounded by n after extraction from tables","Guard against empty datasets producing n = 0"],"tags":["java","statistics","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}