Skip to content

Commit 61912b7

Browse files
docs: improve examples of stats/base/dists/exponential namespace
PR-URL: #2688 Closes: #1624 --------- Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent c00f27a commit 61912b7

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

lib/node_modules/@stdlib/stats/base/dists/exponential/README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,51 @@ var y = dist.logpdf( 0.8 );
109109
<!-- eslint no-undef: "error" -->
110110

111111
```javascript
112-
var objectKeys = require( '@stdlib/utils/keys' );
112+
var Float64Array = require( '@stdlib/array/float64' );
113+
var randomExponential = require( '@stdlib/random/array/exponential' );
114+
var dcusum = require( '@stdlib/blas/ext/base/dcusum' );
113115
var exponential = require( '@stdlib/stats/base/dists/exponential' );
114116

115-
console.log( objectKeys( exponential ) );
117+
// Simulate interarrival times of customers entering a store:
118+
var lambda = 0.5; // Average rate (customers per minute)
119+
var numCustomers = 10;
120+
121+
// Generate interarrival times using the exponential distribution:
122+
var interarrivalTimes = randomExponential( numCustomers, lambda, {
123+
'dtype': 'float64'
124+
});
125+
126+
console.log( 'Simulated interarrival times for ' + numCustomers + ' customers:' );
127+
console.log( interarrivalTimes );
128+
129+
// Calculate cumulative arrival times by computing the cumulative sum of interarrival times:
130+
var arrivalTimes = new Float64Array( interarrivalTimes.length );
131+
dcusum( interarrivalTimes.length, 0.0, interarrivalTimes, 1, arrivalTimes, 1 );
132+
133+
console.log( '\nCustomer arrival times:' );
134+
console.log( arrivalTimes );
135+
136+
// Probability that a customer arrives within two minutes:
137+
var x = 2.0;
138+
var prob = exponential.cdf( x, lambda );
139+
console.log( '\nProbability that a customer arrives within ' + x + ' minutes: ' + prob.toFixed(4) );
140+
141+
// Expected time until the next customer arrives:
142+
var mean = exponential.mean( lambda );
143+
console.log( 'Expected interarrival time: ' + mean + ' minutes' );
144+
145+
var dist = new exponential.Exponential( lambda );
146+
147+
var median = dist.median;
148+
console.log( 'Median interarrival time: ' + median + ' minutes' );
149+
150+
// Evaluate the PDF at x = 1.0:
151+
var out = dist.pdf( 1.0 );
152+
console.log( 'PDF at x = 1: ' + out.toFixed(4) );
153+
154+
// Evaluate the MGF at t = 0.1:
155+
out = dist.mgf( 0.1 );
156+
console.log( 'MGF at t = 0.5: ' + out.toFixed(4) );
116157
```
117158

118159
</section>

lib/node_modules/@stdlib/stats/base/dists/exponential/examples/index.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,48 @@
1818

1919
'use strict';
2020

21-
var objectKeys = require( '@stdlib/utils/keys' );
21+
var Float64Array = require( '@stdlib/array/float64' );
22+
var randomExponential = require( '@stdlib/random/array/exponential' );
23+
var dcusum = require( '@stdlib/blas/ext/base/dcusum' );
2224
var exponential = require( './../lib' );
2325

24-
console.log( objectKeys( exponential ) );
26+
// Simulate interarrival times of customers entering a store:
27+
var lambda = 0.5; // Average rate (customers per minute)
28+
var numCustomers = 10;
29+
30+
// Generate interarrival times using the exponential distribution:
31+
var interarrivalTimes = randomExponential( numCustomers, lambda, {
32+
'dtype': 'float64'
33+
});
34+
35+
console.log( 'Simulated interarrival times for ' + numCustomers + ' customers:' );
36+
console.log( interarrivalTimes );
37+
38+
// Calculate cumulative arrival times by computing the cumulative sum of interarrival times:
39+
var arrivalTimes = new Float64Array( interarrivalTimes.length );
40+
dcusum( interarrivalTimes.length, 0.0, interarrivalTimes, 1, arrivalTimes, 1 );
41+
42+
console.log( '\nCustomer arrival times:' );
43+
console.log( arrivalTimes );
44+
45+
// Probability that a customer arrives within two minutes:
46+
var x = 2.0;
47+
var prob = exponential.cdf( x, lambda );
48+
console.log( '\nProbability that a customer arrives within ' + x + ' minutes: ' + prob.toFixed(4) );
49+
50+
// Expected time until the next customer arrives:
51+
var mean = exponential.mean( lambda );
52+
console.log( 'Expected interarrival time: ' + mean + ' minutes' );
53+
54+
var dist = new exponential.Exponential( lambda );
55+
56+
var median = dist.median;
57+
console.log( 'Median interarrival time: ' + median + ' minutes' );
58+
59+
// Evaluate the PDF at x = 1.0:
60+
var out = dist.pdf( 1.0 );
61+
console.log( 'PDF at x = 1: ' + out.toFixed(4) );
62+
63+
// Evaluate the MGF at t = 0.1:
64+
out = dist.mgf( 0.1 );
65+
console.log( 'MGF at t = 0.5: ' + out.toFixed(4) );

0 commit comments

Comments
 (0)