Skip to content

Commit 80e8e4a

Browse files
docs: update examples in random/iter
PR-URL: #1769 Closes: #1607 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent dc4b7b1 commit 80e8e4a

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

lib/node_modules/@stdlib/random/iter/README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,43 @@ The namespace contains the following functions for creating iterator protocol-co
104104
<!-- eslint no-undef: "error" -->
105105

106106
```javascript
107-
var objectKeys = require( '@stdlib/utils/keys' );
108-
var ns = require( '@stdlib/random/iter' );
109-
110-
console.log( objectKeys( ns ) );
107+
var roundn = require( '@stdlib/math/base/special/roundn' );
108+
var mean = require( '@stdlib/stats/base/mean' );
109+
var iter = require( '@stdlib/random/iter' );
110+
111+
var initialPrice = 100.0;
112+
var currentPrice = initialPrice;
113+
var numDays = 30;
114+
var volatility = 0.02; // 2% daily volatility
115+
116+
// Create iterator for random price movements:
117+
var priceIter = iter.normal( 0.0, volatility );
118+
var prices = [ initialPrice ];
119+
var dailyReturns = [];
120+
121+
// Simulate price movements:
122+
var change;
123+
var i;
124+
for ( i = 0; i < numDays; i++ ) {
125+
change = priceIter.next().value;
126+
currentPrice *= ( 1.0 + change );
127+
prices.push( roundn( currentPrice, -2 ) );
128+
dailyReturns.push( change * 100.0 );
129+
}
130+
131+
// Calculate summary statistics:
132+
var totalReturn = ( ( currentPrice - initialPrice ) / initialPrice ) * 100.0;
133+
var avgReturn = mean( numDays, dailyReturns, 1 );
134+
135+
// Print results:
136+
console.log( 'Stock Price Simulation Results:' );
137+
console.log( '-------------------------------' );
138+
console.log( 'Initial Price: $%d', initialPrice );
139+
console.log( 'Final Price: $%d', roundn( currentPrice, -2 ) );
140+
console.log( 'Total Return: %d%', roundn( totalReturn, -2 ) );
141+
console.log( 'Average Daily Return: %d%', roundn( avgReturn, -2 ) );
142+
console.log( '\nPrice History:' );
143+
console.log( prices.join( '' ) );
111144
```
112145

113146
</section>

lib/node_modules/@stdlib/random/iter/examples/index.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,40 @@
1818

1919
'use strict';
2020

21-
var objectKeys = require( '@stdlib/utils/keys' );
22-
var ns = require( './../lib' );
21+
var roundn = require( '@stdlib/math/base/special/roundn' );
22+
var mean = require( '@stdlib/stats/base/mean' );
23+
var iter = require( './../lib' );
2324

24-
console.log( objectKeys( ns ) );
25+
var initialPrice = 100.0;
26+
var currentPrice = initialPrice;
27+
var numDays = 30;
28+
var volatility = 0.02; // 2% daily volatility
29+
30+
// Create iterator for random price movements:
31+
var priceIter = iter.normal( 0.0, volatility );
32+
var prices = [ initialPrice ];
33+
var dailyReturns = [];
34+
35+
// Simulate price movements:
36+
var change;
37+
var i;
38+
for ( i = 0; i < numDays; i++ ) {
39+
change = priceIter.next().value;
40+
currentPrice *= ( 1.0 + change );
41+
prices.push( roundn( currentPrice, -2 ) );
42+
dailyReturns.push( change * 100.0 );
43+
}
44+
45+
// Calculate summary statistics:
46+
var totalReturn = ( ( currentPrice - initialPrice ) / initialPrice ) * 100.0;
47+
var avgReturn = mean( numDays, dailyReturns, 1 );
48+
49+
// Print results:
50+
console.log( 'Stock Price Simulation Results:' );
51+
console.log( '-------------------------------' );
52+
console.log( 'Initial Price: $%d', initialPrice );
53+
console.log( 'Final Price: $%d', roundn( currentPrice, -2 ) );
54+
console.log( 'Total Return: %d%', roundn( totalReturn, -2 ) );
55+
console.log( 'Average Daily Return: %d%', roundn( avgReturn, -2 ) );
56+
console.log( '\nPrice History:' );
57+
console.log( prices.join( ' → ' ) );

0 commit comments

Comments
 (0)