Skip to content

Correct InverseGamma's mean and support bounds - #151

Merged
jordandouglas merged 1 commit into
masterfrom
Inversegamma
Aug 18, 2026
Merged

Correct InverseGamma's mean and support bounds#151
jordandouglas merged 1 commit into
masterfrom
Inversegamma

Conversation

@walterxie

Copy link
Copy Markdown
Member

Summary:

Building on the earlier CDF/ICDF fix (#149), two more ScalarDistribution methods were found silently returning the internal Gamma helper's statistics instead of InverseGamma's own:

  • getMean() was unoverridden, so it delegated to getApacheDistribution().getMean() — the internal Gamma(alpha, rate=beta) helper's mean (alpha/beta) — instead of InverseGamma's own mean (beta/(alpha-1)). Confirmed numerically: α=3, β=2 returned 1.5 instead of the correct 1.0.
  • Fixed by overriding getMean() with beta/(alpha-1) for alpha > 1. For alpha ≤ 1 the defining integral diverges; since X > 0 always, that divergence is provably to +Infinity (never negative or indeterminate), so Double.POSITIVE_INFINITY is returned there rather than NaN — otherwise there'd be a discontinuity between alpha=1 (NaN) and values just above it (correctly huge, finite numbers approaching +Infinity).

Went further on getApacheDistribution() itself: it e if it were "the" Apache distribution backingInverseGamma, which is exactly what caused this bug (and the earlier CDF/ICDF one) — the Gamma object doesn't represent InverseGamma's own density/CDF/mean at all, it's onlling via x = 1/y. Per the method's own documentedcontract ("or null otherwise"), it now returns null, so any future ScalarDistribution method that isn't explicitly overridden hewill throw instead of silently returning a Gamma-flae thing that implicitly relied on it —getLowerBoundOfParameter()/getUpperBoundOfParameter() — is now overridden directly with the hardcoded correct support, (0, +Infinity), rather than derived from getApacheDistri
Test plan (InverseGammaTest, +3 tests):

  • testGetMeanMatchesInverseGammaNotGamma: checks beta/(alpha-1) against the existing reference cases, plus alpha=2,beta=1 (mean still finite, but the boundary where variance stops of the mean as alpha → 1⁺, and +Infinity (not NaN)for alpha ∈ {1.0, 0.9, 0.5}.
  • testGetApacheDistributionIsNull: checks the null rndOfParameter/getUpperBoundOfParameter overrides, and that inverseCumulativeProbability's p≤0/p≥1 boundary handling (which calls those) still works without getApacheDistribution().
  • testSampleDrawsFromInverseGamma: sample() — the meoriginal bug report — had no test exercising it atall; this draws 200k samples and checks the empirical mean and empirical CDF against getMean()/reference values, with tolerances
    sized from the distribution's own finite variance soross repeated runs).

mvn test -Dtest=InverseGammaTest → 6/6 pass. Full beass, 0 regressions.

@jordandouglas
jordandouglas merged commit 4531415 into master Aug 18, 2026
1 check passed
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.

2 participants