{"record":{"id":"40d57e67a2a8a658","repo":"stanfordnlp/CoreNLP","slug":"invalid-fisher-s-exact-k-k-n-n","errorCode":null,"errorMessage":"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)","messagePattern":"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\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/SloppyMath.java","lineNumber":551,"sourceCode":"\n\n  /**\n   * Find a one-tailed Fisher's exact probability.  Chance of having seen\n   * this or a more extreme departure from what you would have expected\n   * given independence.  I.e., k &ge; the value passed in.\n   * Warning: this was done just for collocations, where you are\n   * concerned with the case of k being larger than predicted.  It doesn't\n   * correctly handle other cases, such as k being smaller than expected.\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 Fisher's exact p-value\n   */\n  public static double oneTailedFishersExact(int k, int n, int r, int m) {\n    if (k < 0 || k < (m + r) - n || k > r || k > m || r > n || m > n) {\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));\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\n    double total = 0.0;","sourceCodeStart":533,"sourceCodeEnd":569,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/SloppyMath.java#L533-L569","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the per-condition booleans in the message (e.g. k>r=true) to identify the failed constraint","Verify the argument encoding: k = successes drawn, n = population, r = total successes, m = draws, with r <= n and m <= n","Clamp/validate k into [max(0,(m+r)-n), min(r,m)] before calling"],"exampleFix":"// before\ndouble p = SloppyMath.oneTailedFishersExact(k, n, r, m); // k > m\n// after\nint lo = Math.max(0, (m + r) - n), hi = Math.min(r, m);\nif (k < lo || k > hi || r > n || m > n) {\n  throw new IllegalArgumentException(\"table invalid: k=\" + k + \" range=[\" + lo + \",\" + hi + \"]\");\n}\ndouble p = SloppyMath.oneTailedFishersExact(k, n, r, m);","handlingStrategy":"validation","validationCode":"int lo = Math.max(0, (m + r) - n), hi = Math.min(r, m);\nif (r > n || m > n || k < lo || k > hi) throw new IllegalArgumentException(\"invalid 2x2 table encoding\");","typeGuard":"static boolean validFishersTable(int k, int n, int r, int m) {\n  return r <= n && m <= n && k >= 0 && k >= (m + r) - n && k <= r && k <= m;\n}","tryCatchPattern":"try {\n  double p = SloppyMath.oneTailedFishersExact(k, n, r, m);\n} catch (IllegalArgumentException e) {\n  // message lists each failed condition; log and fall back\n  log.error(\"fisher exact input invalid: \" + e.getMessage());\n}","preventionTips":["On failure, read the per-condition booleans in the message to pinpoint the bad constraint","Double-check row/column total vs cell-count encoding when building the table","Validate r <= n and m <= n right after totals are computed"],"tags":["java","statistics","fishers-exact","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"}