Wednesday, March 12, 2014

Compute Inverse Gamma PDF and CDF in MATLAB

Although MATLAB does not have built-in functions for the PDF and CDF of the inverse gamma distribution, the two functions can be implemented in MATLAB easily using the known formula.

PDF

The PDF of the inverse gamma distribution for a random variable (RV) x is
a: shape parameter
b: scale parameter
Γ(•): gamma function.

The gamma function can be computed in MATLAB using the gamma function.

The above PDF formula can be implemented as

function [ Y ] = inversegampdf( X,A,B )
%inversegampdf Inverse gamma probability density function.
%   Y = inversegampdf(X,A,B) returns the inverse gamma probability density
%   function with shape and scale parameters A and B, respectively, at the
%   values in X. The size of Y is the common size of the input arguments. A
%   scalar input functions is a constant matrix of the same size as the
%   other inputs.

Y = B^A/gamma(A)*X.^(-A-1).*exp(-B./X);

end

Examples of the results of the above function are shown in this figure:


You can compare the results with those on Wikipedia.

CDF

The CDF of the inverse gamma distribution for a random variable (RV) x is
a: shape parameter
b: scale parameter.

The numerator is the upper incomplete gamma function, which in MATLAB can be computed using the gammainc function.

The above CDF formula can be implemented in MATLAB as

function [ P ] = inversegamcdf( X,A,B )
%inversegamcdf Inverse gamma cumulative distribution function.
%   Y = inversegamcdf(X,A,B) returns the inverse gamma cumulative
%   distribution function with shape and scale parameters A and B,
%   respectively, at the values in X. The size of P is the common size of
%   the input arguments. A scalar input functions is a constant matrix of
%   the same size as the other inputs.

P = gammainc(B./X,A,'upper');

end

Examples of the results of the above function are shown in this figure:


You can compare the results with those on Wikipedia.

No comments:

Post a Comment