Gamma_ct: The Central Gamma Distribution

Gamma_ctR Documentation

The Central Gamma Distribution

Description

Distribution function and random generation for the center (between a lower and an upper bound) of the Gamma distribution with shape and rate parameters. These functions provide numerically stable evaluation and sampling when the truncation interval is narrow or when the Gamma density is highly skewed.

Usage

rgamma_ct(n, shape, rate, lower_prec = NULL, upper_prec = NULL)

Arguments

n

Number of draws to generate. If length(n) > 1, the length is taken to be the number required.

shape

Shape parameter of the Gamma distribution.

rate

Rate parameter of the Gamma distribution.

lower_prec

Lower truncation point on the precision scale. If NULL, no lower truncation is applied.

upper_prec

Upper truncation point on the precision scale. If NULL, no upper truncation is applied.

Details

The function pgamma_ct computes the probability mass between a lower bound a and an upper bound b under a Gamma density with the specified shape and rate parameters. This is particularly useful when the interval b - a is small, where the naive computation pgamma(b) - pgamma(a) may underflow to zero even when the true probability is positive.

The function ctrgamma provides a numerically robust sampler for the Gamma distribution under one-sided or two-sided truncation. It handles:

  • no truncation (reducing to rgamma)

  • lower truncation only

  • upper truncation only

  • two-sided truncation with lower_prec < upper_prec

  • exact degeneracy when the truncation interval collapses

  • numerical degeneracy when the Gamma CDF collapses in floating point

All computations are performed on the log scale using stable log–CDF and log–sum–exp transformations. This avoids the catastrophic cancellation that occurs when the Gamma CDF values at the truncation points are extremely close.

These functions are primarily intended for use in hierarchical Bayesian models where precision parameters are updated under tight truncation constraints, and where numerical stability is essential for reliable sampling performance. They are used in envelope-based dispersion sampling \insertCiteNygren2006glmbayes.

Value

For pgamma_ct, a vector of probabilities corresponding to the mass of the Gamma distribution between a and b. For rgamma_ct or ctrgamma, a vector of length nn containing random draws from the Gamma distribution restricted to the interval [a, b] (or [lower_prec, upper_prec] on the precision scale).

References

\insertAllCited

See Also

Normal_ct, InvGamma_ct, EnvelopeDispersionBuild

Examples

############################### Start of Gamma_ct example ####################

## Basic usage: rgamma_ct samples from Gamma truncated to [lower_prec, upper_prec]
shape <- 2
rate  <- 1
lower <- 0.999
upper <- 1.001

## Naive pgamma(b) - pgamma(a) can underflow when a and b are very close
pgamma(upper, shape, rate) - pgamma(lower, shape, rate)

## rgamma_ct samples correctly from the narrow interval
set.seed(42)
x <- rgamma_ct(100, shape, rate, lower_prec = lower, upper_prec = upper)
range(x)
mean(x)

## Example where difference between two pgamma calls fails (catastrophic cancellation)
## but rgamma_ct still samples correctly from the narrow interval
a <- 1.0
b <- 1.0 + 1e-14
pgamma(b, 2, 1) - pgamma(a, 2, 1)

## Stable computation via log-space (as used inside rgamma_ct)
log_F_a <- pgamma(a, 2, 1, log.p = TRUE)
log_F_b <- pgamma(b, 2, 1, log.p = TRUE)
exp(log_F_b + log(-expm1(log_F_a - log_F_b)))

## rgamma_ct samples from [a, b] even when the interval is extremely narrow
set.seed(123)
rgamma_ct(5, 2, 1, lower_prec = a, upper_prec = b)

###############################################################################
## End of Gamma_ct example
###############################################################################

glmbayes documentation built on Aug. 5, 2026, 1:07 a.m.

Related to Gamma_ct in glmbayes...