Covariance

Updated: October 2, 2025

Definition (short)
– Covariance measures how two random variables move together. In finance, it usually refers to how the returns of two assets change relative to their averages: if both tend to be above (or below) their means at the same times, covariance is positive; if one tends to be above when the other is below, covariance is negative.

Key formulas
– Sample covariance (common in empirical work)
Cov(X,Y) = Σ[(X_i − X̄) (Y_i − Ȳ)] / (n − 1)
where X̄ and Ȳ are sample means and n is the number of observations.
– Relation to correlation
Cov(X,Y) = ρ(X,Y) · σ_X · σ_Y
where ρ is the correlation coefficient and σ_X, σ_Y are standard deviations.
– Beta (CAPM shorthand)
β_asset = Cov(asset, market) / Var(market)

How to compute covariance — step‑by‑step
1. Collect paired observations of returns for the two assets (same dates).
2. Compute each asset’s sample mean.
3. For each date, subtract the mean from that day’s return to get deviations.
4. Multiply the two deviations for each date.
5. Sum those products across all dates.
6. Divide the sum by (n − 1) for the sample covariance (or by n for population covariance).

Worked numeric example
Suppose five daily returns (in percent) for Asset A and Asset B:

– A: 1, 2, 0, 1, 3 (mean = 1.4)
– B: 2, 1, −1, 0, 4 (mean = 1.2)

Deviations and products:
– Day 1: (1 − 1.4) = −0.4 ; (2 − 1.2) = 0.8 ; product = −0.32
– Day 2: 0.6 ; −0.2 ; product = −0.12
– Day 3: −1.4 ; −2.2 ; product = 3.08
– Day 4: −0.4 ; −1.2 ; product = 0.48
– Day 5:

– Day 5: (3 − 1.4) = 1.6 ; (4 − 1.2) = 2.8 ; product = 4.48

Sum of products = −0.32 + (−0.12) + 3.08 + 0.48 + 4.48 = 7.60

Sample covariance = sum / (n − 1) = 7.60 / 4 = 1.90 (percent^2)

Interpretation
– A positive covariance (1.90 here) means the two assets tended to move in the same direction over these five days. Because covariance is in squared-percent units, its magnitude is hard to interpret alone; correlation standardizes it between −1 and +1.
– Correlation = covariance / (σA · σB). Compute sample standard deviations from the same data:
– VarianceA = 1.30 ⇒ σA ≈ 1.14%
– VarianceB = 3.70 ⇒ σB ≈ 1.92%
– Correlation ≈ 1.90 / (1.14 · 1.92) ≈ 0.87 (strong positive linear relationship)

Using covariance in portfolio variance
– Formula for two assets: Var(portfolio) = wA^2 VarA + wB^2 VarB + 2 wA wB Cov(A,B).
– Example: wA = 0.6, wB = 0.4
– Var(portfolio) = 0.36·1.30 + 0.16·3.70 + 2·0.6·0.4·1.90
– = 0.468 + 0.592 + 0.912 = 1.972 (percent^2)
– Portfolio SD ≈ sqrt(1.972) ≈ 1.40%

Practical checklist — compute covariance
1. Collect paired observations (equal-length series).
2. Choose population (divide by n) vs sample (divide by n − 1).
3. Compute each series’ mean.
4. Form products of deviations for each pair.
5. Sum and divide by n or (n − 1).
6. Optionally convert to correlation by dividing by the product of standard deviations.

Quick Excel and Python tips
– Excel: use COVARIANCE.S(range1, range2) for sample covariance or COVARIANCE.P for population. CORREL(range1, range2) returns correlation.
– Microsoft docs: https://support.microsoft.com/en-us/office/covariance-s-function-2bff9d6b-945f-4bd2-9a07-d8

Python examples and practical code notes

– Quick note on NumPy vs Pandas:
– NumPy’s numpy.cov computes a covariance matrix. By default it returns sample covariance (normalizes by n − 1). To get a population covariance (divide by n) set ddof=0 or bias=True.
– Pandas’ DataFrame.cov returns sample covariances (ddof=1) and is convenient with labeled data and aligned indices.

– Minimal NumPy example (compute sample covariance matrix for two series):
– Data arrays: x = [x1, x2, …], y = [y1, y2, …]
– Code idea:
– import numpy as np
– X = np.column_stack((x, y))
– cov_matrix = np.cov(X, rowvar=False) # sample covariance matrix
– cov_xy = cov_matrix[0,1]

– Minimal Pandas example:
– import pandas as pd
– df = pd.DataFrame({‘A’: x, ‘B’: y})
– cov_matrix = df.cov() # sample covariance matrix
– cov_ab = cov_matrix.loc[‘A’,’B’]

Worked numeric check (using values from the earlier portfolio example)
– Given: Var(A) = 1.30 (percent^2), Var(B) = 3.70 (percent^2), Cov(A,B) = 1.90 (percent^2).
– Compute standard deviations:
– SD(A) = sqrt(1.30) ≈ 1.1402 (%)
– SD(B) = sqrt(3.70) ≈ 1.9235 (%)
– Convert covariance to correlation:
– Corr(A,B) = Cov(A,B) / (SD(A)·SD(B)) = 1.90 / (1.1402·1.9235) ≈ 1.90 / 2.1932 ≈ 0.867
– Interpretation: positive and relatively strong linear association between A and B.
– Portfolio variance formula (matrix form):
– Let w = [wA, wB] (column vector), Σ = covariance matrix.
– Var(portfolio) = w’ Σ w
– For two assets this expands to: Var_p = wA^2 Var(A) + wB^2 Var(B) + 2 wA wB Cov(A,B)
– Using wA=0.6, wB=0.4 gives Var_p = 1.972 (percent^2) and