Skip to content

Bound integer gamma factorial work and stabilize large-shape densities - #180

Merged
jmh530 merged 2 commits into
libmir:masterfrom
jmh530:codex/distribution-range-audit
Oct 18, 2026
Merged

jmh530 merged 2 commits into
libmir:masterfrom
jmh530:codex/distribution-range-audit

Conversation

@jmh530

@jmh530 jmh530 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Fix two issues in gamma density evaluation: excessive factorial work for large integer shapes and cancellation near the mean for large shapes.

The integer PDF previously evaluated an O(shape) factorial before deciding whether to discard the direct result. Limit that calculation to shapes at most 32 and use the existing log-factorial fallback for larger shapes.

For large shapes near the mean, the logarithmic density subtracts large, nearly equal terms. For example, at argument and shape 1e16, this can produce a log-density of zero or 64 instead of approximately -19.34. Extract the existing centered chi-square calculation into a shared internal helper and use it in both gamma PDF and LPDF overloads when shape >= 32 and abs(x / scale - shape) < shape / 2. Existing calculations remain in use outside that region.

For integer shapes, compute the displacement by subtracting the high and low pieces separately, preserving low shape bits even when real has a 53-bit significand. Add independent reference cases, scale and region-boundary coverage, maximum finite shapes, and integer shapes above 2^53.

Validation:

  • Full DMD and LDC unit suites passed with released dependencies.
  • Optimized focused DMD and LDC tests passed using the isolated factorial fix from Fix factorial forward references by deferring UInt view return types mir-algorithm#486, which addresses an existing standalone compilation issue.
  • GDC 14 syntax checks and the DMD documentation build passed.
  • An independent 100-digit reference sweep passed on DMD and LDC using the pending factorial fix and an overlay of the upstream Phobos gamma-function module.

No dependency requirements or selections are changed. The two commits were rebased onto current master with unchanged patches.

AI-Assisted-By: Codex (GPT-6 Astra Medium)

The direct integer-shape calculation evaluates factorial(shape - 1)
before deciding whether to use the logarithmic fallback. Its O(shape)
cost makes large shapes impractical even when the density is zero.

Limit the direct factorial calculation to shapes at most 32. Larger
shapes use the existing log-factorial fallback, retaining the integer
shape without performing the linear factorial calculation.

Document the limit and cover a shape of one billion with float,
double, and real regression cases.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
The logarithmic gamma density subtracts large, nearly equal terms
near the mean. At shape and argument 1e16, this cancellation can
produce a log-density of zero or 64 instead of about -19.34.

Extract the existing centered chi-square calculation into a shared
internal helper and use it in both gamma PDF and LPDF overloads for
finite shapes at least 32 within half the shape of the mean. Retain
the existing calculations outside that region.

Preserve the displacement from large integer shapes by subtracting
their high and low pieces separately before normalization. This
avoids losing low shape bits when real has a 53-bit significand.

Add independent density references, scale and region-boundary cases,
maximum finite shape coverage, and integer shapes above 2^53.
Document the centered calculation and register the helper in Meson.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
@codecov

codecov Bot commented Oct 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@jmh530
jmh530 merged commit f39a2f7 into libmir:master Oct 18, 2026
26 checks passed
@jmh530
jmh530 deleted the codex/distribution-range-audit branch October 18, 2026 23:47
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