Skip to content

Commit

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

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 11, 2024
1 parent 4a94497 commit caaf0d9
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 4 deletions.
90 changes: 88 additions & 2 deletions lib/node_modules/@stdlib/stats/base/dists/geometric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,96 @@ y = dist.logpmf( 2.3 );
<!-- eslint no-undef: "error" -->

```javascript
var objectKeys = require( '@stdlib/utils/keys' );
var geometricRandomFactory = require( '@stdlib/random/base/geometric' ).factory;
var negativeBinomial = require( '@stdlib/stats/base/dists/negative-binomial' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var variance = require( '@stdlib/stats/base/variance' );
var linspace = require( '@stdlib/array/base/linspace' );
var mean = require( '@stdlib/stats/base/mean' );
var abs = require( '@stdlib/math/base/special/abs' );
var geometric = require( '@stdlib/stats/base/dists/geometric' );

console.log( objectKeys( geometric ) );
// Define the success probability:
var p = 0.3; // Probability of success on each trial

// Generate an array of x values (number of failures before first success):
var x = linspace( 0, 10, 11 ); // Geometric distribution is discrete

// Compute the PMF for each x:
var geometricPMF = geometric.pmf.factory( p );
var pmf = filledarrayBy( x.length, 'float64', geometricPMF );

// Compute the CDF for each x:
var geometricCDF = geometric.cdf.factory( p );
var cdf = filledarrayBy( x.length, 'float64', geometricCDF );

// Output the PMF and CDF values:
console.log( 'x values:', x );
console.log( 'PMF values:', pmf );
console.log( 'CDF values:', cdf );

// Compute statistical properties:
var theoreticalMean = geometric.mean( p );
var theoreticalVariance = geometric.variance( p );
var theoreticalSkewness = geometric.skewness( p );
var theoreticalKurtosis = geometric.kurtosis( p );

console.log( 'Theoretical Mean:', theoreticalMean );
console.log( 'Theoretical Variance:', theoreticalVariance );
console.log( 'Skewness:', theoreticalSkewness );
console.log( 'Kurtosis:', theoreticalKurtosis );

// Generate random samples from the geometric distribution:
var rgeom = geometricRandomFactory( p );
var n = 1000;
var samples = filledarrayBy( n, 'float64', rgeom );

// 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 );

// Demonstrate the memoryless property:
var s = 2.0;
var t = 3.0;
var prob1 = ( 1.0 - geometric.cdf( s + t - 1.0, p ) ) /
( 1.0 - geometric.cdf( s - 1.0, p ));

This comment has been minimized.

Copy link
@kgryte

kgryte Oct 11, 2024

Member

@Planeshifter Missing space between parens.

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Oct 12, 2024

Author Member

Resolved.

var prob2 = 1.0 - geometric.cdf( t - 1.0, p );

console.log( 'P(X > s + t | X > s):', prob1 );

This comment has been minimized.

Copy link
@kgryte

kgryte Oct 11, 2024

Member

@Planeshifter I believe you want spaces after the colon for each log here.

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Oct 12, 2024

Author Member

Added.

console.log( 'P(X > t):', prob2 );
console.log( 'Difference:', abs( prob1 - prob2 ) );

// Demonstrate that the sum of k independent geometric random variables follows a negative binomial distribution:
var k = 5;
function drawSum() {
var sum = 0;
var j;
for ( j = 0; j < k; j++ ) {
sum += rgeom();
}
return sum;
}
var sumSamples = filledarrayBy( n, 'float64', drawSum );

// Compute sample mean and variance for the sum:
var sumSampleMean = mean( n, sumSamples, 1 );
var sumSampleVariance = variance( n, 1, sumSamples, 1 );

// Theoretical mean and variance of Negative Binomial distribution:
var nbMean = negativeBinomial.mean( k, p );
var nbVariance = negativeBinomial.variance( k, p );

console.log( 'Sum Sample Mean:', sumSampleMean );

This comment has been minimized.

Copy link
@kgryte

kgryte Oct 11, 2024

Member

@Planeshifter Same comment regarding space after : here and elsewhere.

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Oct 12, 2024

Author Member

Added.

console.log( 'Sum Sample Variance:', sumSampleVariance );
console.log( 'Negative Binomial Mean:', nbMean );
console.log( 'Negative Binomial Variance:', nbVariance );

// Compare sample statistics to theoretical values:
console.log( 'Difference in Mean:', abs( nbMean - sumSampleMean ) );
console.log( 'Difference in Variance:', abs( nbVariance - sumSampleVariance ) );
```

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

'use strict';

var objectKeys = require( '@stdlib/utils/keys' );
var geometricRandomFactory = require( '@stdlib/random/base/geometric' ).factory;
var negativeBinomial = require( '@stdlib/stats/base/dists/negative-binomial' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var variance = require( '@stdlib/stats/base/variance' );
var linspace = require( '@stdlib/array/base/linspace' );
var mean = require( '@stdlib/stats/base/mean' );
var abs = require( '@stdlib/math/base/special/abs' );
var geometric = require( './../lib' );

console.log( objectKeys( geometric ) );
// Define the success probability:
var p = 0.3; // Probability of success on each trial

// Generate an array of x values (number of failures before first success):
var x = linspace( 0, 10, 11 ); // Geometric distribution is discrete

// Compute the PMF for each x:
var geometricPMF = geometric.pmf.factory( p );
var pmf = filledarrayBy( x.length, 'float64', geometricPMF );

// Compute the CDF for each x:
var geometricCDF = geometric.cdf.factory( p );
var cdf = filledarrayBy( x.length, 'float64', geometricCDF );

// Output the PMF and CDF values:
console.log( 'x values:', x );
console.log( 'PMF values:', pmf );
console.log( 'CDF values:', cdf );

// Compute statistical properties:
var theoreticalMean = geometric.mean( p );
var theoreticalVariance = geometric.variance( p );
var theoreticalSkewness = geometric.skewness( p );
var theoreticalKurtosis = geometric.kurtosis( p );

console.log( 'Theoretical Mean:', theoreticalMean );
console.log( 'Theoretical Variance:', theoreticalVariance );
console.log( 'Skewness:', theoreticalSkewness );
console.log( 'Kurtosis:', theoreticalKurtosis );

// Generate random samples from the geometric distribution:
var rgeom = geometricRandomFactory( p );
var n = 1000;
var samples = filledarrayBy( n, 'float64', rgeom );

// 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 );

// Demonstrate the memoryless property:
var s = 2.0;
var t = 3.0;
var prob1 = ( 1.0 - geometric.cdf( s + t - 1.0, p ) ) /

This comment has been minimized.

Copy link
@kgryte

kgryte Oct 11, 2024

Member

@Planeshifter Same comments as for README here and elsewhere.

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Oct 12, 2024

Author Member

Addressed.

( 1.0 - geometric.cdf( s - 1.0, p ));
var prob2 = 1.0 - geometric.cdf( t - 1.0, p );

console.log( 'P(X > s + t | X > s):', prob1 );
console.log( 'P(X > t):', prob2 );
console.log( 'Difference:', abs( prob1 - prob2 ) );

// Demonstrate that the sum of k independent geometric random variables follows a negative binomial distribution:
var k = 5;
function drawSum() {
var sum = 0;
var j;
for ( j = 0; j < k; j++ ) {
sum += rgeom();
}
return sum;
}
var sumSamples = filledarrayBy( n, 'float64', drawSum );

// Compute sample mean and variance for the sum:
var sumSampleMean = mean( n, sumSamples, 1 );
var sumSampleVariance = variance( n, 1, sumSamples, 1 );

// Theoretical mean and variance of Negative Binomial distribution:
var nbMean = negativeBinomial.mean( k, p );
var nbVariance = negativeBinomial.variance( k, p );

console.log( 'Sum Sample Mean:', sumSampleMean );
console.log( 'Sum Sample Variance:', sumSampleVariance );
console.log( 'Negative Binomial Mean:', nbMean );
console.log( 'Negative Binomial Variance:', nbVariance );

// Compare sample statistics to theoretical values:
console.log( 'Difference in Mean:', abs( nbMean - sumSampleMean ) );
console.log( 'Difference in Variance:', abs( nbVariance - sumSampleVariance ) );

1 comment on commit caaf0d9

@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/geometric $\color{green}177/177$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}0/0$
$\color{green}+100.00\%$
$\color{green}177/177$
$\color{green}+100.00\%$

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

Please sign in to comment.