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

Sample Covariance Calculation and Interpretation

Install and import#

bash
npm install fintech-algorithms
ts
import { sampleCovarianceCalculationAndInterpretation } from "fintech-algorithms/foundations/dependence-regression-and-model-foundations/sample-covariance-calculation-and-interpretation";

Signature#

sampleCovarianceCalculationAndInterpretation(input)

Computes the sample covariance between input.x and input.y using the n-1 denominator, and reports which way the two series move together.

Parameters#

NameTypeNotes
inputD00InputOne record holding the two aligned series x and y, one observation per position.
x: non-empty list of finite numbers, at least two, not all identical · y: finite numbers, same length as x

Returns#

D00Output

An object with sampleCovariance and direction, which reads positive when the covariance is above zero and negative otherwise, exactly zero included.

Errors#

  • When x or y is missing, empty, or contains a non-finite number — throws RangeError
  • When x and y have different lengths, or fewer than two observations — the least-squares fit is computed for every topic in the family before the topic branch is taken — throws RangeError
  • When x is constant, which leaves the regression slope undefined — 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
{
  "x": [1, 2, 3, 4, 5, 6],
  "y": [2, 3, 5, 4, 6, 7],
  "groups": ["A", "A", "A", "B", "B", "B"],
  "predictX": 7,
  "otherPredictor": [2, 4, 5, 8, 9, 13]
}

Call#

sampleCovarianceCalculationAndInterpretation(input)

Returns#

object with 2 fields: sampleCovariance, direction

{
  "sampleCovariance": 3.3,
  "direction": "positive"
}

Diagrams#

Sample Covariance Calculation and Interpretation — article hero
Sample Covariance Calculation and Interpretation — calculation ledger
Sample Covariance Calculation and Interpretation — concept anatomy
Sample Covariance Calculation and Interpretation — failure boundary
Sample Covariance Calculation and Interpretation — method map
Sample Covariance Calculation and Interpretation — 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 Dependence, Regression, and Model Foundations family#