About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Inverse gamma distribution.
npm install @stdlib/stats-base-dists-invgamma
Alternatively,
- To load the package in a website via a
script
tag without installation and bundlers, use the ES Module available on theesm
branch (see README). - If you are using Deno, visit the
deno
branch (see README for usage intructions). - For use in Observable, or in browser/node environments, use the Universal Module Definition (UMD) build available on the
umd
branch (see README).
The branches.md file summarizes the available branches and displays a diagram illustrating their relationships.
To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
var invgamma = require( '@stdlib/stats-base-dists-invgamma' );
Inverse gamma distribution.
var dist = invgamma;
// returns {...}
The namespace contains the following distribution functions:
cdf( x, alpha, beta )
: inverse Gamma distribution cumulative distribution function.logpdf( x, alpha, beta )
: evaluate the natural logarithm of the probability density function (PDF) for an inverse gamma distribution.pdf( x, alpha, beta )
: inverse gamma distribution probability density function (PDF).quantile( p, alpha, beta )
: inverse gamma distribution quantile function.
The namespace contains the following functions for calculating distribution properties:
entropy( alpha, beta )
: inverse gamma distribution differential entropy.kurtosis( alpha, beta )
: inverse gamma distribution excess kurtosis.mean( alpha, beta )
: inverse gamma distribution expected value.mode( alpha, beta )
: inverse gamma distribution mode.skewness( alpha, beta )
: inverse gamma distribution skewness.stdev( alpha, beta )
: inverse gamma distribution standard deviation.variance( alpha, beta )
: inverse gamma distribution variance.
The namespace contains a constructor function for creating a inverse gamma distribution object.
InvGamma( [alpha, beta] )
: inverse gamma distribution constructor.
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
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 ) );
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
See LICENSE.
Copyright © 2016-2025. The Stdlib Authors.