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

Discrete and Continuous Random Variables

Install and import#

bash
npm install fintech-algorithms
ts
import { discreteAndContinuousRandomVariables } from "fintech-algorithms/foundations/probability-and-random-variables/discrete-and-continuous-random-variables";

Signature#

discreteAndContinuousRandomVariables(input)

Validates a discrete random variable given as a support and a matching probability vector, returning the confirmed kind, the support, and the total mass it carries. Continuous densities are explained in the article but are not evaluated here.

Parameters#

NameTypeNotes
input{ randomValues: number[]; probabilities: number[]; randomVariableKind?: "discrete"; pA?: number; pB?: number; pAB?: number }randomValues is the support and probabilities the mass on each point, position by position. randomVariableKind declares which kind of random variable the caller is describing; the topic facade confirms it is discrete and defaults it to discrete when absent, because the calculation checks a probability mass function and has no continuous branch. The shared F06 branch also validates the family-wide pA, pB and pAB before any topic runs; they are unused here, so the facade fills them with zero when they are absent.
probabilities: same length as `randomValues`, every entry nonnegative, summing to 1 within 1e-12 · randomVariableKind: optional; must be the string `discrete` when supplied · 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#

{ kind: string; support: number[]; probabilityMass: number }

kind is the confirmed label, always discrete; support is the coerced randomValues; and probabilityMass is the sum of the probabilities, which the guard has already pinned to 1 within 1e-12.

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 randomVariableKind is supplied as anything other than discrete — throws RangeError in TypeScript and ValueError in Python
  • When randomValues or probabilities is missing, empty, or holds a non-finite entry — throws RangeError
  • When randomValues and probabilities differ in length, a probability is negative, or the probabilities do not sum to one within 1e-12 — throws RangeError

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

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
{
  "randomValues": [0, 1, 2],
  "probabilities": [0.2, 0.5, 0.3],
  "randomVariableKind": "discrete"
}

Call#

discreteAndContinuousRandomVariables(input)

Returns#

object with 2 fields: kind, support

{
  "kind": "discrete",
  "support": [0, 1, 2]
}

Diagrams#

Discrete and Continuous Random Variables — article hero
Discrete and Continuous Random Variables — calculation ledger
Discrete and Continuous Random Variables — concept anatomy
Discrete and Continuous Random Variables — failure boundary
Discrete and Continuous Random Variables — method map
Discrete and Continuous Random Variables — scenario contrast

Calculation flow#

Reasoning flow — D00-F06-A06
flowchart LR
    A["Synthetic input + metadata"] --> B{"Contract valid?"}
    B -->|No| C["Reject or route with reason"]
    B -->|Yes| D["Apply Discrete and Continuous Random Variables"]
    D --> E["Formula: X:Ω→ℝ; Σp(x)=1 for discrete support"]
    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#