Skip to content

Commit

Permalink
docs: improve README examples of stats/base/dists/negative-binomial
Browse files Browse the repository at this point in the history
PR-URL: #1774
Closes: #1637

---------

Signed-off-by: Philipp Burckhardt <[email protected]>
Co-authored-by: Philipp Burckhardt <[email protected]>
Reviewed-by: Philipp Burckhardt <[email protected]>
  • Loading branch information
Rejoan-Sardar and Planeshifter authored Mar 12, 2024
1 parent d8f6700 commit 37f46fe
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,43 @@ var y = dist.pmf( 4.0 );
<!-- eslint no-undef: "error" -->

```javascript
var objectKeys = require( '@stdlib/utils/keys' );
var negativeBinomial = require( '@stdlib/stats/base/dists/negative-binomial' );

console.log( objectKeys( negativeBinomial ) );
/*
* Let's take an example of flipping a biased coin until getting 5 heads.
* This situation can be modeled using a Negative Binomial distribution with r = 5 and p = 1/2.
*/

var r = 5.0;
var p = 1/2;

// Mean can be used to calculate the average number of trials needed to get 5 heads:
console.log( negativeBinomial.mean( r, p ) );
// => 5

// PMF can be used to calculate the probability of getting heads on a specific trial (say on the 8th trial):
console.log( negativeBinomial.pmf( 8, r, p ) );
// => ~0.06

// CDF can be used to calculate the probability up to a certain number of trials (say up to 8 trials):
console.log( negativeBinomial.cdf( 8, r, p ) );
// => ~0.867

// Quantile can be used to calculate the number of trials at which you can be 80% confident that the actual number will not exceed:
console.log( negativeBinomial.quantile( 0.8, r, p ) );
// => 7

// Standard deviation can be used to calculate the measure of the spread of trials around the mean:
console.log( negativeBinomial.stdev( r, p ) );
// => ~3.162

// Skewness can be used to calculate the asymmetry of the distribution of trials:
console.log( negativeBinomial.skewness( r, p ) );
// => ~0.949

// MGF can be used for more advanced statistical analyses and generating moments of the distribution:
console.log( negativeBinomial.mgf( 0.5, r, p ) );
// => ~2277.597
```

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

'use strict';

var objectKeys = require( '@stdlib/utils/keys' );
var negativeBinomial = require( './../lib' );

console.log( objectKeys( negativeBinomial ) );
/*
* Let's take an example of flipping a biased coin until getting 5 heads.
* This situation can be modeled using a Negative Binomial distribution with r = 5 and p = 1/2.
*/

var r = 5.0;
var p = 1/2;

// Mean can be used to calculate the average number of trials needed to get 5 heads:
console.log( negativeBinomial.mean( r, p ) );
// => 5

// PMF can be used to calculate the probability of getting heads on a specific trial (say on the 8th trial):
console.log( negativeBinomial.pmf( 8, r, p ) );
// => ~0.06

// CDF can be used to calculate the probability up to a certain number of trials (say up to 8 trials):
console.log( negativeBinomial.cdf( 8, r, p ) );
// => ~0.867

// Quantile can be used to calculate the number of trials at which you can be 80% confident that the actual number will not exceed:
console.log( negativeBinomial.quantile( 0.8, r, p ) );
// => 7

// Standard deviation can be used to calculate the measure of the spread of trials around the mean:
console.log( negativeBinomial.stdev( r, p ) );
// => ~3.162

// Skewness can be used to calculate the asymmetry of the distribution of trials:
console.log( negativeBinomial.skewness( r, p ) );
// => ~0.949

// MGF can be used for more advanced statistical analyses and generating moments of the distribution:
console.log( negativeBinomial.mgf( 0.5, r, p ) );
// => ~2277.597

1 comment on commit 37f46fe

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

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

Please sign in to comment.