| Gamma_ct | R Documentation |
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.
rgamma_ct(n, shape, rate, lower_prec = NULL, upper_prec = NULL)
n |
Number of draws to generate. If |
shape |
Shape parameter of the Gamma distribution. |
rate |
Rate parameter of the Gamma distribution. |
lower_prec |
Lower truncation point on the precision scale. If
|
upper_prec |
Upper truncation point on the precision scale. If
|
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.
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).
Normal_ct, InvGamma_ct, EnvelopeDispersionBuild
############################### 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
###############################################################################
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.