Joint, Marginal, and Conditional Distributions
Install and import#
npm install fintech-algorithmsimport { jointMarginalAndConditionalDistributions } from "fintech-algorithms/foundations/probability-and-random-variables/joint-marginal-and-conditional-distributions";Signature#
jointMarginalAndConditionalDistributions(input)Collapses a joint probability table over each axis in turn to give the two marginal distributions, then renormalises one slice of it to give the conditional distribution of X at a chosen value of Y.
Parameters#
| Name | Type | Notes |
|---|---|---|
input | { pA: number; pB: number; pAB: number; randomValues: number[]; probabilities: number[]; joint: { x: number; y: number; p: number }[]; conditionY: number | string } | joint is the probability table as a flat list of cells, each carrying an x level, a y level, and its probability p. conditionY selects the Y level to condition on and is matched by string form, so 1 and "1" select the same slice. The family-wide pA, pB, pAB, randomValues, and probabilities are validated on every call.pA: 0 <= pA <= 1 · pB: 0 <= pB <= 1 · pAB: 0 <= pAB <= min(pA, pB) · joint: non-empty, every `p` nonnegative, all `p` summing to 1 within 1e-12 · conditionY: must name a Y level whose marginal probability is not zero |
Returns#
{ marginalX: Record<string, number>; marginalY: Record<string, number>; conditionalXGivenY: Record<string, number> }
marginalX and marginalY sum the cell probabilities by X level and by Y level, keyed by the level rendered as a string. conditionalXGivenY divides the cells at the selected Y level by that level's marginal, so its entries sum to 1.
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 - When
jointis empty, a cell probability is negative, or the cell probabilities do not sum to one within 1e-12 — throws RangeError - When
conditionYnames a Y level with zero or missing marginal probability — 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#
jointMarginalAndConditionalDistributions(input)Returns#
object with 2 fields: marginalX, marginalY
{
"marginalX": {
"0": 0.5,
"1": 0.5
},
"marginalY": {
"0": 0.4,
"1": 0.6000000000000001
}
}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