Quiz 2

Correlation — Measuring Relationships Between Numerical Variables

3458 words
17 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Correlation — Measuring Relationships Between Numerical Variables ## 🎯 Learning Objectives After completing this topic, you will be able to: - Create and interpret **scatter plots** for bivariate numerical data - Compute and interpret **covariance** - Calculate and interpret **Pearson's correlation coefficient (r...

Correlation — Measuring Relationships Between Numerical Variables

🎯 Learning Objectives

After completing this topic, you will be able to:
  • Create and interpret scatter plots for bivariate numerical data
  • Compute and interpret covariance
  • Calculate and interpret Pearson's correlation coefficient (r)
  • Distinguish correlation from causation
  • Recognize the effect of outliers and non-linear relationships on correlation
  • Know when correlation is and isn't appropriate

📋 Prerequisites


📖 Core Content

8.1 Intuition: How Two Variables Move Together

We've studied variables in isolation (univariate). Now we ask: Do two variables change together?
  • As temperature rises, do ice cream sales increase? → Positive relationship
  • As a car's age increases, does its value decrease? → Negative relationship
  • Is there no connection between shoe size and IQ? → No relationship Correlation quantifies the strength and direction of a linear relationship between two numerical variables.
Everyday analogy: Imagine you and a friend are walking. If you both tend to walk forward together (both increase at the same time), you're positively correlated. If one of you walks forward while the other walks backward (one increases as the other decreases), you're negatively correlated. If your movements are unrelated (one walks while the other hops on one foot), there's no correlation. 🔑 Key Insight: Correlation measures linear relationships only. Two variables could be perfectly related (e.g., y=x2y = x^2) but have zero correlation because the relationship is curved, not straight.

8.2 Scatter Plots

Before computing any numbers, always create a scatter plot.

Anatomy of a Scatter Plot

  • X-axis: Independent variable (predictor)
  • Y-axis: Dependent variable (response)
  • Each point: One observation (x, y) (Diagram)

Reading Patterns from a Scatter Plot

PatternVisualCorrelation
Positive linearPoints cluster around an upward-sloping liner > 0
Negative linearPoints cluster around a downward-sloping liner < 0
No patternPoints scattered randomlyr ≈ 0
CurvedPoints follow a curver may be weak even if relationship is strong
ClustersTwo distinct groupsr can be misleading

8.3 Covariance

Covariance measures how two variables vary together. It's the bivariate version of variance.

Definition

Cov(x,y)=i=1n(xixˉ)(yiyˉ)n1\text{Cov}(x, y) = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{n - 1}

Intuition

For each data point:
  • If both xx and yy are above their means → (xixˉ)(yiyˉ)>0(x_i - \bar{x})(y_i - \bar{y}) > 0positive contribution
  • If both are below their means → both differences are negative, product positive → positive contribution
  • If one is above and other below → product negative → negative contribution Sum all contributions → positive covariance means X and Y tend to move in the same direction.

Limitation

Covariance depends on the units of measurement. If you measure height in cm vs meters, covariance changes dramatically. This makes it hard to interpret and compare.

8.4 Pearson's Correlation Coefficient (r)

8.4.1 Intuition

Pearson's r standardizes covariance by dividing by the product of standard deviations. The result is a unitless number between -1 and +1.
r=Cov(x,y)sxsy=(xixˉ)(yiyˉ)(xixˉ)2(yiyˉ)2r = \frac{\text{Cov}(x, y)}{s_x s_y} = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum (x_i - \bar{x})^2} \sqrt{\sum (y_i - \bar{y})^2}}

8.4.2 Interpreting r

r ValueInterpretation
r = +1Perfect positive linear relationship
r = +0.7 to +0.9Strong positive
r = +0.4 to +0.6Moderate positive
r = +0.1 to +0.3Weak positive
r = 0No linear relationship
r = -0.1 to -0.3Weak negative
r = -0.4 to -0.6Moderate negative
r = -0.7 to -0.9Strong negative
r = -1Perfect negative linear relationship
Note: These are rough guidelines. Context matters — in physics, r = 0.99 is expected; in social sciences, r = 0.3 might be considered strong.

8.4.3 The Coefficient of Determination (r²)

r2=(correlation coefficient)2r^2 = \text{(correlation coefficient)}^2
Interpretation: r² is the proportion of the variance in one variable that can be explained by the other variable (in a linear model). Example: If r = 0.8 between study time and exam score, then r² = 0.64 = 64%. So 64% of the variation in exam scores can be explained by study time. The other 36% is due to other factors.

8.5 Step-by-Step Calculation

Example: Find the correlation between hours studied (x) and exam score (y) for 5 students.
Studentx (hours)y (score)
1265
2470
3675
4885
51095
Step 1: Calculate means
xˉ=2+4+6+8+105=305=6\bar{x} = \frac{2+4+6+8+10}{5} = \frac{30}{5} = 6 yˉ=65+70+75+85+955=3905=78\bar{y} = \frac{65+70+75+85+95}{5} = \frac{390}{5} = 78
Step 2: Calculate deviations and products
xyx- xˉ\bar{x}y- yˉ\bar{y}(x- xˉ\bar{x} )(y- yˉ\bar{y} )(x- xˉ\bar{x}(y- yˉ\bar{y}
265-4-135216169
470-2-816464
6750-3009
8852714449
10954176816289
Step 3: Sum the columns
  • (xxˉ)(yyˉ)=52+16+0+14+68=150\sum (x-\bar{x})(y-\bar{y}) = 52 + 16 + 0 + 14 + 68 = 150
  • (xxˉ)2=16+4+0+4+16=40\sum (x-\bar{x})^2 = 16 + 4 + 0 + 4 + 16 = 40
  • (yyˉ)2=169+64+9+49+289=580\sum (y-\bar{y})^2 = 169 + 64 + 9 + 49 + 289 = 580 Step 4: Compute r
r=15040×580=15023200=150152.3150.985r = \frac{150}{\sqrt{40} \times \sqrt{580}} = \frac{150}{\sqrt{23200}} = \frac{150}{152.315} \approx 0.985
Interpretation: r ≈ 0.985 indicates a very strong positive linear relationship between hours studied and exam score. The more hours studied, the higher the score.
r20.970=97%r^2 \approx 0.970 = 97\%
97% of the variation in exam scores is explained by hours studied.

8.6 Properties of r

PropertyExplanation
-1 ≤ r ≤ 1Always between -1 and +1
UnitlessDoesn't depend on measurement units
Symmetricr(x,y) = r(y,x) — correlation of X with Y equals correlation of Y with X
Only measures LINEAR relationshipsA perfect curve can have r = 0
Sensitive to outliersA single outlier can dramatically change r
Does NOT imply causationThe biggest mistake in statistics!

8.7 Correlation vs. Causation

This is the single most important concept in statistical literacy. Just because two variables are correlated does NOT mean one causes the other. Possible explanations for a correlation:
  1. Causation: X → Y (more studying causes higher scores)
  2. Reverse causation: Y → X (higher scores cause more studying — maybe?)
  3. Confounding variable: Z → X and Z → Y (a third variable causes both)
  4. Coincidence: Random chance (especially with small samples)
  5. Selection bias: The data was collected in a biased way

Classic Spurious Correlations

  • Ice cream sales and drowning deaths are positively correlated. Does ice cream cause drowning? No! Both are caused by a confounder: hot weather (more people swim, more people eat ice cream).
  • Number of storks and number of babies in a region are correlated. Do storks deliver babies? No — both are related to the region being rural (more space for storks, higher birth rates in rural areas).
  • Chocolate consumption and Nobel laureates per capita are correlated. Does chocolate make you smarter? Probably not — both are correlated with the country's wealth.
Memory trick: "Correlation does not imply causation" is the first principle of statistical thinking.

8.8 Outliers and Correlation

A single outlier can dramatically change the correlation coefficient. Example: Without outlier: (1,2), (2,3), (3,4), (4,5), (5,6) → r = 1 (perfect) With outlier: Add (10,1) → r drops significantly! The new point (10,1) has: high x, low y → it's far from the main trend → it pulls the correlation coefficient toward zero (or even negative). Lesson: Check your scatter plot for outliers. Consider reporting correlation with and without outliers.

8.9 Non-Linear Relationships

Correlation only measures linear relationships. A non-linear relationship can be strong but have r ≈ 0. Example: y=x2y = x^2 for x = -5 to 5
  • The relationship is perfect (y is completely determined by x)
  • But r ≈ 0 because the relationship is U-shaped (not linear) How to detect: Look at the scatter plot! If you see a curve, correlation is the wrong tool.

8.10 Worked Examples

Example 1: Computing r (Easy)

Data: (1,2), (2,4), (3,6), (4,8), (5,10) Compute r. Solution:
xyx- xˉ\bar{x}y- yˉ\bar{y}(x- xˉ\bar{x} )(y- yˉ\bar{y} )(x- xˉ\bar{x}(y- yˉ\bar{y}
12-2-48416
24-1-2214
3600000
4812214
510248416
Sum201040
xˉ=3\bar{x} = 3, yˉ=6\bar{y} = 6
r=2010×40=20400=2020=1.0r = \frac{20}{\sqrt{10} \times \sqrt{40}} = \frac{20}{\sqrt{400}} = \frac{20}{20} = 1.0
Interpretation: Perfect positive linear correlation. (Indeed, y = 2x exactly.)

Example 2: Interpreting r (Medium)

Scenario: A study of 100 students finds r = -0.65 between hours watching TV and exam score. a) Interpret r. b) Interpret r². c) Can we conclude TV causes lower scores? Solution: a) r = -0.65: Moderate to strong negative linear relationship. Students who watch more TV tend to have lower exam scores. b) r² = 0.4225 ≈ 42.25%: About 42% of the variation in exam scores can be explained by TV watching hours. The remaining 58% is due to other factors (study habits, prior knowledge, sleep, etc.). c) Causation? No! Possible explanations:
  • TV watching might cause lower scores (by replacing study time)
  • Lower-scoring students might watch more TV (reverse causation)
  • A confounder like socioeconomic status might affect both TV access and educational outcomes
  • Generally unmotivated students might watch more TV AND study less

Example 3: Comparing Relationships (Harder)

Scenario: Three datasets each have 20 points.
DatasetrScatter Plot Description
A0.95Tight cluster around upward line
B0.45Loose scatter, slight upward trend
C-0.02Random scatter, no pattern
Which dataset shows: a) The strongest linear relationship? b) No linear relationship? c) A weak relationship? Solution: a) Dataset A (r = 0.95) — very strong positive linear relationship b) Dataset C (r ≈ 0) — no linear relationship c) Dataset B (r = 0.45) — weak to moderate positive relationship Additional insight: Dataset C might still have a non-linear relationship (e.g., a U-shape). The scatter plot would reveal this.

8.11 Edge Cases & Gotchas

All Points on a Vertical Line

If all x-values are identical (e.g., all measurements at the same time point), the standard deviation of x is zero. Correlation is undefined (division by zero).

Correlation of a Variable with Itself

If you compute correlation of X with X, you get r = 1. Always. (Because a variable is perfectly linearly related to itself.)

The "Restriction of Range" Problem

If your sample only covers a narrow range of one variable, the correlation will be weaker than in the full population. Example: Height and weight are correlated in the general population. But if you only study people who are 170-171 cm tall, the correlation between height and weight will be near zero — there's not enough height variation to detect the relationship.

8.12 Why This Matters

Correlation is the foundation of:
  • Regression (linear models): Regression extends correlation to prediction
  • Factor analysis: Correlations among variables reveal underlying patterns
  • Machine learning: Feature selection uses correlations
  • Portfolio theory: Asset correlations determine diversification benefits Later topics that build on correlation:
  • BSMA1003 (Maths 2): Linear algebra view of correlation matrices
  • BSMA1004 (Stats 2): Inference for correlation, multiple regression
  • BSCS2004 (ML Foundations): Feature selection, multicollinearity

📐 Key Formulas / Concepts

ConceptFormulaInterpretation
Covariance(xxˉ)(yyˉ)n1\frac{\sum (x-\bar{x})(y-\bar{y})}{n-1}Direction of relationship (unit-dependent)
Pearson's r(xxˉ)(yyˉ)(xxˉ)2(yyˉ)2\frac{\sum (x-\bar{x})(y-\bar{y})}{\sqrt{\sum (x-\bar{x})^2}\sqrt{\sum (y-\bar{y})^2}}Strength & direction of linear relationship (-1 to +1)
Coefficient of Determinationr2r^2Proportion of variance explained
Scatter PlotVisual: points at (x,y)Always check before computing r

⚠️ Common Pitfalls

Pitfall 1: Assuming Correlation Implies Causation

The mistake: Reading a study that found a correlation between eating breakfast and higher grades, and concluding that eating breakfast causes better grades. Why it happens: It's natural to think "A is associated with B → A might cause B." How to avoid: List at least 3 alternative explanations for any observed correlation (reverse causation, confounders, coincidence, bias).

Pitfall 2: Only Reporting r Without a Scatter Plot

The mistake: Computing r and drawing conclusions without looking at the data. Why it happens: r is a single number — easy to compute and report. How to avoid: Always create a scatter plot first. Anscombe's Quartet shows four very different datasets with identical r = 0.816. Only the scatter plots reveal the truth.

Pitfall 3: Treating r = 0 as "No Relationship"

The mistake: Concluding two variables are unrelated because r ≈ 0. Why it happens: r = 0 is taught as "no linear relationship," but people drop the word "linear." How to avoid: Remember: r measures linear relationships only. A perfect parabola has r ≈ 0. Check the scatter plot for non-linear patterns.

Pitfall 4: Comparing r Across Different Contexts

The mistake: Saying "r = 0.8 is always a strong correlation." Why it happens: The guidelines for "weak/moderate/strong" are taught as if they're universal. How to avoid: Context matters. In particle physics, r = 0.99 might be weak (they expect near-perfect). In social science, r = 0.3 might be strong. Always interpret r relative to the field and the specific variables.

📝 Practice Questions

Q1: Compute r
Find r for: (1,1), (2,3), (3,2), (4,4), (5,5)
<details> <strong>Solution</strong>
xˉ=3\bar{x} = 3, yˉ=3\bar{y} = 3
xyxxˉx-\bar{x}yyˉy-\bar{y}Product(xxˉ)2(x-\bar{x})^2(yyˉ)2(y-\bar{y})^2
11-2-2444
23-10010
320-1001
4411111
5522444
Sum91010
r=91010=910=0.9r = \frac{9}{\sqrt{10}\sqrt{10}} = \frac{9}{10} = 0.9
r=0.9\boxed{r = 0.9}
</details> > **Q2: Interpret r²** > > A study finds r = 0.6 between exercise hours and sleep quality. Find and interpret r². > > <details> <strong>Solution</strong> > > r² = 0.6² = 0.36 > > **Interpretation:** 36% of the variation in sleep quality can be explained by exercise hours (in the linear model). The remaining 64% is due to other factors (diet, stress, medical conditions, etc.). </details> > **Q3: Identify the Pattern** > > For each scatter plot description, predict r: > > a) Points tightly clustered around a downward-sloping line b) Points randomly scattered with no pattern c) Points following a clear upward curve (U-shape opening upward) > > <details> <strong>Solution</strong> > > a) **r near -1** (strong negative linear relationship) > > b) **r near 0** (no linear relationship) > > c) **r near 0** but for the wrong reason! The relationship is clear (curved), but r only measures linearity. This is a classic example of why you need a scatter plot — r misses the non-linear relationship. </details> > **Q4: Outlier Effect** > > Main data: (1,2), (2,4), (3,6), (4,8) > > Add outlier: (10,1) > > Find r without and with the outlier. > > <details> <strong>Solution</strong> > > **Without outlier:** x̄ = 2.5, ȳ = 5 > >
xyxxˉx-\bar{x}yyˉy-\bar{y}Product(xxˉ)2(x-\bar{x})^2(yyˉ)2(y-\bar{y})^2
12-1.5-34.52.259
24-0.5-10.50.251
360.510.50.251
481.534.52.259
Sum10520
r_without = 10 / √(5×20) = 10/10 = 1.0 ✅
With outlier (10,1): x̄ = (1+2+3+4+10)/5 = 20/5 = 4 ȳ = (2+4+6+8+1)/5 = 21/5 = 4.2
xyxxˉx-\bar{x}yyˉy-\bar{y}Product(xxˉ)2(x-\bar{x})^2(yyˉ)2(y-\bar{y})^2
12-3-2.26.694.84
24-2-0.20.440.04
36-11.8-1.813.24
4803.80014.44
1016-3.2-19.23610.24
Sum-145032.8
r_with = -14 / √(50×32.8) = -14 / √1640 = -14 / 40.497 ≈ -0.346
Dramatic effect: r went from +1.0 to -0.346! One outlier completely reversed the relationship. This shows why outliers must be investigated.
</details> > **Q5: Multiple Choice** > > </strong> > > Which of the following does NOT affect Pearson's r? > > a) An outlier b) Changing the units of measurement (cm → inches) c) A non-linear relationship d) Adding an extreme value > > <details> <strong>Solution</strong> > > **Answer: b)** Changing units does NOT affect r. > > - **a) Outlier:** YES — affects r significantly > - **b) Units:** **NO** — r is unitless, standardized by standard deviations > - **c) Non-linear:** YES — r depends on the relationship being linear > - **d) Extreme value:** YES — this is an outlier, which affects r </details> > **Q6: Spurious Correlation** > > </strong> > > Correlation between the number of fire stations in a city and the number of fires is r = 0.85. Does having more fire stations cause more fires? Explain. > > <details> <strong>Solution</strong> > > **No! This is a spurious correlation.** > > The fire department's interpretation: More fire stations don't cause more fires. Instead, cities with more fires build more fire stations to respond to them. This is **reverse causation**. > > Or, more likely: **confounding variable** — larger cities have more buildings (population), which means both more fires AND more fire stations. The size of the city is the confounder. > > **Alternative explanations:** > > 1. Larger population → more fires + more fire stations (confounding) > 2. Cities with more fires respond by building more stations (reverse causation) </details> > **Q7: Which Has Stronger Correlation?** > > </strong> > > Study A: r = 0.45, n = 500 Study B: r = 0.40, n = 50 > > Which correlation is stronger? Which is more reliable? > > <details> <strong>Solution</strong> > > **Strength:** Study A (r = 0.45) has a slightly stronger correlation than Study B (r = 0.40). > > **Reliability:** Study A (n = 500) is much more reliable. With n = 50, the margin of error around r is much larger — the true population correlation could be anywhere from 0.15 to 0.65. With n = 500, the estimate is more precise. > > **Lesson:** Strength (r value) and reliability (sample size) are different things. A strong correlation with a small sample might not be statistically significant. </details> > **Q8: Application** > > </strong> > > A real estate researcher collects data on 200 houses: > >
Variable 1Variable 2r
Size (sq ft)Price0.72
Age (years)Price-0.55
BedroomsPrice0.48
Distance to cityPrice-0.63
a) Which variable has the strongest linear relationship with price? b) Interpret r for Size vs Price. c) Why might Age be negatively correlated with Price? d) If you could only know ONE variable to predict price, which would you choose?
<details> <strong>Solution</strong>
a) Size (sq ft) has the strongest relationship with price (|r| = 0.72).
b) Size vs Price (r = 0.72): Strong positive linear relationship. Larger houses tend to have higher prices. r² = 0.52, so 52% of the variation in price can be explained by house size alone.
c) Age vs Price (r = -0.55): Older houses tend to be cheaper (negative correlation). Why?
  • Wear and tear, need for renovations
  • Older designs may be less desirable
  • However, some old houses in good locations (heritage areas) may be expensive — this is why the correlation is only moderate, not perfect
d) Best predictor: Size (highest |r| = 0.72). Size alone explains 52% of price variation. But using ALL variables together (multiple regression) would give even better predictions.
</details> > **Q9: Simpson's Paradox with Correlation** > > </strong> > > A school has two programs. Within each program, there's a positive correlation between hours studied and grades (r = 0.5 in each). But when combining all students, the overall correlation is r = -0.2. How is this possible? > > <details> <strong>Solution</strong> > > **This is Simpson's Paradox for correlation.** It happens when: > > - **Program A** is harder (lower grades for the same study hours) AND has students who study more > - **Program B** is easier (higher grades) AND has students who study less > > Within each program: more study → higher grades ✅ Between programs: Program A students study more but get lower grades (because the program is harder) > > When you combine them, the between-program difference dominates, creating the illusion of a negative correlation. > > **Visualized:** > > ```pseudo > Grades ↑ > | ·· (Program B, easy, less study, higher grades) > |·· > | > | ·· (Program A, hard, more study, lower grades) > | ·· > |________________→ Hours studied > ``` > > **Lesson:** Check for subgroups before interpreting overall correlation! </details> > **Q10: What r Doesn't Tell You** > > Match each dataset to the correct r (choose from: r = 0, r = 0.5, r = 0.9, r = -0.5): > > a) Points forming a tight upward-sloping line b) Points forming a loose cloud, slightly upward c) Points forming a perfectly symmetric U-shape d) Points forming a slight downward slope > > <details> <strong>Solution</strong> > > a) **r = 0.9** — tight upward trend → strong positive correlation > > b) **r = 0.5** — loose upward trend → moderate positive correlation > > c) **r ≈ 0** — perfectly symmetric U-shape → no linear relationship (but clear non-linear relationship!) > > d) **r = -0.5** — slight downward slope → moderate negative correlation </details> * * * ## 🔗 Cross-References - **Next topic:** [Counting — Fundamental Principle of Counting](/notes/01-foundation-bsma1002-stats-1-week05-08-counting-fundamentals) — shifting gears to combinatorics, the foundation of probability - **Previous:** [Contingency Tables](/notes/01-foundation-bsma1002-stats-1-week04-06-contingency-tables) — association for categorical data - **Week 10 (Expectation):** Covariance and correlation for random variables - **BSMA1004 (Stats 2):** Inference for correlation, multiple regression - **BSCS2004 (ML Foundations):** Feature correlation, multicollinearity - **BSMA1001-maths-1:** Functions and linear relationships [Join Discord](https://discord.gg/gE2m4Qrdqv) [Previous**Contingency Tables**](/notes/01-foundation-bsma1002-stats-1-week04-06-contingency-tables)[Next**Counting Fundamentals**](/notes/01-foundation-bsma1002-stats-1-week05-08-counting-fundamentals)
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.