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:
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:
Raw data:
Examples:
// Array of 30 beta variates with shape parameters 2 and 5
PD.rbeta(30, 2, 5)
Cauchy:
Number: Location: Scale:
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:
Raw data:
Examples:
// Array of 10 Chi Square variates with 5 degrees of freedom
PD.rchisq(10, 5)
Exponential:
Number: Rate:
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:
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:
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:
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:
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:
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:
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:
Raw data:
Examples:
// Array of 12 poisson variates with mean/variance of 7
PD.rpois(12, 7)
Uniform:
Number: Min: Max:
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:
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:
Number:
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:
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:
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: "∞", // 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">•</span>',
significantDigits: 0 // 0 will show as many as JS provides, or choose a number to limit
}