stanfordnlp/CoreNLP · error · RuntimeException
total mass must be positive!
Error message
total mass must be positive!
What it means
The Multinomial constructor normalizes the supplied Counter by its total mass; a total mass of zero or negative makes normalization meaningless, so the constructor throws immediately. This guards against building a distribution from an empty or all-zero counter.
Solutions
- Verify totalCount() > 0 before constructing; if not, fall back to a uniform distribution
- Add smoothing (e.g. add-k smoothing) so total mass is positive even for unseen events
- Fix upstream counting code that produced an empty/zero counter
Example fix
// before Multinomial<String> m = new Multinomial<>(counts); // throws if total is 0 // after counts.incrementCount(UNKNOWN, 1.0); // smooth Multinomial<String> m = new Multinomial<>(counts);
Defensive patterns
Strategy: validation
Validate before calling
if (counter == null || counter.totalCount() <= 0.0) {
counter = new ClassicCounter<>(); counter.incrementCount(DEFAULT_KEY, 1.0); // uniform fallback
} Type guard
boolean isPositiveMass(Counter<?> c) { return c != null && c.totalCount() > 0.0; } Try / catch
try {
return new Multinomial<>(counter);
} catch (RuntimeException e) {
if ("total mass must be positive!".equals(e.getMessage())) {
return uniformMultinomial(counter == null ? Collections.emptySet() : counter.keySet());
}
throw e;
} Prevention
- Add-k smoothing before building multinomials from sparse data
- Check totalCount() after any counting pipeline stage
- Treat empty contexts explicitly rather than passing empty counters
When it happens
Trigger: new Multinomial(counter) where counter.totalCount() <= 0.0 — an empty counter, a counter whose entries are all 0, or one with only negative counts.
Common situations: Estimating a multinomial from training data that produced no counts for a context (e.g. unseen n-gram context); forgetting to set counts before constructing; smearing/counting bugs that zero out all entries.
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
- no negative parameters allowed!
- Bad arguments: " + x + " and " + lambda
- Can't parse a zero-length sentence!
- Cannot combine MWT out of only
- Cannot make a Lemmatize with no nodeName
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/d9b1feaeee854531.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/stats/Multinomial.java:25
* a counter. It is assumed that the Counter's keySet() contains all of the parameters (i.e., there are not other
* possible values which are set to 0). It makes a copy of the Counter, so tha parameters cannot be changes,
* and it normalizes the values if they are not already normalized.
*
* @author Jenny Finkel
*/
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)) {View on GitHub (pinned to 1b7edd19c4)