Skip to content

fix(distributions): use torch probs convention in NegativeBinomial - #429

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/negative-binomial-probs
Open

fix(distributions): use torch probs convention in NegativeBinomial#429
ChrisW09 wants to merge 1 commit into
mainfrom
fix/negative-binomial-probs

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #418

Problem

NegativeBinomialDistribution.compute_loss built the distribution with the scipy success-probability convention:

r = torch.tensor(1.0) / dispersion
p = r / (r + mean)
dist.NegativeBinomial(total_count=r, probs=p)

torch defines mean = total_count * probs / (1 - probs), so probs must be mean / (r + mean). As written, the distribution mean is r²/µ instead of µ (verified: µ=10, r=1 → distribution mean 0.1).

Training still converges — the network just learns whatever head output makes softplus(head) equal r²/µ — but the column trained as "mean" is not the mean, so predict(), mean-based metrics, and NegativeBinomialDeviance all silently consume garbage.

Fix

p = mean / (r + mean), plus a comment recording the convention and the resulting variance form.

Tests

  • test_mean_head_parameterizes_the_mean — the distribution built from a transformed mean of µ actually has mean µ.
  • test_nll_minimized_near_true_mean — NLL of data with mean 10 is lower at mean=10 than at 1 or 100.

Full tests/test_distributions.py passes.

🤖 Generated with Claude Code

probs was computed as r/(r+mean), the scipy convention, but torch's
NegativeBinomial defines mean = total_count * probs/(1-probs). The
constructed distribution therefore had mean r^2/mu instead of mu: the
network learned to output a value that is not the mean, so predict(),
_extract_mean-based RMSE/MAE, and NegativeBinomialDeviance (which reads
column 0 as mu) all received garbage. probs is now mean/(r+mean).

Fixes #418

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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] NegativeBinomial uses the scipy probs convention with torch — the mean head parameterizes r²/µ

1 participant