Inverse Gamma

Inverse gamma distribution.

Usage

var invgamma = require( '@stdlib/stats/base/dists/invgamma' );

invgamma

Inverse gamma distribution.

var dist = invgamma;
// returns {...}

The namespace contains the following distribution functions:

The namespace contains the following functions for calculating distribution properties:

The namespace contains a constructor function for creating a inverse gamma distribution object.

var InvGamma = require( '@stdlib/stats/base/dists/invgamma' ).InvGamma;

var dist = new InvGamma( 2.0, 4.0 );

var y = dist.cdf( 0.5 );
// returns ~0.003

Examples

var invgammaRandomFactory = require( '@stdlib/random/base/invgamma' ).factory;
var filledarrayBy = require( '@stdlib/array/filled-by' );
var variance = require( '@stdlib/stats/base/variance' );
var linspace = require( '@stdlib/array/base/linspace' );
var gamma = require( '@stdlib/stats/base/dists/gamma' );
var mean = require( '@stdlib/stats/base/mean' );
var abs = require( '@stdlib/math/base/special/abs' );
var invgamma = require( '@stdlib/stats/base/dists/invgamma' );

// Define the shape and scale parameters:
var alpha = 5.0; // shape parameter (α)
var beta = 1.0;  // scale parameter (β)

// Generate an array of x values:
var x = linspace( 0.01, 3.0, 100 );

// Compute the PDF for each x:
var invgammaPDF = invgamma.pdf.factory( alpha, beta );
var pdf = filledarrayBy( x.length, 'float64', invgammaPDF );

// Compute the CDF for each x:
var invgammaCDF = invgamma.cdf.factory( alpha, beta );
var cdf = filledarrayBy( x.length, 'float64', invgammaCDF );

// Output the PDF and CDF values:
console.log( 'x values: %s', x );
console.log( 'PDF values: %s', pdf );
console.log( 'CDF values: %s', cdf );

// Compute statistical properties:
var theoreticalMean = invgamma.mean( alpha, beta );
var theoreticalVariance = invgamma.variance( alpha, beta );
var theoreticalSkewness = invgamma.skewness( alpha, beta );
var theoreticalKurtosis = invgamma.kurtosis( alpha, beta );

console.log( 'Theoretical Mean: %s', theoreticalMean );
console.log( 'Theoretical Variance: %s', theoreticalVariance );
console.log( 'Skewness: %s', theoreticalSkewness );
console.log( 'Kurtosis: %s', theoreticalKurtosis );

// Generate random samples from the inverse gamma distribution:
var rinvGamma = invgammaRandomFactory( alpha, beta );
var n = 1000;
var samples = filledarrayBy( n, 'float64', rinvGamma );

// Compute sample mean and variance:
var sampleMean = mean( n, samples, 1 );
var sampleVariance = variance( n, 1, samples, 1 );

console.log( 'Sample Mean: %s', sampleMean );
console.log( 'Sample Variance: %s', sampleVariance );

// Compare sample statistics to theoretical values:
console.log( 'Difference in Mean: %s', abs( theoreticalMean - sampleMean ) );
console.log( 'Difference in Variance: %s', abs( theoreticalVariance - sampleVariance ) );

// Demonstrate the relationship between inverse gamma and gamma distributions:
var y = 0.5;
var invGammaCDF = invgamma.cdf( y, alpha, beta );
var gammaCDF = 1.0 - gamma.cdf( 1.0 / y, alpha, 1.0 / beta );

console.log( 'Inverse Gamma CDF at y = %s: %s', y, invGammaCDF );
console.log( '1 - Gamma CDF at 1/y = %s: %s', 1 / y, gammaCDF );
console.log( 'Difference: %s', abs( invGammaCDF - gammaCDF ) );
Did you find this page helpful?