fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Bayes' Theorem and Base Rates

Install and import#

bash
npm install fintech-algorithms
ts
import { bayesTheoremAndBaseRates } from "fintech-algorithms/foundations/probability-and-random-variables/bayes-theorem-and-base-rates";

Signature#

bayesTheoremAndBaseRates(input)

Turns a prior, a true-positive rate, and a false-positive rate into the posterior probability of the condition given a positive result, along with the unconditional probability of that result.

Parameters#

NameTypeNotes
input{ prior: number; sensitivity: number; falsePositiveRate: number; pA?: number; pB?: number; pAB?: number }prior is the base rate of the condition, sensitivity the chance of a positive result when the condition holds, and falsePositiveRate the chance of a positive result when it does not. Those three fields are the whole input. The shared F06 branch also validates the family-wide pA, pB and pAB before any topic runs; Bayes uses none of them, so the topic facade fills them with zero when they are absent. They may still be supplied, and are validated when they are.
prior: 0 <= prior <= 1 · sensitivity: 0 <= sensitivity <= 1 · falsePositiveRate: 0 <= falsePositiveRate <= 1 · pA: optional; 0 <= pA <= 1 when supplied, otherwise 0 · pB: optional; 0 <= pB <= 1 when supplied, otherwise 0 · pAB: optional; 0 <= pAB <= min(pA, pB) when supplied, otherwise 0

Returns#

{ posterior: number; evidenceProbability: number }

evidenceProbability is sensitivity * prior + falsePositiveRate * (1 - prior), the total chance of a positive result, and posterior is sensitivity * prior divided by it.

Errors#

  • When input is null, an array, or not an object — throws TypeError
  • When a supplied pA, pB, or pAB falls outside [0, 1], or pAB exceeds min(pA, pB) — throws RangeError
  • When prior, sensitivity, or falsePositiveRate falls outside [0, 1] — throws RangeError
  • When the evidence probability works out to exactly zero, leaving the posterior undefined — throws RangeError

Complexity: time O(1), space O(1).

Worked example#

verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

Input#

input
{
  "prior": 0.01,
  "sensitivity": 0.9,
  "falsePositiveRate": 0.05
}

Call#

bayesTheoremAndBaseRates(input)

Returns#

object with 2 fields: posterior, evidenceProbability

{
  "posterior": 0.15384615384615385,
  "evidenceProbability": 0.0585
}

Diagrams#

Bayes' Theorem and Base Rates — article hero
Bayes' Theorem and Base Rates — calculation ledger
Bayes' Theorem and Base Rates — concept anatomy
Bayes' Theorem and Base Rates — failure boundary
Bayes' Theorem and Base Rates — method map
Bayes' Theorem and Base Rates — scenario contrast

Calculation flow#

Reasoning flow — D00-F06-A05
flowchart LR
    A["Synthetic input + metadata"] --> B{"Contract valid?"}
    B -->|No| C["Reject or route with reason"]
    B -->|Yes| D["Apply Bayes' Theorem and Base Rates"]
    D --> E["Formula: P(H|E)=P(E|H)P(H)/[P(E|H)P(H)+P(E|¬H)P(¬H)]"]
    E --> F["Verified fixture output"]
    F --> G{"Interpretation within boundary?"}
    G -->|Yes| H["Report value + convention + audit"]
    G -->|No| I["Add companion view or narrower claim"]

How it works#

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References#

The rest of the Probability and Random Variables family#