Repository navigation
fix(metrics): use the size r = 1 / alpha in NegativeBinomialDeviance - #506
Merged
Merged
Conversation
NegativeBinomialDistribution predicts the dispersion alpha with r = 1 / alpha, but NegativeBinomialDeviance plugged alpha into the deviance formula where r belongs, so the score was only correct for alpha == 1. Compute the deviance with r = 1 / alpha (also for default_alpha) and document the parameterization. Fixes #505
mkumar73
approved these changes
Oct 31, 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 #505
Problem
NegativeBinomialDistributionpredicts the dispersionalphaand uses the sizer = 1 / alpha, so the variance ismu + alpha * mu**2.NegativeBinomialDeviance, the default metric of thenegativebinomfamily, putalphainto the deviance formula whererbelongs, so its value was correct only foralpha == 1. #418 fixed the distribution's convention but left the metric unchanged.Fix
deeptab/metrics/distributional.py:NegativeBinomialDeviancecomputes2 * mean(y * log(y / mu) + (y + r) * log((mu + r) / (y + r)))withr = 1 / alpha.default_alphafallback is treated the same way and clipped like the column value, which also avoids a division by zero.NegativeBinomialDistribution.Tests
In
tests/metrics/test_distributional_metrics.py, both new tests compare against twice the saturated-minus-model log-likelihood computed withscipy.stats.nbinom, including a zero target:test_nb_deviance_reads_dispersion_column: per-row dispersion.test_nb_deviance_default_alpha_is_dispersion: the single-columndefault_alphapath.Both fail on
mainand pass with this change.Docs
The class docstring, which is rendered in the API reference, now states the parameterization. The metrics table in
docs/api/metrics/index.rstneeds no change.Checks
just test: 3238 passed, 46 skipped, 28 xfailed; coverage 91.30%just docs: build succeeded (warnings treated as errors)just check: all hooks passed (ruff, prettier, pyright)