Skip to content

Internal Documentation

Documentation for CensoredDistributions.jl's internal interface.

Contents

Index

Internal API

Base.rand Method
julia
rand(
    rng::Random.AbstractRNG,
    d::CensoredDistributions.IntervalCensored
) -> Any

Generate a random sample by discretising a sample from the underlying distribution.

See also: quantile

source
Base.rand Method
julia
rand(
    rng::Random.AbstractRNG,
    d::CensoredDistributions.PrimaryCensored
) -> Any

Generate a random sample by summing samples from delay and primary event distributions.

See also: quantile

source
Base.rand Method
julia
rand(
    rng::Random.AbstractRNG,
    d::CensoredDistributions.Weighted
) -> Any

Generate a random sample (delegates to underlying distribution).

See also: quantile

source
Base.rand Method
julia
rand(rng::Random.AbstractRNG, d::ExponentiallyTilted) -> Any

Generate a random sample using inverse transform sampling.

See also: quantile

source
CensoredDistributions._cdf_ad_safe Method
julia
_cdf_ad_safe(
    dist::Distributions.UnivariateDistribution,
    u::Real
) -> Any

AD-safe cdf(dist, u) companion to _logcdf_ad_safe. Same dispatch idea: route Gamma through _gamma_cdf so the numeric-path optimisations that call cdf(dist, lower) for early termination remain differentiable under reverse-mode AD.

source
CensoredDistributions._collect_unique_boundaries Method
julia
_collect_unique_boundaries(
    d::CensoredDistributions.IntervalCensored,
    x::AbstractVector{<:Real}
) -> Any
julia
_collect_unique_boundaries(d::IntervalCensored, x::AbstractVector)

Collect all unique interval boundaries needed for vectorised PDF computation.

Returns a sorted vector of unique boundaries with appropriate type promotion.

source
CensoredDistributions._compute_pdfs_with_cache Method
julia
_compute_pdfs_with_cache(
    d::CensoredDistributions.IntervalCensored,
    x::AbstractVector{<:Real},
    cdf_lookup::Dict
) -> Any
julia
_compute_pdfs_with_cache(d::IntervalCensored, x::AbstractVector, cdf_lookup::Dict)

Compute PDFs efficiently using cached CDF values.

Uses the same boundary case handling as the scalar method.

source
CensoredDistributions._gamma_cdf Method
julia
_gamma_cdf(k::Real, θ::Real, x::Real) -> Any

AD-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:

  • CensoredDistributionsChainRulesCoreExt defines the reverse-mode rrule and forward-mode frule (analytical partials, primal via gamma_inc).

  • CensoredDistributionsMooncakeExt lifts both the rrule and frule into Mooncake (reverse and forward mode).

  • CensoredDistributionsReverseDiffExt lifts the rrule into ReverseDiff.

  • CensoredDistributionsForwardDiffExt defines Dual methods on _gamma_cdf directly (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.

source
CensoredDistributions._gamma_cdf_value_and_partials Method
julia
_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/θ) · dx

  • dk = _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.

source
CensoredDistributions._grad_p_a_series Method
julia
_grad_p_a_series(a::Real, z::Real; rtol, maxiter) -> Any

Partial 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 CensoredDistributionsChainRulesCoreExt and by the forward-mode Dual methods in CensoredDistributionsForwardDiffExt.

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.

source
CensoredDistributions._logcdf_ad_safe Method
julia
_logcdf_ad_safe(
    dist::Distributions.UnivariateDistribution,
    u::Real
) -> Any

AD-safe logcdf(dist, u) for use inside differentiable integrands.

Generic dispatch falls through to Distributions.logcdf. The Gamma method routes through _gamma_cdf so its ChainRulesCore.rrule is picked up by reverse-mode AD inside the numeric primarycensored_cdf path; without this, the integrand calls gamma_inc and breaks under every supported AD backend.

source
CensoredDistributions._make_weibull_g Method
julia
_make_weibull_g(
    k::Real,
    λ::Real
) -> CensoredDistributions.var"#weibull_g_specialized#_make_weibull_g##0"{<:Real, <:Real}

Function factory for optimized Weibull g function.

Creates a specialized function with pre-computed constants for g(t; k, λ) = γ(1 + 1/k, (t/λ)^k) where γ is the lower incomplete gamma function.

Pre-computes inv_k = 1/k and a = 1 + inv_k to avoid repeated computation in the returned specialized function.

Arguments

  • k::Real: Weibull shape parameter

  • λ::Real: Weibull scale parameter

Returns

A specialized function weibull_g_specialized(t::Real) that efficiently computes the Weibull g function using pre-computed constants.

Examples

julia
weibull_g_func = _make_weibull_g(2.0, 1.5)
g_val = weibull_g_func(3.0)
source
CensoredDistributions._quantile_optimization Method
julia
_quantile_optimization(
    d,
    p::Real;
    initial_guess_fn,
    result_postprocess_fn,
    check_nan
) -> Any

Internal function for quantile optimization using numerical methods.

Solves the equation cdf(d, q) - p = 0 using the Nelder-Mead algorithm. This is shared logic used by both PrimaryCensored and IntervalCensored quantile functions.

Arguments

  • d: The distribution for which to compute the quantile

  • p: The probability value in [0, 1]

Keyword Arguments

  • initial_guess_fn: Function that takes (d, p) and returns initial guess vector. If nothing, uses quantile(get_dist(d), p) as scalar initial guess.

  • result_postprocess_fn: Function to post-process the optimization result. Defaults to identity (no post-processing).

  • check_nan: If true, explicitly check for NaN input values.

Returns

The quantile value after optimization and post-processing.

Implementation Details

  • Validates that p ∈ [0, 1] (with optional NaN checking)

  • Handles boundary cases p=0 (minimum) and p=1 (maximum) analytically

  • Creates objective function (cdf(d, q) - p)^2 with support checking

  • Uses heavy penalty for values outside distribution support

  • Solves with Nelder-Mead algorithm with tight tolerances

  • Checks convergence and applies post-processing to result

Type Stability

This function is designed to maintain type stability when the initial guess function and post-processing function are type-stable.

source
CensoredDistributions.combine_weights Method
julia
combine_weights(_::Missing, _::Missing) -> Missing

Combine constructor weight with observation weight using dispatch-based rules.

Weight combination rules:

  • missing, missing → missing (both missing means no weight)

  • w1, missing → w1 (use constructor weight)

  • missing, w2 → w2 (use observation weight)

  • w1, w2 → w1 * w2 (multiply weights)

Vector Extensions

For Product distributions, additional methods handle vectorised weight combinations:

  • Vector, Vector → combine_weights.(vector1, vector2) (element-wise combination)

  • Vector, missing → Vector (keep constructor weights)

  • Vector, scalar → [combine_weights(w, scalar) for w in Vector] (broadcast scalar)

source
Distributions.ccdf Method
julia
ccdf(d::CensoredDistributions.Weighted, x::Real) -> Any

Compute the complementary cumulative distribution function (delegates to underlying distribution).

See also: cdf

source
Distributions.cdf Method
julia
cdf(
    d::CensoredDistributions.Convolved,
    x::AbstractVector{<:Real}
) -> Any

Compute the CDF for a vector of evaluation points using a single quadrature solve (the integrand returns a vector).

See also: cdf

source
Distributions.cdf Method
julia
cdf(d::CensoredDistributions.Convolved, x::Real) -> Any

Compute the cumulative distribution function.

Uses an analytical convolution when Distributions.convolve applies to all component pairs, otherwise AD-safe numeric quadrature.

See also: logcdf

source
Distributions.cdf Method
julia
cdf(
    d::CensoredDistributions.IntervalCensored,
    x::Real
) -> Any

Compute the cumulative distribution function.

See also: logcdf

source
Distributions.cdf Method
julia
cdf(
    d::CensoredDistributions.PrimaryCensored,
    x::Real
) -> Any

Compute the cumulative distribution function.

See also: logcdf

source
Distributions.cdf Method
julia
cdf(d::CensoredDistributions.Weighted, x::Real) -> Any

Compute the cumulative distribution function (delegates to underlying distribution).

See also: logcdf

source
Distributions.cdf Method
julia
cdf(d::ExponentiallyTilted, x::Real) -> Any

Compute the cumulative distribution function.

See also: logcdf, quantile

source
Distributions.logccdf Method
julia
logccdf(d::CensoredDistributions.Weighted, x::Real) -> Any

Compute the log complementary cumulative distribution function (delegates to underlying distribution).

See also: logcdf

source
Distributions.logcdf Method
julia
logcdf(d::CensoredDistributions.Convolved, x::Real) -> Any

Compute the log cumulative distribution function.

See also: cdf

source
Distributions.logcdf Method
julia
logcdf(
    d::CensoredDistributions.IntervalCensored,
    x::Real
) -> Any

Compute the log cumulative distribution function.

See also: cdf

source
Distributions.logcdf Method
julia
logcdf(
    d::CensoredDistributions.PrimaryCensored,
    x::Real
) -> Any

Compute the log cumulative distribution function.

See also: cdf

source
Distributions.logcdf Method
julia
logcdf(d::CensoredDistributions.Weighted, x::Real) -> Any

Compute the log cumulative distribution function (delegates to underlying distribution).

See also: cdf

source
Distributions.logcdf Method
julia
logcdf(d::ExponentiallyTilted, x::Real) -> Any

Compute the log cumulative distribution function.

See also: cdf

source
Distributions.logpdf Method
julia
logpdf(
    d::CensoredDistributions.Convolved,
    x::AbstractVector{<:Real}
) -> Any

Compute log densities for a vector of points, reusing the batched PDF solve for the numeric path.

See also: logpdf, pdf

source
Distributions.logpdf Method
julia
logpdf(d::CensoredDistributions.Convolved, x::Real) -> Any

Compute the log probability density function.

See also: pdf, logcdf

source
Distributions.logpdf Method
julia
logpdf(
    d::CensoredDistributions.IntervalCensored,
    x::AbstractVector{<:Real}
) -> Any

Compute log probability masses for an array of values using optimised PDF computation.

See also: pdf, logpdf

source
Distributions.logpdf Method
julia
logpdf(
    d::CensoredDistributions.IntervalCensored,
    x::Real
) -> Any

Compute the log probability mass for the interval containing x.

See also: pdf, logcdf

source
Distributions.logpdf Method
julia
logpdf(
    d::CensoredDistributions.PrimaryCensored,
    x::Real
) -> Any

Compute the log probability density function using numerical differentiation of the log CDF.

See also: pdf, logcdf

source
Distributions.logpdf Method
julia
logpdf(
    d::CensoredDistributions.Weighted,
    obs::NamedTuple{(:value, :weight)}
) -> Any

Return the weighted log-probability for joint observations as NamedTuple.

Combines constructor weight with observation weight via multiplication. Expected format: (value = x, weight = w).

See also: pdf

source
Distributions.logpdf Method
julia
logpdf(d::CensoredDistributions.Weighted, x::Real) -> Any

Return the weighted log-probability for scalar observations.

See also: pdf

source
Distributions.logpdf Method
julia
logpdf(
    d::Distributions.Product{<:Distributions.ValueSupport, <:CensoredDistributions.Weighted, <:AbstractVector{<:CensoredDistributions.Weighted}},
    x::AbstractVector{<:Real}
) -> Any

Efficient vectorised log-probability computation for Product{<:ValueSupport, <:Weighted} with vector observations.

See also: logpdf

source
Distributions.logpdf Method
julia
logpdf(
    d::Distributions.Product{<:Distributions.ValueSupport, <:CensoredDistributions.Weighted, <:AbstractVector{<:CensoredDistributions.Weighted}},
    obs::NamedTuple{(:values, :weights)}
) -> Any

Efficient vectorised log-probability computation for Product{<:ValueSupport, <:Weighted} with joint observations.

Handles joint observations and weight stacking. Expected format: (values = [...], weights = [...]).

See also: logpdf

source
Distributions.logpdf Method
julia
logpdf(d::ExponentiallyTilted, x::Real) -> Any

Compute the log probability density function.

See also: pdf, cdf

source
Distributions.pdf Method
julia
pdf(
    d::CensoredDistributions.Convolved,
    x::AbstractVector{<:Real}
) -> Any

Compute densities for a vector of points using a single quadrature solve (the integrand returns a vector).

See also: pdf

source
Distributions.pdf Method
julia
pdf(d::CensoredDistributions.Convolved, x::Real) -> Any

Compute the probability density function.

Uses the exact analytical convolved density where Distributions.convolve applies to all component pairs, otherwise the AD-safe numeric density convolution    .

See also: logpdf

source
Distributions.pdf Method
julia
pdf(
    d::CensoredDistributions.IntervalCensored,
    x::AbstractVector{<:Real}
) -> Any

Compute probability masses for an array of values using optimised vectorisation.

This method collects unique interval boundaries, computes CDFs once, then uses cached values for efficient PDF computation across the array.

See also: pdf, logpdf

source
Distributions.pdf Method
julia
pdf(
    d::CensoredDistributions.IntervalCensored,
    x::Real
) -> Any

Compute the probability mass for the interval containing x.

See also: logpdf, cdf

source
Distributions.pdf Method
julia
pdf(
    d::CensoredDistributions.PrimaryCensored,
    x::Real
) -> Any

Compute the probability density function using numerical differentiation.

See also: logpdf

source
Distributions.pdf Method
julia
pdf(d::CensoredDistributions.Weighted, x::Real) -> Any

Return the probability density from the underlying distribution (unweighted).

See also: logpdf

source
Distributions.pdf Method
julia
pdf(d::ExponentiallyTilted, x::Real) -> Any

Compute the probability density function.

See also: logpdf

source
Distributions.sampler Method
julia
sampler(
    d::CensoredDistributions.Weighted
) -> Union{CensoredDistributions.Weighted{D, Missing} where D<:(Distributions.UnivariateDistribution), CensoredDistributions.Weighted{D, T} where {D<:(Distributions.UnivariateDistribution), T<:Real}}

Create a sampler for efficient sampling (delegates to underlying distribution).

See also: rand

source
Statistics.mean Method
julia
mean(d::ExponentiallyTilted) -> Any

Compute the mean of the distribution.

See also: var, std

source
Statistics.median Method
julia
median(d::ExponentiallyTilted) -> Any

Compute the median of the distribution.

See also: quantile, mean

source
Statistics.quantile Method
julia
quantile(
    d::CensoredDistributions.IntervalCensored,
    p::Real
) -> Any

Compute the quantile using numerical optimization.

The returned quantile respects the interval structure:

  • For regular intervals: quantiles are multiples of the interval width

  • For arbitrary intervals: quantiles correspond to interval boundary values

See also: cdf

source
Statistics.quantile Method
julia
quantile(
    d::CensoredDistributions.PrimaryCensored,
    p::Real
) -> Any

Compute the quantile (inverse CDF) using numerical optimization.

See also: cdf

source
Statistics.quantile Method
julia
quantile(d::CensoredDistributions.Weighted, p::Real) -> Any

Compute the quantile function (delegates to underlying distribution).

See also: cdf

source
Statistics.quantile Method
julia
quantile(d::ExponentiallyTilted, p::Real) -> Any

Compute the quantile function (inverse CDF) using analytical formula.

See also: cdf

source
Statistics.std Method
julia
std(d::ExponentiallyTilted) -> Any

Compute the standard deviation of the distribution.

See also: mean, var

source
Statistics.var Method
julia
var(d::ExponentiallyTilted) -> Any

Compute the variance of the distribution.

See also: mean, std

source
StatsAPI.loglikelihood Method
julia
loglikelihood(
    d::CensoredDistributions.Weighted,
    obs::NamedTuple{(:value, :weight)}
) -> Any

Compute log-likelihood for single Weighted distribution with joint observations.

Handles joint observations as NamedTuple: (value = x, weight = w).

See also: logpdf

source
StatsAPI.loglikelihood Method
julia
loglikelihood(
    d::CensoredDistributions.Weighted,
    obs::NamedTuple{(:values, :weights)}
) -> Any

Compute log-likelihood for single Weighted distribution with vectorized joint observations.

Handles joint observations as NamedTuple: (values = [...], weights = [...]). This is useful when a single weighted distribution is used with multiple observations.

See also: logpdf

source
StatsAPI.loglikelihood Method
julia
loglikelihood(
    d::Distributions.Product{<:Distributions.ValueSupport, <:CensoredDistributions.Weighted, <:AbstractVector{<:CensoredDistributions.Weighted}},
    obs::NamedTuple{(:values, :weights)}
) -> Any

Compute log-likelihood for Product{<:ValueSupport, <:Weighted} with joint observations.

Handles joint observations as NamedTuple: (values = [...], weights = [...]).

See also: logpdf

source