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

Covariance Matrices, Portfolio Variance, and Diversification

Install and import#

bash
npm install fintech-algorithms
ts
import { covarianceMatricesPortfolioVarianceAndDiversification } from "fintech-algorithms/foundations/financial-risk-and-performance-statistics/covariance-matrices-portfolio-variance-and-diversification";

Signature#

covarianceMatricesPortfolioVarianceAndDiversification(input)

Combines portfolio weights with a covariance matrix into portfolio variance, and isolates the share of it that comes from the off-diagonal terms.

Parameters#

NameTypeNotes
inputD00InputReads returns and benchmark, two aligned non-empty lists of finite periodic returns, frequency, confidence, plus weights, the asset weights summing to one, and covarianceMatrix, a square matrix of the same order as weights.

Returns#

D00Output

portfolioVariance is the quadratic form of the weights against the matrix, floored at zero, portfolioVolatility its square root, covarianceContribution the amount by which it exceeds the sum of the squared weights times the diagonal variances, and weightsSum the total of the supplied weights.

Errors#

  • When returns or benchmark is absent, empty, or holds a non-finite number — throws RangeError
  • When returns and benchmark differ in length, or hold fewer than two observations — throws RangeError
  • When frequency is zero or negative — throws RangeError
  • When weights and covarianceMatrix are not square-aligned, or weights does not sum to one within 1e-12 — throws RangeError
  • When the quadratic form comes out below -1e-12, so the matrix is not positive semi-definite — throws RangeError
  • When confidence is absent, or outside the range zero to one — the engine reads it for every topic from A04 onward, including those that never use it — throws RangeError

Complexity: time O(n log n + k^2), 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
{
  "returns": [0.01, -0.02, 0.015, -0.01, 0.03],
  "benchmark": [0.008, -0.01, 0.012, -0.006, 0.02],
  "frequency": 252,
  "target": 0,
  "confidence": 0.8,
  "riskFree": 0.0001,
  "weights": [0.6, 0.4],
  "covarianceMatrix": [
    [0.04, 0.01],
    [0.01, 0.09]
  ]
}

Call#

covarianceMatricesPortfolioVarianceAndDiversification(input)

Returns#

object with 2 fields: portfolioVariance, portfolioVolatility

{
  "portfolioVariance": 0.033600000000000005,
  "portfolioVolatility": 0.18330302779823363
}

Diagrams#

Covariance Matrices, Portfolio Variance, and Diversification — article hero
Covariance Matrices, Portfolio Variance, and Diversification — calculation ledger
Covariance Matrices, Portfolio Variance, and Diversification — concept anatomy
Covariance Matrices, Portfolio Variance, and Diversification — failure boundary
Covariance Matrices, Portfolio Variance, and Diversification — method map
Covariance Matrices, Portfolio Variance, and Diversification — 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 Financial Risk and Performance Statistics family#