npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

think-bayes

v0.1.3-alpha.7

Published

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

Downloads

12

Readme

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:

| param | type | description | |-------|--------|-------------| | p | number | float 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:

| param | type | description | |-------|--------|-------------------------------| | o | number | float 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:

| param | type | description | |-------|--------|----------------------------| | yes | number | int or float odds in favor | | no | number | int or float odds in favor |

Computes a percentile of a given Pmf.

@Params:

| param | type | description | |------------|--------|-------------| | pmf | pmf | | | percentage | number | float 0-100 |

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

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

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

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

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

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

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

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

Evaluates the binomial pmf.

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

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

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

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

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

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

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 |

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 |

Makes a normalized PMF from a Hist object.

@Params:

| param | type | description | |-------|--------|-------------| | hist | hist | Hist object | | name | string | string name |

@Returns: Pmf object

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

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

Make a uniform Pmf.

@Params:

| param | type | description | |-------|--------|---------------------------| | low | number | lowest value (inclusive) | | high | number | highest value (inclusize) | | n | number | number of values |

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

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

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

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

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

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

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 |

Makes a normalized suite from a Hist object.

@Params:

| param | type | description | |-------|--------|-------------| | hist | hist | Hist object | | name | string | string 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:

import { Decimal } from 'think-bayes';

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