stanfordnlp/CoreNLP · error · RuntimeException

Parameters must have positive mass!

Error message

Parameters must have positive mass!

What it means

Besides being non-negative, a Dirichlet parameter vector must contain positive total mass; a distribution with total concentration 0 (empty or all-zero counter) is degenerate and cannot be sampled. checkParameters throws RuntimeException when parameters.totalCount() <= 0.

Solutions

  1. Add a positive pseudo-count/smoothing term to every parameter before construction (e.g. +1 or a small alpha).
  2. Guard against empty/all-zero counters and skip or use a default prior when totalCount() <= 0.
  3. Fix the accumulation pipeline so observations are actually added to the parameter counter.

Example fix

// before
Dirichlet<String> d = new Dirichlet<>(observedCounts); // may be all-zero
// after
for (String k : vocab) observedCounts.incrementCount(k, 1.0); // smoothing
Dirichlet<String> d = new Dirichlet<>(observedCounts);
Defensive patterns

Strategy: validation

Validate before calling

if (parameters == null || parameters.totalCount() <= 0.0) {
  throw new IllegalArgumentException("Dirichlet parameters must have positive total mass");
}

Try / catch

try {
  Dirichlet<E> d = new Dirichlet<>(parameters);
} catch (RuntimeException e) {
  if (e.getMessage().contains("positive mass")) {
    parameters = addSmoothing(parameters, 1.0); // fall back to smoothed prior
    Dirichlet<E> d = new Dirichlet<>(parameters);
  } else throw e;
}

Prevention

When it happens

Trigger: Constructing a Dirichlet with an empty Counter, or a Counter whose every count is 0 (e.g. no observations accumulated).

Common situations: Estimating Dirichlet parameters from data that produced an empty counter (empty training set, filtered-out vocabulary), or a smoothing constant of 0 combined with no counts.

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/46a086eae26d5e45. Report an issue: GitHub.

Appendix: source

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

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);
    }
    for (F o : multParameters.keySet()) {
      multParameters.setCount(o, multParameters.getCount(o)/sum);
    }

View on GitHub (pinned to 1b7edd19c4)