Skip to content
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
41 changes: 37 additions & 4 deletions lib/node_modules/@stdlib/random/iter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,43 @@ The namespace contains the following functions for creating iterator protocol-co
<!-- eslint no-undef: "error" -->

```javascript
var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( '@stdlib/random/iter' );

console.log( objectKeys( ns ) );
var roundn = require( '@stdlib/math/base/special/roundn' );
var mean = require( '@stdlib/stats/base/mean' );
var iter = require( '@stdlib/random/iter' );

var initialPrice = 100.0;
var currentPrice = initialPrice;
var numDays = 30;
var volatility = 0.02; // 2% daily volatility

// Create iterator for random price movements:
var priceIter = iter.normal( 0.0, volatility );
var prices = [ initialPrice ];
var dailyReturns = [];

// Simulate price movements:
var change;
var i;
for ( i = 0; i < numDays; i++ ) {
change = priceIter.next().value;
currentPrice *= ( 1.0 + change );
prices.push( roundn( currentPrice, -2 ) );
dailyReturns.push( change * 100.0 );
}

// Calculate summary statistics:
var totalReturn = ( ( currentPrice - initialPrice ) / initialPrice ) * 100.0;
var avgReturn = mean( numDays, dailyReturns, 1 );

// Print results:
console.log( 'Stock Price Simulation Results:' );
console.log( '-------------------------------' );
console.log( 'Initial Price: $%d', initialPrice );
console.log( 'Final Price: $%d', roundn( currentPrice, -2 ) );
console.log( 'Total Return: %d%', roundn( totalReturn, -2 ) );
console.log( 'Average Daily Return: %d%', roundn( avgReturn, -2 ) );
console.log( '\nPrice History:' );
console.log( prices.join( ' → ' ) );
```

</section>
Expand Down
39 changes: 36 additions & 3 deletions lib/node_modules/@stdlib/random/iter/examples/index.js
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 ns = require( './../lib' );
var roundn = require( '@stdlib/math/base/special/roundn' );
var mean = require( '@stdlib/stats/base/mean' );
var iter = require( './../lib' );

console.log( objectKeys( ns ) );
var initialPrice = 100.0;
var currentPrice = initialPrice;
var numDays = 30;
var volatility = 0.02; // 2% daily volatility

// Create iterator for random price movements:
var priceIter = iter.normal( 0.0, volatility );
var prices = [ initialPrice ];
var dailyReturns = [];

// Simulate price movements:
var change;
var i;
for ( i = 0; i < numDays; i++ ) {
change = priceIter.next().value;
currentPrice *= ( 1.0 + change );
prices.push( roundn( currentPrice, -2 ) );
dailyReturns.push( change * 100.0 );
}

// Calculate summary statistics:
var totalReturn = ( ( currentPrice - initialPrice ) / initialPrice ) * 100.0;
var avgReturn = mean( numDays, dailyReturns, 1 );

// Print results:
console.log( 'Stock Price Simulation Results:' );
console.log( '-------------------------------' );
console.log( 'Initial Price: $%d', initialPrice );
console.log( 'Final Price: $%d', roundn( currentPrice, -2 ) );
console.log( 'Total Return: %d%', roundn( totalReturn, -2 ) );
console.log( 'Average Daily Return: %d%', roundn( avgReturn, -2 ) );
console.log( '\nPrice History:' );
console.log( prices.join( ' → ' ) );
Loading