0.1.3-alpha.7 • Published 4 years ago

think-bayes v0.1.3-alpha.7

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

think-bayes

npm npm npm

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

In progress...

适用于 浏览器Node.js 环境的概率统计算法集(非正式版本,功能亟待完善,努力 coding 中...)

Install

yarn add think-bayes # OR npm i --save think-bayes

Quickstart

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

import { Suite } from 'think-bayes';

class Cookie extends Suite {
  mixes = {
    Bowl1: {
      vanilla: 0.75,
      chocolate: 0.25,
    },
    Bowl2: {
      vanilla: 0.5,
      chocolate: 0.5,
    },
  };

  likelihood(data, hypo) {
    const mix = this.mixes[hypo];
    const like = mix[data];
    return like;
  }
}

const hypos = ['Bowl1', 'Bowl2'];
const pmf = new Cookie(hypos);
pmf.update('vanilla');

const result = pmf.render();
console.log(result); // [ [ 'Bowl1', 0.6 ], [ 'Bowl2', 0.4 ] ]

// You can also print the result as a table
pmf.print();
// | Value | Prob |
// |-------|------|
// | Bowl1 | 0.6  |
// | Bowl2 | 0.4  |

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:

import { Pmf, Cdf, Pdf, Suite } from 'think-bayes';

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:

import { Util } from 'think-bayes';
const { odds, probability, percentile } = Util;

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:

paramtypedescription
pnumberfloat 0~1

@Returns: float odds

Computes the probability corresponding to given odds.

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

@Params:

paramtypedescription
onumberfloat odds, strictly positive

@Returns: float probability

Computes the probability corresponding to given odds.

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

@Params:

paramtypedescription
yesnumberint or float odds in favor
nonumberint or float odds in favor

Computes a percentile of a given Pmf.

@Params:

paramtypedescription
pmfpmf
percentagenumberfloat 0-100

Computes a credible interval for a given distribution.

If percentage=90, computes the 90% CI.

@Params:

paramtypedescription
pmfpmfPmf object representing a posterior distribution
percentagenumberfloat between 0 and 100

@Returns: sequence of two floats, low and high

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

@Params:

paramtypedescription
pmf1pmfPmf object
pmf2pmfPmf object

@Returns: float probability

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

@Params:

paramtypedescription
pmf1pmfPmf object
pmf2pmfPmf object

@Returns: float probability

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

@Params:

paramtypedescription
pmf1pmfPmf object
pmf2pmfPmf object

@Returns: float probability

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

@Params:

paramtypedescription
distsarraysequence of Pmf or Cdf objects

@Returns: numerical sum

Draws a sample of sums from a list of distributions.

@Params:

paramtypedescription
distsarraysequence of Pmf or Cdf objects
nnumbersample size

@Returns: new Pmf of sums

Computes the unnormalized PDF of the normal distribution.

@Params:

paramtypedescription
xnumbervalue
munumbermean
sigmanumberstandard deviation

@Returns: float probability density

Makes a PMF discrete approx to a Gaussian distribution.

@Params:

paramtypedescription
munumberfloat mean
sigmanumberfloat standard deviation
numSigmasnumberhow many sigmas to extend in each direction
nnumbernumber of values in the Pmf

@Returns: normalized Pmf

Evaluates the binomial pmf.

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

Computes the Poisson PMF.

@Params:

paramtypedescription
knumbernumber of events
lamnumberparameter lambda in events per unit time

@Returns: float probability

Joint distribution of values from pmf1 and pmf2.

@Params:

paramtypedescription
pmf1pmfPmf object
pmf2pmfPmf object

@Returns: Joint pmf of value pairs

Makes a histogram from an unsorted sequence of values.

@Params:

paramtypedescription
tarraysequence of numbers
namestringstring name for this histogram

@Returns: Hist object

Makes a histogram from a map from values to frequencies.

@Params:

paramtypedescription
dobjectmapdictionary that maps values to frequencies
namestringstring name for this histogram

@Returns: Hist object

Makes a PMF from an unsorted sequence of values.

@Params:

paramtypedescription
tarraysequence of numbers
namestringstring name for this PMF

@Returns: Pmf object

Makes a PMF from a map from values to probabilities.

@Params:

paramtypedescription
dobjectmapdictionary that maps values to probabilities
namestringstring name for this PMF * @returns Pmf object

Makes a PMF from a sequence of value-probability pairs

@Params:

paramtypedescription
tarraysequence of value-probability pairs
namestringstring name for this PMF * @returns Pmf object

Makes a normalized PMF from a Hist object.

@Params:

paramtypedescription
histhistHist object
namestringstring name

@Returns: Pmf object

Makes a normalized Pmf from a Cdf object.

@Params:

paramtypedescription
cdfcdfCdf object
namestringstring name for the new Pmf

@Returns: Pmf object

Make a mixture distribution.

@Params:

paramtypedescription
metapmfpmfPmf that maps from Pmfs to probs.
namestringstring name for the new Pmf

@Returns: Pmf object

Make a uniform Pmf.

@Params:

paramtypedescription
lownumberlowest value (inclusive)
highnumberhighest value (inclusize)
nnumbernumber of values

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

@Params:

paramtypedescription
itemsarrayunsorted sequence of (value, frequency) pairs
namestringstring name for this CDF

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

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

@Params:

paramtypedescription
dobjectmapdictionary that maps values to frequencies.
namestringstring name for the data.

@Returns: Cdf object

Makes a CDF from a Hist object.

@Params:

paramtypedescription
histhistHist object
namestringstring name for the data.

@Returns: Cdf object

Creates a CDF from an unsorted sequence.

@Params:

paramtypedescription
seqarrayunsorted sequence of sortable values
namestringstring name for the cdf

@Returns: Cdf object

Makes a CDF from a Pmf object.

@Params:

paramtypedescription
pmfpmfPmf object
namestringstring name for the data.

@Returns: Cdf object

Makes a suite from a map from values to probabilities.

@Params:

paramtypedescription
dobjectmapdictionary that maps values to probabilities
namestringstring name for this suite

@Returns: Suite object

Makes a suite from an unsorted sequence of values.

@Params:

paramtypedescription
tarraysequence of numbers
namestringstring name for this suite

Makes a normalized suite from a Hist object.

@Params:

paramtypedescription
histhistHist object
namestringstring name

Makes a normalized Suite from a Cdf object.

@Params:

paramtypedescription
cdfcdfCdf object
namestringstring 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:

import { Decimal } from 'think-bayes';

Decimal.add(0.1, 0.2).toNumber() === 0.3; // true