Skip to content

Commit 6c3b249

Browse files
docs: improve README examples of stats/base/dists/invgamma namespace
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]>
1 parent 656fde0 commit 6c3b249

File tree

2 files changed

+124
-4
lines changed

2 files changed

+124
-4
lines changed

lib/node_modules/@stdlib/stats/base/dists/invgamma/README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,70 @@ var y = dist.cdf( 0.5 );
106106
<!-- eslint no-undef: "error" -->
107107

108108
```javascript
109-
var objectKeys = require( '@stdlib/utils/keys' );
109+
var invgammaRandomFactory = require( '@stdlib/random/base/invgamma' ).factory;
110+
var filledarrayBy = require( '@stdlib/array/filled-by' );
111+
var variance = require( '@stdlib/stats/base/variance' );
112+
var linspace = require( '@stdlib/array/base/linspace' );
113+
var gamma = require( '@stdlib/stats/base/dists/gamma' );
114+
var mean = require( '@stdlib/stats/base/mean' );
115+
var abs = require( '@stdlib/math/base/special/abs' );
110116
var invgamma = require( '@stdlib/stats/base/dists/invgamma' );
111117

112-
console.log( objectKeys( invgamma ) );
118+
// Define the shape and scale parameters:
119+
var alpha = 5.0; // shape parameter (α)
120+
var beta = 1.0; // scale parameter (β)
121+
122+
// Generate an array of x values:
123+
var x = linspace( 0.01, 3.0, 100 );
124+
125+
// Compute the PDF for each x:
126+
var invgammaPDF = invgamma.pdf.factory( alpha, beta );
127+
var pdf = filledarrayBy( x.length, 'float64', invgammaPDF );
128+
129+
// Compute the CDF for each x:
130+
var invgammaCDF = invgamma.cdf.factory( alpha, beta );
131+
var cdf = filledarrayBy( x.length, 'float64', invgammaCDF );
132+
133+
// Output the PDF and CDF values:
134+
console.log( 'x values:', x );
135+
console.log( 'PDF values:', pdf );
136+
console.log( 'CDF values:', cdf );
137+
138+
// Compute statistical properties:
139+
var theoreticalMean = invgamma.mean( alpha, beta );
140+
var theoreticalVariance = invgamma.variance( alpha, beta );
141+
var theoreticalSkewness = invgamma.skewness( alpha, beta );
142+
var theoreticalKurtosis = invgamma.kurtosis( alpha, beta );
143+
144+
console.log( 'Theoretical Mean:', theoreticalMean );
145+
console.log( 'Theoretical Variance:', theoreticalVariance );
146+
console.log( 'Skewness:', theoreticalSkewness );
147+
console.log( 'Kurtosis:', theoreticalKurtosis );
148+
149+
// Generate random samples from the inverse gamma distribution:
150+
var rinvGamma = invgammaRandomFactory( alpha, beta );
151+
var n = 1000;
152+
var samples = filledarrayBy( n, 'float64', rinvGamma );
153+
154+
// Compute sample mean and variance:
155+
var sampleMean = mean( n, samples, 1 );
156+
var sampleVariance = variance( n, 1, samples, 1 );
157+
158+
console.log( 'Sample Mean:', sampleMean );
159+
console.log( 'Sample Variance:', sampleVariance );
160+
161+
// Compare sample statistics to theoretical values:
162+
console.log( 'Difference in Mean:', abs( mean - sampleMean ) );
163+
console.log( 'Difference in Variance:', abs( variance - sampleVariance ) );
164+
165+
// Demonstrate the relationship between inverse gamma and gamma distributions:
166+
var y = 0.5;
167+
var invGammaCDF = invgamma.cdf( y, alpha, beta );
168+
var gammaCDF = 1.0 - gamma.cdf( 1.0 / y, alpha, 1.0 / beta );
169+
170+
console.log( 'Inverse Gamma CDF at y =', y, ':', invGammaCDF );
171+
console.log( '1 - Gamma CDF at 1/y =', 1 / y, ':', gammaCDF );
172+
console.log( 'Difference:', abs( invGammaCDF - gammaCDF ) );
113173
```
114174

115175
</section>

lib/node_modules/@stdlib/stats/base/dists/invgamma/examples/index.js

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,67 @@
1818

1919
'use strict';
2020

21-
var objectKeys = require( '@stdlib/utils/keys' );
21+
var invgammaRandomFactory = require( '@stdlib/random/base/invgamma' ).factory;
22+
var filledarrayBy = require( '@stdlib/array/filled-by' );
23+
var variance = require( '@stdlib/stats/base/variance' );
24+
var linspace = require( '@stdlib/array/base/linspace' );
25+
var gamma = require( '@stdlib/stats/base/dists/gamma' );
26+
var mean = require( '@stdlib/stats/base/mean' );
27+
var abs = require( '@stdlib/math/base/special/abs' );
2228
var invgamma = require( './../lib' );
2329

24-
console.log( objectKeys( invgamma ) );
30+
// Define the shape and scale parameters:
31+
var alpha = 5.0; // shape parameter (α)
32+
var beta = 1.0; // scale parameter (β)
33+
34+
// Generate an array of x values:
35+
var x = linspace( 0.01, 3.0, 100 );
36+
37+
// Compute the PDF for each x:
38+
var invgammaPDF = invgamma.pdf.factory( alpha, beta );
39+
var pdf = filledarrayBy( x.length, 'float64', invgammaPDF );
40+
41+
// Compute the CDF for each x:
42+
var invgammaCDF = invgamma.cdf.factory( alpha, beta );
43+
var cdf = filledarrayBy( x.length, 'float64', invgammaCDF );
44+
45+
// Output the PDF and CDF values:
46+
console.log( 'x values:', x );
47+
console.log( 'PDF values:', pdf );
48+
console.log( 'CDF values:', cdf );
49+
50+
// Compute statistical properties:
51+
var theoreticalMean = invgamma.mean( alpha, beta );
52+
var theoreticalVariance = invgamma.variance( alpha, beta );
53+
var theoreticalSkewness = invgamma.skewness( alpha, beta );
54+
var theoreticalKurtosis = invgamma.kurtosis( alpha, beta );
55+
56+
console.log( 'Theoretical Mean:', theoreticalMean );
57+
console.log( 'Theoretical Variance:', theoreticalVariance );
58+
console.log( 'Skewness:', theoreticalSkewness );
59+
console.log( 'Kurtosis:', theoreticalKurtosis );
60+
61+
// Generate random samples from the inverse gamma distribution:
62+
var rinvGamma = invgammaRandomFactory( alpha, beta );
63+
var n = 1000;
64+
var samples = filledarrayBy( n, 'float64', rinvGamma );
65+
66+
// Compute sample mean and variance:
67+
var sampleMean = mean( n, samples, 1 );
68+
var sampleVariance = variance( n, 1, samples, 1 );
69+
70+
console.log( 'Sample Mean:', sampleMean );
71+
console.log( 'Sample Variance:', sampleVariance );
72+
73+
// Compare sample statistics to theoretical values:
74+
console.log( 'Difference in Mean:', abs( mean - sampleMean ) );
75+
console.log( 'Difference in Variance:', abs( variance - sampleVariance ) );
76+
77+
// Demonstrate the relationship between inverse gamma and gamma distributions:
78+
var y = 0.5;
79+
var invGammaCDF = invgamma.cdf( y, alpha, beta );
80+
var gammaCDF = 1.0 - gamma.cdf( 1.0 / y, alpha, 1.0 / beta );
81+
82+
console.log( 'Inverse Gamma CDF at y =', y, ':', invGammaCDF );
83+
console.log( '1 - Gamma CDF at 1/y =', 1 / y, ':', gammaCDF );
84+
console.log( 'Difference:', abs( invGammaCDF - gammaCDF ) );

0 commit comments

Comments
 (0)