Compute the triangular variance from the side widths - #57
Merged
Merged
Conversation
The textbook form squared the raw bounds, so bounds large next to their spread cancelled to 0 or a negative variance. The mean is now measured from the lower bound so it cannot overflow near double.MaxValue. Fixes #49 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VPis8BNX5ETFM2XytgJCz7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VPis8BNX5ETFM2XytgJCz7
|
This was referenced Sep 28, 2026
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.



Fixes #49
What changed
TriangularDistributionProvider.Varianceused(a² + b² + c² − ab − ac − bc) / 18on the raw bounds. When the bounds are large compared with their spread, those squares cancel away every significant digit.Triangular(1e9, 1e9+1, 1e9+2).Variancecame out as0. At1e12it came out negative, which madeStandardDeviationreturn NaN.Variancenow uses the widths the provider already stores:(lw² + uw² + lw·uw) / 18. The two forms are algebraically identical, but this one depends only on the triangle's shape and not on where it sits.Meanis nowMinimum + (lowerWidth + width) / 3, so bounds neardouble.MaxValueno longer overflow the sum.Tests
Triangular_Moments_Do_Not_Depend_On_Where_The_Triangle_Sits. It covers the three repro cases from the issue, the standard deviation at1e12, and a mean neardouble.MaxValue.1e9case: expected 0.1667, got 0). With the fix it passes.Essentials.Testssuite passes: 917/917.🤖 Generated with Claude Code
https://claude.ai/code/session_01VPis8BNX5ETFM2XytgJCz7
Generated by Claude Code