Skip to content

Commit

Permalink
docs: improve README examples of stats/base/dists/invgamma namespace
Browse files Browse the repository at this point in the history
PR-URL: #1808
Ref: #1631

Co-authored-by: Philipp Burckhardt <[email protected]>
Co-authored-by: shivam Ahir <[email protected]>
Reviewed-by: Philipp Burckhardt <[email protected]>
  • Loading branch information
ShivamAhir and Planeshifter authored Oct 10, 2024
1 parent 656fde0 commit 6c3b249
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 4 deletions.
64 changes: 62 additions & 2 deletions lib/node_modules/@stdlib/stats/base/dists/invgamma/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,70 @@ var y = dist.cdf( 0.5 );
<!-- eslint no-undef: "error" -->

```javascript
var objectKeys = require( '@stdlib/utils/keys' );
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' );

console.log( objectKeys( 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:', x );

This comment has been minimized.

Copy link
@kgryte

kgryte Oct 16, 2024

Member

@Planeshifter I believe you want a space after the colon here and below.

console.log( 'PDF values:', pdf );
console.log( 'CDF values:', 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:', theoreticalMean );
console.log( 'Theoretical Variance:', theoreticalVariance );
console.log( 'Skewness:', theoreticalSkewness );
console.log( 'Kurtosis:', 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:', sampleMean );
console.log( 'Sample Variance:', sampleVariance );

// Compare sample statistics to theoretical values:
console.log( 'Difference in Mean:', abs( mean - sampleMean ) );
console.log( 'Difference in Variance:', abs( variance - 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 =', y, ':', invGammaCDF );

This comment has been minimized.

Copy link
@kgryte

kgryte Oct 16, 2024

Member

Same comment here.

console.log( '1 - Gamma CDF at 1/y =', 1 / y, ':', gammaCDF );
console.log( 'Difference:', abs( invGammaCDF - gammaCDF ) );
```

</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,67 @@

'use strict';

var objectKeys = require( '@stdlib/utils/keys' );
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( './../lib' );

console.log( objectKeys( 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:', x );

This comment has been minimized.

Copy link
@kgryte

kgryte Oct 16, 2024

Member

@Planeshifter Same comments as in README.

console.log( 'PDF values:', pdf );
console.log( 'CDF values:', 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:', theoreticalMean );
console.log( 'Theoretical Variance:', theoreticalVariance );
console.log( 'Skewness:', theoreticalSkewness );
console.log( 'Kurtosis:', 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:', sampleMean );
console.log( 'Sample Variance:', sampleVariance );

// Compare sample statistics to theoretical values:
console.log( 'Difference in Mean:', abs( mean - sampleMean ) );
console.log( 'Difference in Variance:', abs( variance - 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 =', y, ':', invGammaCDF );
console.log( '1 - Gamma CDF at 1/y =', 1 / y, ':', gammaCDF );
console.log( 'Difference:', abs( invGammaCDF - gammaCDF ) );

1 comment on commit 6c3b249

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
stats/base/dists/invgamma $\color{green}150/150$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}0/0$
$\color{green}+100.00\%$
$\color{green}150/150$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.