# think-bayes

0.1.3-alpha.7 • Public • Published

# think-bayes

An algorithm collection of probability and statistics for browser and Node.js environment.

In progress...

## Quickstart

Let us resolve the cookie problem by using the class `Suite`:

In addition, here are some simple demos you can refer directly to resolve some classic problems of probability and statistics.

## Algorithm Classes

This library provides some ES Classes following for calculations related to probability and statistics.

These classes can be imported by the same way following:

DictWrapper

Pmf `inherits DictWrapper`

Cdf `inherits DictWrapper`

Pdf

Suite `inherits Pmf`

Hist `inherits DictWrapper`

Interpolater

Joint `inherits Pmf`

GaussianPdf `inherits Pdf`

GaussianKde

EstimatedPdf `inherits Pdf`

## Utility Functions

This library provides some Utility Functions following for calculations related to probability and statistics.

These functions can be imported by the same way following:

odds(p)

Computes odds for a given probability.

Example: p=0.75 means 75 for and 25 against, or 3:1 odds in favor.

Note: when p=1, the formula for odds divides by zero, which is

normally undefined. But I think it is reasonable to define Odds(1)

to be infinity, so that's what this function does.

@Params:

param type description
p number float 0~1

@Returns: float odds

probability(o)

Computes the probability corresponding to given odds.

Example: o=2 means 2:1 odds in favor, or 2/3 probability

@Params:

param type description
o number float odds, strictly positive

@Returns: float probability

probability2(yes, no)

Computes the probability corresponding to given odds.

Example: yes=2, no=1 means 2:1 odds in favor, or 2/3 probability.

@Params:

param type description
yes number int or float odds in favor
no number int or float odds in favor
percentile(pmf, percentage)

Computes a percentile of a given Pmf.

@Params:

param type description
pmf pmf
percentage number float 0-100
credibleInterval(pmf, percentage = 90)

Computes a credible interval for a given distribution.

If percentage=90, computes the 90% CI.

@Params:

param type description
pmf pmf Pmf object representing a posterior distribution
percentage number float between 0 and 100

@Returns: sequence of two floats, low and high

pmfProbLess(pmf1, pmf2)

Probability that a value from pmf1 is less than a value from pmf2.

@Params:

param type description
pmf1 pmf Pmf object
pmf2 pmf Pmf object

@Returns: float probability

pmfProbGreater(pmf1, pmf2)

Probability that a value from pmf1 is greater than a value from pmf2.

@Params:

param type description
pmf1 pmf Pmf object
pmf2 pmf Pmf object

@Returns: float probability

pmfProbEqual(pmf1, pmf2)

Probability that a value from pmf1 equals a value from pmf2.

@Params:

param type description
pmf1 pmf Pmf object
pmf2 pmf Pmf object

@Returns: float probability

randomSum(dists)

Chooses a random value from each dist and returns the sum.

@Params:

param type description
dists array sequence of Pmf or Cdf objects

@Returns: numerical sum

sampleSum(dists, n)

Draws a sample of sums from a list of distributions.

@Params:

param type description
dists array sequence of Pmf or Cdf objects
n number sample size

@Returns: new Pmf of sums

evalGaussianPdf(x, mu, sigma)

Computes the unnormalized PDF of the normal distribution.

@Params:

param type description
x number value
mu number mean
sigma number standard deviation

@Returns: float probability density

makeGaussianPdf(mu, sigma, numSigmas, n = 201)

Makes a PMF discrete approx to a Gaussian distribution.

@Params:

param type description
mu number float mean
sigma number float standard deviation
numSigmas number how many sigmas to extend in each direction
n number number of values in the Pmf

@Returns: normalized Pmf

evalBinomialPmf(k, n, p)

Evaluates the binomial pmf.

@Returns: the probabily of k successes in n trials with probability p.

evalPoissonPmf(k, lam)

Computes the Poisson PMF.

@Params:

param type description
k number number of events
lam number parameter lambda in events per unit time

@Returns: float probability

makeJoint(pmf1, pmf2)

Joint distribution of values from pmf1 and pmf2.

@Params:

param type description
pmf1 pmf Pmf object
pmf2 pmf Pmf object

@Returns: Joint pmf of value pairs

makeHistFromList(t, name)

Makes a histogram from an unsorted sequence of values.

@Params:

param type description
t array sequence of numbers
name string string name for this histogram

@Returns: Hist object

makeHistFromDict(d, name)

Makes a histogram from a map from values to frequencies.

@Params:

param type description
d object map
name string string name for this histogram

@Returns: Hist object

makePmfFromList(t, name)

Makes a PMF from an unsorted sequence of values.

@Params:

param type description
t array sequence of numbers
name string string name for this PMF

@Returns: Pmf object

makePmfFromDict(d, name)

Makes a PMF from a map from values to probabilities.

@Params:

param type description
d object map
name string string name for this PMF * @returns Pmf object
makePmfFromItems(t, name)

Makes a PMF from a sequence of value-probability pairs

@Params:

param type description
t array sequence of value-probability pairs
name string string name for this PMF * @returns Pmf object
makePmfFromHist(hist, name)

Makes a normalized PMF from a Hist object.

@Params:

param type description
hist hist Hist object
name string string name

@Returns: Pmf object

makePmfFromCdf(cdf, name)

Makes a normalized Pmf from a Cdf object.

@Params:

param type description
cdf cdf Cdf object
name string string name for the new Pmf

@Returns: Pmf object

makeMixture(metapmf, name = 'mix')

Make a mixture distribution.

@Params:

param type description
metapmf pmf Pmf that maps from Pmfs to probs.
name string string name for the new Pmf

@Returns: Pmf object

makeUniformPmf(low, high, n)

Make a uniform Pmf.

@Params:

param type description
low number lowest value (inclusive)
high number highest value (inclusize)
n number number of values
makeCdfFromItems(items, name = '')

Makes a cdf from an unsorted sequence of (value, frequency) pairs.

@Params:

param type description
items array unsorted sequence of (value, frequency) pairs
name string string name for this CDF

@Returns: cdf: list of (value, fraction) pairs

makeCdfFromDict(d, name)

Makes a CDF from a dictionary that maps values to frequencies.

@Params:

param type description
d object map
name string string name for the data.

@Returns: Cdf object

makeCdfFromHist(hist, name)

Makes a CDF from a Hist object.

@Params:

param type description
hist hist Hist object
name string string name for the data.

@Returns: Cdf object

makeCdfFromList(seq, name)

Creates a CDF from an unsorted sequence.

@Params:

param type description
seq array unsorted sequence of sortable values
name string string name for the cdf

@Returns: Cdf object

makeCdfFromPmf(pmf, name)

Makes a CDF from a Pmf object.

@Params:

param type description
pmf pmf Pmf object
name string string name for the data.

@Returns: Cdf object

makeSuiteFromDict(d, name)

Makes a suite from a map from values to probabilities.

@Params:

param type description
d object map
name string string name for this suite

@Returns: Suite object

makeSuiteFromList(t, name)

Makes a suite from an unsorted sequence of values.

@Params:

param type description
t array sequence of numbers
name string string name for this suite
makeSuiteFromHist(hist, name)

Makes a normalized suite from a Hist object.

@Params:

param type description
hist hist Hist object
name string string name
makeSuiteFromCdf(cdf, name)

Makes a normalized Suite from a Cdf object.

@Params:

param type description
cdf cdf Cdf object
name string string name for the new Suite

@Returns: Suite object

## Q&A

### How to reduce the precision loss caused by the calculation of float point number in javascript?

This library use decimal.js to handle the problem what calculation of float point number, in the same way, you can use it in this library:

## Package Sidebar

### Install

`npm i think-bayes`

### Repository

github.com/parksben/think-bayes

1

0.1.3-alpha.7

MIT

287 kB

61