Sub topic 10
Covariance and correlation
This chapter assumes you can compute a mean, variance and standard deviation, and that you know the sample versus population distinction (dividing by n - 1 versus n). It also assumes you have met the idea of a test statistic from the earlier chapters. Covariance and correlation are the foundation for the next two topics: testing a correlation and building a regression line.
What covariance measures
Variance describes how one variable spreads around its mean. Covariance describes how two variables move together. Take paired observations (x, y). For each pair, multiply how far x is above or below its mean by how far y is above or below its mean. Then average those products.
The sample covariance is:
Cov(X, Y) = Σ (xᵢ - x̄)(yᵢ - ȳ) / (n - 1)
The sign tells you the direction of the relationship:
- A positive covariance means the variables tend to be above their means together, or below their means together.
- A negative covariance means one tends to be above its mean when the other is below.
- A covariance near zero means there is no consistent linear pattern.
Dividing by n - 1 makes the sample estimate unbiased, as with the sample variance. The CFA curriculum uses n - 1 for sample statistics, so use it unless a question says the data are the whole population.
Covariance of a variable with itself is its variance: Cov(X, X) = Var(X).
Why covariance is hard to read
The size of the covariance depends on the units of both variables. If you measure x in centimetres instead of metres, the covariance becomes 100 times larger. The relationship has not changed. So you cannot tell from the number alone whether a relationship is strong or weak. A covariance of 4.975 could be a very tight relationship or a loose one, depending on the scales.
Correlation: covariance without the units
The correlation coefficient fixes the scale problem by dividing the covariance by both standard deviations:
r = Cov(X, Y) / (sₓ · s_y)
Dividing cancels the units, so r is a pure number between -1 and +1.
| Value of r | Interpretation |
|---|---|
| +1 | Perfect positive linear relationship |
| Between 0 and +1 | Positive linear tendency, stronger as r approaches 1 |
| 0 | No linear relationship |
| Between -1 and 0 | Negative linear tendency, stronger as r approaches -1 |
| -1 | Perfect negative linear relationship |
Changing units, such as multiplying x by 100 or adding a constant to y, does not change r. This invariance makes r the standard measure of strength.
A worked example
Use five paired observations: x = 1, 2, 3, 4, 5 and y = 2.1, 3.9, 6.2, 7.8, 10.1.
- The means are x̄ = 3 and ȳ = 6.02.
- The x deviations are -2, -1, 0, 1, 2. The y deviations are -3.92, -2.12, 0.18, 1.78, 4.08.
- The products of deviations are 7.84, 2.12, 0, 1.78 and 8.16. They sum to 19.9.
- The covariance is 19.9 / 4 = 4.975.
- The sum of squared x deviations is 10, so Var(x) = 2.5 and sₓ ≈ 1.5811. The sum of squared y deviations is 39.708, so Var(y) = 9.927 and s_y ≈ 3.1507.
- The correlation is 4.975 / (1.5811 × 3.1507) ≈ 0.9987.
The covariance is positive and the correlation is close to +1, so x and y have a very strong positive linear relationship.
Checking the arithmetic in code
The script below repeats the calculation by hand, compares it with NumPy, and then shows two points from the rest of the chapter. Rescaling x changes the covariance but not the correlation. A perfect nonlinear relationship can produce a correlation of zero.
import numpy as np
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([2.1, 3.9, 6.2, 7.8, 10.1])
n = len(x)
cov_manual = ((x - x.mean()) * (y - y.mean())).sum() / (n - 1)
r_manual = cov_manual / (x.std(ddof=1) * y.std(ddof=1))
print("manual:", cov_manual, r_manual)
print("numpy: ", np.cov(x, y, ddof=1)[0, 1], np.corrcoef(x, y)[0, 1])
# Rescaling x by 100 changes the covariance but not the correlation
x_scaled = x * 100
print("scaled:", np.cov(x_scaled, y, ddof=1)[0, 1], np.corrcoef(x_scaled, y)[0, 1])
# A perfect but nonlinear relationship: y2 = x2 squared
x2 = np.arange(-3, 4, dtype=float)
y2 = x2 ** 2
print("nonlinear r:", round(np.corrcoef(x2, y2)[0, 1], 10))The manual and NumPy values agree. The scaled covariance is 100 times larger (497.5), while r stays at about 0.9987. The last line prints 0.0, even though y2 is completely determined by x2.
Limitations of the correlation coefficient
The CFA exam tests these limitations often, so learn them well.
Only linear relationships
Correlation measures linear association. In the example above, y is an exact function of x, yet r is zero because the relationship is a U shape, not a line. A correlation of zero does not mean the variables are unrelated.
Outliers
One extreme pair can raise or lower r a lot, because the calculation multiplies deviations and extreme deviations dominate the sum. Always look at a scatter plot, not just the number.
Spurious correlation
A high r can arise without any meaningful link. There are three common causes:
- Pure chance in a small or heavily searched data set.
- A third variable that drives both series.
- A relationship that exists only in a subset of the data.
Correlation does not establish causation. If you test many pairs of variables, some will show a high correlation by luck alone.
Sample estimate, not the truth
The value of r computed from a sample is an estimate of the population correlation. It varies from sample to sample. Whether the population correlation differs from zero is a hypothesis-testing question, which the next chapter handles.
Relating r to regression
Correlation and simple regression are closely linked. The regression slope is the covariance divided by the variance of x:
b₁ = Cov(X, Y) / Var(X)
You can also write b₁ = r × (s_y / sₓ). In the worked example the slope is 4.975 / 2.5 = 1.99. Correlation is symmetric, so r between x and y equals r between y and x. The slope is not symmetric, because it depends on which variable you treat as dependent. In simple regression, R² equals r². Here that is about 0.9973, so x explains about 99.7% of the variation in y.
Common errors
- Reporting a covariance as if its size indicated strength. Use r for strength.
- Dividing by n instead of n - 1 for sample data.
- Reading a high r as proof that one variable causes the other.
- Forgetting that r only captures straight-line patterns.
- Mixing up the sign of r with the sign of covariance. They always match, since the standard deviations are positive.
Exercise
The sample covariance between two stock returns is 12 (in %²). The sample standard deviations are 4% and 5%. Compute the correlation. If both returns are then expressed in decimals instead of percent, what happens to the covariance and the correlation?
Show answer
r = 12 / (4 × 5) = 0.6. Converting percent to decimals divides each return by 100, so the covariance is divided by 100 × 100 = 10,000 and becomes 0.0012. The correlation stays 0.6, because it has no units.
Exercise
A scatter plot shows y = x² for x values spread evenly from -3 to +3, and the computed correlation is 0. Does this show that x and y are unrelated? Explain briefly.
Show answer
No. The correlation measures only linear association. y is perfectly determined by x, but the relationship is a U shape, and the positive and negative products of deviations cancel. A zero correlation rules out a linear relationship, not a nonlinear one.