Skip to content

Fix uniform distribution when its width, mean or variance overflows - #240

Open
youqad wants to merge 3 commits into
haskell:masterfrom
youqad:fix/uniform-wide-bounds
Open

youqad wants to merge 3 commits into
haskell:masterfrom
youqad:fix/uniform-wide-bounds

Conversation

@youqad

@youqad youqad commented Nov 8, 2026 •

Copy link
Copy Markdown

Hi! Here are fixes for the uniform distribution when b - a, a + b or (b - a) * (b - a) overflows although the result is representable (one commit for the width, one for the mean and the variance, and a third one for the changelog).

With d = uniformDistr (-1e308) 1e308:

Expression On master With this PR
logDensity d 0 -Infinity about -709.889355822726
density d 0 0.0 5.0e-309
cumulative d 1e308 NaN 1.0
quantile d 0.5 Infinity 0.0
complQuantile d 0.5 -Infinity 0.0
stdDev d Infinity 5.773502691896258e307
entropy d Infinity 709.889355822726

cumulative, complCumulative and quantile are wrong at other points as well (quantile d 0 is NaN).

1. The width overflows

Both bounds are finite and uniformDistrE accepts them, but b - a overflows to Infinity. Uniform has no logDensity of its own, so the class default log . density applies: density is 1 / (b - a), which is 0, and log 0 is -Infinity.

The first commit adds halfWidth, which is 0.5 * b - 0.5 * a and cannot overflow for finite bounds, and uses it where b - a overflows while both bounds are finite (widthOverflows):

Function When b - a overflows
density 0.5 / halfWidth d
cumulative (0.5 * x - 0.5 * a) / halfWidth d
quantile (1 - p) * a + p * b
complQuantile p * a + (1 - p) * b
stdDev 0.5773502691896258 * halfWidth d
entropy log (halfWidth d) + log 2

The stdDev constant is twice the one of the ordinary branch, since b - a is twice halfWidth. logDensity stays the class default, which is right once density is: 0.5 / halfWidth d is at least 0.5 / maxDouble, a positive subnormal, so log never sees 0.

2. The mean and the variance overflow

mean (uniformDistr 1e308 1.5e308) returns Infinity, because a + b overflows, whereas the mean is 1.25e308. variance (uniformDistr 0 3e154) returns Infinity, because (b - a) * (b - a) overflows, whereas the variance is about 7.5e307. The second commit computes mean as 0.5 * a + 0.5 * b when a + b overflows, and variance as s * s with s = stdDev d when the square overflows. For uniformDistr (-1e308) 1e308, whose variance is too large for a Double, variance still returns Infinity.

Tests

The tests check each of these functions on uniformDistr (-1e308) 1e308, and cumulative, quantile and complQuantile on the asymmetric interval from -2^1023 to 1.5 * 2^1023, where the expected values are exact by construction. The values that go through log or a rounded constant (logDensity, stdDev, entropy) are compared with eqRelErr, since eq also accepts an infinite result, and the others are compared exactly. I ran statistics-tests on GHC 9.6.7, 9.12.4 and 9.14.1, and all 471 tests pass (the 451 already on master and the 20 new ones).

The last commit adds an ## Unreleased section at the top of changelog.md, with one entry for these fixes.

@Shimuuar

Shimuuar commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Thank you for your contributions. I won't be able to review them for next two weeks

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