Repository navigation
Preserve precision in beta normalization and logarithmic combinatorics - #219
Merged
Merged
Conversation
Avoid subtracting large, nearly equal gamma logarithms when evaluating logBeta. That subtraction could return zero for logBeta(1e100, 1), also producing an incorrect beta log-density at the endpoint. Combine the Stirling terms using shape ratios without forming the potentially overflowing shape sum. Use the gamma recurrence for subnormal shapes to avoid overflowing intermediates inside logGamma. Retain the direct calculation for small normal shapes and keep the fallback out of line to limit ordinary-call overhead. Document the numerical paths and add independent reference checks, threshold and symmetry cases, subnormal and extreme-shape identities, and public betaLPDF endpoint and nearby-value regressions. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Avoid subtracting large gamma logarithms when evaluating short products at large starting values. Combine the Stirling terms directly and share their correction polynomial with logBeta. Handle empty products explicitly and select the small-product path without unsigned endpoint overflow. Convert before forming large gamma arguments so wrapped sums cannot select an enormous factorial loop. Evaluate large binomial coefficients through beta normalization while retaining the existing small-product and trivial-coefficient paths. Add float, double, and real regressions for large starting values, thresholds, unsigned limits, symmetry, and high-precision references. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Convert the reference logarithm to the tested type and use a tight tolerance. Older Phobos returns real for float and double log inputs, making exact equality fail despite correct results. AI-Assisted-By: Codex (GPT-6 Astra Medium)
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Test zero, NaN, and infinity in either argument across float, double, and real, verifying the preserved gamma-log fallback behavior. AI-Assisted-By: Codex (GPT-6 Astra Medium)
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.
Fix cancellation and unsigned endpoint overflow in the shared logarithmic helpers. Subtracting large gamma logarithms could discard the entire result:
logBeta(1e100, 1)returned zero instead of approximately −230.26, andlogFactorial(1, 10^16)could return zero instead of approximately 36.84. The beta normalization error also affectedbetaLPDF.Changes
betaLPDF.Existing signatures and return types are preserved. The small-shape beta calculation retains its existing accuracy limitations.
Validation
Paired double-precision benchmarks showed ordinary-case timings close to baseline, with some low-single-digit overhead. Large binomial-coefficient cases improved by approximately 35–59% in the final Windows measurements. These are focused, host-dependent measurements rather than general performance guarantees.
AI-Assisted-By: Codex (GPT-6 Astra Medium)