Relationship Status
Correlation quantifies the strength and direction of the relationship between two variables. It is the bedrock of predictive modeling. If is correlated with , then knowing helps us predict . On TensorTonic you implement a Pearson correlation matrix and a covariance matrix from scratch, with tests that check your output.
But beware: correlation can lie. It can show relationships that don't exist, hide relationships that do, and most dangerously, suggest causation where there is none.
What is the difference between Pearson and Spearman correlation?
Pearson correlation measures how closely two variables follow a straight line, using their raw values, while Spearman correlation measures how closely they follow any consistently increasing or decreasing trend, using their ranks. Use Pearson for roughly linear data without large outliers, and Spearman for curved but monotonic relationships, ordinal data or data with outliers.
| Pearson | Spearman | |
|---|---|---|
| Measures | Linear relationships | Monotonic relationships |
| Computed on | Raw values | Ranks of the values |
| Effect of outliers | One extreme point can change it a lot | Much more robust |
| Works for ordinal data | No | Yes, such as ratings from 1 to 5 |
| Range | From -1 to 1 | From -1 to 1 |
Covariance: The Raw Ingredient
Before Correlation, we must understand Covariance. It measures how two variables change together.
X up, Y up. They move together.
X up, Y down. They move opposite.
No linear relationship (could still be non-linear! Mutual information can detect it).
Problem with Covariance: It's not scaled. If you measure height in meters, Cov is X. If you measure in centimeters, Cov is 10,000X. The number itself is meaningless without context.
Correlation Coefficient ()
Correlation is standardized covariance. We divide by the product of standard deviations to get a unitless number between -1 and 1.
Interpreting r
When to use Pearson or Spearman
Not all relationships are straight lines. Choosing the right correlation measure depends on your data.
Pearson Correlation ()
- Measures linear relationships.
- Requires both variables to be approximately normal.
- Sensitive to outliers (one bad point can destroy your r).
- If , Pearson may be near 0 even though relationship is perfect.
Spearman Rank Correlation ()
- Measures monotonic relationships (always increasing or decreasing).
- Converts values to Ranks (1st, 2nd, 3rd) then runs Pearson on ranks.
- Robust to outliers and non-normal data.
- Perfect for ordinal data (ratings 1-5).
Case Study: Bulb Factory Quality Control
The Question
Is there a relationship between the voltage supplied during manufacturing and the lifespan of the light bulb? If so, we can optimize the voltage setting.
The Data
We sample 50 bulbs, record the manufacturing voltage (V) and the hours until failure (H).
The Interpretation
Strong positive correlation. Bulbs manufactured at higher voltage tend to last longer. But wait! Does higher voltage cause longer life? Or do better quality filaments (which last longer) also happen to tolerate higher voltage? We need to run an experiment (A/B test) to prove causation.
Interactive Simulator
Adjust the correlation coefficient. See how the cloud of points tightens into a line as or . Notice how at , the cloud is a perfect circle.
Correlation (r)
Note: At r=0, points form a circular cloud (σ=1). As |r| → 1, the Y-noise diminishes, collapsing the cloud onto the regression line.
Correlation ≠ Causation
This is perhaps the most important lesson in statistics. Just because A and B move together, doesn't mean A causes B. There are several possibilities:
1. A causes B (Direct Causation)
Rain causes wet ground. Smoking causes cancer.
2. B causes A (Reverse Causation)
Cities with more police officers have higher crime rates. Does hiring police cause crime? No, high crime rates cause cities to hire more police. The effect (crime) is driving the apparent cause (police presence), not the other way around.
3. C causes both A and B (Confounding Variable)
Ice cream sales and drowning deaths are correlated. Summer (heat) causes both. This is the most common trap.
4. Pure Coincidence (Spurious)
With enough variables and time series, you'll find spurious correlations by chance.
Spurious Correlations
Sometimes, things correlate purely by accident. These are hilarious to look at but dangerous if you take them seriously.
"The number of people who drowned by falling into a pool correlates with films Nicolas Cage appeared in."
(Real stat: r = 0.66 from 1999-2009).
Bulb Example: A Warning
You notice that bulbs manufactured on Fridays have higher defect rates. Before blaming the day of the week, check if Friday is when the junior shift works, or if Friday is when the machine gets its weekly maintenance (and isn't fully calibrated afterward). The correlation is real, but the cause is something else entirely.
ML Applications
Feature Selection
We want features that correlate highly with the target (predictive power) but low with each other (no redundancy). A correlation matrix heatmap is your first step.
Multicollinearity
In Linear Regression, if two features are perfectly correlated (e.g., "Temperature in °C" and "Temperature in °F"), the matrix becomes singular and cannot be inverted. You must drop one.
VIF (Variance Inflation Factor)
VIF measures how much the variance of a regression coefficient is inflated due to multicollinearity. is a red flag. Calculate as where is R-squared of regressing feature i on all other features.
PCA Preprocessing
If features are highly correlated, PCA can transform them into uncorrelated principal components. This helps with numerical stability and can reduce overfitting.
Frequently asked questions about correlation
Why use correlation instead of covariance?
Covariance is not scaled, so measuring height in centimeters instead of meters multiplies it by 10,000. Correlation divides covariance by the product of the standard deviations, which gives a unitless number between -1 and 1.
Does a correlation of 0 mean two variables are unrelated?
No, a correlation near 0 only means there is no linear relationship. If Y equals X squared, Pearson correlation can be near 0 even though the relationship is perfect.
Practice this
On TensorTonic you implement correlation and covariance from scratch and test them in the browser.
- Compute Pearson Correlation Matrix to compute correlations between every pair of features.
- Correlation Significance to test whether a correlation is larger than chance would produce.
- Covariance Matrix to build the covariance that correlation normalizes.