Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * as Random from './random';
export * as Regression from './regression';
export * from './statistics';
export * from './vector';
export * from './probability';
90 changes: 90 additions & 0 deletions src/probability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Probability Library

import { factorial } from "./combinatorics";

// Factorial function

// Combinations function
export function combinations(n: number, r: number): number {
if (n < 0 || r < 0 || n < r || !Number.isInteger(n) || !Number.isInteger(r)) {
throw new Error('Invalid input for combinations.');
}
return factorial(n) / (factorial(r) * factorial(n - r));
}

// Probability Mass Function (PMF) for a discrete random variable
export function pmf(x: number, p: number): number {
// Assuming x is a non-negative integer
if (x < 0 || !Number.isInteger(x)) {
throw new Error('PMF is only defined for non-negative integers.');
}
return Math.pow(1 - p, x - 1) * p;
}

// Cumulative Distribution Function (CDF) for a discrete random variable
export function cdf(x: number, p: number): number {
// Assuming x is a non-negative integer
if (x < 0 || !Number.isInteger(x)) {
throw new Error('CDF is only defined for non-negative integers.');
}
return 1 - Math.pow(1 - p, x);
}

// Binomial Distribution Probability
export function binomialProbability(x: number, n: number, p: number): number {
// Assuming x is a non-negative integer
if (x < 0 || !Number.isInteger(x)) {
throw new Error('Binomial distribution is only defined for non-negative integers.');
}
return combinations(n, x) * Math.pow(p, x) * Math.pow(1 - p, n - x);
}

// Geometric Distribution Probability
export function geometricProbability(x: number, p: number): number {
// Assuming x is a non-negative integer
if (x < 0 || !Number.isInteger(x)) {
throw new Error('Geometric distribution is only defined for non-negative integers.');
}
return Math.pow(1 - p, x - 1) * p;
}

// Poisson Distribution Probability
export function poissonProbability(x: number, λ: number): number {
// Assuming x is a non-negative integer
if (x < 0 || !Number.isInteger(x)) {
throw new Error('Poisson distribution is only defined for non-negative integers.');
}
return Math.exp(-λ) * Math.pow(λ, x) / factorial(x);
}

// Normal Distribution Probability
export function normalProbability(x: number, mean: number, stdDev: number): number {
return (1 / (stdDev * Math.sqrt(2 * Math.PI))) * Math.exp(-0.5 * Math.pow((x - mean) / stdDev, 2));
}

// Exponential Distribution Probability
export function exponentialProbability(x: number, λ: number): number {
return λ * Math.exp(-λ * x);
}

// Uniform Distribution Probability
export function uniformProbability(x: number, a: number, b: number): number {
if (x >= a && x <= b) {
return 1 / (b - a);
}
return 0;
}

// Joint Probability of two independent events
export function jointProbability(eventA: number, eventB: number): number {
return eventA * eventB;
}

// Conditional Probability of event A given that event B has occurred
export function conditionalProbability(eventA: number, eventB: number): number {
if (eventB === 0) {
throw new Error('Cannot calculate conditional probability when eventB has zero probability.');
}
return eventA / eventB;
}