๐ Data Science Roadmap 2026
๐ Phase 2: Mathematics for Data Science
๐ Topic 8: Covariance and Correlation
Welcome back! ๐
In the previous lesson, you learned about Range, Percentiles, Quartiles, IQR, and the Five-Number Summary.
Now let's learn two extremely important concepts for understanding relationships between variables:
โข Covariance
โข Correlation
These concepts are used extensively in Exploratory Data Analysis (EDA), feature selection, machine learning, and statistical analysis.
๐น 1. Why Do We Need Covariance and Correlation?
Suppose you're analyzing student data:
โข Hours Studied | Exam Score
โข 2 | 50
โข 4 | 60
โข 6 | 70
โข 8 | 80
โข 10 | 90
You can observe that as study hours increase, exam scores also increase.
But how can we mathematically measure this relationship?
That's where covariance and correlation come in.
๐น 2. What is Covariance?
Covariance measures the direction in which two variables change together.
It tells us whether two variables tend to increase or decrease together.
Three possibilities:
โข Positive Covariance: When one variable increases, the other tends to increase. X โ โ Y โ. Example: Study hours โ โ Exam score โ
โข Negative Covariance: When one variable increases, the other tends to decrease. X โ โ Y โ. Example: Product price โ โ Demand โ
โข Covariance Near Zero: There is little or no linear relationship between the variables. X โ โ No consistent change in Y
๐น 3. Covariance Formula
For population data:
โข Cov(X,Y) = Sum of (Xi - Mean X) ** (Yi - Mean Y) / N
Where:
โข Xi = Individual X value
โข Yi = Individual Y value
โข Mean X = Mean of X
โข Mean Y = Mean of Y
โข N = Number of observations
The calculation essentially asks: When X is above or below its average, is Y also above or below its average?
๐น 4. Simple Covariance Example
Consider:
โข X = [1, 2, 3]
โข Y = [2, 4, 6]
Means:
โข Mean(X) = 2
โข Mean(Y) = 4
Now calculate deviations:
โข X | X - Mean X | Y | Y - Mean Y | Product
โข 1 | -1 | 2 | -2 | 2
โข 2 | 0 | 4 | 0 | 0
โข 3 | 1 | 6 | 2 | 2
Sum of products: 2 + 0 + 2 = 4
Population covariance: Cov(X,Y) = 4 / 3 = 1.33
So covariance is positive. That makes sense because Y increases whenever X increases.
๐น 5. The Problem with Covariance
โข Covariance tells us the direction of a relationship, but its magnitude depends on the units of the variables.
โข For example: Height in centimeters, Weight in kilograms
โข Changing centimeters to meters can change the numerical value of covariance.
โข Therefore, covariance isn't always easy to interpret or compare.
โข This leads us to correlation.
๐น 6. What is Correlation? โญ
โข Correlation measures both the direction and strength of a linear relationship between two variables.
โข Unlike covariance, correlation is standardized.
โข Its value always lies between: -1 <= r <= 1
๐น 7. Interpreting Correlation
โข r = +1: Perfect positive linear relationship. X โ โ Y โ
โข r = -1: Perfect negative linear relationship. X โ โ Y โ
โข r = 0: No linear relationship.
โข Important: r = 0 does not necessarily mean there is no relationship at all. A strong nonlinear relationship can still exist.
๐น 8. Correlation Strength
A rough interpretation:
Post #4552
1.82K
- โค 8