stanfordnlp/CoreNLP · error · RuntimeException

Parameters must be non-negative!

Error message

Parameters must be non-negative!

What it means

A Dirichlet distribution's parameters are concentration values and must be non-negative; negative counts are mathematically invalid. The constructor copies the given Counter and validates it via checkParameters, throwing RuntimeException if any parameter count is negative.

Solutions

  1. Validate/clip counts before constructing: Math.max(0, count) for each parameter.
  2. Fix the upstream computation producing negative counts (e.g. subtracting more than present).
  3. If negative values are legitimate pseudo-data offsets, shift all counts by a constant to make them non-negative.

Example fix

// before
Counter<String> params = diffOfCounters(prior, posterior); // may go negative
Dirichlet<String> d = new Dirichlet<>(params); // throws
// after
for (String k : params.keySet()) params.setCount(k, Math.max(0.0, params.getCount(k)));
Dirichlet<String> d = new Dirichlet<>(params);
Defensive patterns

Strategy: validation

Validate before calling

boolean valid = parameters.keySet().stream().allMatch(k -> parameters.getCount(k) >= 0.0);
if (!valid) throw new IllegalArgumentException("Dirichlet parameters must be non-negative");

Try / catch

try {
  Dirichlet<E> d = new Dirichlet<>(parameters);
} catch (RuntimeException e) {
  if (e.getMessage().contains("non-negative")) {
    parameters.keySet().forEach(k -> parameters.setCount(k, Math.max(0.0, parameters.getCount(k))));
    // retry once with clipped parameters
  } else throw e;
}

Prevention

When it happens

Trigger: Calling a Dirichlet constructor with a Counter containing at least one negative count, e.g. built from differences or decremented counts that went below zero.

Common situations: Parameter counters derived from arithmetic (posterior updates, subtraction of expected counts) that produced negative values due to numerical issues or logic bugs.

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/82069c38c8ca3947. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/stats/Dirichlet.java:25

 *
 * @author Jenny Finkel
 */

public class Dirichlet<E> implements ConjugatePrior<Multinomial<E>, E> {

  private static final long serialVersionUID = 1L;

  private Counter<E> parameters;

  public Dirichlet(Counter<E> parameters) {
    checkParameters(parameters);
    this.parameters = new ClassicCounter<>(parameters);
  }

  private void checkParameters(Counter<E> parameters) {
    for (E o : parameters.keySet()) {
      if (parameters.getCount(o) < 0.0) {
        throw new RuntimeException("Parameters must be non-negative!");
      }
    }
    if (parameters.totalCount() <= 0.0) {
      throw new RuntimeException("Parameters must have positive mass!");
    }
  }

  public Multinomial<E> drawSample(Random random) {
    return drawSample(random, parameters);
  }
  
  public static <F> Multinomial<F> drawSample(Random random, Counter<F> parameters) {
    Counter<F> multParameters = new ClassicCounter<>();
    double sum = 0.0;
    for (F o : parameters.keySet()) {
      double parameter = Gamma.drawSample(random, parameters.getCount(o));
      sum += parameter;
      multParameters.setCount(o, parameter);

View on GitHub (pinned to 1b7edd19c4)