Covariance Calculator
Find the covariance between two paired data sets — population or sample. Enter your values below, and see the formula, a worked example, and ready-to-use Python code underneath.
Enter your data
Comma, space, or newline separated.
Must have the same number of values as X, in matching order.
Please enter two equal-length lists of at least 2 paired values.
What covariance tells you
Covariance measures the direction of the linear relationship between two paired variables: whether they tend to increase together (positive covariance), move in opposite directions (negative covariance), or show no consistent linear pattern (covariance near zero).
Because covariance is expressed in the product of the two variables' original units, its magnitude is hard to compare across different data sets. That's why the correlation coefficient — covariance rescaled to always fall between -1 and 1 — is usually reported alongside it, or instead of it.
Covariance formula
Sample: Cov(X,Y) = Σ(xᵢ − x̄)(yᵢ − ȳ) / (n − 1)
Python code
You can compute covariance manually, or with Python's statistics or NumPy:
# Method 1 — manual calculation (sample) X = [2, 4, 6, 8, 10] Y = [65, 70, 75, 85, 95] n = len(X) mean_x, mean_y = sum(X) / n, sum(Y) / n cov_sample = sum((x - mean_x) * (y - mean_y) for x, y in zip(X, Y)) / (n - 1) print(cov_sample) # 37.5 # Method 2 — statistics module (Python 3.10+, sample) import statistics statistics.covariance(X, Y) # Method 3 — NumPy (returns a 2x2 covariance matrix) import numpy as np np.cov(X, Y, ddof=1)[0, 1] # sample covariance np.cov(X, Y, ddof=0)[0, 1] # population covariance
Worked example
Using the same hours-studied (X) and exam-score (Y) pairs as the correlation calculator example:
| Hours studied (X) | 2 | 4 | 6 | 8 | 10 |
|---|---|---|---|---|---|
| Exam score (Y) | 65 | 70 | 75 | 85 | 95 |
- 1Find the means.
x̄ = 6, ȳ = 78 - 2Multiply each pair's deviations and sum them.
Σ(xᵢ−x̄)(yᵢ−ȳ) = 150 - 3Divide by n (population) or n − 1 (sample).
Population: 150 / 5 = 30 | Sample: 150 / 4 = 37.5
If these five students are the entire class, the population covariance is 30. If they're a sample from a larger class, the sample covariance is 37.5 — either way, a positive value confirming hours studied and exam score move together.
Frequently asked questions
What does a positive or negative covariance mean?
Positive covariance means the two variables tend to move in the same direction — when one is above its mean, the other tends to be too. Negative covariance means they tend to move in opposite directions. A covariance near zero suggests little linear relationship.
Why is covariance hard to interpret on its own?
Covariance is expressed in the product of the two variables' units (for example, dollars times hours), and its size depends on the scale of the data, not just the strength of the relationship. This makes it hard to judge whether a given covariance is "large" without more context — which is why correlation, a standardized version of covariance, is usually reported instead.
Should I use population or sample covariance?
Use the population formula (divide by N) only when your paired data represents every member of the group you care about. Use the sample formula (divide by n − 1) when your data is a subset drawn from a larger population, which is the more common situation in practice.
What is the covariance of a variable with itself?
The covariance of a variable with itself equals its variance. This follows directly from the formula: if X and Y are the same variable, each (xᵢ − mean) times (yᵢ − mean) becomes a squared deviation, which is exactly what variance averages.