{"record":{"id":"d119f1ab4fb05f39","repo":"TheAlgorithms/Java","slug":"alpha-must-be-greater-than-0","errorCode":null,"errorMessage":"alpha must be greater than 0","messagePattern":"alpha must be greater than 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/machinelearning/MultinomialNaiveBayesClassifier.java","lineNumber":37,"sourceCode":" *\n * @author Vraj Prajapati(Rosander0)\n */\npublic final class MultinomialNaiveBayesClassifier {\n\n    private final double alpha;\n    private final Map<Integer, Double> logPriors;\n    private final Map<Integer, double[]> logLikelihoods;\n    private int numFeatures;\n\n    /**\n     * Constructs a classifier with the given Laplace smoothing parameter.\n     *\n     * @param alpha smoothing constant; must be greater than 0. A value of 1.0\n     *              corresponds to standard Laplace smoothing.\n     */\n    public MultinomialNaiveBayesClassifier(double alpha) {\n        if (alpha <= 0) {\n            throw new IllegalArgumentException(\"alpha must be greater than 0\");\n        }\n        this.alpha = alpha;\n        this.logPriors = new HashMap<>();\n        this.logLikelihoods = new HashMap<>();\n    }\n\n    /** Constructs a classifier using the standard Laplace smoothing constant of 1.0. */\n    public MultinomialNaiveBayesClassifier() {\n        this(1.0);\n    }\n\n    /**\n     * Fits the classifier on the given feature matrix and labels.\n     *\n     * @param features training samples, each row a vector of non-negative\n     *                 feature counts\n     * @param labels   class label for each row of {@code features}\n     */","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/machinelearning/MultinomialNaiveBayesClassifier.java#L19-L55","documentation":"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.","triggerScenarios":"Constructing new MultinomialNaiveBayesClassifier(0), new MultinomialNaiveBayesClassifier(-1.0), or passing a computed alpha that evaluates to <= 0.","commonSituations":"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.","solutions":["Use the no-arg constructor for standard Laplace smoothing (alpha=1.0).","Validate alpha > 0 before constructing; if 'no smoothing' is desired, this classifier is not suitable.","Clamp small alpha to a tiny positive value (e.g. 1e-9) if near-zero smoothing is intended."],"exampleFix":"// before\nnew MultinomialNaiveBayesClassifier(0); // alpha must be > 0\n\n// after\nnew MultinomialNaiveBayesClassifier(1.0); // standard Laplace smoothing","handlingStrategy":"validation","validationCode":"if (alpha <= 0) {\n    throw new IllegalArgumentException(\"alpha must be > 0; got \" + alpha);\n}\nnew MultinomialNaiveBayesClassifier(alpha);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the no-arg constructor when you want standard Laplace smoothing (alpha=1.0).","If alpha is user-configurable, validate it at the config layer before reaching the constructor.","Document that this classifier does not support zero smoothing; pick another estimator if needed."],"tags":["machine-learning","validation","argument-range","naive-bayes","smoothing"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}