{"record":{"id":"d60f68f7c07b21c4","repo":"stanfordnlp/CoreNLP","slug":"neg-log-lik-smaller-than-0-s","errorCode":null,"errorMessage":"neg log lik smaller than 0: \" + s","messagePattern":"neg log lik smaller than 0: \" \\+ s","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/maxent/iis/LambdaSolve.java","lineNumber":914,"sourceCode":"      } //for\n\n    } //for fNo\n\n    for (int x = 0; x < probConds.length; x++) {\n      //again\n      zlambda[x] = ArrayMath.logSum(probConds[x]); // cpu samples #4,#15: 4.5%\n      //log.info(\"zlambda \"+x+\" \"+zlambda[x]);\n      s += zlambda[x] * p.data.ptildeX(x) * p.data.getNumber();\n\n      for (int y = 0; y < probConds[x].length; y++) {\n        probConds[x][y] = divide(probConds[x][y], zlambda[x]); // cpu samples #13: 1.6%\n        //log.info(\"prob \"+x+\" \"+y+\" \"+probConds[x][y]);\n      } //y\n\n    }//x\n\n    if (s < 0) {\n      throw new IllegalStateException(\"neg log lik smaller than 0: \" + s);\n    }\n\n    return s;\n  }\n\n  // -- stuff for CG version below -------\n\n  /**\n   * calculate the log likelihood from scratch, hashing the conditional\n   * probabilities in pcond which we will use for the derivative later.\n   *\n   * @return The log likelihood of the data\n   */\n  public double logLikelihoodScratch() {\n    // zero all the variables\n    double s = 0;\n    for (int i = 0; i < probConds.length; i++) {\n      for (int j = 0; j < probConds[i].length; j++) {","sourceCodeStart":896,"sourceCodeEnd":932,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/maxent/iis/LambdaSolve.java#L896-L932","documentation":"LambdaSolve's log-likelihood computation (2LogLikelihood / related CG variant) asserts an internal invariant: the negative log likelihood over the model must never be negative. A negative value means the computed conditional probabilities are inconsistent, so it throws IllegalStateException.","triggerScenarios":"Computing log likelihood after IIS/LCG training when lambdas have drifted to values making the sum s negative — e.g. calling p_iCond / 2LogLikelihood with lambdas read from an incompatible or corrupted model file.","commonSituations":"Loading lambdas trained on a different problem (different features/empirical distributions); numerical precision drift with extreme lambda values; manual lambda edits.","solutions":["Verify the lambdas were produced for the SAME problem instance (same features and data) being evaluated","Re-train the model instead of loading questionable lambdas","Sanity-check lambda values (magnitude, NaN) before computing likelihood"],"exampleFix":"// before\ndouble ll = problem.twoLogLikelihood(lambdasFromOtherProblem);\n// after\nif (lambdasFromOtherProblem.length != problem.fValues.length) {\n  throw new IllegalArgumentException(\"Lambda vector does not match this problem\");\n}\ndouble ll = problem.twoLogLikelihood(lambdasFromOtherProblem);","handlingStrategy":"validation","validationCode":"if (lambdas.length != expectedLambdaCount || Arrays.stream(lambdas).anyMatch(v -> !Double.isFinite(v))) {\n  throw new IllegalArgumentException(\"Lambdas invalid or mismatched with problem\");\n}","typeGuard":null,"tryCatchPattern":"try { double s = problem.check(lambdas); } catch (IllegalStateException e) { /* reload matching model or retrain */ }","preventionTips":["Keep lambdas and problem instance paired as one artifact","Validate lambda finiteness and length before evaluation","Retrain rather than mixing model files"],"tags":["java","maxent","numerical","invariant"],"backgroundTag":"internal-invariant-violation","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"}