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

Covariance and Correlation of Random Variables

Install and import#

bash
npm install fintech-algorithms
ts
import { covarianceAndCorrelationOfRandomVariables } from "fintech-algorithms/foundations/probability-and-random-variables/covariance-and-correlation-of-random-variables";

Signature#

covarianceAndCorrelationOfRandomVariables(input)

Computes the probability-weighted covariance of the two variables in a joint distribution table and divides it by the product of their standard deviations to give the correlation.

Parameters#

NameTypeNotes
input{ pA: number; pB: number; pAB: number; randomValues: number[]; probabilities: number[]; joint: { x: number; y: number; p: number }[] }joint is the probability table as a flat list of cells, each carrying an x level, a y level, and its probability p. These are distributional moments taken over the table, not sample moments over observed data. 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, and neither variable degenerate

Returns#

{ covariance: number; correlation: number }

covariance is the probability-weighted sum of the cross-deviations from the two expected values, and correlation is that covariance divided by the square root of the product of the two variances.

Errors#

  • When input is null, an array, or not an object — throws TypeError
  • When pA, pB, or pAB falls outside [0, 1], or pAB exceeds min(pA, pB) — throws RangeError
  • 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
  • When joint is empty, a cell probability is negative, or the cell probabilities do not sum to one within 1e-12 — throws RangeError
  • When either variable has zero variance under the joint table, leaving the correlation denominator at zero — 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
{
  "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#

covarianceAndCorrelationOfRandomVariables(input)

Returns#

object with 2 fields: covariance, correlation

{
  "covariance": 0.1,
  "correlation": 0.4082482904638631
}

Diagrams#

Covariance and Correlation of Random Variables — article hero
Covariance and Correlation of Random Variables — calculation ledger
Covariance and Correlation of Random Variables — concept anatomy
Covariance and Correlation of Random Variables — failure boundary
Covariance and Correlation of Random Variables — method map
Covariance and Correlation of Random Variables — scenario contrast

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#