@@ -112,10 +112,96 @@ y = dist.logpmf( 2.3 );
112112<!-- eslint no-undef: "error" -->
113113
114114``` javascript
115- var objectKeys = require ( ' @stdlib/utils/keys' );
115+ var geometricRandomFactory = require ( ' @stdlib/random/base/geometric' ).factory ;
116+ var negativeBinomial = require ( ' @stdlib/stats/base/dists/negative-binomial' );
117+ var filledarrayBy = require ( ' @stdlib/array/filled-by' );
118+ var variance = require ( ' @stdlib/stats/base/variance' );
119+ var linspace = require ( ' @stdlib/array/base/linspace' );
120+ var mean = require ( ' @stdlib/stats/base/mean' );
121+ var abs = require ( ' @stdlib/math/base/special/abs' );
116122var geometric = require ( ' @stdlib/stats/base/dists/geometric' );
117123
118- console .log ( objectKeys ( geometric ) );
124+ // Define the success probability:
125+ var p = 0.3 ; // Probability of success on each trial
126+
127+ // Generate an array of x values (number of failures before first success):
128+ var x = linspace ( 0 , 10 , 11 ); // Geometric distribution is discrete
129+
130+ // Compute the PMF for each x:
131+ var geometricPMF = geometric .pmf .factory ( p );
132+ var pmf = filledarrayBy ( x .length , ' float64' , geometricPMF );
133+
134+ // Compute the CDF for each x:
135+ var geometricCDF = geometric .cdf .factory ( p );
136+ var cdf = filledarrayBy ( x .length , ' float64' , geometricCDF );
137+
138+ // Output the PMF and CDF values:
139+ console .log ( ' x values:' , x );
140+ console .log ( ' PMF values:' , pmf );
141+ console .log ( ' CDF values:' , cdf );
142+
143+ // Compute statistical properties:
144+ var theoreticalMean = geometric .mean ( p );
145+ var theoreticalVariance = geometric .variance ( p );
146+ var theoreticalSkewness = geometric .skewness ( p );
147+ var theoreticalKurtosis = geometric .kurtosis ( p );
148+
149+ console .log ( ' Theoretical Mean:' , theoreticalMean );
150+ console .log ( ' Theoretical Variance:' , theoreticalVariance );
151+ console .log ( ' Skewness:' , theoreticalSkewness );
152+ console .log ( ' Kurtosis:' , theoreticalKurtosis );
153+
154+ // Generate random samples from the geometric distribution:
155+ var rgeom = geometricRandomFactory ( p );
156+ var n = 1000 ;
157+ var samples = filledarrayBy ( n, ' float64' , rgeom );
158+
159+ // Compute sample mean and variance:
160+ var sampleMean = mean ( n, samples, 1 );
161+ var sampleVariance = variance ( n, 1 , samples, 1 );
162+
163+ console .log ( ' Sample Mean:' , sampleMean );
164+ console .log ( ' Sample Variance:' , sampleVariance );
165+
166+ // Demonstrate the memoryless property:
167+ var s = 2.0 ;
168+ var t = 3.0 ;
169+ var prob1 = ( 1.0 - geometric .cdf ( s + t - 1.0 , p ) ) /
170+ ( 1.0 - geometric .cdf ( s - 1.0 , p ));
171+ var prob2 = 1.0 - geometric .cdf ( t - 1.0 , p );
172+
173+ console .log ( ' P(X > s + t | X > s):' , prob1 );
174+ console .log ( ' P(X > t):' , prob2 );
175+ console .log ( ' Difference:' , abs ( prob1 - prob2 ) );
176+
177+ // Demonstrate that the sum of k independent geometric random variables follows a negative binomial distribution:
178+ var k = 5 ;
179+ function drawSum () {
180+ var sum = 0 ;
181+ var j;
182+ for ( j = 0 ; j < k; j++ ) {
183+ sum += rgeom ();
184+ }
185+ return sum;
186+ }
187+ var sumSamples = filledarrayBy ( n, ' float64' , drawSum );
188+
189+ // Compute sample mean and variance for the sum:
190+ var sumSampleMean = mean ( n, sumSamples, 1 );
191+ var sumSampleVariance = variance ( n, 1 , sumSamples, 1 );
192+
193+ // Theoretical mean and variance of Negative Binomial distribution:
194+ var nbMean = negativeBinomial .mean ( k, p );
195+ var nbVariance = negativeBinomial .variance ( k, p );
196+
197+ console .log ( ' Sum Sample Mean:' , sumSampleMean );
198+ console .log ( ' Sum Sample Variance:' , sumSampleVariance );
199+ console .log ( ' Negative Binomial Mean:' , nbMean );
200+ console .log ( ' Negative Binomial Variance:' , nbVariance );
201+
202+ // Compare sample statistics to theoretical values:
203+ console .log ( ' Difference in Mean:' , abs ( nbMean - sumSampleMean ) );
204+ console .log ( ' Difference in Variance:' , abs ( nbVariance - sumSampleVariance ) );
119205```
120206
121207</section >
0 commit comments