The Probability Distributions Project is a TypeScript library that provides implementations of various probability distributions, including Bernoulli, Binomial, Poisson, Geometric, and Negative Binomial distributions. It offers methods to calculate the expected value, variance, and probability at specific values for each distribution. The project also includes examples of rendering distributions to generate visual charts.
// index.ts
import { log } from './utils'
import {
BinomialDistribution,
BernoulliDistribution,
PoissonDistribution,
GeometricDistribution,
} from './distributions'
// Create instances of distributions
const bernoulli = new BernoulliDistribution(0.5)
const binomial = new BinomialDistribution(4, 1 / 3)
const poisson = new PoissonDistribution(5)
const geometric = new GeometricDistribution(0.5)
// Log information about the distributions
log(bernoulli)
log(binomial, 2)
log(poisson, 4)
log(geometric, 2)
Code: Creates instances of each distribution with specific parameters. Finally, it uses the log function from 'utils.ts' to log information about each distribution, including the calculated value, expected value, variance, and probability at a specific value (if provided).
Run npm run log
to see the output logged in the terminal.
The render/[distribution_name].ts
files provide examples of rendering distributions. Each file demonstrates the rendering process for a specific distribution. Below are the available render examples:
It generates a chart showing the distribution of values obtained from multiple calculations of the geometric distribution.
- Geometric Distribution:
npm run render:geo
- Negative Binomial Distribution:
npm run render:negbin
- Negative Binomial Distribution Chart (10_000 calculations for getting 3rd win on 10th try with 8%):
- Geometric Distribution Chart (100_000 times trying until getting %1):
First: Add a .ts file in the render
directory to create a new example. Import the class of your distribution. Then:
- Update the
totalTimes
variable to specify the number of calculations to perform. - Run the file using a TypeScript compiler or runtime environment. Or simply add it to the package.json file as a script and run it using
npm run render:[name]
. - The script will output the progress of the calculations as a percentage.
- Once the calculations are complete, a chart image will be saved as
./generated/{distribution}_chart.png
, where{distribution}
represents the specific distribution used.
The utils.ts
file provides two utility functions and a Calc
class.
The noop
function is an empty function that does nothing. It can be used as a placeholder or a no-operation function.
The log
function is an asynchronous function that logs information about a given distribution. It takes the distribution instance (dist
) and an optional value (probAt
) as parameters. It calculates the value of the distribution, expected value, variance, and probability at the specified value (if provided) using the methods of the distribution class. The logged information includes the distribution's ID, calculated value, expected value, variance, and probability.
The Calc
class provides mathematical calculation functions used by the distributions. It includes functions for factorial, power, combination, and the mathematical constant e
.
Make sure to import the log
function and Calc
class from utils.ts
when using them.