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, echoing back the declared kind, the support, and the total mass it carries.
Parameters#
| Name | Type | Notes |
|---|---|---|
input | { pA: number; pB: number; pAB: number; randomValues: number[]; probabilities: number[]; randomVariableKind: string } | randomValues is the support and probabilities the mass on each point, position by position. randomVariableKind is a caller-supplied label that is passed through untouched. The family-wide pA, pB, and pAB are validated on every call.pA: 0 <= pA <= 1 · pB: 0 <= pB <= 1 · pAB: 0 <= pAB <= min(pA, pB) · probabilities: same length as `randomValues`, every entry nonnegative, summing to 1 within 1e-12 |
Returns#
{ kind: string; support: number[]; probabilityMass: number }
kind is the label as supplied, 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
pA,pB, orpABfalls outside [0, 1], orpABexceedsmin(pA, pB)— throws RangeError - 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#
{
"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#
discreteAndContinuousRandomVariables(input)Returns#
object with 2 fields: kind, support
{
"kind": "discrete",
"support": [0, 1, 2]
}Diagrams#
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