fix(naive-bayes): GaussianNB var_smoothing = var_smoothing*max_var (sklearn parity) — F-GAUSSIANNB-EPSILON-003 (PMAT-890) - #2181
Merged
Conversation
…klearn parity) — F-GAUSSIANNB-EPSILON-003 (PMAT-890) GaussianNB::fit added a raw additive `var_smoothing` (1e-9) directly to each per-class feature variance. scikit-learn instead defines the smoothing as a single scalar `epsilon = var_smoothing * X.var(axis=0).max()` — `var_smoothing` SCALED by the largest feature variance (over all training rows, biased/population variance), added uniformly to every per-class feature variance. On a mixed-scale dataset (one feature var ~3.25e6, another ~1e-4) the raw additive constant makes the smoothed variance for the small feature ~33.5x too small, mis-scaling the Gaussian log-likelihood / Mahalanobis term and able to flip predict. This is a LIVE wrong-answer bug vs the provably-correct sklearn reference. Fix: compute max_feature_var = max_j Var(X[:,j]) over all training rows, set epsilon = var_smoothing * max_feature_var, and add that single epsilon to every per-class feature variance. `var_smoothing` stays configurable (default 1e-9). - RED: test_gaussian_nb_var_smoothing_scaled_by_max_feature_var fails on main — stored=1.000009943e-4 vs sklearn_expected=3.350000000e-3 (rel_err=0.97). - GREEN: passes after fix; predict_proba rows finite + sum to ~1. - Full crate: 13955 passed; 0 failed (no sibling test encoded the old behavior). - Contract: F-GAUSSIANNB-EPSILON-003 proof_obligation + single-line falsifier added to contracts/naive-bayes-v1.yaml; `pv lint contracts/` PASS. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PMAT-890 — Pillar-1 GaussianNB var_smoothing parity (F-GAUSSIANNB-EPSILON-003)
LIVE wrong-answer bug vs scikit-learn (the provably-correct reference).
Defect
GaussianNB::fit(incrates/aprender-core/src/classification/linear_svm.rs) added a raw additivevar_smoothing(default1e-9) directly to each per-class feature variance:scikit-learn instead defines the smoothing as a single scalar scaled by the largest feature variance:
added uniformly to every per-class feature variance. On a mixed-scale dataset (one feature var ~
3.25e6, another ~1e-4) the raw additive constant makes the smoothed variance for the small feature ~33.5× too small, mis-scaling the Gaussian log-likelihood / Mahalanobis term — able to flippredict.Fix
Compute
max_feature_var = max_j Var(X[:,j])over all training rows (biased/population variance, matching the existing convention), setepsilon = var_smoothing * max_feature_var, add that singleepsilonto every per-class feature variance.var_smoothingstays configurable (default1e-9).RED → GREEN evidence
Falsifier
test_gaussian_nb_var_smoothing_scaled_by_max_feature_var(no Python/sklearn required; closed-form sklearn reference):stored=1.000009943e-4vssklearn_expected=3.350000000e-3(epsilon=3.25e-3),rel_err=0.97→ FAILpredict_probarows finite and sum to ~1Full crate tests
cargo test -p aprender-core --lib→ 13955 passed; 0 failed; 2 ignored (no sibling test encoded the old raw-1e-9 behavior).cargo fmt --all --checkclean,cargo clippy -p aprender-core -- -D warningsclean.Contract
F-GAUSSIANNB-EPSILON-003proof_obligation + single-linecargo testfalsifier added tocontracts/naive-bayes-v1.yaml.pv lint contracts/→ PASS (0 errors).🤖 Generated with Claude Code