Skip to content

fix(metrics): use the size r = 1 / alpha in NegativeBinomialDeviance - #506

Merged
mkumar73 merged 2 commits into
mainfrom
fix/nb-deviance-dispersion
Oct 31, 2026
Merged

mkumar73 merged 2 commits into
mainfrom
fix/nb-deviance-dispersion

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #505

Problem

NegativeBinomialDistribution predicts the dispersion alpha and uses the size r = 1 / alpha, so the variance is mu + alpha * mu**2. NegativeBinomialDeviance, the default metric of the negativebinom family, put alpha into the deviance formula where r belongs, so its value was correct only for alpha == 1. #418 fixed the distribution's convention but left the metric unchanged.

Fix

deeptab/metrics/distributional.py:

  • NegativeBinomialDeviance computes 2 * mean(y * log(y / mu) + (y + r) * log((mu + r) / (y + r))) with r = 1 / alpha.
  • The default_alpha fallback is treated the same way and clipped like the column value, which also avoids a division by zero.
  • The docstring spells out the parameterization, matching NegativeBinomialDistribution.

Tests

In tests/metrics/test_distributional_metrics.py, both new tests compare against twice the saturated-minus-model log-likelihood computed with scipy.stats.nbinom, including a zero target:

  • test_nb_deviance_reads_dispersion_column: per-row dispersion.
  • test_nb_deviance_default_alpha_is_dispersion: the single-column default_alpha path.

Both fail on main and 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.rst needs 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)

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
mkumar73 merged commit e1e87f7 into main Oct 31, 2026
35 checks passed
@mkumar73
mkumar73 deleted the fix/nb-deviance-dispersion branch October 31, 2026 21:37
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] NegativeBinomialDeviance treats the dispersion alpha as the size parameter r

2 participants