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

Probability Rules, Complements, Unions, and Intersections

Install and import#

bash
npm install fintech-algorithms
ts
import { probabilityRulesComplementsUnionsAndIntersections } from "fintech-algorithms/foundations/probability-and-random-variables/probability-rules-complements-unions-and-intersections";

Signature#

probabilityRulesComplementsUnionsAndIntersections(input)

Applies the complement and addition rules to a pair of events, deriving the probability that A does not happen and the probability that at least one of A or B happens.

Parameters#

NameTypeNotes
input{ pA: number; pB: number; pAB: number }pA and pB are the marginal probabilities of the two events and pAB the probability of both together. The joint probability is taken as given, not inferred, so it must be consistent with the two marginals.
pA: 0 <= pA <= 1 · pB: 0 <= pB <= 1 · pAB: 0 <= pAB <= min(pA, pB)

Returns#

{ complementA: number; union: number; intersection: number }

complementA is 1 - pA, union is pA + pB - pAB, and intersection echoes the supplied pAB so all three rule terms sit side by side.

Errors#

  • When input is null, an array, or not an object — throws TypeError
  • When pA, pB, or pAB falls outside [0, 1], or pAB exceeds min(pA, pB) — 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
{
  "pA": 0.6,
  "pB": 0.5,
  "pAB": 0.3,
  "outcomes": ["up", "flat", "down"],
  "event": ["up", "flat"],
  "prior": 0.01,
  "sensitivity": 0.9,
  "falsePositiveRate": 0.05,
  "randomValues": [0, 1, 2],
  "probabilities": [0.2, 0.5, 0.3],
  "randomVariableKind": "discrete",
  "joint": [
    {
      "x": 0,
      "y": 0,
      "p": 0.3
    },
    {
      "x": 0,
      "y": 1,
      "p": 0.2
    },
    {
      "x": 1,
      "y": 0,
      "p": 0.1
    }
  ],
  "conditionY": 1
}

Call#

probabilityRulesComplementsUnionsAndIntersections(input)

Returns#

object with 2 fields: complementA, union

{
  "complementA": 0.4,
  "union": 0.8
}

Diagrams#

Probability Rules, Complements, Unions, and Intersections — article hero
Probability Rules, Complements, Unions, and Intersections — calculation ledger
Probability Rules, Complements, Unions, and Intersections — concept anatomy
Probability Rules, Complements, Unions, and Intersections — failure boundary
Probability Rules, Complements, Unions, and Intersections — method map
Probability Rules, Complements, Unions, and Intersections — scenario contrast

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#