Skip to content

Fix gamma CDF normalization and incomplete gamma fraction - #23

Open
gaoflow wants to merge 1 commit into
phillbaker:masterfrom
gaoflow:fix-gamma-cdf-regularization
Open

Fix gamma CDF normalization and incomplete gamma fraction#23
gaoflow wants to merge 1 commit into
phillbaker:masterfrom
gaoflow:fix-gamma-cdf-regularization

Conversation

@gaoflow

@gaoflow gaoflow commented Jul 31, 2026

Copy link
Copy Markdown

GammaDistribution#cdf returns values outside [0,1]: shape=2, scale=3 gives 3.0 at x=500, and shape=5, scale=10 saturates at 0.4167. incomplete_gamma is already the regularized CDF, so the extra scale / Gamma(shape) factors are spurious — they only cancel when scale == Gamma(shape).

The same path also mis-computes the upper tail: gamma_fraction returned after its first loop iteration and never applied the Lentz update, leaving ~0.5–1.5% error for x/scale >= shape+1. The update now runs every iteration with the standard XMININ guards, matching the structure of beta_fraction in the same file.

Tests cover the [0,1] range and monotonicity for scale != Gamma(shape), plus cdf reference values from mpmath over both incomplete-gamma branches. Existing cdf deltas are tightened from 0.01, which previously absorbed the tail error.

Repro: Rubystats::GammaDistribution.new(2.0, 3.0).cdf(500.0) → 3.0 before, 1.0 after.

GammaDistribution#cdf was scaled by scale/Gamma(shape), so it returned values outside [0,1] (e.g. 3.0 for shape=2, scale=3) and saturated below 1.0 whenever the two did not cancel. incomplete_gamma is already regularized.

gamma_fraction returned after its first loop iteration and never applied the Lentz update, leaving a 0.5-1.5% error on the x >= a+1 branch. The update now runs every iteration with the XMININ guards, mirroring beta_fraction, and the result is returned after the loop.

Tests cover the [0,1] range for scale != Gamma(shape) and mpmath reference values on both incomplete-gamma branches.
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.

1 participant