Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 12: Independence, Covariance, and Correlation

Introduction

In the previous chapter, we explored how to describe the probability distributions of multiple random variables simultaneously using joint distributions. We saw how to derive marginal and conditional distributions from the joint distribution. Now, we delve deeper into the relationships between random variables.

How can we quantify if knowing the value of one variable tells us something about the other? Are they completely unrelated (independent)? Or do they tend to move together or in opposite directions? This chapter introduces three fundamental concepts for describing these relationships:

  1. Independence: The strongest form of “unrelatedness,” where the value of one variable provides no information about the value of the other.

  2. Covariance: A measure of the direction of the linear relationship between two variables.

  3. Correlation: A standardized measure of the strength and direction of the linear relationship between two variables.

We will also explore how these concepts affect the variance of sums of random variables, a crucial calculation in fields like finance (portfolio variance) and engineering (error propagation).

Learning Objectives:

Independence of Random Variables

Recall from Chapter 5 that two events AA and BB are independent if P(A∩B)=P(A)P(B)P(A \cap B) = P(A)P(B). We extend this concept to random variables.

Definition: Two random variables XX and YY are independent if for any sets AA and BB (in the range of XX and YY, respectively), the events {X∈A}\{X \in A\} and {Y∈B}\{Y \in B\} are independent. That is:

P(X∈A,Y∈B)=P(X∈A)P(Y∈B)P(X \in A, Y \in B) = P(X \in A) P(Y \in B)

This is equivalent to saying that their joint distribution function factors into the product of their marginal distribution functions:

Intuition: If XX and YY are independent, knowing the outcome of XX provides no information about the outcome of YY, and vice-versa.

Example:

Non-Example:

Checking for Independence

In practice, we often assume independence based on the physical nature of the processes generating the random variables (like separate coin flips). If we have the joint distribution, we can check if it factorizes into the product of the marginals. If we only have data, testing for independence rigorously is complex (involving statistical hypothesis tests beyond the scope of basic probability). However, we’ll see soon that calculating the correlation can give us a clue (if correlation is non-zero, they are dependent; if correlation is zero, they might be independent).

Covariance

If variables are not independent, they are dependent. Covariance is a measure that describes the direction of the linear relationship between two random variables.

Definition: The covariance between two random variables XX and YY, denoted Cov(X,Y)\mathrm{Cov}(X, Y) or σXY\sigma_{XY}, is:

Cov(X,Y)=E[(X−E[X])(Y−E[Y])]\mathrm{Cov}(X, Y) = E[(X - E[X])(Y - E[Y])]

This formula calculates the expected value of the product of the deviations of XX and YY from their respective means.

A more convenient formula for calculation is often:

Cov(X,Y)=E[XY]−E[X]E[Y]\mathrm{Cov}(X, Y) = E[XY] - E[X]E[Y]

Interpretation:

Properties:

  1. Cov(X,X)=E[(X−E[X])2]=Var(X)\mathrm{Cov}(X, X) = E[(X - E[X])^2] = \mathrm{Var}(X)

  2. Cov(X,Y)=Cov(Y,X)\mathrm{Cov}(X, Y) = \mathrm{Cov}(Y, X) (Symmetric)

  3. Cov(aX+b,cY+d)=acCov(X,Y)\mathrm{Cov}(aX + b, cY + d) = ac \mathrm{Cov}(X, Y) for constants a,b,c,da, b, c, d. (Scaling affects covariance)

  4. Cov(X+Y,Z)=Cov(X,Z)+Cov(Y,Z)\mathrm{Cov}(X+Y, Z) = \mathrm{Cov}(X, Z) + \mathrm{Cov}(Y, Z) (Distributive)

  5. If XX and YY are independent, then E[XY]=E[X]E[Y]E[XY] = E[X]E[Y], which implies Cov(X,Y)=0\mathrm{Cov}(X, Y) = 0.

Important Note: The value of covariance depends on the units of XX and YY. For example, Cov(Height in cm,Weight in kg)\mathrm{Cov}(\text{Height in cm}, \text{Weight in kg}) will be much larger than Cov(Height in m,Weight in kg)\mathrm{Cov}(\text{Height in m}, \text{Weight in kg}), even though the underlying relationship is the same. This makes it hard to judge the strength of the relationship from covariance alone.

Calculating Covariance with NumPy

NumPy’s np.cov() function calculates the covariance matrix. For two 1D arrays x and y, np.cov(x, y) returns a 2x2 matrix:

(Var(X)Cov(X,Y)Cov(Y,X)Var(Y))\begin{pmatrix} \mathrm{Var}(X) & \mathrm{Cov}(X, Y) \\ \mathrm{Cov}(Y, X) & \mathrm{Var}(Y) \end{pmatrix}

We are usually interested in the off-diagonal elements, Cov(X,Y)\mathrm{Cov}(X, Y).

Covariance Matrix (X, Y_pos):
[[0.95886385 1.89799796]
 [1.89799796 4.00526524]]

Cov(X, Y_pos): 1.8980
Cov(X, Y_neg): -1.4580
Cov(X, Y_indep): 0.0213

As expected:

Correlation Coefficient

To overcome the unit-dependency of covariance and get a measure of the strength of the linear relationship, we use the correlation coefficient.

Definition: The Pearson correlation coefficient between two random variables XX and YY, denoted ρ(X,Y)\rho(X, Y), ρXY\rho_{XY}, or sometimes Corr(X,Y)\mathrm{Corr}(X,Y), is defined as:

ρ(X,Y)=Cov(X,Y)σXσY=Cov(X,Y)Var(X)Var(Y)\rho(X, Y) = \frac{\mathrm{Cov}(X, Y)}{\sigma_X \sigma_Y} = \frac{\mathrm{Cov}(X, Y)}{\sqrt{\mathrm{Var}(X) \mathrm{Var}(Y)}}

where σX\sigma_X and σY\sigma_Y are the standard deviations of XX and YY, assuming they are non-zero.

Properties:

  1. Range: −1≤ρ(X,Y)≤1-1 \le \rho(X, Y) \le 1. The correlation coefficient is dimensionless.

  2. Linear Relationship:

    • ρ(X,Y)=1\rho(X, Y) = 1: Perfect positive linear relationship (Y=aX+bY = aX + b with a>0a > 0).

    • ρ(X,Y)=−1\rho(X, Y) = -1: Perfect negative linear relationship (Y=aX+bY = aX + b with a<0a < 0).

    • ρ(X,Y)=0\rho(X, Y) = 0: No linear relationship.

  3. Symmetry: ρ(X,Y)=ρ(Y,X)\rho(X, Y) = \rho(Y, X).

  4. Invariance to Linear Transformation: ρ(aX+b,cY+d)=sign(ac)ρ(X,Y)\rho(aX + b, cY + d) = \mathrm{sign}(ac) \rho(X, Y), assuming a≠0,c≠0a \ne 0, c \ne 0. Scaling and shifting variables doesn’t change the magnitude of the correlation, only potentially the sign.

  5. Independence Implies Zero Correlation: If XX and YY are independent, then Cov(X,Y)=0\mathrm{Cov}(X, Y) = 0, which means ρ(X,Y)=0\rho(X, Y) = 0.

Crucial Warning: Correlation does not imply causation! Just because two variables are correlated doesn’t mean one causes the other. There might be a lurking (confounding) variable influencing both. (Classic example: Ice cream sales and crime rates are correlated, but both are caused by warmer weather).

Crucial Warning 2: Zero correlation does not imply independence! Correlation measures only linear dependence. It’s possible for XX and YY to be strongly dependent in a non-linear way, yet have zero correlation.

Consider X∼Uniform(−1,1)X \sim \text{Uniform}(-1, 1) and Y=X2Y = X^2. Clearly, YY is perfectly dependent on XX. E[X]=0E[X] = 0. E[Y]=E[X2]=∫−11x2(1/2)dx=[x3/6]−11=1/3E[Y] = E[X^2] = \int_{-1}^{1} x^2 (1/2) dx = [x^3/6]_{-1}^1 = 1/3. E[XY]=E[X3]=∫−11x3(1/2)dx=[x4/8]−11=0E[XY] = E[X^3] = \int_{-1}^{1} x^3 (1/2) dx = [x^4/8]_{-1}^1 = 0. Cov(X,Y)=E[XY]−E[X]E[Y]=0−(0)(1/3)=0\mathrm{Cov}(X, Y) = E[XY] - E[X]E[Y] = 0 - (0)(1/3) = 0. Thus, ρ(X,Y)=0\rho(X, Y) = 0, even though YY is completely determined by XX.

Calculating and Visualizing Correlation

We can use np.corrcoef() or Pandas DataFrame’s .corr() method. Scatter plots are essential for visualizing the relationship.

Correlation Matrix (X, Y_pos):
[[1.         0.96850446]
 [0.96850446 1.        ]]

Corr(X, Y_pos): 0.9685
Corr(X, Y_neg): -0.9483
Corr(X, Y_indep): 0.0221
<Figure size 1800x500 with 3 Axes>

The scatter plots visually confirm the relationships indicated by the correlation coefficients. Note how the spread around the potential line affects the magnitude of ρ\rho. Y_pos has a stronger linear relationship (less noise relative to the slope) than Y_neg in our simulation, resulting in ∣ρXYpos∣>∣ρXYneg∣|\rho_{XY_{pos}}| > |\rho_{XY_{neg}}|. The independent case shows no discernible linear pattern.

Example: Correlation between Study Hours and Exam Scores

Let’s simulate some data where exam scores depend linearly on study hours, but with some random variation.

Covariance between Study Hours and Exam Score: 29.70
Correlation between Study Hours and Exam Score: 0.85
<Figure size 800x500 with 1 Axes>

The positive correlation confirms the simulated relationship: students who study more tend to get higher scores. The correlation isn’t 1 because of the random noise component (representing other factors like innate ability, test anxiety, luck).

Variance of Sums of Random Variables

Knowing the covariance or correlation is crucial when calculating the variance of a sum or difference of random variables.

Theorem: For any two random variables XX and YY, and constants aa and bb:

Var(aX+bY)=a2Var(X)+b2Var(Y)+2abCov(X,Y)\mathrm{Var}(aX + bY) = a^2 \mathrm{Var}(X) + b^2 \mathrm{Var}(Y) + 2ab \mathrm{Cov}(X, Y)

Proof Sketch: Var(aX+bY)=E[((aX+bY)−E[aX+bY])2]\mathrm{Var}(aX + bY) = E[((aX + bY) - E[aX + bY])^2] =E[(a(X−E[X])+b(Y−E[Y]))2]= E[(a(X - E[X]) + b(Y - E[Y]))^2] =E[a2(X−E[X])2+b2(Y−E[Y])2+2ab(X−E[X])(Y−E[Y])]= E[a^2(X - E[X])^2 + b^2(Y - E[Y])^2 + 2ab(X - E[X])(Y - E[Y])] Use linearity of expectation: =a2E[(X−E[X])2]+b2E[(Y−E[Y])2]+2abE[(X−E[X])(Y−E[Y])]= a^2 E[(X - E[X])^2] + b^2 E[(Y - E[Y])^2] + 2ab E[(X - E[X])(Y - E[Y])] =a2Var(X)+b2Var(Y)+2abCov(X,Y)= a^2 \mathrm{Var}(X) + b^2 \mathrm{Var}(Y) + 2ab \mathrm{Cov}(X, Y)

Special Case: Sum of Variables (a=1,b=1a=1, b=1)

Var(X+Y)=Var(X)+Var(Y)+2Cov(X,Y)\mathrm{Var}(X + Y) = \mathrm{Var}(X) + \mathrm{Var}(Y) + 2 \mathrm{Cov}(X, Y)

Special Case: Difference of Variables (a=1,b=−1a=1, b=-1)

Var(X−Y)=Var(X)+(−1)2Var(Y)+2(1)(−1)Cov(X,Y)\mathrm{Var}(X - Y) = \mathrm{Var}(X) + (-1)^2 \mathrm{Var}(Y) + 2(1)(-1) \mathrm{Cov}(X, Y)
Var(X−Y)=Var(X)+Var(Y)−2Cov(X,Y)\mathrm{Var}(X - Y) = \mathrm{Var}(X) + \mathrm{Var}(Y) - 2 \mathrm{Cov}(X, Y)

Crucial Simplification: Independent Variables

If XX and YY are independent, then Cov(X,Y)=0\mathrm{Cov}(X, Y) = 0. The formulas simplify significantly:

Var(aX+bY)=a2Var(X)+b2Var(Y)(if independent)\mathrm{Var}(aX + bY) = a^2 \mathrm{Var}(X) + b^2 \mathrm{Var}(Y) \quad (\text{if independent})
Var(X+Y)=Var(X)+Var(Y)(if independent)\mathrm{Var}(X + Y) = \mathrm{Var}(X) + \mathrm{Var}(Y) \quad (\text{if independent})
Var(X−Y)=Var(X)+Var(Y)(if independent)\mathrm{Var}(X - Y) = \mathrm{Var}(X) + \mathrm{Var}(Y) \quad (\text{if independent})

Extension to Multiple Variables: For nn random variables X1,X2,...,XnX_1, X_2, ..., X_n:

Var(∑i=1naiXi)=∑i=1nai2Var(Xi)+∑i≠jaiajCov(Xi,Xj)\mathrm{Var}\left(\sum_{i=1}^n a_i X_i\right) = \sum_{i=1}^n a_i^2 \mathrm{Var}(X_i) + \sum_{i \ne j} a_i a_j \mathrm{Cov}(X_i, X_j)
Var(∑i=1naiXi)=∑i=1nai2Var(Xi)+2∑i<jaiajCov(Xi,Xj)\mathrm{Var}\left(\sum_{i=1}^n a_i X_i\right) = \sum_{i=1}^n a_i^2 \mathrm{Var}(X_i) + 2 \sum_{i < j} a_i a_j \mathrm{Cov}(X_i, X_j)

If all XiX_i are independent, then all Cov(Xi,Xj)=0\mathrm{Cov}(X_i, X_j) = 0 for i≠ji \ne j, and:

Var(∑i=1naiXi)=∑i=1nai2Var(Xi)(if independent)\mathrm{Var}\left(\sum_{i=1}^n a_i X_i\right) = \sum_{i=1}^n a_i^2 \mathrm{Var}(X_i) \quad (\text{if independent})

Example Application: Portfolio Variance Imagine a simple portfolio with investment aa in Stock A (return XX) and investment bb in Stock B (return YY). The total return is R=aX+bYR = aX + bY. The risk (variance) of the portfolio is: Var(R)=a2Var(X)+b2Var(Y)+2abCov(X,Y)\mathrm{Var}(R) = a^2 \mathrm{Var}(X) + b^2 \mathrm{Var}(Y) + 2ab \mathrm{Cov}(X, Y). If the stocks are negatively correlated (Cov(X,Y)<0\mathrm{Cov}(X, Y) < 0), the portfolio variance is reduced compared to holding uncorrelated assets. This is the principle of diversification.

Hands-on: Demonstrating Variance Rules via Simulation

Let’s simulate and verify the variance rules.

--- Independent Case ---
Theoretical Var(X): 4.0000, Empirical Var(X): 4.0113
Theoretical Var(Y): 9.0000, Empirical Var(Y): 8.9801
Theoretical Cov(X, Y): 0.0000, Empirical Cov(X, Y): 0.0392
Theoretical Var(X+Y): 13.0000, Empirical Var(X+Y): 13.0699
Check: Var(X)+Var(Y) = 12.9915
Theoretical Var(X-Y): 13.0000, Empirical Var(X-Y): 12.9131
Check: Var(X)+Var(Y) = 12.9915

--- Dependent Case (Positive Correlation) ---
Empirical Var(X): 4.0113
Theoretical Var(Y_dep): 3.2500, Empirical Var(Y_dep): 3.2587
Theoretical Cov(X, Y_dep): 2.0000, Empirical Cov(X, Y_dep): 2.0126
Theoretical Var(X+Y_dep): 11.2500, Empirical Var(X+Y_dep): 11.2953
Check: Var(X)+Var(Y)+2Cov(X,Y) = 11.2953
Theoretical Var(X-Y_dep): 3.2500, Empirical Var(X-Y_dep): 3.2448
Check: Var(X)+Var(Y)-2Cov(X,Y) = 3.2447

The simulation results closely match the theoretical calculations based on the variance formulas, both for independent and dependent variables. Notice how the positive covariance in the dependent case increases the variance of the sum and decreases the variance of the difference compared to the independent case.

Summary

This chapter introduced key concepts for understanding the relationships between random variables:

We saw how to calculate covariance and correlation using NumPy and Pandas, visualize relationships using scatter plots, and verify the variance rules through simulation. These concepts are fundamental for multivariate statistics, machine learning (feature selection/engineering), finance (portfolio theory), and many other fields where understanding variable interactions is crucial.

Exercises

  1. Conceptual: Give an example of two variables that you expect to be: a) Positively correlated. b) Negatively correlated. c) Uncorrelated but dependent. d) Independent. Justify your reasoning.

  2. Calculation: Let XX have E[X]=2,Var(X)=9E[X]=2, \mathrm{Var}(X)=9. Let YY have E[Y]=−1,Var(Y)=4E[Y]=-1, \mathrm{Var}(Y)=4. Let Cov(X,Y)=−3\mathrm{Cov}(X, Y) = -3. Calculate: a) E[3X−2Y+5]E[3X - 2Y + 5] b) Var(X+Y)\mathrm{Var}(X + Y) c) Var(X−Y)\mathrm{Var}(X - Y) d) Var(3X−2Y+5)\mathrm{Var}(3X - 2Y + 5) e) ρ(X,Y)\rho(X, Y)

  3. Simulation (Correlation): a) Generate 500 samples of X∼Normal(0,1)X \sim \text{Normal}(0, 1). b) Generate 500 samples of Y=−2X+ϵY = -2X + \epsilon, where ϵ∼Normal(0,σ2)\epsilon \sim \text{Normal}(0, \sigma^2) is noise. Try σ=0.5\sigma = 0.5 and σ=2\sigma = 2. c) For each value of σ\sigma, create a scatter plot of XX vs YY and calculate the sample correlation coefficient ρ(X,Y)\rho(X, Y). d) How does the noise level σ\sigma affect the correlation coefficient? Explain why.

  4. Simulation (Variance of Sums): a) Let X∼Poisson(λ=3)X \sim \text{Poisson}(\lambda=3) and Y∼Poisson(λ=5)Y \sim \text{Poisson}(\lambda=5). Assume XX and YY are independent. b) Generate 10,000 samples for XX and YY. c) Calculate the empirical variance of XX, YY, and X+YX+Y. d) The theoretical variance of a Poisson(λ\lambda) distribution is λ\lambda. Compare the empirical Var(X+Y)\mathrm{Var}(X+Y) to the theoretical prediction Var(X)+Var(Y)\mathrm{Var}(X) + \mathrm{Var}(Y) (since they are independent). Are they close? e) It’s a known property that the sum of independent Poisson variables is also Poisson, so X+Y∼Poisson(λX+λY)X+Y \sim \text{Poisson}(\lambda_X + \lambda_Y). What is the theoretical variance of X+YX+Y based on this property? Does it match your findings?

  5. Pandas Correlation: Load a dataset (e.g., the tips dataset from Seaborn: tips = sns.load_dataset('tips')). Calculate the correlation matrix for the numerical columns (e.g., total_bill, tip, size). Interpret the correlation between total_bill and tip. Visualize this relationship with a scatter plot.