7 · Five-number summary & correlation
Boxplots, covariance, Pearson and Spearman — the toolkit for summarising relationships and spotting multicollinearity.
Correlation quantifies the linear relationship between two variables — but it says nothing about causation or non-linear structure.
Without this:
Without correlation matrices and boxplots, you can't detect feature leakage or multicollinearity before they silently corrupt your model.
Two classic tools for exploring a single variable's distribution and the relationship between two variables:
The five-number summary collapses a distribution to five key quantiles:
- Min — smallest value
- Q1 (25th percentile) — bottom quarter boundary
- Median (50th percentile) — the robust centre
- Q3 (75th percentile) — top quarter boundary
- Max — largest value
Its visual companion, the boxplot, draws a box from Q1 to Q3 (the IQR), a line at the median, and "whiskers" extending to the most extreme non-outlier values. Points beyond the whiskers are plotted individually as outliers.
Covariance and correlation summarize the relationship between two variables:
- Covariance Cov(X, Y) = E[(X − μx)(Y − μy)]: positive → variables tend to move together; negative → one goes up as the other goes down. The magnitude is in mixed units (e.g., kg·cm) and hard to interpret directly.
- Pearson correlation ρ = Cov(X, Y) / (σx · σy): normalizes covariance to [-1, 1]. Now scale-independent and directly interpretable: ρ = 1 is perfect linear positive relationship, ρ = 0 is no linear relationship, ρ = -1 is perfect negative.
- Spearman correlation: Pearson applied to the ranks of the data rather than the values themselves. Captures monotonic (but not necessarily linear) relationships and is more robust to outliers.
Python (in browser)
The boxplot encodes the five-number summary visually. The whiskers extend to Q3 + 1.5·IQR (upper) and Q1 − 1.5·IQR (lower); points beyond are plotted as individual outlier markers. Open /tmp/box.png to view.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Python (in browser)
Covariance and Pearson correlation computed manually and verified with numpy. The manual formula ρ = Cov(X,Y)/(σxσy) matches np.corrcoef. Note that the covariance is in mixed units (cm·kg) while ρ is dimensionless.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Python (in browser)
For y = x³ + noise, Spearman ρ is higher than Pearson ρ because the relationship is perfectly monotonic but non-linear. Spearman measures rank correlation — whether 'when X goes up, Y tends to go up' — regardless of the exact functional form.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
What is the range of Pearson correlation ρ?
Summary Statistics and Independence — covers covariance, correlation, and the formal statement of independence (Cov(X,Y) = 0 ↔ uncorrelated, but not necessarily independent). Read it for the matrix-form covariance derivation.
- The **five-number summary** (min, Q1, median, Q3, max) and its visual form the **boxplot** give a robust, outlier-visible snapshot of any distribution.
- **Pearson ρ ∈ [−1, 1]** measures linear association. **Spearman ρ** measures monotonic association and is more robust. Always compute both when unsure about the relationship's form.
- **Correlation ≠ causation.** High correlation between a feature and the target could indicate predictive power *or* data leakage *or* a shared confounder. Investigate before shipping.
Correlation matrices are the heatmap step in feature engineering — multicollinear features (ρ > 0.9 with each other) add noise without information. Boxplots reveal class-by-class feature distributions during EDA, exposing class separation immediately.
If you remove it: You can't detect feature leakage (e.g., a feature with ρ > 0.99 with the target — it's probably the target itself in disguise) or multicollinearity that destabilizes regression coefficients.