stanfordnlp/CoreNLP · error · RuntimeException

no negative parameters allowed!

Error message

no negative parameters allowed!

What it means

After checking total mass is positive, the Multinomial constructor normalizes each count by the total; a negative individual count would yield a negative probability, so it throws. Probabilities in a multinomial must all be non-negative.

Solutions

  1. Ensure all counts are non-negative before constructing (clamp negatives to 0 or reject the input)
  2. Use the correct data structure — Multinomial expects counts, not signed scores
  3. Inspect the counter contents to find which key has the negative value and fix the producer

Example fix

// before
Multinomial<String> m = new Multinomial<>(deltaCounts);
// after
deltaCounts.keySet().removeIf(k -> deltaCounts.getCount(k) < 0.0);
Multinomial<String> m = new Multinomial<>(deltaCounts);
Defensive patterns

Strategy: validation

Validate before calling

for (Object k : counter.keySet()) {
  if (counter.getCount(k) < 0.0) throw new IllegalArgumentException("negative count for " + k);
}

Type guard

boolean hasNonNegativeCounts(Counter<?> c) {
  return c.keySet().stream().allMatch(k -> c.getCount(k) >= 0.0);
}

Try / catch

try {
  return new Multinomial<>(counter);
} catch (RuntimeException e) {
  if ("no negative parameters allowed!".equals(e.getMessage())) {
    counter.keySet().removeIf(k -> counter.getCount(k) < 0.0);
    return new Multinomial<>(counter);
  }
  throw e;
}

Prevention

When it happens

Trigger: new Multinomial(counter) where any key's getCount(object) < 0.0 — e.g. a counter used for gradient/weight deltas that accumulated negative values.

Common situations: Passing a counter that stores differences or log-ratios rather than raw counts; subtracting counts during online updates; float underflow bugs producing small negatives.

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/6ceaaa83f95c163e. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/stats/Multinomial.java:32

public class Multinomial<E> implements ProbabilityDistribution<E> {

  /**
   * 
   */
  private static final long serialVersionUID = -697457414113362926L;
  private Counter<E> parameters;

  public Multinomial(Counter<E> parameters) {
    double totalMass = parameters.totalCount();
    if (totalMass <= 0.0) {
      throw new RuntimeException("total mass must be positive!");
    }

    this.parameters = new ClassicCounter<>();
    for (E object : parameters.keySet()) {
      double oldCount = parameters.getCount(object);
      if (oldCount < 0.0) {
        throw new RuntimeException("no negative parameters allowed!");
      }
      this.parameters.setCount(object, oldCount/totalMass);
    }
  }

  public Counter<E> getParameters() {
    return new ClassicCounter<>(parameters);
  }
  
  public double probabilityOf(E object) {
    if (!parameters.keySet().contains(object)) {
      throw new RuntimeException("Not a valid object for this multinomial!");
    }
    return parameters.getCount(object);
  }

  public double logProbabilityOf(E object) {
    if (!parameters.keySet().contains(object)) {

View on GitHub (pinned to 1b7edd19c4)