Skip to content

Reject non-finite parameters in distribution constructors - #238

Open
youqad wants to merge 4 commits into
haskell:masterfrom
youqad:fix/reject-non-finite-parameters
Open

youqad wants to merge 4 commits into
haskell:masterfrom
youqad:fix/reject-non-finite-parameters

Conversation

@youqad

@youqad youqad commented Nov 8, 2026 •

Copy link
Copy Markdown

Hi! The safe constructors of several distributions accept an infinite parameter (a few accept NaN too), and the distribution they build then returns NaN, is 0 everywhere, or never terminates. With this change they return Nothing or Left for those inputs.

Accepted on master What the distribution then does
exponentialE (1/0) density at 1 is NaN
gammaDistrE (1/0) 1, studentTE (1/0), weibullDistrErr (1/0) 1 density at 1 is NaN
negativeBinomialE (1/0) 0.5 probability at 1 is NaN, and cumulative never terminates
poissonE (1/0) logProbability at 1 is NaN
betaDistrE (1/0) 1 density at 0.5 is NaN, and cumulative at 0.5 never terminates
fDistributionRealE (1/0) 5 cumulative never terminates
normalDistrE (1/0) 1 density at 1 is 0.0, and mean is Infinity
cauchyDistributionE (1/0) 1, laplaceE (1/0) 1 density at 1 is 0.0
uniformDistrE (-1/0) 0 cumulative at 0.5 is NaN
normalDistrE (0/0) 1, weibullDistrErr (0/0) 1 density at 1 is NaN
lognormalDistrErr 400 1 accepted, although the parameters are out of range

1. Infinite parameters

These constructors now check x > 0 && x < 1/0 for each parameter (x >= 0 for the improper gamma and beta, and for poissonE).

An infinite location or scale gives a distribution with density 0 everywhere, and so does an infinite scale in each of the normal, Cauchy and Laplace constructors. The location is now checked with abs m < 1/0 and the scale with s < 1/0. uniformDistrE a b returns Nothing when abs a >= 1/0 || abs b >= 1/0, before it orders the two bounds.

2. NaN parameters

NaN got through for a different reason. The normal, Cauchy and Laplace constructors only checked the scale and never looked at the location (Cauchy and Laplace accepted (0/0) 1 too). For weibullDistrErr (0/0) 1, the guards were k <= 0 and l <= 0, and NaN <= 0 is False, so the NaN passes them (the same happens with weibullDistrErr 1 (0/0)). The guards are now in the positive form above, which a NaN fails.

3. Lognormal with a large mean

The check was

sig >= sqrt (log m_huge - 2 * mu)

and for mu above about 354.9 (log m_huge / 2) that bound is NaN, so the comparison is False for every sigma and the check passes. It's now

sig < sqrt (log m_huge - 2 * mu)

which NaN fails, together with abs mu < 1/0: lognormalDistrErr 354 1 is accepted and lognormalDistrErr 355 1 is rejected. lognormalDistrErr (1/0) 1 was accepted for the same reason.

The JSON string {"mean":400,"stdDev":1} also decoded to a lognormal distribution, because FromJSON LognormalDistribution was derived and never called lognormalDistrErr. It's now an explicit instance that reads the same two fields, mean and stdDev, through lognormalDistrErr.

The Read and Binary decoders of the lognormal distribution (read "lognormalDistr 400.0 1.0" returned a distribution, and so did decoding the Binary encoding of (400, 1)), and those of the other changed distributions, go through the checked constructors, so they reject these values too, and so do the unsafe wrappers such as normalDistr.

4. Smaller changes

studentTE (1/0) is rejected now too, although the t distribution tends to the standard normal as the degrees of freedom grow: the module has no value for that limit, since logDensityUnscaled evaluates logBeta and log1p at infinity and gives NaN. The Cauchy Binary decoder now calls fail where it called error, and fromSample for the exponential and Laplace distributions goes through exponentialE and laplaceE.

Tests

Each wrong value above has a new test, which fails on master (with GHC 9.6.7) and passes with these commits. I ran statistics-tests on GHC 9.6.7, 9.12.4 and 9.14.1, and all 514 tests pass (the 451 already on master and the 63 new ones).

The last commit adds an ## Unreleased section at the top of changelog.md, with one entry per fix.

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