Guozhen AIGlobal AI field notes and model intelligence

English translation

Compute variances

Published:

Category: Probability for AI Beginners

Read time: 4 min

Reads: 0

Lesson #13Views are counted together with the original Chinese articleImages are preserved from the source page

Concept Diagram: Covariance and Correlation

Covariance measures whether two variables change together; correlation removes the influence of scale. A high correlation does not imply causation.

Covariance and Correlation Checklist Diagram

I always start with a scatter plot. A single correlation coefficient cannot reveal outliers, nonlinear relationships, or subgroup structures.

In the previous article, we explored the properties of variance, learning how to quantify the dispersion of a single random variable. This article continues our discussion of key concepts in probability theory: covariance and correlation—essential tools for analyzing relationships between random variables, widely applied in machine learning and data analysis.

Definition of Covariance

Covariance is a measure describing the linear relationship between two random variables. Given random variables XX and YY, their covariance is defined as:

Covariance & Correlation Decision Card

When learning covariance and correlation, first assess whether the two variables move in the same direction; then examine the sign and magnitude of the standardized correlation coefficient.

Cov(X,Y)=E[(XE[X])(YE[Y])]\text{Cov}(X, Y) = \mathbb{E}[(X - \mathbb{E}[X])(Y - \mathbb{E}[Y])]

This formula helps us intuitively understand covariance: it quantifies how deviations of one variable from its mean relate to deviations of the other variable from its mean.

Properties of Covariance

  1. Interpretation of Sign:

    • If Cov(X,Y)>0\text{Cov}(X, Y) > 0, XX and YY are positively associated: when one tends to increase, the other tends to increase as well.
    • If Cov(X,Y)<0\text{Cov}(X, Y) < 0, XX and YY are negatively associated.
    • If Cov(X,Y)=0\text{Cov}(X, Y) = 0, there is no linear relationship between them.
  2. Unit Sensitivity:

    • Covariance carries units equal to the product of the units of XX and YY, making interpretation less intuitive.

Example

Suppose XX and YY represent a student’s study time (in hours) and exam score (in points), respectively. Observed data are shown below:

Study Time (XX) Exam Score (YY)
1 50
2 55
3 60
4 70
5 75

First, compute the expectations:

E[X]=1+2+3+4+55=3\mathbb{E}[X] = \frac{1 + 2 + 3 + 4 + 5}{5} = 3 E[Y]=50+55+60+70+755=62\mathbb{E}[Y] = \frac{50 + 55 + 60 + 70 + 75}{5} = 62

Then apply the covariance formula:

Cov(X,Y)=15i=15(XiE[X])(YiE[Y])\text{Cov}(X, Y) = \frac{1}{5} \sum_{i=1}^{5} (X_i - \mathbb{E}[X])(Y_i - \mathbb{E}[Y])
import numpy as np

X = np.array([1, 2, 3, 4, 5])
Y = np.array([50, 55, 60, 70, 75])

cov_xy = np.cov(X, Y)[0][1]  # Extract covariance between X and Y
cov_xy

The computed covariance Cov(X,Y)\text{Cov}(X, Y) is positive, indicating a positive linear association between study time and exam score.

Definition and Computation of Correlation

Correlation is the standardized version of covariance—designed specifically to eliminate unit dependence. It is typically quantified using the Pearson correlation coefficient, defined as:

Probability Reading Map Card

When reading “Covariance and Correlation,” begin by reviewing the tasks, core concepts, exercises, and decision points illustrated in the accompanying figures—then return to the main text to fill in details. This approach helps you quickly identify which real-world scenarios this content applies to.

rXY=Cov(X,Y)Var(X)Var(Y)r_{XY} = \frac{\text{Cov}(X, Y)}{\sqrt{\text{Var}(X) \cdot \text{Var}(Y)}}

where Var(X)\text{Var}(X) and Var(Y)\text{Var}(Y) denote the variances of XX and YY, respectively.

Properties of Correlation

  1. Range:
    • The correlation coefficient rXYr_{XY} always lies in the interval [1,1][-1, 1].
    • rXY=1r_{XY} = 1 indicates perfect positive linear correlation; rXY=1r_{XY} = -1, perfect negative linear correlation; rXY=0r_{XY} = 0, no linear correlation.

Example

Continuing with the earlier example, we compute the Pearson correlation coefficient between study time and exam score.

# Compute variances
var_x = np.var(X)
var_y = np.var(Y)

# Compute correlation coefficient
correlation = cov_xy / (np.sqrt(var_x) * np.sqrt(var_y))
correlation

Running this code yields the correlation coefficient rr. Suppose the result is r=0.95r = 0.95: this indicates a strong positive linear relationship between study time and exam performance.

Covariance and Correlation Application Review Card

When reviewing “Covariance and Correlation,” place key concepts, procedural steps, and observable outcomes on the same page for efficient revision.

Covariance and Correlation Application Checklist Card

When practicing “Covariance and Correlation,” write input conditions, processing steps, and observable outcomes together—making future review straightforward.

Summary

In this article, we introduced covariance and correlation, fundamental tools for investigating relationships between two random variables. By computing covariance and correlation coefficients, we gain deeper insight into underlying data structure and dependencies—laying essential groundwork for the next topic: the Law of Large Numbers.

In the following article, we will delve into the Law of Large Numbers, exploring how sample means converge toward the population mean as sample size increases. We hope you’ll apply these concepts confidently to analyze real-world problems!

Continue

Keep reading from here

Browse English site

Reader Messages

Reader messages

Questions, corrections, extra sources, or hands-on results can be left here. No login is required.

Max 800 characters

To reduce spam, each message is checked for length, link count, and posting frequency.

0/800

Messages

0 messages
Loading messages...