Skip to content

Preserve precision in beta normalization and logarithmic combinatorics - #219

Merged
jmh530 merged 4 commits into
libmir:masterfrom
jmh530:codex/log-beta-stability
Oct 28, 2026
Merged

jmh530 merged 4 commits into
libmir:masterfrom
jmh530:codex/log-beta-stability

Conversation

@jmh530

@jmh530 jmh530 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

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, and logFactorial(1, 10^16) could return zero instead of approximately 36.84. The beta normalization error also affected betaLPDF.

Changes

  • Evaluate large-shape beta normalization with algebraically combined Stirling terms. Preserve the existing calculation for small normal shapes and use the gamma recurrence for subnormal shapes.
  • Evaluate logarithmic products without subtracting nearly equal gamma logarithms. Handle empty products explicitly and prevent unsigned endpoint overflow from selecting the iterative factorial path.
  • Evaluate large binomial coefficients through beta normalization, retaining the existing small exact-product and trivial-coefficient paths.
  • Share the Stirling correction between the internal beta and product helpers, and document the normalization behavior in betaLPDF.

Existing signatures and return types are preserved. The small-shape beta calculation retains its existing accuracy limitations.

Validation

  • Full Windows LDC suite: 69 modules passed.
  • Focused optimized tests passed with Windows DMD and Windows/Linux LDC, covering float, double, and real.
  • Additional checks compared 231 cases per Windows compiler against 420-digit references, including threshold boundaries and unsigned limits.
  • Added regressions for asymmetric and subnormal beta shapes, public beta log densities, empty and short products, large endpoints, and binomial symmetry.

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)

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

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

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)
@jmh530
jmh530 merged commit ae48661 into libmir:master Oct 28, 2026
26 checks passed
@jmh530
jmh530 deleted the codex/log-beta-stability branch October 28, 2026 10:16
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