Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

think-bayes

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

think-bayes

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

  • 0.1.3-alpha.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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;
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:

paramtypedescription
pnumberfloat 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:

paramtypedescription
onumberfloat 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:

paramtypedescription
yesnumberint or float odds in favor
nonumberint or float odds in favor
percentile(pmf, percentage)

Computes a percentile of a given Pmf.

@Params:

paramtypedescription
pmfpmf
percentagenumberfloat 0-100
credibleInterval(pmf, percentage = 90)

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

pmfProbLess(pmf1, pmf2)

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

@Params:

paramtypedescription
pmf1pmfPmf object
pmf2pmfPmf object

@Returns: float probability

pmfProbGreater(pmf1, pmf2)

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

@Params:

paramtypedescription
pmf1pmfPmf object
pmf2pmfPmf object

@Returns: float probability

pmfProbEqual(pmf1, pmf2)

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

@Params:

paramtypedescription
pmf1pmfPmf object
pmf2pmfPmf object

@Returns: float probability

randomSum(dists)

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

@Params:

paramtypedescription
distsarraysequence of Pmf or Cdf objects

@Returns: numerical sum

sampleSum(dists, n)

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

evalGaussianPdf(x, mu, sigma)

Computes the unnormalized PDF of the normal distribution.

@Params:

paramtypedescription
xnumbervalue
munumbermean
sigmanumberstandard deviation

@Returns: float probability density

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

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

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:

paramtypedescription
knumbernumber of events
lamnumberparameter lambda in events per unit time

@Returns: float probability

makeJoint(pmf1, pmf2)

Joint distribution of values from pmf1 and pmf2.

@Params:

paramtypedescription
pmf1pmfPmf object
pmf2pmfPmf object

@Returns: Joint pmf of value pairs

makeHistFromList(t, name)

Makes a histogram from an unsorted sequence of values.

@Params:

paramtypedescription
tarraysequence of numbers
namestringstring name for this histogram

@Returns: Hist object

makeHistFromDict(d, name)

Makes a histogram from a map from values to frequencies.

@Params:

paramtypedescription
dobjectmap
namestringstring name for this histogram

@Returns: Hist object

makePmfFromList(t, name)

Makes a PMF from an unsorted sequence of values.

@Params:

paramtypedescription
tarraysequence of numbers
namestringstring name for this PMF

@Returns: Pmf object

makePmfFromDict(d, name)

Makes a PMF from a map from values to probabilities.

@Params:

paramtypedescription
dobjectmap
namestringstring name for this PMF * @returns Pmf object
makePmfFromItems(t, name)

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
makePmfFromHist(hist, name)

Makes a normalized PMF from a Hist object.

@Params:

paramtypedescription
histhistHist object
namestringstring name

@Returns: Pmf object

makePmfFromCdf(cdf, name)

Makes a normalized Pmf from a Cdf object.

@Params:

paramtypedescription
cdfcdfCdf object
namestringstring name for the new Pmf

@Returns: Pmf object

makeMixture(metapmf, name = 'mix')

Make a mixture distribution.

@Params:

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

@Returns: Pmf object

makeUniformPmf(low, high, n)

Make a uniform Pmf.

@Params:

paramtypedescription
lownumberlowest value (inclusive)
highnumberhighest value (inclusize)
nnumbernumber of values
makeCdfFromItems(items, name = '')

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

makeCdfFromDict(d, name)

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

@Params:

paramtypedescription
dobjectmap
namestringstring name for the data.

@Returns: Cdf object

makeCdfFromHist(hist, name)

Makes a CDF from a Hist object.

@Params:

paramtypedescription
histhistHist object
namestringstring name for the data.

@Returns: Cdf object

makeCdfFromList(seq, name)

Creates a CDF from an unsorted sequence.

@Params:

paramtypedescription
seqarrayunsorted sequence of sortable values
namestringstring name for the cdf

@Returns: Cdf object

makeCdfFromPmf(pmf, name)

Makes a CDF from a Pmf object.

@Params:

paramtypedescription
pmfpmfPmf object
namestringstring name for the data.

@Returns: Cdf object

makeSuiteFromDict(d, name)

Makes a suite from a map from values to probabilities.

@Params:

paramtypedescription
dobjectmap
namestringstring name for this suite

@Returns: Suite object

makeSuiteFromList(t, name)

Makes a suite from an unsorted sequence of values.

@Params:

paramtypedescription
tarraysequence of numbers
namestringstring name for this suite
makeSuiteFromHist(hist, name)

Makes a normalized suite from a Hist object.

@Params:

paramtypedescription
histhistHist object
namestringstring name
makeSuiteFromCdf(cdf, 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

Keywords

FAQs

Package last updated on 31 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc