Measure how two variables move together: sample covariance = sum((xi−mx)× (yi−my)) / (n−1). Three loops: one for mean_x, one for mean_y, one for the sum of products. Divide by n−1 (sample covariance, matching the variance convention). With numpy, np.cov(x, y)[0, 1] defaults to ddof=1 and gives the same value. Data: n=5 pairs, mx=3, my=4, cov_sum=8, cov=2.0.

By hand

With the library

np.cov(x, y) returns a 2×2 covariance matrix; [0, 1] extracts the off-diagonal element (covariance of x and y). Default ddof=1 — same denominator as the naive loop.

naive.py
x = [1, 2, 3, 4, 5]
y = [2, 4, 4, 4, 6]
n = len(x)
total_x = 0.0
for v in x:
    total_x = total_x + v
mx = total_x / n
total_y = 0.0
for v in y:
    total_y = total_y + v
my = total_y / n
cov_sum = 0.0
for i in range(n):
    cov_sum = cov_sum + (x[i] - mx) * (y[i] - my)
cov = cov_sum / (n - 1)
print('RESULT:', round(cov, 10))
library.py
import numpy as np
from dalib.display import set_display
set_display()

x = [1, 2, 3, 4, 5]
y = [2, 4, 4, 4, 6]
cov = np.cov(x, y)[0, 1]
print('cov (ddof=1):', round(float(cov), 10))
print('RESULT:', round(float(cov), 10))
cov (ddof=1): 2.0
RESULT: 2.0

Honesty

This lesson shows the computation of covariance exactly, on a tiny pinned sample. The value is correct and reproducible, but on so few points it demonstrates the mechanism, not a population conclusion — the sign and magnitude here are not a valid statistical finding. Real inference needs an adequate sample size and assumption checks; read it as "how the statistic is computed," not as evidence about how the two variables move together in general.

Implementation notes

  • Sign indicates direction: positive means x and y tend to rise together; negative means one rises as the other falls; zero means no linear relationship.
  • Covariance is scale-dependent: doubling all y values doubles the covariance. This makes comparing covariances across datasets unintuitive — normalizing by the standard deviations gives the unitless Pearson r.
  • Sample (n−1) vs population (n): use n−1 to get an unbiased estimate of the population covariance from a sample, consistent with sample-variance.
  • Cross-reference: pearson-correlation (this chapter) for the normalized version; sample-variance (ch02) for the n−1 convention.