Skip to content

fix(distributions): keep zero targets exact in the Tweedie deviance - #526

Merged
mkumar73 merged 2 commits into
mainfrom
fix/tweedie-zero-deviance
Nov 2, 2026
Merged

mkumar73 merged 2 commits into
mainfrom
fix/tweedie-zero-deviance

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #525

Problem

TweedieDistribution.evaluate_nll() computed the deviance term y ** (2 - p) / ((1 - p) * (2 - p)) on y.clamp(min=1e-8). For a zero target, the term is exactly 0 because 1 < p < 2. With the clamp, every zero added 2 * 1e-8 ** (2 - p) / ((1 - p)(2 - p)) to the deviance: about -0.0008 for p = 1.5 and -3.5 for p = 1.9. Zero targets are the common case for Tweedie data, so tweedie_deviance came out too small and could even be negative.

Fix

deeptab/distributions/tweedie.py clamps at 0 instead of 1e-8. That still keeps negative values out, and 0 ** (2 - p) = 0 is well defined.

Tests

tests/distributions/test_distributions.py::TestTweedieDistribution::test_evaluate_nll_deviance_matches_sklearn_with_zero_targets[1.5|1.9] compares evaluate_nll()["tweedie_deviance"] with sklearn.metrics.mean_tweedie_deviance on targets that include zeros. Both cases fail on main and 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)

ChrisW09 and others added 2 commits October 21, 2026 20:40
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
mkumar73 merged commit ebc936a into main Nov 2, 2026
35 checks passed
@mkumar73
mkumar73 deleted the fix/tweedie-zero-deviance branch November 2, 2026 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] TweedieDistribution.evaluate_nll() reports a wrong tweedie_deviance for zero targets

2 participants