stanfordnlp/CoreNLP · error · IllegalArgumentException

Invalid Fisher's exact: " + "k=" + k + " n=" + n + " r=" +…

Error message

Invalid Fisher's exact: " + "k=" + k + " n=" + n + " r=" + r + " m=" + m + " k<0=" + (k < 0) + " k<(m+r)-n=" + (k < (m + r) - n) + " k>r=" + (k > r) + " k>m=" + (k > m) + " r>n=" + (r > n) + "m>n=" + (m > n)

What it means

SloppyMath.oneTailedFisher'sExact(k, n, r, m) validates the 2x2 table parameters against the hypergeometric support: k must satisfy 0 <= k, (m+r)-n <= k <= min(r, m), with 0 <= r <= n and 0 <= m <= n. The thrown message includes each individual condition's boolean so you can see exactly which constraint failed.

Solutions

  1. Read the per-condition booleans in the message (e.g. k>r=true) to identify the failed constraint
  2. Verify the argument encoding: k = successes drawn, n = population, r = total successes, m = draws, with r <= n and m <= n
  3. Clamp/validate k into [max(0,(m+r)-n), min(r,m)] before calling

Example fix

// before
double p = SloppyMath.oneTailedFishersExact(k, n, r, m); // k > m
// after
int lo = Math.max(0, (m + r) - n), hi = Math.min(r, m);
if (k < lo || k > hi || r > n || m > n) {
  throw new IllegalArgumentException("table invalid: k=" + k + " range=[" + lo + "," + hi + "]");
}
double p = SloppyMath.oneTailedFishersExact(k, n, r, m);
Defensive patterns

Strategy: validation

Validate before calling

int lo = Math.max(0, (m + r) - n), hi = Math.min(r, m);
if (r > n || m > n || k < lo || k > hi) throw new IllegalArgumentException("invalid 2x2 table encoding");

Type guard

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

Try / catch

try {
  double p = SloppyMath.oneTailedFishersExact(k, n, r, m);
} catch (IllegalArgumentException e) {
  // message lists each failed condition; log and fall back
  log.error("fisher exact input invalid: " + e.getMessage());
}

Prevention

When it happens

Trigger: Calling with a k outside [max(0,(m+r)-n), min(r,m)], or r/m exceeding n — typically from a malformed contingency table or swapped arguments.

Common situations: Building the table with row/column totals transposed, counts from an empty or filtered dataset (making r or m exceed n), off-by-one when converting cell counts to the (k,n,r,m) encoding.

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/40d57e67a2a8a658. Report an issue: GitHub.

Appendix: source

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


  /**
   * Find a one-tailed Fisher's exact probability.  Chance of having seen
   * this or a more extreme departure from what you would have expected
   * given independence.  I.e., k &ge; the value passed in.
   * Warning: this was done just for collocations, where you are
   * concerned with the case of k being larger than predicted.  It doesn't
   * correctly handle other cases, such as k being smaller than expected.
   *
   * @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 Fisher's exact p-value
   */
  public static double oneTailedFishersExact(int k, int n, int r, int m) {
    if (k < 0 || k < (m + r) - n || k > r || k > m || r > n || m > n) {
      throw new IllegalArgumentException("Invalid Fisher's exact: " + "k=" + k + " n=" + n + " r=" + r + " m=" + m + " k<0=" + (k < 0) + " k<(m+r)-n=" + (k < (m + r) - n) + " k>r=" + (k > r) + " k>m=" + (k > m) + " r>n=" + (r > n) + "m>n=" + (m > n));
    }
    // 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

    double total = 0.0;

View on GitHub (pinned to 1b7edd19c4)