What is Correlation?
Correlation measures how consistently two variables move together. If taller people tend to weigh more, height and weight are positively correlated. If more hours studied tends to mean fewer errors made, study time and error count are negatively correlated. If shoe size tells you nothing about salary, they're uncorrelated.
The key word is tend — correlation is a statistical tendency across many observations, not a rule that holds for every individual case.
Pearson's Correlation Coefficient (r)
The most common correlation measure. It calculates how much two variables deviate from their means in the same direction at the same time.
You don't need to calculate this by hand — but understanding what it measures helps: if whenever x is above its mean, y is also above its mean, the numerator grows large and positive. If they move in opposite directions, it's negative. The denominator normalises so the result always falls between −1 and +1.
These thresholds are conventions, not hard rules. A correlation of 0.3 might be practically irrelevant in physics but highly meaningful in psychology or social science.
Pearson's r only measures linear relationships. Two variables can be strongly related in a curved way and still score r ≈ 0. Always plot your data — don't trust the number alone.
Correlation ≠ Causation
This is the single most important lesson in statistics. Finding that A and B are correlated does not tell you that A causes B, that B causes A, or that either causes the other.
Three possibilities when A and B correlate:
- A causes B — studying more causes fewer errors
- B causes A — people who make fewer errors feel more confident and study more
- C causes both — academic motivation (C) causes both more studying and fewer errors
Establishing causation requires a controlled experiment (randomised assignment) or sophisticated statistical methods — not just a correlation coefficient.
The Correlation Matrix & Heatmap
When you have multiple numeric variables, you can compute the correlation between every possible pair and arrange them in a grid — the correlation matrix. Visualised as a heatmap with colour coding, it lets you scan an entire dataset's relationships at a glance.
The only strong relationship: prep_time and total_time at 0.686. This is a mathematical identity — total_time = prep_time + delivery_time, so of course they correlate. It's not a discovery; it's arithmetic.
Multivariate Analysis — Looking at More Than Two Variables
Real-world relationships rarely involve just two variables. A customer's satisfaction might depend on delivery time, food quality, order cost, and time of day — simultaneously. Multivariate analysis examines these joint relationships.
Common approaches used in the FoodHub project:
- Groupby + aggregation: Average order cost grouped by cuisine type. Average delivery time grouped by day of week. This is "conditional analysis" — how does Y change across values of X?
- Pairplot: Visualise all pairwise relationships simultaneously. A quick scan for anything worth investigating.
- Heatmap: Quantify correlation strengths across all variable pairs at once.
The multivariate analysis revealed that cost, prep time, and delivery time are all largely independent of each other — and none of them strongly predicts customer ratings. The only meaningful correlation (prep_time ↔ total_time at 0.686) is a mathematical consequence of how total_time is defined. The weekday vs. weekend delivery difference, confirmed statistically, emerged from groupby analysis — not from the correlation matrix. See Chapter 10 — Multivariate Analysis →