{"record":{"id":"46a086eae26d5e45","repo":"stanfordnlp/CoreNLP","slug":"parameters-must-have-positive-mass","errorCode":null,"errorMessage":"Parameters must have positive mass!","messagePattern":"Parameters must have positive mass!","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/stats/Dirichlet.java","lineNumber":29,"sourceCode":"public class Dirichlet<E> implements ConjugatePrior<Multinomial<E>, E> {\n\n  private static final long serialVersionUID = 1L;\n\n  private Counter<E> parameters;\n\n  public Dirichlet(Counter<E> parameters) {\n    checkParameters(parameters);\n    this.parameters = new ClassicCounter<>(parameters);\n  }\n\n  private void checkParameters(Counter<E> parameters) {\n    for (E o : parameters.keySet()) {\n      if (parameters.getCount(o) < 0.0) {\n        throw new RuntimeException(\"Parameters must be non-negative!\");\n      }\n    }\n    if (parameters.totalCount() <= 0.0) {\n      throw new RuntimeException(\"Parameters must have positive mass!\");\n    }\n  }\n\n  public Multinomial<E> drawSample(Random random) {\n    return drawSample(random, parameters);\n  }\n  \n  public static <F> Multinomial<F> drawSample(Random random, Counter<F> parameters) {\n    Counter<F> multParameters = new ClassicCounter<>();\n    double sum = 0.0;\n    for (F o : parameters.keySet()) {\n      double parameter = Gamma.drawSample(random, parameters.getCount(o));\n      sum += parameter;\n      multParameters.setCount(o, parameter);\n    }\n    for (F o : multParameters.keySet()) {\n      multParameters.setCount(o, multParameters.getCount(o)/sum);\n    }","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/stats/Dirichlet.java#L11-L47","documentation":"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.","triggerScenarios":"Constructing a Dirichlet with an empty Counter, or a Counter whose every count is 0 (e.g. no observations accumulated).","commonSituations":"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.","solutions":["Add a positive pseudo-count/smoothing term to every parameter before construction (e.g. +1 or a small alpha).","Guard against empty/all-zero counters and skip or use a default prior when totalCount() <= 0.","Fix the accumulation pipeline so observations are actually added to the parameter counter."],"exampleFix":"// before\nDirichlet<String> d = new Dirichlet<>(observedCounts); // may be all-zero\n// after\nfor (String k : vocab) observedCounts.incrementCount(k, 1.0); // smoothing\nDirichlet<String> d = new Dirichlet<>(observedCounts);","handlingStrategy":"validation","validationCode":"if (parameters == null || parameters.totalCount() <= 0.0) {\n  throw new IllegalArgumentException(\"Dirichlet parameters must have positive total mass\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  Dirichlet<E> d = new Dirichlet<>(parameters);\n} catch (RuntimeException e) {\n  if (e.getMessage().contains(\"positive mass\")) {\n    parameters = addSmoothing(parameters, 1.0); // fall back to smoothed prior\n    Dirichlet<E> d = new Dirichlet<>(parameters);\n  } else throw e;\n}","preventionTips":["Always add a smoothing/pseudo-count (>0) before constructing a Dirichlet from observed counts.","Check totalCount() > 0 before sampling from or constructing a Dirichlet.","Verify the data pipeline actually populates the parameter counter."],"tags":["validation","empty-counter","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-17T15:17:12.973Z"}