Skip to content

fix: correct Tweedie deviance formula for power not in {0, 1} - #270

Open
AlejandroCoronadoN wants to merge 3 commits into
Nixtla:mainfrom
AlejandroCoronadoN:fix-tweedie-deviance-factor
Open

fix: correct Tweedie deviance formula for power not in {0, 1}#270
AlejandroCoronadoN wants to merge 3 commits into
Nixtla:mainfrom
AlejandroCoronadoN:fix-tweedie-deviance-factor

Conversation

@AlejandroCoronadoN

Copy link
Copy Markdown

Problem

For a general Tweedie power (and for the power == 2 gamma case), the factor
of 2 was only applied to the first deviance term:

2 * ( y^(2-p) / ((1-p)(2-p)) )   # only this term is doubled
- ( y * m^(1-p) / (1-p) )        # not doubled
+ ( m^(2-p) / (2-p) )            # not doubled

The unit Tweedie deviance doubles all three terms. Because of this, the result
was wrong for every power other than 0 and 1, and even negative for
1 < power < 2 (a deviance can never be negative).

power=1.5: tweedie_deviance = -6.67   sklearn mean_tweedie_deviance = 0.066
power=2.0: tweedie_deviance =  0.069   sklearn                       = 0.045

The bug went unnoticed because the loss comparison test only covered powers 0,
1 and 2, and its own reference for power == 2 repeated the same mistake.

Fix

Distribute the 2 * over all three terms in both the general-power branch and
the power == 2 branch.

Verification

After the fix, tweedie_deviance matches sklearn.metrics.mean_tweedie_deviance
for powers 0, 1, 1.2, 1.5, 1.8, 2.0, 2.5 and 3.0 (max abs diff < 1e-6). The test
reference for power == 2 is corrected to the same formula, and general-power
cases (1.5 and 2.5) are added to test_loss, which fail without the code change
and pass with it. The full test_losses.py suite (106 tests, pandas and polars)
passes.

The factor of 2 was applied to only the first deviance term for a general
power and for the power == 2 (gamma) case, so the other terms were left
un-doubled. This produced wrong values, including negative "deviances" for
1 < power < 2. Distribute the 2 over all three terms so the result matches
scikit-learn's mean_tweedie_deviance for every power. The test reference for
power == 2 carried the same mistake, so it is corrected too, and general-power
cases (1.5 and 2.5) are added to the loss comparison test.
@CLAassistant

CLAassistant commented Aug 13, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ MMenchero
❌ AlejandroCoronadoN
You have signed the CLA already but the status is still pending? Let us recheck it.

@MMenchero

Copy link
Copy Markdown
Contributor

Hi @AlejandroCoronadoN thanks for this fix! I just updated the documentation to show the correct formula.

Can you sign the license so we can merge your PR? We would like to make a release asap, including your fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants