Discrete and Continuous Random Variables
Install and import#
npm install fintech-algorithmsimport { 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#
| Name | Type | Notes |
|---|---|---|
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
inputis null, an array, or not an object — throws TypeError - When a supplied
pA,pB, orpABfalls outside [0, 1], orpABexceedsmin(pA, pB)— throws RangeError - When
randomVariableKindis supplied as anything other thandiscrete— throws RangeError in TypeScript and ValueError in Python - When
randomValuesorprobabilitiesis missing, empty, or holds a non-finite entry — throws RangeError - When
randomValuesandprobabilitiesdiffer 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#
{
"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#
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.
References#
- Probability Distributions — NIST/SEMATECH e-Handbook
- Probability Distributions — SciPy User Guide
- Historical-example decision