TensorTau PathToAGI Probability and Uncertainty Level 03

Level 03 · Probability and Uncertainty · Chapter 031

Covariance and Correlation

How can we measure whether two uncertain quantities move together?

1. The Problem

Suppose we record study hours XX and exam score YY. The mean tells us each variable’s center, but not whether students above the mean in study hours also tend to be above the mean in score.

We need a measure of co-movement.


2. Center Both Variables

For one observation, compare deviations from the means:

X−μX,Y−μY.X-\mu_X,\qquad Y-\mu_Y.

Multiply them.

  • both above their means → positive product,
  • both below → positive product,
  • one above and one below → negative product.

Average these products:

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

Equivalent form:

Cov(X,Y)=E[XY]−E[X]E[Y].Cov(X,Y)=E[XY]-E[X]E[Y].


3. Tiny Example

Let

X = [1, 2, 3]
Y = [2, 4, 6]

Means are 2 and 4. Centered values:

Xc = [-1, 0, 1]
Yc = [-2, 0, 2]

Products:

[2, 0, 2]

Their average is positive, so the variables move together.

If Y=[6,4,2]Y=[6,4,2], the products become negative and covariance is negative.


4. The Scale Problem

Covariance depends on units. Change meters to centimeters and covariance changes dramatically even though the relationship is identical.

Normalize by standard deviations:

ρXY=Cov(X,Y)σXσY.\boxed{\rho_{XY}=\frac{Cov(X,Y)}{\sigma_X\sigma_Y}}.

This is correlation. It lies between -1 and 1.

  • +1: perfect positive linear relation,
  • 0: no linear correlation,
  • -1: perfect negative linear relation.

5. Correlation Is Not Causation

Ice-cream sales and sunburn cases may be correlated because both rise with hot weather. Buying ice cream does not cause sunburn.

A correlation describes a statistical relationship, not a causal mechanism.

Also, zero correlation does not necessarily mean independence. A nonlinear relationship can have zero linear correlation.

Example: if XX is symmetric around zero and Y=X2Y=X^2, knowing XX tells you YY exactly, yet their correlation can be zero.


6. Covariance Matrix

For a vector x∈Rd\mathbf{x}\in\mathbb{R}^d:

Σ=E[(x−μ)(x−μ)T].\Sigma=E[(\mathbf{x}-\mu)(\mathbf{x}-\mu)^T].

Entry (i,j)(i,j) is the covariance between features ii and jj.

Diagonal entries are variances.

For 3 features:

Σ∈R3×3.\Sigma\in\mathbb{R}^{3\times3}.

This is the same object that appeared in PCA. Now we understand its probabilistic meaning.


7. Code

import numpy as np

X = np.array([1., 2., 3., 4.])
Y = np.array([2., 3., 5., 8.])

xc = X - X.mean()
yc = Y - Y.mean()

cov = (xc * yc).mean()
corr = cov / (X.std() * Y.std())

print(cov, corr)

Compare with np.cov carefully: libraries may use n−1n-1 instead of nn in sample covariance.


8. Shape Check

Data matrix:

X: [1000, 50]

1000 examples, 50 features.

After centering, XcX_c stays [1000, 50].

Then

XcTXcX_c^TX_c

has shape

[50, 50]

That is why a feature covariance matrix is square in the number of features.


9. Real-World Connection

Covariance and correlation appear in PCA, Gaussian models, portfolio risk, feature analysis, whitening, uncertainty propagation and diagnostics. In deep learning, correlated activations can make optimization geometry awkward; normalization and representation learning often change these relationships.


Mastery Check

  1. Why multiply centered deviations?
  2. What does the sign of covariance mean?
  3. Why normalize covariance to obtain correlation?
  4. Why does correlation not prove causation?
  5. Can dependent variables have zero correlation?
  6. Why is a 50-feature covariance matrix 50×50?

Bridge to Chapter 032

We can now describe center, spread and relationships. One distribution appears constantly because many noisy effects combine into a bell-shaped pattern.

Why does the Gaussian distribution appear everywhere?

Six ways to work the chapter, climbing from explaining it in your own words to investigating it like a researcher. Reading is not enough — this is where the chapter lands.

Explain

Explain positive, negative and near-zero covariance using three scatter plots you draw by hand.

Calculate

For X=[1,2,3], compute covariance with Y1=[2,4,6] and Y2=[6,4,2] from centered values.

Predict

Multiply every value of X by 100. Predict what happens to covariance and correlation before calculating.

Break

Construct a nonlinear dependent relationship with correlation near zero. Explain why “zero correlation means independent” fails.

Build

Implement covariance and correlation from scratch with NumPy primitives, then compare with library functions.

Investigate

Generate 2D Gaussian clouds with correlations -0.9, 0, 0.9. Plot them and connect geometry to the covariance matrix.

Shape Check

For X.shape == [500, 12], what shape is its feature covariance matrix? Why?

Ready to Continue?

Continue when you can see covariance simultaneously as centered co-movement, a matrix entry, and a geometric description of a data cloud.