{"record":{"id":"80e36f63df288029","repo":"stanfordnlp/CoreNLP","slug":"error-negative-count-dblcount-for-item-item","errorCode":null,"errorMessage":"ERROR: negative count dblCount for item item!","messagePattern":"ERROR: negative count dblCount for item item!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/Distribution.java","lineNumber":493,"sourceCode":"      Integer count = (int) Math.round(entry.getValue());\n      dist.counter.setCount(item, probsByCount.getCount(count));\n    }\n    dist.numberOfKeys = numberOfKeys;\n    dist.reservedMass = sgt.getProbabilityForUnseen();\n    return dist;\n\n  }\n\n  /* Helper to simpleGoodTuringSmoothedCounter() */\n  private static <E> void validateCounter(Counter<E> counts) {\n    for (Map.Entry<E, Double> entry : counts.entrySet()) {\n      E item = entry.getKey();\n      Double dblCount = entry.getValue();\n      if (dblCount == null) {\n        throw new IllegalArgumentException(\"ERROR: null count for item \" + item + \"!\");\n      }\n      if (dblCount < 0) {\n        throw new IllegalArgumentException(\"ERROR: negative count \" + dblCount + \" for item \" + item + \"!\");\n      }\n    }\n  }\n\n  /* Helper to simpleGoodTuringSmoothedCounter() */\n  private static <E> Counter<Integer> collectCountCounts(Counter<E> counts) {\n    Counter<Integer> cc = new ClassicCounter<>(); // counts of counts\n    for (Map.Entry<E, Double> entry : counts.entrySet()) {\n      //E item = entry.getKey();\n      Integer count = (int) Math.round(entry.getValue());\n      cc.incrementCount(count);\n    }\n    return cc;\n  }\n\n  /* Helper to simpleGoodTuringSmoothedCounter() */\n  private static int[][] countCounts2IntArrays(Counter<Integer> countCounts) {\n    int size = countCounts.size();","sourceCodeStart":475,"sourceCodeEnd":511,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/Distribution.java#L475-L511","documentation":"The same validateCounter helper rejects negative counts, since a probability distribution cannot be built from negative frequencies. If any item's count is < 0, an IllegalArgumentException reports the negative value and the item. Good-Turing smoothing assumes non-negative count-of-count statistics, so negatives break the math downstream.","triggerScenarios":"Calling simpleGoodTuring() on a counter whose entries include negative values, typically produced by subtracting counts, applying weights incorrectly, or ingesting noisy external data.","commonSituations":"Differencing two counters (e.g. computing count deltas between corpora) yielding negative entries; faulty preprocessing pipelines that scale counts with negative factors.","solutions":["Inspect and fix the counter construction so counts are never negative (clip at 0 or fix the subtraction)","Validate with Counters or a loop before calling simpleGoodTuring","If deltas are intended, take absolute values or rebuild the statistic so smoothing operates on valid frequencies"],"exampleFix":"// before\nCounter<String> c = deltaCounters(c1, c2); // may have negatives\nDistribution<String> d = Distribution.simpleGoodTuring(c, totalKeys);\n// after\nfor (String k : new ArrayList<>(c.keySet())) {\n  if (c.getCount(k) < 0) c.setCount(k, 0.0);\n}\nDistribution<String> d = Distribution.simpleGoodTuring(c, totalKeys);","handlingStrategy":"validation","validationCode":"for (Map.Entry<E, Double> e : counter.entrySet()) {\n  if (e.getValue() != null && e.getValue() < 0) throw new IllegalStateException(\"negative count for \" + e.getKey());\n}","typeGuard":null,"tryCatchPattern":"try {\n  Distribution<E> d = Distribution.simpleGoodTuring(counter, n);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"ERROR: negative count\")) { /* clean counter and retry */ }\n}","preventionTips":["Clip counts at 0 after any counter subtraction","Audit scaling/weighting code for negative multipliers","Validate counters from external data sources before smoothing"],"tags":["java","counter-validation","negative-value","statistics"],"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"}