-
-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: improve README examples of
stats/base/dists/geometric
namespace
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
1 parent
4a94497
commit caaf0d9
Showing
2 changed files
with
176 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
var prob2 = 1.0 - geometric.cdf( t - 1.0, p ); | ||
|
||
console.log( 'P(X > s + t | X > s):', prob1 ); | ||
This comment has been minimized.
Sorry, something went wrong.
kgryte
Member
|
||
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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit caaf0d9
There was a problem hiding this comment.
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 |
|
|
|
|
The above coverage report was generated for the changes in this push.
@Planeshifter Missing space between parens.