https://github.com/parksben/think-bayes
An algorithm collection of probability and statistics for browser and Node.js environment. 适用于 浏览器 和 Node.js 环境的概率统计算法集
https://github.com/parksben/think-bayes
Last synced: about 1 year ago
JSON representation
An algorithm collection of probability and statistics for browser and Node.js environment. 适用于 浏览器 和 Node.js 环境的概率统计算法集
- Host: GitHub
- URL: https://github.com/parksben/think-bayes
- Owner: parksben
- License: mit
- Created: 2020-01-04T18:29:52.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T04:51:41.000Z (over 3 years ago)
- Last Synced: 2024-10-19T21:40:17.856Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 441 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# think-bayes



An algorithm collection of probability and statistics for **browser** and **Node.js** environment.
In progress...
> 适用于 **浏览器** 和 **Node.js** 环境的概率统计算法集(非正式版本,功能亟待完善,努力 coding 中...)
## Install
```bash
yarn add think-bayes # OR npm i --save think-bayes
```
## Quickstart
Let us resolve [the cookie problem](https://www.oreilly.com/library/view/think-bayes/13333JSONBOOK/a0000000336.html) by using the class `Suite`:
```js
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](./demo) 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:
```js
import { Pmf, Cdf, Pdf, Suite } from 'think-bayes';
```
[**DictWrapper**](esm/DictWrapper)
[**Pmf**](esm/Pmf) inherits DictWrapper
[**Cdf**](esm/Cdf) inherits DictWrapper
[**Pdf**](esm/Pdf)
[**Suite**](esm/Suite) inherits Pmf
[**Hist**](esm/Hist) inherits DictWrapper
[**Interpolater**](esm/Interpolater)
[**Joint**](esm/Joint) inherits Pmf
[**GaussianPdf**](esm/GaussianPdf) inherits Pdf
[**GaussianKde**](esm/GaussianKde)
[**EstimatedPdf**](esm/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:
```js
import { Util } from 'think-bayes';
const { odds, probability, percentile } = Util;
```
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 | dictionary that maps values to frequencies |
| 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 | dictionary that maps values to probabilities |
| 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 | dictionary that maps values to frequencies. |
| 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 | dictionary that maps values to probabilities |
| 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](http://mikemcl.github.io/decimal.js/)** to handle the problem what calculation of float point number, in the same way, you can use it in this library:
```js
import { Decimal } from 'think-bayes';
Decimal.add(0.1, 0.2).toNumber() === 0.3; // true
```