npm install @jwalsh/stochastic --save
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
var stochastic = require('@jwalsh/stochastic');
var norm = stochastic.norm(1, 1, 100);
import * as stochastic from '@jwalsh/stochastic';
const exp = stochastic.exp(20);
<script src="https://cdn.rawgit.com/jwalsh/stochastic/master/dist/bundle.min.js"></script>
<script>
console.log(stochastic.exp(20));
</script>
Returns an array with the times of each arrival in a Poisson Process with rate lambda
until time T
.
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
Parameters
lambda
number (rate) (optional, default0
)T
number time as positive number (optional, default0
)path
boolean (optional, defaulttrue
)
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
Returns the average.
Parameters
Examples
const avg = stochastic.average([1, 2, 3]);
Returns number average
Returns the mode.
Parameters
Examples
const mode = stochastic.mode([1, 2, 3]);
Returns number mode
Returns the standard deviation.
Parameters
Examples
const std = stochastic.std([2, 3, 4, 4, 4, 5, 6]);
Returns number standard deviation as positive number
Provides a summary of a set of data.
Parameters
Examples
const summary = stochastic.summary([1, 2, 3]);
Returns object R-like summary of values
Returns a mock data set that uses the same standard deviation and average.
Parameters
Examples
const mock = stochastic.mock(stochastic.norm(1, 1, 100));
Returns number standard deviation as positive number
Returns the Skew-Normal (SN) probability distribution. http://azzalini.stat.unipd.it/SN/
Parameters
n
numberlocation
numberscale
numbershape
number 0 is the normal distribution (optional, default/*: number */0
)
Examples
const rsn = stochastic.rsn(10000, 1.256269, 1.605681, 5);
Returns an array with num
normal random variables in a normal distribution of mean mu
and standard deviation sigma
.
Parameters
mu
number the mean or expectation of the distribution (and also its median and mode) (optional, default1
)sigma
number standard deviation as positive number (optional, default0
)num
number a positive integer (optional, default1
)xi
Examples
const norm = stochastic.norm(1, 1, 100);
Returns Array<number> normal random values
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)
.
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, defaulttrue
)
Examples
const brown = stochastic.brown(1.0, -0.1, +0.1, 100, true);
Returns Array<number> Brownian motion path
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)
.
Parameters
S0
number initialized process valuemu
number drift parametersigma
number volatility parameter (strictly positive real)T
number time (strictly positive real)steps
number (positive integer)path
boolean (optional, defaulttrue
)
Examples
const GBM = stochastic.GBM(1.0, -0.1, 0.1, 1.0, 100, true);
Returns Array<number> geometric Brownian motion
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).
Parameters
transMatrix
Array<Array<number>>steps
number (positive integer)start
numberpath
boolean (optional, defaulttrue
)
Examples
const DTMC = stochastic.DTMC([[0,1,0],[0,0,1],[1,0,0]], 20, 0, true);
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
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
).
Parameters
Examples
const CTMC = stochastic.CTMC([[0,1,0],[0,0,1],[1,0,0]], 20, 0, true);
Returns Object Continuous-time Markov chain
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
Generates an exponential random variable with rate parameter lambda
.
Parameters
lambda
number (positive) (optional, default1
)
Examples
const exp = stochastic.exp(20);
Returns number variable
Generates a Pareto random variables with parameters x_m
and alpha
.
Parameters
Examples
const pareto = stochastic.pareto(+20.0, -1.0);
Returns number distribution
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.
Parameters
Examples
const hist = stochastic.hist([1,1,1,1,2,3,3,4,4,4]);
Returns Object histogram
Testing is provided with the JavaScript implementation of
http://clojure.org/guides/spec in tests/
.
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.