Internal Documentation
Documentation for EpiAwareADTools's internal interface.
Contents
Index
EpiAwareADTools._gamma_cdfEpiAwareADTools._gamma_cdf_value_and_partialsEpiAwareADTools._grad_p_a_series
Internal API
EpiAwareADTools._gamma_cdf Function
_gamma_cdf(k::Real, θ::Real, x::Real) -> AnyAD-safe Gamma CDF, P(k, x/θ).
Primal goes through SpecialFunctions.gamma_inc for every Real subtype it supports (Float64, Float32, BigFloat) — same path the non-AD hot path uses, full accuracy across all z/a regimes. AD coverage is supplied by per-backend extensions:
EpiAwareADToolsChainRulesCoreExtdefines the reverse-moderruleand forward-modefrule(analytical partials, primal viagamma_inc).EpiAwareADToolsMooncakeExtlifts both the rrule and frule into Mooncake (reverse and forward mode).EpiAwareADToolsReverseDiffExtlifts the rrule into ReverseDiff.EpiAwareADToolsForwardDiffExtdefinesDualmethods on_gamma_cdfdirectly (forward-mode dispatches on argument types, not via ChainRules).
The α-partial that gamma_inc's ChainRule leaves as @not_implemented is supplied by _grad_p_a_series, following the series form Moore (1982) introduced as Algorithm AS 187 and that Stan (grad_reg_inc_gamma) and JAX (igamma_grad_a) both use. This whole machinery stands in for a differentiable gamma_inc upstream (SpecialFunctions.jl issue #531) and is deleted once that exists.
EpiAwareADTools._gamma_cdf_value_and_partials Function
_gamma_cdf_value_and_partials(
k::Real,
θ::Real,
x::Real
) -> NTuple{4, Any}Primal value and analytical partials (Ω, dk, dθ, dx) for _gamma_cdf. Shared by every per-backend AD extension so the formulas live in one place:
dx = pdf(Gamma(k, θ), x)dθ = -(x/θ) · dxdk = _grad_p_a_series(k, x/θ)
The non-positive-x branch returns zeros for the primal and all three partials, matching _gamma_cdf's early-return behaviour.
EpiAwareADTools._grad_p_a_series Function
_grad_p_a_series(a::Real, z::Real; rtol, maxiter) -> AnyPartial of the regularised lower incomplete gamma with respect to the shape parameter — the term SpecialFunctions.gamma_inc leaves as @not_implemented in its ChainRule. Computed by term-by-term differentiation of the Tricomi absolutely-convergent series for P(a, z) = z^a e^{-z} Σ_{n ≥ 0} z^n / Γ(a + n + 1):
with ψ(a + n + 1) = ψ(a + n) + 1 / (a + n) propagated alongside the term recurrence term_{n+1} = term_n · z / (a + n + 1). Used by the reverse-mode rule in EpiAwareADToolsChainRulesCoreExt and by the forward-mode Dual methods in EpiAwareADToolsForwardDiffExt.
This is the analytic α-partial the upstream gamma_inc ChainRule should eventually supply: SpecialFunctions.jl issue #531 (implement the shape-parameter partial for gamma_inc as a convergent series) tracks it. The helper is deleted once that rule lands.
References
The series + digamma-recurrence form is Moore (1982), "Algorithm AS 187: Derivatives of the Incomplete Gamma Integral", Applied Statistics 31:330-335. The same construction is used by Stan (stan/math/prim/fun/grad_reg_inc_gamma.hpp) and JAX (jax._src.scipy.special.random_gamma_grad / igamma_grad_a) for the shape derivative of the regularised lower incomplete gamma.