Conditional Probability
Install and import#
npm install fintech-algorithmsimport { conditionalProbability } from "fintech-algorithms/foundations/probability-and-random-variables/conditional-probability";Signature#
conditionalProbability(input)Rescales the joint probability of two events by each marginal in turn, giving the probability of A once B is known and the probability of B once A is known.
Parameters#
| Name | Type | Notes |
|---|---|---|
input | { pA: number; pB: number; pAB: number } | pA and pB are the marginal probabilities and pAB the joint. Both marginals must be strictly positive, since each one is used as a denominator.pA: 0 <= pA <= 1 · pB: 0 <= pB <= 1 · pAB: 0 <= pAB <= min(pA, pB) · conditioning: pA and pB must both be strictly greater than 0 |
Returns#
{ pAGivenB: number; pBGivenA: number }
pAGivenB is pAB / pB and pBGivenA is pAB / pA.
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
pAorpBis exactly zero, which would divide by a zero-probability conditioning event — 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#
{
"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#
conditionalProbability(input)Returns#
object with 2 fields: pAGivenB, pBGivenA
{
"pAGivenB": 0.6,
"pBGivenA": 0.5
}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