{"record":{"id":"df55b04d3efb63e8","repo":"stanfordnlp/CoreNLP","slug":"bad-arguments-x-and-lambda","errorCode":null,"errorMessage":"Bad arguments: \" + x + \" and \" + lambda","messagePattern":"Bad arguments: \" \\+ x \\+ \" and \" \\+ lambda","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/math/SloppyMath.java","lineNumber":661,"sourceCode":"    if (cosValue < -1.0 || cosValue > 1.0) {\n      throw new IllegalArgumentException(\"Cosine is not between -1 and 1: \" + cosValue);\n    }\n    int numSamples = 10000;\n    if (acosCache == null) {\n      acosCache = new float[numSamples + 1];\n      for (int i = 0; i <= numSamples; ++i) {\n        double x = 2.0 / ((double) numSamples) * ((double) i) - 1.0;\n        acosCache[i] = (float) Math.acos(x);\n      }\n    }\n\n    int i = ((int) (((cosValue + 1.0) / 2.0) * ((double) numSamples)));\n    return acosCache[i];\n  }\n\n\n  public static double poisson(int x, double lambda) {\n    if (x<0 || lambda<=0.0) throw new RuntimeException(\"Bad arguments: \" + x + \" and \" + lambda);\n    double p = (Math.exp(-lambda) * Math.pow(lambda, x)) / factorial(x);\n    if (Double.isInfinite(p) || p<=0.0) throw new RuntimeException(Math.exp(-lambda) +\" \"+ Math.pow(lambda, x) + ' ' + factorial(x));\n    return p;\n  }\n\n  /**\n   * Uses floating point so that it can represent the really big numbers that come up.\n   * @param x Argument to take factorial of\n   * @return Factorial of argument\n   */\n  public static double factorial(int x) {\n    double result = 1.0;\n    for (int i=x; i>1; i--) {\n      result *= i;\n    }\n    return result;\n  }\n","sourceCodeStart":643,"sourceCodeEnd":679,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/math/SloppyMath.java#L643-L679","documentation":"SloppyMath.poisson(int x, double lambda) evaluates the Poisson probability mass function; it requires x >= 0 and lambda > 0. Violations make exp(-lambda)*lambda^x/factorial(x) meaningless, so a RuntimeException(\"Bad arguments: ...\") is thrown with both values.","triggerScenarios":"Calling poisson with a negative observed count x, or lambda <= 0 — e.g. an estimated mean rate of 0 from an empty sample, or x computed as -1 by a sentinel/missing-value convention.","commonSituations":"Fitting a rate from no data, using -1 as a 'missing' marker for counts, or parameter search over an unbounded range touching lambda = 0 or negative values.","solutions":["Validate inputs before the call: if (x < 0 || lambda <= 0) skip or handle","Fix the upstream estimate so lambda > 0 (e.g. add a small epsilon floor)","Replace -1 missing-value sentinels with proper filtering before the call"],"exampleFix":"// before\ndouble p = SloppyMath.poisson(x, lambda); // lambda = 0.0\n// after\nif (x >= 0 && lambda > 0.0) {\n  double p = SloppyMath.poisson(x, lambda);\n} else {\n  // handle degenerate case\n}","handlingStrategy":"validation","validationCode":"if (x < 0 || lambda <= 0.0) { /* skip or floor lambda */ lambda = Math.max(lambda, Double.MIN_VALUE); }","typeGuard":"static boolean validPoissonArgs(int x, double lambda) { return x >= 0 && lambda > 0.0; }","tryCatchPattern":"try {\n  double p = SloppyMath.poisson(x, lambda);\n} catch (RuntimeException e) {\n  log.warn(\"poisson args invalid: \" + e.getMessage());\n  // handle degenerate case (p undefined)\n}","preventionTips":["Filter missing-value sentinels (like -1) out of count data before modeling","Floor estimated rates with a small epsilon so lambda can never be 0","Bound parameter-search ranges to lambda > 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"}