stanfordnlp/CoreNLP · error · IllegalArgumentException

Invalid hypergeometric

Error message

Invalid hypergeometric

What it means

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.

Solutions

  1. Validate arguments before the call: n > 0, 0 <= k, 0 <= r <= n, 0 <= m <= n
  2. Check argument ordering (k, n, r, m) — swapped r/m is a common cause
  3. Fix upstream count extraction so counts are non-negative and bounded by n

Example fix

// before
double p = SloppyMath.hypergeometric(k, n, r, m); // r > n
// after
if (n <= 0 || k < 0 || r < 0 || m < 0 || r > n || m > n) {
  throw new IllegalArgumentException("bad hypergeometric args");
}
double p = SloppyMath.hypergeometric(k, n, r, m);
Defensive patterns

Strategy: validation

Validate before calling

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);

Type guard

static boolean validHypergeometric(int k, int n, int r, int m) {
  return n > 0 && k >= 0 && r >= 0 && m >= 0 && r <= n && m <= n;
}

Try / catch

try {
  double p = SloppyMath.hypergeometric(k, n, r, m);
} catch (IllegalArgumentException e) {
  log.warn("invalid table, returning NaN");
  double p = Double.NaN;
}

Prevention

When it happens

Trigger: 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.

Common situations: Population size computed as 0 from an empty dataset, swapped argument order (r and m transposed), or negative counts from a diff of counters.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/17f84e19c5ed3868. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/math/SloppyMath.java:421

  }

  /**
   * Find a hypergeometric distribution.  This uses exact math, trying
   * fairly hard to avoid numeric overflow by interleaving
   * multiplications and divisions.
   * (To do: make it even better at avoiding overflow, by using loops
   * that will do either a multiple or divide based on the size of the
   * intermediate result.)
   *
   * @param k The number of black balls drawn
   * @param n The total number of balls
   * @param r The number of black balls
   * @param m The number of balls drawn
   * @return The hypergeometric value
   */
  public static double hypergeometric(int k, int n, int r, int m) {
    if (k < 0 || r > n || m > n || n <= 0 || m < 0 || r < 0) {
      throw new IllegalArgumentException("Invalid hypergeometric");
    }

    // exploit symmetry of problem
    if (m > n / 2) {
      m = n - m;
      k = r - k;
    }
    if (r > n / 2) {
      r = n - r;
      k = m - k;
    }
    if (m > r) {
      int temp = m;
      m = r;
      r = temp;
    }
    // now we have that k <= m <= r <= n/2

View on GitHub (pinned to 1b7edd19c4)