Joint Distributions, Covariance, Correlation & Tower Property
Slice 2D tables live, see why zero correlation misses curved patterns, diversify portfolio risk, and learn why regression is literally E[Y | X]
Let X be picked equally from {−1, 0, +1}, and let Y = X². Since Y is 100% determined by X (zero randomness left!), what is their Pearson Correlation ρ(X, Y)?
- Read a 2D Joint Probability Table like a pro — extracting Marginal totals along the edges and Conditional slices along rows.
- Feel Covariance and Correlation ρ as 'do X and Y move up/down together along a straight line?'
- Prove with a 3-point example why Correlation ρ = 0 does NOT mean X and Y are independent!
- Use the Tower Property E[Y] = E[E[Y | X]] and EVE's Law to solve two-stage random problems effortlessly.
In real ML datasets, variables never live alone — weather affects commute delays, ad spend affects revenue, and features interact with labels. Joint tables let you see two variables at once, Correlation measures straight-line teamwork, and Conditional Expectation is the secret mathematical bridge from Probability straight into Machine Learning Regression!
1. Joint, Marginal & Conditional — Touch a Live 2D Table!
Track your morning train for 100 workdays: each day is either Sunny or Rainy, and your train is either On Time or Late. Put all 100 days into a tiny 2×2 grid. Add across the rows (in the right margin) to get Sunny vs. Rainy totals — that's why they're called Marginal probabilities! Now zoom into ONLY the Rainy row — that's a Conditional distribution!
Three Questions, One Table
- Joint cell: "Chance it's Rainy AND Late?" →
24 / 100 = 24%. - Marginal edge: "Overall chance it's Late (any weather)?" →
12 + 24 = 36 / 100 = 36%. - Conditional slice: "GIVEN it's Rainy, chance it's Late?" →
24 / 40 = 60%!
Click the buttons in the interactive 100-day commute table below to slice between All Days, Sunny Days, and Rainy Days!
Click Sunny, Rainy, or All 100 Days to see Marginal probabilities (totals along the edge) vs. Conditional probabilities (slicing ONE row)!
| Weather \ Train | On Time | Late | Row Total |
|---|---|---|---|
| Sunny | 48 | 12 | 60 |
| Rainy | 16 | 24 | 40 |
| Col Total | 64 | 36 | 100 |
Overall P(Late) is 36%, yet on Rainy days it shifts to 60% — proving Weather and Train Delay are DEPENDENT!
A 2×2 joint table of 100 users shows: Mobile+Buy = 20, Mobile+NoBuy = 30, Desktop+Buy = 20, Desktop+NoBuy = 30. Compute P(Buy | Mobile) and P(Buy | Desktop). Are Device and Buying independent?
2. Covariance, Correlation & Why Diversification Crushes Risk
Imagine owning two stocks: Umbrella Corp (booms on rainy days, dips on sunny days) and Sunscreen Inc (booms on sunny days, dips on rainy days). Each stock alone is a wild roller coaster. Yet put 50% of your money in EACH, and on every day one winner offsets the other loser — your combined portfolio becomes calm and steady! Why? Negative Covariance!
Reading Covariance & Correlation in Plain English
- If is above average whenever is above average: → Positive Covariance.
- If is above average when is below average: → Negative Covariance.
- Divide by to erase units and get Correlation !
Click each colored chip of the Portfolio / Ensemble Variance formula below to see why negative correlation (or uncorrelated Random Forest trees!) slashes variance!
Why Random Forests subsample features and investors diversify across negatively correlated assets!
The baseline risk you'd get if the two stocks/models were completely independent strangers.
Pause & Check: X and Y are INDEPENDENT with Var(X) = 5 and Var(Y) = 3. What is Var(X − Y)? (Careful with the minus sign!)
Two assets X and Y each have standard deviation σ = 10 (Var = 100) and correlation ρ = −0.5. Find the variance of P = 0.5X + 0.5Y.
3. Tower Property (Adam's Law) & Total Variance (EVE's Law)
Want to find the average height of all college students in a country? You don't have to line up 10 million students at once! Just find the average height inside each university, and then take the weighted average of those university averages! That totally common-sense two-step average has a majestic name: the Tower Property (Law of Total Expectation)!
The Two Super-Laws for Two-Stage Random Stories
- Tower Property (Adam's Law): Average of the slice averages = overall average!
- Law of Total Variance (EVE's Law): Total spread = (average wiggle inside slices) + (how much the slice averages themselves jump around):
- Bivariate Normal Regression Shortcut: When is a 2D Gaussian cloud with correlation :
Coffee Shop Hourly Revenue (Random # of Customers × Random Spend!)
A café gets N ~ Poisson(20) customers per hour (so E[N] = 20, Var(N) = 20). Each customer independently spends an average of $6 with variance 9. Find the Mean and Variance of total hourly revenue S!
If exactly customers walk in, total spend is the sum of independent orders:
- Conditional Mean:
- Conditional Variance:
(X, Y) is Bivariate Normal with μ_X = 10, σ_X = 2, μ_Y = 50, σ_Y = 10, and ρ = 0.6. Predict E[Y | X = 12] and compute Var(Y | X = 12).
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
NO! Correlation ≠ Causation! Classic real-world example: monthly Ice Cream Sales and Shark Attacks have a massive positive correlation ()! Does buying ice cream summon sharks? No — a hidden third variable (Summer Heat!) drives BOTH up at the same time!
MSE Regression = Learning E[Y | X]
Every regression model trained with squared error loss is mathematically estimating the conditional expectation curve E[Y | X = x].
Random Forest Feature Subsampling
Averaging M trees with pairwise correlation ρ yields Var = σ²(ρ + (1−ρ)/M). Picking random features at each split lowers ρ directly!
PCA & Covariance Matrices
Principal Component Analysis diagonalizes the d × d Covariance matrix Σ to find uncorrelated directions of maximum variance.
Bellman Equation in Reinforcement Learning
The Tower Property E[G_t | s] = E[R_{t+1} + γ E[G_{t+1} | S_{t+1}] | s] IS literally the Bellman Equation!
import numpy as np
# 1. Sample from a 2D Gaussian with exact correlation rho = 0.75
rng = np.random.default_rng(7)
mu = np.array([10.0, 50.0])
sx, sy, rho = 2.0, 10.0, 0.75
cov = np.array([[sx**2, rho * sx * sy], [rho * sx * sy, sy**2]])
pts = rng.multivariate_normal(mu, cov, size=50_000)
X, Y = pts[:, 0], pts[:, 1]
# 2. Check the vertical slice around X ≈ 12!
mask = np.abs(X - 12.0) < 0.1
print(f"Empirical E[Y|X≈12] = {Y[mask].mean():.2f} (exact = {50 + rho*(sy/sx)*2:.2f})")
# 3. Demonstrate dependent Y = X^2 having zero correlation!
x_sym = rng.standard_normal(100_000)
print("Corr(X, X^2):", round(float(np.corrcoef(x_sym, x_sym**2)[0, 1]), 4))