Skip to content

hgupta/likelihoods

Repository files navigation

likelihoods

Probability Distributions library for JavaScript

Demo

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

Install

npm install --save [hgupta/]likelihoods
yarn add [hgupta/]likelihoods

Build / Test

yarn install
yarn build
yarn test

# or if you prefer npm
npm install
npm run build
npm run test

Psuedo-random number generation

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.

Usage

ES7 / ES6 / Modules

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'

NodeJS / CommonJS

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')

Browser

<script src="./dist/likelihoods.min.js">

Adds a global name likelihoods in window object.

Examples

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, so shape: [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()

List of Distributions

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

References

TODO:

  • Complete Test Suite using Ava
  • Add Travis CI
  • Better / Faster Normal Distribution Algorithm
  • Better Binomial Distribution Algorithm (BTPE & Inversion)

License

MIT License, Copyright (c) 2018 Harsh Gupta, LICENSE

About

Probability Distributions library for JavaScript

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published