Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Correlation

Correlation measures the strength and direction of a relationship between two numerical variables; engineers use it to find associations, choose features, and check assumptions before modelling.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Correlation tells an engineer whether two variables move together and how strongly. You use correlation when selecting input features for a model, checking whether sensor readings agree, or summarising relationships in exploratory data analysis before moving to regression or hypothesis testing.

Key ideas

  • Covariance: covariance is a measure of joint variability of two variables. If X and Y increase together, covariance is positive; if one increases while the other decreases, covariance is negative. Formally for a sample, the sample covariance is defined below in Formulas.
  • Pearson correlation coefficient (sample): the standard measure of linear correlation. It is the sample covariance divided by the product of the sample standard deviations. It measures linear association only. Exact sample formula is given in Formulas and derived next. If either variable has zero variance (all values identical), r is undefined.

Derivation of the sample Pearson correlation formula (step-by-step that examiners expect):

  1. Start with the sample covariance numerator: Σ (xi − x̄)(yi − ȳ). This is a measure of how paired deviations from their means multiply.
  2. The sample variances use the same deviations: Σ (xi − x̄)² and Σ (yi − ȳ)².
  3. To make a dimensionless and scaled measure, divide covariance by the product of the root-sum-of-squares of each variable: r = Σ (xi − x̄)(yi − ȳ) / sqrt(Σ (xi − x̄)² Σ (yi − ȳ)²).
  4. This r lies between −1 and 1. r = 1 means perfect positive linear relation; r = −1 means perfect negative linear relation; r = 0 means no linear relation (but there may be non-linear relations).

Important assumptions for using Pearson r (state these in an exam):

  • Linearity: the true relationship between X and Y is approximately linear.
  • Scale: X and Y are measured on interval or ratio scales (not ordinal categories).
  • Homoscedasticity: spread (variance) of Y around the regression line is roughly constant for values of X.
  • Bivariate normality: for exact inference (p-values, confidence intervals) we assume (X,Y) are jointly normal. For large n we can relax this by the central limit theorem. Additionally, observations (xi, yi) should be independent across i; for time-series or clustered data, use methods that account for dependence.

Other correlation measures:

  • Spearman rank correlation (ρs): correlation of ranks. Replace raw values by their ranks and compute Pearson r of ranks. Use when relationship is monotonic (one-directional) but not linear, or when data are ordinal. Handle ties by assigning average ranks. Range: −1 to +1.
  • Kendall's tau: counts concordant and discordant pairs; a robust measure for small samples and ordinal data. Range: −1 to +1.

Hypothesis testing for correlation (what to write):

  • Null hypothesis H0: population correlation ρ = 0 (no linear association). Alternative H1: ρ ≠ 0 (two-sided) or ρ > 0 / ρ < 0 (one-sided).
  • Test statistic for Pearson r: t = r · sqrt((n − 2)/(1 − r²)), which under H0 follows Student's t with n − 2 degrees of freedom when bivariate normality holds. For confidence intervals for ρ (and testing H0: ρ = ρ0 ≠ 0), use Fisher z: z' = atanh(r), SE = 1 / sqrt(n − 3); a 95% CI is tanh( z' ± 1.96 · SE ).

Interpretation and coefficient of determination:

  • Coefficient of determination (R²): R² = r² (for simple linear regression with one predictor). It is the fraction of variance in Y explained by X using the linear model. R² ranges from 0 to 1 and is unitless.

What to draw in an exam (diagram):

  • Draw a scatter plot: x-axis labelled with variable name and units (e.g., "Hours studied (h)"), y-axis labelled (e.g., "Marks (points)"). Plot points (xi, yi). If fitting a regression line, draw the best-fit straight line; mark means x̄ and ȳ and show a typical residual (vertical distance from point to line) labelled with its value and unit.

Formulas

x̄ = (1/n) · Σ xi — sample mean of X; xi are measurements in unit of X (e.g., hours), n is sample size.

ȳ = (1/n) · Σ yi — sample mean of Y; yi in unit of Y (e.g., marks).

r = Σ (xi − x̄)(yi − ȳ) / sqrt( Σ (xi − x̄)² · Σ (yi − ȳ)² ) — sample Pearson correlation coefficient; unitless; applies to paired numerical data; assumes interval/ratio scale and linearity.

cov_sample = Σ (xi − x̄)(yi − ȳ) / (n − 1) — sample covariance; units = unit_X · unit_Y; used when you want covariance estimate with unbiased denominator.

r = cov_sample / (s_x · s_y) where s_x = sqrt( Σ (xi − x̄)² / (n − 1) ), s_y similar — same Pearson r but expressed with sample variances; unitless. Computational shortcut (useful for exams/calculators, no means needed explicitly): r = [ n · Σ(xi yi) − (Σ xi)(Σ yi) ] / sqrt( [ n · Σ xi² − (Σ xi)² ] · [ n · Σ yi² − (Σ yi)² ] ).

t = r · sqrt( (n − 2) / (1 − r²) ) — test statistic for H0: ρ = 0; follows t_{n−2} under bivariate normality.

R² = r² — coefficient of determination for simple linear regression; unitless; fraction of variance in Y explained by X.

ρ_s = 1 − (6 · Σ d_i²) / (n (n² − 1)) — Spearman rank correlation when there are no tied ranks; d_i = rank(xi) − rank(yi); unitless.

Worked examples

Example 1 — compute Pearson r and R² (numerical, carry units) Data (n = 5): Hours studied X (h): 1 h, 2 h, 3 h, 4 h, 5 h. Marks Y (points): 2, 4, 5, 4, 5 points. Step 1 — means: x̄ = (1+2+3+4+5)/5 = 15/5 = 3 h. ȳ = (2+4+5+4+5)/5 = 20/5 = 4 points. Step 2 — deviations and products: xi − x̄: −2, −1, 0, 1, 2 (h) yi − ȳ: −2, 0, 1, 0, 1 (points) Products (xi−x̄)(yi−ȳ): 4, 0, 0, 0, 2 → Σ = 6 (h·points). Σ (xi − x̄)² = 4+1+0+1+4 = 10 (h²). Σ (yi − ȳ)² = 4+0+1+0+1 = 6 (points²). Step 3 — Pearson r: r = 6 / sqrt(10 · 6) = 6 / sqrt(60) = 6 / 7.7459667 = 0.7745967 (unitless). Step 4 — coefficient of determination: R² = r² = 0.7745967² = 0.600000 → 60.0% of variance in marks is explained by hours studied (by the linear model). Step 5 — test significance (t-test): t = r · sqrt((n − 2)/(1 − r²)) = 0.7745967 · sqrt(3/(1 − 0.6)) = 0.7745967 · sqrt(3/0.4) = 0.7745967 · 2.7386128 = 2.1213203. Degrees of freedom = n − 2 = 3. For α = 0.05 two-tailed, critical t ≈ 3.182 (df=3). Since 2.121 < 3.182, we do not reject H0 at 5% — correlation not significant at 5% with this small sample. Final answers: r = 0.7746, R² = 0.60, t = 2.121 (p ≈ 0.12).

Example 2 — compute Spearman rank correlation (small sample, no ties) Data (n = 5): X: 10, 20, 30, 40, 50 (units irrelevant). Y: 8, 5, 9, 7, 6. Step 1 — ranks (smallest rank 1): rank(X) = 1,2,3,4,5. rank(Y) sorted ascending values 5,6,7,8,9 → ranks for Y: 8→4, 5→1, 9→5, 7→3, 6→2. So rank(Y) = 4,1,5,3,2. Step 2 — differences d = rank(X) − rank(Y) = [1−4, 2−1, 3−5, 4−3, 5−2] = [−3,1,−2,1,3]. d² = [9,1,4,1,9] → Σ d² = 24. Step 3 — Spearman ρ_s: ρ_s = 1 − (6 · 24) / (5 · (25 − 1)) = 1 − 144 / 120 = 1 − 1.2 = −0.2. Interpretation: a small negative monotonic association; not strong.

Example 3 — regression slope from example 1 (link with correlation) Slope b = Σ (xi − x̄)(yi − ȳ) / Σ (xi − x̄)² = 6 / 10 = 0.6 (points per hour). Intercept a = ȳ − b x̄ = 4 − 0.6 · 3 = 2.2 points. Regression line: ŷ = 2.2 + 0.6 x. For x = 6 h predicts ŷ = 2.2 + 0.6·6 = 5.8 points. (Units shown.)

Common mistakes

  • Forgetting assumptions for the t-test (bivariate normality); applying small-sample p-values without checking normality.
  • Not handling ties in Spearman correctly: tied values need average ranks; failing to do so produces wrong ρ_s.
  • Mixing population ρ formula with sample r (forgetting denominators cancel sometimes). Always state whether you use sample or population formula.
  • Interpreting r² as “percentage of Y explained” without specifying the model (R² applies to linear models; non-linear fit will have a different R² meaning).
  • Failing to report variable units when describing the data (correlation itself is unitless). Do not standardise specifically to compute r (it already standardises); standardisation is needed for distance-based methods or when combining variables on different scales in other analyses.

For your exam

Types of questions you will see:

  • Definitions: define correlation, covariance, Pearson r, Spearman ρ_s, and R² — include units and domain of values.
  • Short notes: assumptions behind Pearson correlation; when to use Spearman or Kendall; interpretation of r and R².
  • Derivations: show step-by-step derivation of Pearson r from covariance and sample variances; derive the t-statistic for testing H0: ρ = 0 and state assumptions.
  • Numericals: compute r by hand for small datasets (show means, deviations, sums, final r to 3–4 significant digits), compute ρ_s including tie handling, compute regression slope/intercept from sums.
  • Diagrams: draw a labelled scatter plot with regression line, show residual for one point and label axes with units. Full-marks answers include: clear definitions with bold terms, statement of assumptions, stepwise algebra for derivations, all intermediate sums/means in numericals, and conclusion with interpretation in context including units.

Quick check

  1. What is the range of values that Pearson r can take? 2) When should you use Spearman rather than Pearson? 3) State the test statistic used to test H0: ρ = 0 and its degrees of freedom. Answers: 1) −1 to +1. 2) Use Spearman when data are ordinal, have outliers, or the relationship is monotonic but non-linear (or when normality fails). 3) t = r · sqrt((n − 2)/(1 − r²)) with df = n − 2.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?