Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improve examples of stats/base/dists/exponential namespace #2688

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) );
```

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