Simulation of common stochastic processes with native JavaScript
JavaScript HTML CSS
Clone or download
Pull request Compare This branch is 132 commits ahead, 1 commit behind NathanEpstein:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.nyc_output
bin
dist
examples
lib
out
report
research
src
tests
.babelrc
.eslintrc.json
.flowconfig
.gitignore
.jsdoc.json
.nvmrc
README.md
package-lock.json
package.json
reading.md
scenarios.org
stochastic-extern.js
stochastic.d.ts
webpack.config.js
yarn.lock

README.md

stochastic

Install

npm install @jwalsh/stochastic --save

Examples

Real-world examples will be provided from some of the following texts or courses:

  • 18.445 Introduction to Stochastic Processes
  • 6.041 / 6.431 Probabilistic Systems Analysis and Applied Probability
  • Introduction to Stochastic Processes, Erhan Cinlar
  • Stochastic Modeling: Analysis & Simulation, Barry L. Nelson
  • Markov Models: An Introduction to Markov Models, Steven Taylor

Usage

CommonJS

var stochastic = require('@jwalsh/stochastic');
var norm = stochastic.norm(1, 1, 100);

ES2015

import * as stochastic from '@jwalsh/stochastic';
const exp = stochastic.exp(20);

CDN

<script src="https://cdn.rawgit.com/jwalsh/stochastic/master/dist/bundle.min.js"></script>
<script>
  console.log(stochastic.exp(20));
</script>

API

poissP

Returns an array with the times of each arrival in a Poisson Process with rate lambda until time T.

poissP

Exercise: Assuming you get 10 emails per hour over the course of an 8 hour day, what's the distribution of the number of emails you receive over the course of a standard 261 work-day year

poissP-emails

Parameters

  • lambda number (rate) (optional, default 0)
  • T number time as positive number (optional, default 0)
  • path boolean (optional, default true)

Examples

const poissP = stochastic.poissP(1, 100, true);
const emails = stochastic.hist(Array(261).fill().map(e => stochastic.poissP(10, 8, true).length));

Returns Array<number> times of each arrival in a Poisson Process

average

Returns the average.

Parameters

Examples

const avg = stochastic.average([1, 2, 3]);

Returns number average

mode

Returns the mode.

Parameters

Examples

const mode = stochastic.mode([1, 2, 3]);

Returns number mode

std

Returns the standard deviation.

Parameters

Examples

const std = stochastic.std([2, 3, 4, 4, 4, 5, 6]);

Returns number standard deviation as positive number

summary

Provides a summary of a set of data.

Parameters

Examples

const summary = stochastic.summary([1, 2, 3]);

Returns object R-like summary of values

mock

Returns a mock data set that uses the same standard deviation and average.

norm

Parameters

Examples

const mock = stochastic.mock(stochastic.norm(1, 1, 100));

Returns number standard deviation as positive number

rsn

Returns the Skew-Normal (SN) probability distribution. http://azzalini.stat.unipd.it/SN/

Parameters

Examples

const rsn = stochastic.rsn(10000, 1.256269, 1.605681, 5);

Returns Array<number> average

norm

Returns an array with num normal random variables in a normal distribution of mean mu and standard deviation sigma.

norm

Parameters

  • mu number the mean or expectation of the distribution (and also its median and mode) (optional, default 1)
  • sigma number standard deviation as positive number (optional, default 0)
  • num number a positive integer (optional, default 1)
  • xi

Examples

const norm = stochastic.norm(1, 1, 100);

Returns Array<number> normal random values

brown

Returns an array corresponding to the path of Brownian motion from time 0 to T with drift parameter mu and volatility parameter sigma (the process is initialized to be 0). The i-th entry in the array corresponds to the Brownian process at time i * (T / steps).

brown

Parameters

  • mu number drift parameter (a real number)
  • sigma number volatility parameter (strictly positive real)
  • T number time (strictly positive real)
  • steps number (positive integer)
  • path boolean (optional, default true)

Examples

const brown = stochastic.brown(1.0, -0.1, +0.1, 100, true);

Returns Array<number> Brownian motion path

GBM

Returns an array corresponding to the path of geometric Brownian motion from time 0 to T with drift parameter mu and volatility parameter sigma (the process is initialized to be S0). The i-th entry in the array corresponds to the geometric Brownian process at time i * (T/steps).

GBM

Parameters

  • S0 number initialized process value
  • mu number drift parameter
  • sigma number volatility parameter (strictly positive real)
  • T number time (strictly positive real)
  • steps number (positive integer)
  • path boolean (optional, default true)

Examples

const GBM = stochastic.GBM(1.0, -0.1, 0.1, 1.0, 100, true);

Returns Array<number> geometric Brownian motion

DTMC

Returns an array with the states at each step of the discrete-time Markov Chain given by transMatrix (a square matrix). The number of transitions is given by steps. The initial state is given by start (the states are indexed from 0 to n-1 where n is the number of arrays in transMatrix).

DTMC

Parameters

Examples

const DTMC = stochastic.DTMC([[0,1,0],[0,0,1],[1,0,0]], 20, 0, true);

Returns Array<number>

collate

Returns the transMatrix for an array of mapped states to numerical values.

Parameters

Examples

const collate = stochastic.collate([0,1,0,0,0,1,1,0,0]);

Returns Array<Array<number>> transMatrix

CTMC

Returns an object with the {key:value} pair {time:state} at each step of the continuous-time Markov Chain given by transMatrix (a square matrix). The Markov Chain is simulated until time T. The initial state is given by start (the states are indexed from 0 to n-1 where n is the number of arrays in transMatrix).

CTMC

Parameters

Examples

const CTMC = stochastic.CTMC([[0,1,0],[0,0,1],[1,0,0]], 20, 0, true);

Returns Object Continuous-time Markov chain

sample

Generates a random sample (with replacement) from array arr of observations. Number of observations n is specified by the user.

Parameters

Examples

const sample = stochastic.sample([1,2,3,4,5], +10);

Returns Array<number> random sample

exp

Generates an exponential random variable with rate parameter lambda.

Parameters

  • lambda number (positive) (optional, default 1)

Examples

const exp = stochastic.exp(20);

Returns number variable

pareto

Generates a Pareto random variables with parameters x_m and alpha.

Parameters

Examples

const pareto = stochastic.pareto(+20.0, -1.0);

Returns number distribution

hist

Generates a histogram object from an array of data. Keys denote the lower bound of each bin and the values indicate the frequency of data in each bin.

hist

Parameters

Examples

const hist = stochastic.hist([1,1,1,1,2,3,3,4,4,4]);

Returns Object histogram

Development

Testing is provided with the JavaScript implementation of http://clojure.org/guides/spec in tests/.

License

The MIT License (MIT)

Copyright (c) 2014 Nathan Epstein

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.