A SAS programmer asked how to implement the generalized inverse Gaussian (GIG) distribution in SAS. The GIG distribution is used as a conjugate prior for the variance of a normal distribution, so it appears in some Bayesian models. As its name implies, the generalized inverse Gaussian distribution includes the inverse Gaussian (Wald) distribution for special values of its parameters. The gamma and inverse gamma distributions are asymptotically related distributions.
The Wikipedia article about the GIG distribution provides a formula for the probability density function of the GIG distribution. This article shows how to implement that formula in SAS. In addition, this article shows how to implement the log-PDF function, which is used in log-likelihood computations.
The PDF of the generalized inverse Gaussian distribution
The generalized inverse Gaussian distribution, GIG(x; a, b, p),
specifies the density of a positive random variable, X. The distribution has three parameters: a > 0, b > 0, and a real value p. The formula for the PDF is
\(
f(y; a, b, p) = \frac{(a/b)^{p/2}}{2 K_p(\sqrt{ab})} y^{p-1} \exp\left( -\frac{1}{2}\left(ay + \frac{b}{y}\right) \right)
\)
where Kp is the modified Bessel function of the second kind. The roles of the three parameters are as follows:
- a > 0 is a rate parameter. It controls the rate of decay for the right tail of the distribution as x → ∞.
- b > 0 is a parameter that controls the rate of decay for the left tail of the distribution near x=0.
- p is a real index parameter. It is the "generalization" parameter that links the GIG to other distributions. Most notably, if p = -1/2, the GIG becomes the ordinary inverse Gaussian distribution. If p > 0, then in the limit as b → 0, the GIG approaches the gamma distribution. If p < 0, then as a → 0, the GIG approaches the inverse gamma distribution.
It is sometimes useful to compute the log-PDF, which is
log f(y; a, b, p) = (p/2) log(a/b) - log(2) - log(Kp(sqrt(a*b))) + (p-1) log(y) - (1/2)(a*y + b/y)
where log indicates the natural logarithm.
The log-PDF is useful if you want to compute the log-likelihood. The log-likelihood function is simply the sum of the log-PDF for each point in a data sample:
LL = Σi log( f(yi; a, b, p) ),
where {y1, y2, ..., yn} is a sample of data.
Not only does the modified Bessel function, Kp, appear in the definition of the PDF, but the Bessel function also is used in formulas for the mean and variance of the distribution. See Wikipedia for the formulas.
Implement the PDF of the GIG distribution in SAS
The GIG PDF requires evaluating the modified Bessel function of the second kind, which is not natively supported in SAS. However, a previous article shows how to evaluate the modified Bessel function in SAS IML software by numerically evaluating an integral. The following program assumes that you have defined and stored the BesselK function from the previous blog post. You can then implement the log-PDF. For the full PDF, exponentiate the log-PDF, as follows:
/* Before running this program, STORE the BesselK function and helper functions from
https://blogs.sas.com/content/iml/2026/10/12/besselk-function-sas.html
*/
proc iml;
load module=_all_; /* load BesselK function */
/* Log-PDF for the generalized inverse Gaussian distribution
evaluated at a column vector of values x, x[i] > 0 for all i */
start logPDF_GIG(x, a, b, p);
n = nrow(x);
logPDF = j(n, 1, .);
/* calculate log(BesselK): https://blogs.sas.com/content/iml/2026/10/12/besselk-function-sas.html */
sqrt_ab = sqrt(a * b);
exp_K = BesselK(p, sqrt_ab, 1); /* returns exp(y)*K_p(y) for y=sqrt(a*b) */
log_K = log(exp_K) - sqrt_ab;
C = (p/2)*log(a/b) - log(2) - log_K; /* normalizing constant */
i = loc(x > 0);
if ncol(i)>0 then
logPDF[i] = C + (p-1)*log(x[i]) - 0.5*(a*x[i] + b/x[i]);
return(logPDF);
finish;
/* PDF for the generalized inverse Gaussian distribution
evaluated at a column vector of values x, x[i] > 0 for all i */
start PDF_GIG(x, a, b, p);
logPDF = logPDF_GIG(x, a, b, p);
return( exp(logPDF) );
finish;
/* test the functions. Visualize the PDF */
a = 1.4;
b = 2.7;
p = -0.5;
x = T(do(0, 3, 0.01));
f = PDF_GIG(x, a, b, p);
title "PDF of Generalized Inverse Gaussian Distribution";
title2 "(a,b,p) = (1.4, 2.7, -0.5)";
call series(x, f) grid={x y};
As mentioned previously, when p = -1/2, the generalized inverse Gaussian distribution reduces to the ordinary inverse Gaussian, so this curve shape might look familiar to some readers. You can vary the values of the p parameter to see how the curve changes for p = 0, 1, and 2. As mentioned earlier, the a parameter controls the rate of decay of the right tail; the b parameter controls the decay of the left side of the PDF.
Summary
This short article shows how to use SAS IML to evaluate the PDF and log-PDF of the generalized inverse Gaussian (GIG) distribution. The distribution is defined in terms of the modified Bessel function of the second kind. I have not used the GIG distribution myself, but various internet sources claim that it is a flexible distribution that is used to model strictly positive data and to model variance parameters in Bayesian models.