Skip to content

Commit f0ab00b

Browse files
docs: improve README examples of stats/base/dists/chi namespace
PR-URL: #1803 Ref: #1618 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent caaf0d9 commit f0ab00b

File tree

2 files changed

+160
-6
lines changed

2 files changed

+160
-6
lines changed

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

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,91 @@ var mu = dist.mean;
101101

102102
## Examples
103103

104-
<!-- TODO: better examples -->
105-
106104
<!-- eslint no-undef: "error" -->
107105

108106
```javascript
109-
var objectKeys = require( '@stdlib/utils/keys' );
107+
var chiRandomFactory = require( '@stdlib/random/base/chi' ).factory;
108+
var filledarrayBy = require( '@stdlib/array/filled-by' );
109+
var variance = require( '@stdlib/stats/base/variance' );
110+
var linspace = require( '@stdlib/array/base/linspace' );
111+
var rayleigh = require( '@stdlib/stats/base/dists/rayleigh' );
112+
var mean = require( '@stdlib/stats/base/mean' );
113+
var abs = require( '@stdlib/math/base/special/abs' );
110114
var chi = require( '@stdlib/stats/base/dists/chi' );
111115

112-
console.log( objectKeys( chi ) );
116+
// Define the degrees of freedom parameter:
117+
var k = 2;
118+
119+
// Generate an array of x values:
120+
var x = linspace( 0, 10, 100 );
121+
122+
// Compute the PDF for each x:
123+
var chiPDF = chi.pdf.factory( k );
124+
var pdf = filledarrayBy( x.length, 'float64', chiPDF );
125+
126+
// Compute the CDF for each x:
127+
var chiCDF = chi.cdf.factory( k );
128+
var cdf = filledarrayBy( x.length, 'float64', chiCDF );
129+
130+
// Output the PDF and CDF values:
131+
console.log( 'x values:', x );
132+
console.log( 'PDF values:', pdf );
133+
console.log( 'CDF values:', cdf );
134+
135+
// Compute statistical properties:
136+
var theoreticalMean = chi.mean( k );
137+
var theoreticalVariance = chi.variance( k );
138+
var theoreticalSkewness = chi.skewness( k );
139+
var theoreticalKurtosis = chi.kurtosis( k );
140+
141+
console.log( 'Theoretical Mean:', theoreticalMean );
142+
console.log( 'Theoretical Variance:', theoreticalVariance );
143+
console.log( 'Skewness:', theoreticalSkewness );
144+
console.log( 'Kurtosis:', theoreticalKurtosis );
145+
146+
// Generate random samples from the Chi distribution:
147+
var rchi = chiRandomFactory( k );
148+
var n = 1000;
149+
var samples = filledarrayBy( n, 'float64', rchi );
150+
151+
// Compute sample mean and variance:
152+
var sampleMean = mean( n, samples, 1 );
153+
var sampleVariance = variance( n, 1, samples, 1 );
154+
155+
console.log( 'Sample Mean:', sampleMean );
156+
console.log( 'Sample Variance:', sampleVariance );
157+
158+
// Compare sample statistics to theoretical values:
159+
console.log( 'Difference in Mean:', abs( theoreticalMean - sampleMean ) );
160+
console.log( 'Difference in Variance:', abs( theoreticalVariance - sampleVariance ) );
161+
162+
// Demonstrate the relationship with the Rayleigh distribution when k=2:
163+
var rayleighPDF = rayleigh.pdf.factory( 1.0 );
164+
var rayleighCDF = rayleigh.cdf.factory( 1.0 );
165+
166+
// Compute Rayleigh PDF and CDF for each x:
167+
var rayleighPDFValues = filledarrayBy( x.length, 'float64', rayleighPDF );
168+
169+
var rayleighCDFValues = filledarrayBy( x.length, 'float64', rayleighCDF );
170+
171+
// Compare Chi and Rayleigh PDFs and CDFs:
172+
var maxDiffPDF = 0.0;
173+
var maxDiffCDF = 0.0;
174+
var diffPDF;
175+
var diffCDF;
176+
var i;
177+
for ( i = 0; i < x.length; i++ ) {
178+
diffPDF = abs( pdf[ i ] - rayleighPDFValues[ i ] );
179+
if ( diffPDF > maxDiffPDF ) {
180+
maxDiffPDF = diffPDF;
181+
}
182+
diffCDF = abs( cdf[ i ] - rayleighCDFValues[ i ] );
183+
if ( diffCDF > maxDiffCDF ) {
184+
maxDiffCDF = diffCDF;
185+
}
186+
}
187+
console.log( 'Maximum difference between Chi(k=2) PDF and Rayleigh PDF:', maxDiffPDF );
188+
console.log( 'Maximum difference between Chi(k=2) CDF and Rayleigh CDF:', maxDiffCDF );
113189
```
114190

115191
</section>

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

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

1919
'use strict';
2020

21-
var objectKeys = require( '@stdlib/utils/keys' );
21+
var chiRandomFactory = require( '@stdlib/random/base/chi' ).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 rayleigh = require( '@stdlib/stats/base/dists/rayleigh' );
26+
var mean = require( '@stdlib/stats/base/mean' );
27+
var abs = require( '@stdlib/math/base/special/abs' );
2228
var chi = require( './../lib' );
2329

24-
console.log( objectKeys( chi ) );
30+
// Define the degrees of freedom parameter:
31+
var k = 2;
32+
33+
// Generate an array of x values:
34+
var x = linspace( 0, 10, 100 );
35+
36+
// Compute the PDF for each x:
37+
var chiPDF = chi.pdf.factory( k );
38+
var pdf = filledarrayBy( x.length, 'float64', chiPDF );
39+
40+
// Compute the CDF for each x:
41+
var chiCDF = chi.cdf.factory( k );
42+
var cdf = filledarrayBy( x.length, 'float64', chiCDF );
43+
44+
// Output the PDF and CDF values:
45+
console.log( 'x values:', x );
46+
console.log( 'PDF values:', pdf );
47+
console.log( 'CDF values:', cdf );
48+
49+
// Compute statistical properties:
50+
var theoreticalMean = chi.mean( k );
51+
var theoreticalVariance = chi.variance( k );
52+
var theoreticalSkewness = chi.skewness( k );
53+
var theoreticalKurtosis = chi.kurtosis( k );
54+
55+
console.log( 'Theoretical Mean:', theoreticalMean );
56+
console.log( 'Theoretical Variance:', theoreticalVariance );
57+
console.log( 'Skewness:', theoreticalSkewness );
58+
console.log( 'Kurtosis:', theoreticalKurtosis );
59+
60+
// Generate random samples from the Chi distribution:
61+
var rchi = chiRandomFactory( k );
62+
var n = 1000;
63+
var samples = filledarrayBy( n, 'float64', rchi );
64+
65+
// Compute sample mean and variance:
66+
var sampleMean = mean( n, samples, 1 );
67+
var sampleVariance = variance( n, 1, samples, 1 );
68+
69+
console.log( 'Sample Mean:', sampleMean );
70+
console.log( 'Sample Variance:', sampleVariance );
71+
72+
// Compare sample statistics to theoretical values:
73+
console.log( 'Difference in Mean:', abs( theoreticalMean - sampleMean ) );
74+
console.log( 'Difference in Variance:', abs( theoreticalVariance - sampleVariance ) );
75+
76+
// Demonstrate the relationship with the Rayleigh distribution when k=2:
77+
var rayleighPDF = rayleigh.pdf.factory( 1.0 );
78+
var rayleighCDF = rayleigh.cdf.factory( 1.0 );
79+
80+
// Compute Rayleigh PDF and CDF for each x:
81+
var rayleighPDFValues = filledarrayBy( x.length, 'float64', rayleighPDF );
82+
83+
var rayleighCDFValues = filledarrayBy( x.length, 'float64', rayleighCDF );
84+
85+
// Compare Chi and Rayleigh PDFs and CDFs:
86+
var maxDiffPDF = 0.0;
87+
var maxDiffCDF = 0.0;
88+
var diffPDF;
89+
var diffCDF;
90+
var i;
91+
for ( i = 0; i < x.length; i++ ) {
92+
diffPDF = abs( pdf[ i ] - rayleighPDFValues[ i ] );
93+
if ( diffPDF > maxDiffPDF ) {
94+
maxDiffPDF = diffPDF;
95+
}
96+
diffCDF = abs( cdf[ i ] - rayleighCDFValues[ i ] );
97+
if ( diffCDF > maxDiffCDF ) {
98+
maxDiffCDF = diffCDF;
99+
}
100+
}
101+
console.log( 'Maximum difference between Chi(k=2) PDF and Rayleigh PDF:', maxDiffPDF );
102+
console.log( 'Maximum difference between Chi(k=2) CDF and Rayleigh CDF:', maxDiffCDF );

0 commit comments

Comments
 (0)