Skip to content

Commit

Permalink
docs: improve examples of stats/base/dists/exponential namespace
Browse files Browse the repository at this point in the history
PR-URL: #2688 
Closes: #1624

---------

Co-authored-by: Philipp Burckhardt <[email protected]>
Reviewed-by: Philipp Burckhardt <[email protected]>
  • Loading branch information
UtkershBasnet and Planeshifter authored Oct 8, 2024
1 parent c00f27a commit 61912b7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
45 changes: 43 additions & 2 deletions lib/node_modules/@stdlib/stats/base/dists/exponential/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,51 @@ var y = dist.logpdf( 0.8 );
<!-- eslint no-undef: "error" -->

```javascript
var objectKeys = require( '@stdlib/utils/keys' );
var Float64Array = require( '@stdlib/array/float64' );
var randomExponential = require( '@stdlib/random/array/exponential' );
var dcusum = require( '@stdlib/blas/ext/base/dcusum' );
var exponential = require( '@stdlib/stats/base/dists/exponential' );

console.log( objectKeys( exponential ) );
// Simulate interarrival times of customers entering a store:
var lambda = 0.5; // Average rate (customers per minute)
var numCustomers = 10;

// Generate interarrival times using the exponential distribution:
var interarrivalTimes = randomExponential( numCustomers, lambda, {
'dtype': 'float64'
});

console.log( 'Simulated interarrival times for ' + numCustomers + ' customers:' );
console.log( interarrivalTimes );

// Calculate cumulative arrival times by computing the cumulative sum of interarrival times:
var arrivalTimes = new Float64Array( interarrivalTimes.length );
dcusum( interarrivalTimes.length, 0.0, interarrivalTimes, 1, arrivalTimes, 1 );

console.log( '\nCustomer arrival times:' );
console.log( arrivalTimes );

// Probability that a customer arrives within two minutes:
var x = 2.0;
var prob = exponential.cdf( x, lambda );
console.log( '\nProbability that a customer arrives within ' + x + ' minutes: ' + prob.toFixed(4) );

// Expected time until the next customer arrives:
var mean = exponential.mean( lambda );
console.log( 'Expected interarrival time: ' + mean + ' minutes' );

var dist = new exponential.Exponential( lambda );

var median = dist.median;
console.log( 'Median interarrival time: ' + median + ' minutes' );

// Evaluate the PDF at x = 1.0:
var out = dist.pdf( 1.0 );
console.log( 'PDF at x = 1: ' + out.toFixed(4) );

// Evaluate the MGF at t = 0.1:
out = dist.mgf( 0.1 );
console.log( 'MGF at t = 0.5: ' + out.toFixed(4) );

This comment has been minimized.

Copy link
@kgryte

kgryte Oct 11, 2024

Member

@Planeshifter I believe this should be t = 0.1, correct?

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Oct 12, 2024

Author Member

Changed.

```

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

'use strict';

var objectKeys = require( '@stdlib/utils/keys' );
var Float64Array = require( '@stdlib/array/float64' );
var randomExponential = require( '@stdlib/random/array/exponential' );
var dcusum = require( '@stdlib/blas/ext/base/dcusum' );
var exponential = require( './../lib' );

console.log( objectKeys( exponential ) );
// Simulate interarrival times of customers entering a store:
var lambda = 0.5; // Average rate (customers per minute)
var numCustomers = 10;

// Generate interarrival times using the exponential distribution:
var interarrivalTimes = randomExponential( numCustomers, lambda, {
'dtype': 'float64'
});

console.log( 'Simulated interarrival times for ' + numCustomers + ' customers:' );
console.log( interarrivalTimes );

// Calculate cumulative arrival times by computing the cumulative sum of interarrival times:
var arrivalTimes = new Float64Array( interarrivalTimes.length );
dcusum( interarrivalTimes.length, 0.0, interarrivalTimes, 1, arrivalTimes, 1 );

console.log( '\nCustomer arrival times:' );
console.log( arrivalTimes );

// Probability that a customer arrives within two minutes:
var x = 2.0;
var prob = exponential.cdf( x, lambda );
console.log( '\nProbability that a customer arrives within ' + x + ' minutes: ' + prob.toFixed(4) );

// Expected time until the next customer arrives:
var mean = exponential.mean( lambda );
console.log( 'Expected interarrival time: ' + mean + ' minutes' );

var dist = new exponential.Exponential( lambda );

var median = dist.median;
console.log( 'Median interarrival time: ' + median + ' minutes' );

// Evaluate the PDF at x = 1.0:
var out = dist.pdf( 1.0 );
console.log( 'PDF at x = 1: ' + out.toFixed(4) );

// Evaluate the MGF at t = 0.1:
out = dist.mgf( 0.1 );
console.log( 'MGF at t = 0.5: ' + out.toFixed(4) );

This comment has been minimized.

Copy link
@kgryte

kgryte Oct 11, 2024

Member

@Planeshifter Same comment.

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Oct 12, 2024

Author Member

Changed.

1 comment on commit 61912b7

@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/exponential $\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.