Probability Distributions library for JavaScript
Probability Density Function (PDF), Cumulative Density Function (CDF), function for drawing sample(s) for various probability distributions.
It uses the same function names as numpy.random
.
The inspiration for this project comes from Numpy / Scipy
, from where it
also draws some algorithms (namely Gamma
).
You may clone and run the plots in associated Charting library or check Demo
npm install --save [hgupta/]likelihoods
yarn add [hgupta/]likelihoods
yarn install
yarn build
yarn test
# or if you prefer npm
npm install
npm run build
npm run test
Uses crypto-engines library
For NodeJS, it uses in-built crypto
module to generate randomBytes
.
Currently, it uses fixed length of 16.
For browser, it checks for browser's crypto
or msCrypto
object in window
and uses it to generates randomValues
of Uint8Array
.
If not found, it defaults to Math.random
.
You may provide your own random number generator like mersenne-twister
.
Please check usage of random
method.
import likelihoods from 'likelihoods'
// You may `require` a specific distribution instead of all distributions
// this will save you a lot of build space if you need only specific
// distribution
import normal from 'likelihoods/src/normal'
import poisson from 'likelihoods/src/poisson'
const likelihoods = require('likelihoods')
// You may `require` a specific distribution instead of all distributions
// this will save you a lot of build space if you need only specific
// distribution
const normal = require('likelihoods/src/normal')
const poisson = require('likelihoods/src/poisson')
<script src="./dist/likelihoods.min.js">
Adds a global name likelihoods
in window
object.
Initializing a distribution
const norm = likelihoods.normal(/* arguments */)
Drawing a random
norm.random(/* { shape, ranf } */)
random
function accepts an Object
as an optional argument.
ranf
: Custom random number generator. Must be a function with no parameters.shape
: Accepts an array of integers. Returns nested array(s) of given shape. It follows row-wise vector pattern, soshape: [5]
will create 5 inner arrays within an outer array,[[f1], [f2], [f3], [f4], [f5]]
Probability Density Function (PDF) for
x
norm.pdf(/* scalar value or array of scalar values */)
Cumulative Density Function (CDF) for
x
norm.cdf(/* scalar value or array of scalar values */)
Mean of distribution
norm.mean()
Variance of distribution
norm.variance()
Distribution Name | Function Name | Parameters (default) |
---|---|---|
Beta | ||
Bernoulli | bernoulli |
p (0.5) |
Binomial | binomial |
n , p (10, 0.5) |
Cauchy | cauchy |
loc , scale (0, 1) |
Chi-squared | chisquared |
df (1) |
Dirichlet | ||
Exponential | exponential |
lambda (1) |
F | ||
Gamma | gamma |
shape , scale (1, 1) |
Geometric | ||
Gumbel | ||
HyperGeometric | ||
Laplace | laplace |
loc , scale (0, 1) |
Logistic | ||
Log-noral | lognormal |
mean , sigma (0, 1) |
Logseries | ||
Lomax | ||
Multinomial | ||
Multivariate Normal | ||
Neg-Binomial | ||
Normal | normal |
loc , scale (0, 1) |
Poisson | poisson |
lambda (10) |
Power | ||
Rayleigh | ||
Student's t | ||
Triangular | ||
Uniform | uniform |
min , max (0, 1) |
Wald | ||
Weibull | ||
Zipf |
- Handbook of Mathematical Functions
- NIST Digital Library of Mathematical Functions
- Luc Devroye - Non-Uniform Random Variate Generation
- Scipy / Numpy
- Gauss Error Function
- Box-Muller Transformation
- Spouge's approximation for Gamma function
- The transformed rejection method for generating Poisson random variables
- Wikipedia (is not bad for referencing, just be cautious)
- Complete Test Suite using Ava
- Add Travis CI
- Better / Faster Normal Distribution Algorithm
- Better Binomial Distribution Algorithm (BTPE & Inversion)
MIT License, Copyright (c) 2018 Harsh Gupta, LICENSE