- Two quantities are correlated when knowing one tells you something about the other.
- A deviation is how far a value sits from its mean: x − μ.
- Covariance is the average product of the two deviations. Its sign says which way the cloud of points tilts.
- Variance is the covariance of a variable with itself: the average of (x − μ)².
Ice cream, hot chocolate and coffee
A café writes down the temperature and its sales every day. Three patterns show up.
Ice cream goes up on hot days and down on cold days. The two numbers rise and fall together. We say they are positively correlated.
Hot chocolate does the opposite: it sells on cold days. When the temperature goes up, sales go down. They are negatively correlated.
Coffee shows no up-or-down trend with temperature. Knowing whether it is warmer or colder tells you nothing about whether coffee sales go up or down. They are uncorrelated.
The picture: a tilted cloud
Put each day on a scatter plot: temperature across, sales up. Ice-cream days form a cloud that tilts up to the right. Hot-chocolate days tilt down to the right. Coffee days form a cloud with no tilt at all.
We want one number that captures this tilt. Its sign should say which way the cloud leans, and its size should say how strongly.
Step 1: the deviation
Take one day. Its deviation in temperature is how far that day sits from the average temperature: x − μₓ. Do the same for sales: y − μᵧ. A deviation is positive above the average and negative below it.
Step 2: multiply the two deviations
On a hot day with high sales, both deviations are positive, so the product is positive. On a cold day with low sales, both are negative, and the product is positive again. If high goes with low, one deviation is positive and the other negative, so the product is negative.
Step 3: average the products
Average the products over all N days. That average is the covariance:
Here x[k] means the temperature on day k, and y[k] the sales on day k. We also write the covariance σₓᵧ (σ with the two variable names below it). Σ means "add up over all days k". If most products are positive, the covariance is positive. If most are negative, it is negative. If the two variables are unrelated, positives and negatives cancel and the covariance is close to 0. The worked examples below do all four ice-cream days and get cov = 50.
Keep reading with the full course
Finish lesson 3 and you will be able to: track position and velocity in 2D with the matrix Kalman filter.
Still locked here:
- The rest of this explanation
- 3 more examples with step-by-step solutions
- Five extra members-only problems with worked solutions
$20 once, lifetime access. Lessons 1 and 2 stay free.
Examples
Example 1 · Ice cream on four days
Temperatures 10, 14, 18, 22 °C. Ice-cream sales 20, 35, 30, 55.
Show solutionHide solution
Positive: ice cream and temperature move together. One day (18°, 30 sales) went against the trend, and its product is negative, but the other days win.
Example 2 · Hot chocolate on the same days
Same temperatures, hot-chocolate sales 40, 32, 28, 20.
Show solutionHide solution
Negative: when it gets warmer, hot chocolate sells less.

Questions and comments
Stuck on something, or found a mistake? Ask here and I'll answer. You sign in with GitHub.