Probability Distributions Library for JavaScript

Functions for sampling random variables from probability distributions. Uses the same function names as R. You can test out the distributions in your web console with the PD global variable.
If you wish to see a particular distribution added please open an issue at github or submit a pull request.

Installation

Run:
npm install --save probability-distributions

Then put this your js file:
var PD = require("probability-distributions");

Distributions

Binomial:

Number: Trials: Success P:

91011121314151617181920010203040506070
Raw data:

Examples:

// Array of 7 binomial variates based on 12 trials with probability 0.2 
PD.rbinom(7, 12, 0.2)

Beta:

Number: Alpha: Beta P:

00.040.070.110.140.180.220.250.290.320.360.390.430.470.50.540.570.610.650.680.720.750.790.830.86020406080100120140160180
Raw data:

Examples:

// Array of 30 beta variates with shape parameters 2 and 5 
PD.rbeta(30, 2, 5)

Cauchy:

Number: Location: Scale:

-336-319-302-284-267-250-233-215-198-181-163-146-129-111-94-77-59-42-25-7.59.827446279020406080100120140160180
Raw data:

Examples:

// Array of 10 Cauchy variates with scale 2 and location -3 
PD.rchauchy(10, 2, -3)

Chi-Squared:

Number: Degrees of Freedom:

0.220.861.52.12.83.44.14.75.366.67.27.98.59.29.8101112131415160246810121416
Raw data:

Examples:

// Array of 10 Chi Square variates with 5 degrees of freedom 
PD.rchisq(10, 5)

Exponential:

Number: Rate:

00.330.650.981.31.622.32.62.93.33.63.94.24.64.95.25.55.96.26.56.97.27.57.80246810121416182022242628
Raw data:

Examples:

// Array of 5 standard exponential variates
PD.rexp(5)  

// 20 exponential variates with rate parameter 3  
PD.rexp(20, 3) 

F-Distribution:

Number: DF 1: DF 2:

01.22.43.64.867.28.49.611121314161718192022232425262829020406080100120140160180200
Raw data:

Examples:

// Array of 12 F-distribution variates on 2 and 5 degrees of freedom
PD.rf(12, 2, 5)  

Gamma:

Number: Shape: Rate:

00.010.020.030.040.050.060.070.080.090.10.110.120.130.140510152025303540
Raw data:

Examples:

// Array of 20 gamma variates with shape 0.5 and rate 30 
PD.rgamma(20, 0.5, 30)

Laplace:

Number: Mean: Scale:

-6-5.6-5.1-4.6-4.2-3.7-3.2-2.8-2.3-1.8-1.4-0.91-0.450.020.490.951.41.92.32.83.33.74.24.75.10102030405060708090
Raw data:

Examples:

PD.rlaplace(10) // Array of 10 standard Laplace variates
PD.rnorm(20, -2, 3) // Array of 20 normal variates with mean -2 and scale parameter of 3

Log-normal:

Number: Mean log: SD log:

0.050.781.52.333.74.55.25.96.77.48.28.99.6101112131415161718050100150200250300350400
Raw data:

Examples:

PD.rlnorm(10) // Array of 10 standard log normal variates
PD.rlnorm(20, -2, 3) // Array of 20 log normal variates with log of mean -2 and log of standard deviation 3

Negative Binomial:

Number: Size: Success P:

5678910111213141516171819202122232425262728293031323334353605101520253035404550
Raw data:

Examples:

// Array of 10 negative binomial variates where we count the number of failures before 3 successes, 
// with the probability of success set to 0.3 
PD.rnbinom(10, 3, 0.3)

Normal (Gaussian):

Number: Mean: SD:

-3.4-3.1-2.9-2.6-2.4-2.1-1.8-1.6-1.3-1.1-0.8-0.54-0.28-0.020.240.50.7611.31.51.82.12.32.62.80102030405060708090100
Raw data:

Examples:

PD.rnorm(10) // Array of 10 standard normal variates
PD.rnorm(20, -2, 3) // Array of 20 normal variates with mean -2 and standard deviation 3

Poisson:

Number: Mean:

01234567891011024681012141618202224
Raw data:

Examples:

// Array of 12 poisson variates with mean/variance of 7 
PD.rpois(12, 7)

Uniform:

Number: Min: Max:

00.040.080.120.160.20.240.280.320.360.40.440.480.520.560.60.640.680.720.760.80.840.880.920.960246810121416
Raw data:

Examples

// Array of 100 standard uniform variates 
PD.runif(100) 

 // Array of 30 numbers uniformly sampled from -10 to 20  
PD.runif(30, -10, 20)

Uniform (whole numbers):

Number: Min: Max:

-4-3-2-101234567891002468101214161820222426
Raw data:

Examples

// Array of 100 uniform variates from -10 to 3 inclusive, only whole numbers
PD.rint(100, -10, 3) 

 // Array of 30 whole numbers between 2 and 9 exclusive (no 2, no 9) 
PD.rint(30, 2, 9, false)

Experimental

The following distributions are not part of the standard R library.

Unreliable Friend:

For more information on this distribution see StatisticsBlog.com.

Number:

0.121631476278931091241401551711862022172332482642792953103263413573720510152025303540
Raw data:

Examples:

// Array of 20 Unreliable Friend variates 
PD.ruf(20)

FML:

The FML distribution is like a discrete version of the Unreliable Friend. It is based on the number of steps taken to return to the orgin from a given position, with transition probabilities set at the beginning by picking a random variate from U(0,1).

Wheares the UF is infinite in expectation, but finite for any given sampling, each FML sampling infinite with positive probability. Therefore we need to put a limit on how long we let the process run before deciding it will never terminate. If the process hits its limit, -1 is returned.

Number: Steps:

-11.54.16.69.11214171922242729323402468101214161820
Raw data:

Examples:

// Array of 10 FML variates with location set to 5 and max steps set to 1000
// Information about how the value was calculated will be saved to trace. 
// Note you can inject your own transition probability function instead of the default one
var trace = {}
PD.rfml(10, 5, undefined /*(function(){ return .2})*/, 1000, trace)

Tools

Sample:

Sample from an array, with or without replacement. You can send an optional array of weights.

Examples:

// Return a shuffled version of the array 
PD.sample(["Jack", "Queen", "King", "Ace"]) 

// Return a shuffled version heavily biased towards Aces 
PD.sample(["Jack", "Queen", "King", "Ace"],4,false,[1,1,1,7]) 

// Sample 10 items with replacement all with equal weight 
PD.sample([1,2,3,4], 10, true) 

// Flip a biased coin 30 times, with 30% chance of heads 
PD.sample(["H", "T"], 30, true, [0.3, 0.7]) 

Random words:

Given a library of letters (as a string) and a length, returns a string of that length composed of randomly selected letters from the library. If no library is specified, it uses the lowercase English alphabet of 26 letters.

Examples:

// Return a single word of length 8 
PD.rword(8) 

// Return a string of 50 "H" and "T" coin flips 
PD.rword(50, "HT") 

Visualize:

x = -0.51824

This function lets you visualize a random variable by showing an animation of the values it takes on over time. This is based on the suggestion by Terence Tao on his blog. The numbers can be shown directly, or interpreted as waiting times where each arrival is shown with a flashing symbol.

Note this function writes directly to an HTML node (e.g a span or div) with the ID you provide. This node must already exist, as in:
x = <span id="viz"></span>

Examples:

// Show these exact 4 numbers over and over 
PD.visualize([1,2,3,4], "viz") 

1
// Take random values from this library  
PD.visualize(PD.rbeta(1000, 1, 3), "viz") 

0.20831371155086278
// Treat values like arrival times. // Set multiplier to 10000 milliseconds (so value of 0.1 lasts 1 second)  
PD.visualize(PD.rexp(1000), "viz", {arrivalTimes: true, lag: 10000,}) 

[]
// Restrict output based on conditions  
// Limit significant digits to 4  
PD.visualize(PD.runif(1000), "viz", {conditions: "x > -0.75 && x < 0.75", significantDigits: 4, blank: "*"}) 

0.3765

Options:

These are the default options, you can override any of them by passing in your own.
var defaultOptions = {  
   lag: 1000, // Timing multiplier in milliseconds  
   blank: "", // What to show when no number is shown  
   inf: "&infin;", // Symbol to indicate infinity  
   loop: true, // When done, start again at beginning at end of array  
   conditions: "", // This is eval'd and checked against true. Example, "x > 3 && x < 10". Use "x" as the variable.  
   arrivalTimes: false,  
   arrivalSymbol: '<span class="pd-arrival">&#8226;</span>',  
   significantDigits: 0 // 0 will show as many as JS provides, or choose a number to limit  
}