Repository navigation
fix(distributions): keep zero targets exact in the Tweedie deviance - #526
Merged
Merged
Conversation
evaluate_nll() clamped y to 1e-8 before y ** (2 - p), so every zero target added 2 * 1e-8 ** (2 - p) / ((1 - p)(2 - p)) to the deviance (about -3.5 per zero target for p = 1.9). Since 2 - p > 0, 0 ** (2 - p) is already 0; clamp at 0 only to keep negative values out. Fixes #525
mkumar73
approved these changes
Nov 2, 2026
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.
Fixes #525
Problem
TweedieDistribution.evaluate_nll()computed the deviance termy ** (2 - p) / ((1 - p) * (2 - p))ony.clamp(min=1e-8). For a zero target, the term is exactly 0 because1 < p < 2. With the clamp, every zero added2 * 1e-8 ** (2 - p) / ((1 - p)(2 - p))to the deviance: about-0.0008forp = 1.5and-3.5forp = 1.9. Zero targets are the common case for Tweedie data, sotweedie_deviancecame out too small and could even be negative.Fix
deeptab/distributions/tweedie.pyclamps at0instead of1e-8. That still keeps negative values out, and0 ** (2 - p) = 0is well defined.Tests
tests/distributions/test_distributions.py::TestTweedieDistribution::test_evaluate_nll_deviance_matches_sklearn_with_zero_targets[1.5|1.9]comparesevaluate_nll()["tweedie_deviance"]withsklearn.metrics.mean_tweedie_devianceon targets that include zeros. Both cases fail onmainand pass with this change.Docs
No change needed.
Checks
just test: 3238 passed, 46 skipped, 28 xfailed; coverage 91.29%just docs: build succeeded (warnings treated as errors)just check: all hooks passed (ruff, prettier, pyright)