Repository navigation
Conversation
Collaborator
|
Thank you for your contributions. I won't be able to review them for next two weeks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi! Here are fixes for the uniform distribution when
b - a,a + bor(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:logDensity d 0-Infinity-709.889355822726density d 00.05.0e-309cumulative d 1e308NaN1.0quantile d 0.5Infinity0.0complQuantile d 0.5-Infinity0.0stdDev dInfinity5.773502691896258e307entropy dInfinity709.889355822726cumulative,complCumulativeandquantileare wrong at other points as well (quantile d 0isNaN).1. The width overflows
Both bounds are finite and
uniformDistrEaccepts them, butb - aoverflows toInfinity.Uniformhas nologDensityof its own, so the class defaultlog . densityapplies:densityis1 / (b - a), which is0, andlog 0is-Infinity.The first commit adds
halfWidth, which is0.5 * b - 0.5 * aand cannot overflow for finite bounds, and uses it whereb - aoverflows while both bounds are finite (widthOverflows):b - aoverflowsdensity0.5 / halfWidth dcumulative(0.5 * x - 0.5 * a) / halfWidth dquantile(1 - p) * a + p * bcomplQuantilep * a + (1 - p) * bstdDev0.5773502691896258 * halfWidth dentropylog (halfWidth d) + log 2The
stdDevconstant is twice the one of the ordinary branch, sinceb - ais twicehalfWidth.logDensitystays the class default, which is right oncedensityis:0.5 / halfWidth dis at least0.5 / maxDouble, a positive subnormal, solognever sees0.2. The mean and the variance overflow
mean (uniformDistr 1e308 1.5e308)returnsInfinity, becausea + boverflows, whereas the mean is1.25e308.variance (uniformDistr 0 3e154)returnsInfinity, because(b - a) * (b - a)overflows, whereas the variance is about7.5e307. The second commit computesmeanas0.5 * a + 0.5 * bwhena + boverflows, andvarianceass * swiths = stdDev dwhen the square overflows. ForuniformDistr (-1e308) 1e308, whose variance is too large for aDouble,variancestill returnsInfinity.Tests
The tests check each of these functions on
uniformDistr (-1e308) 1e308, andcumulative,quantileandcomplQuantileon the asymmetric interval from-2^1023to1.5 * 2^1023, where the expected values are exact by construction. The values that go throughlogor a rounded constant (logDensity,stdDev,entropy) are compared witheqRelErr, sinceeqalso accepts an infinite result, and the others are compared exactly. I ranstatistics-testson 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
## Unreleasedsection at the top ofchangelog.md, with one entry for these fixes.