TheAlgorithms/Java · error · IllegalArgumentException

alpha must be greater than 0

Error message

alpha must be greater than 0

What it means

MultinomialNaiveBayesClassifier requires the Laplace smoothing constant alpha > 0 because alpha appears in denominators during log-likelihood computation (alpha prevents zero probabilities). alpha <= 0 would cause division by zero or log(0), producing NaN/Infinity results. Standard Laplace smoothing uses alpha=1.0.

Source

Thrown at src/main/java/com/thealgorithms/machinelearning/MultinomialNaiveBayesClassifier.java:37

 *
 * @author Vraj Prajapati(Rosander0)
 */
public final class MultinomialNaiveBayesClassifier {

    private final double alpha;
    private final Map<Integer, Double> logPriors;
    private final Map<Integer, double[]> logLikelihoods;
    private int numFeatures;

    /**
     * Constructs a classifier with the given Laplace smoothing parameter.
     *
     * @param alpha smoothing constant; must be greater than 0. A value of 1.0
     *              corresponds to standard Laplace smoothing.
     */
    public MultinomialNaiveBayesClassifier(double alpha) {
        if (alpha <= 0) {
            throw new IllegalArgumentException("alpha must be greater than 0");
        }
        this.alpha = alpha;
        this.logPriors = new HashMap<>();
        this.logLikelihoods = new HashMap<>();
    }

    /** Constructs a classifier using the standard Laplace smoothing constant of 1.0. */
    public MultinomialNaiveBayesClassifier() {
        this(1.0);
    }

    /**
     * Fits the classifier on the given feature matrix and labels.
     *
     * @param features training samples, each row a vector of non-negative
     *                 feature counts
     * @param labels   class label for each row of {@code features}
     */

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Use the no-arg constructor for standard Laplace smoothing (alpha=1.0).
  2. Validate alpha > 0 before constructing; if 'no smoothing' is desired, this classifier is not suitable.
  3. Clamp small alpha to a tiny positive value (e.g. 1e-9) if near-zero smoothing is intended.

Example fix

// before
new MultinomialNaiveBayesClassifier(0); // alpha must be > 0

// after
new MultinomialNaiveBayesClassifier(1.0); // standard Laplace smoothing
Defensive patterns

Strategy: validation

Validate before calling

if (alpha <= 0) {
    throw new IllegalArgumentException("alpha must be > 0; got " + alpha);
}
new MultinomialNaiveBayesClassifier(alpha);

Prevention

When it happens

Trigger: Constructing new MultinomialNaiveBayesClassifier(0), new MultinomialNaiveBayesClassifier(-1.0), or passing a computed alpha that evaluates to <= 0.

Common situations: User-configurable smoothing parameter left at default 0, a config file with alpha=0 meant to 'disable smoothing' (which this classifier doesn't support), or alpha derived from a computation that can go non-positive.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/d119f1ab4fb05f39. Report an issue: GitHub.