Skip to main content
© 2026 ePowerAI. All rights reserved.
CoursesContact
eAI
CoursesContact
Week 3: Expectation, Variance, and Covariance
Prob & Stats
01Week 1: Probability Spaces and Events
02Week 2: Random Variables and Distributions
03Week 3: Expectation, Variance, and Covariance
04Week 4: Conditioning and Independence
05Week 5: Bayes' Rule and Generative Classification
06Week 6: Multivariate Gaussians
07Week 7: Laws of Large Numbers and the CLT
08Week 8: Point Estimation — Maximum Likelihood
09Week 9: Bayesian Estimation and MAP
10Week 10: Bias, Variance, and Uncertainty
11Week 11: Information Theory for ML
12Week 12: Exponential Families (lite)
13Week 13: Graphical Models (lite)
14Week 14: Capstone — Probabilistic Language of ML
Week 03· Prob & Stats· Foundations22 min read

Week 3: Expectation, Variance, and Covariance

✦Learning Outcomes
  • Compute E[X]\mathbb{E}[X]E[X] for discrete and continuous examples
  • Use linearity of expectation (even for dependent variables)
  • Compute variance and standard deviation; expand Var(aX+b)\mathrm{Var}(aX+b)Var(aX+b)
  • Define covariance and correlation; interpret sign of correlation
◆Prerequisites

Required: Weeks 1–2 (sample spaces, RVs, PMF/PDF/CDF, Bernoulli, Binomial, Uniform, Exponential).

Math: Summation and basic definite integration. Partial derivatives are not needed here; all expectations in this week are univariate or use the joint table directly.

Expectation#

The expectation (expected value, mean) of a random variable XXX is the probability-weighted average of its possible values:

Discrete: E[X]=∑xx⋅pX(x)\displaystyle \mathbb{E}[X] = \sum_x x \cdot p_X(x)E[X]=x∑​x⋅pX​(x)

Continuous: E[X]=∫−∞∞x⋅fX(x) dx\displaystyle \mathbb{E}[X] = \int_{-\infty}^{\infty} x \cdot f_X(x)\, dxE[X]=∫−∞∞​x⋅fX​(x)dx

Expectation is the centre of mass of the distribution — the balancing point if you placed probability mass along a number line.

Worked examples:

  • Bernoulli(p)(p)(p): E[X]=0⋅(1−p)+1⋅p=p\mathbb{E}[X] = 0 \cdot (1-p) + 1 \cdot p = pE[X]=0⋅(1−p)+1⋅p=p.
  • Binomial(n,p)(n, p)(n,p): E[X]=np\mathbb{E}[X] = npE[X]=np (derivable from the sum-of-Bernoullis view).
  • Uniform(a,b)(a, b)(a,b): E[X]=a+b2\mathbb{E}[X] = \frac{a+b}{2}E[X]=2a+b​ (the midpoint).
  • Exponential(λ)(\lambda)(λ): E[X]=1/λ\mathbb{E}[X] = 1 / \lambdaE[X]=1/λ (rate === inverse of mean wait time).

LOTUS — Law of the Unconscious Statistician. To find E[g(X)]\mathbb{E}[g(X)]E[g(X)], you do not need the distribution of g(X)g(X)g(X). You integrate/sum g(x)g(x)g(x) against the distribution of XXX:

E[g(X)]=∑xg(x) pX(x)or∫g(x) fX(x) dx.\mathbb{E}[g(X)] = \sum_x g(x)\, p_X(x) \quad \text{or} \quad \int g(x)\, f_X(x)\, dx.E[g(X)]=x∑​g(x)pX​(x)or∫g(x)fX​(x)dx.

Worked example — E[X2]\mathbb{E}[X^2]E[X2] for a fair die. With p(x)=1/6p(x) = 1/6p(x)=1/6 for x=1,…,6x = 1,\ldots,6x=1,…,6:

E[X2]=16(12+22+32+42+52+62)=1+4+9+16+25+366=916≈15.17.\mathbb{E}[X^2] = \frac{1}{6}(1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2) = \frac{1 + 4 + 9 + 16 + 25 + 36}{6} = \frac{91}{6} \approx 15.17.E[X2]=61​(12+22+32+42+52+62)=61+4+9+16+25+36​=691​≈15.17.

Contrast with (E[X])2=(3.5)2=12.25(\mathbb{E}[X])^2 = (3.5)^2 = 12.25(E[X])2=(3.5)2=12.25 — expectation of the square is larger than the square of the expectation (Jensen).

Linearity of Expectation#

Linearity is the most powerful shortcut in probability. For any random variables X,YX, YX,Y and constants a,ba, ba,b:

E[aX+bY]=a E[X]+b E[Y].\mathbb{E}[aX + bY] = a\,\mathbb{E}[X] + b\,\mathbb{E}[Y].E[aX+bY]=aE[X]+bE[Y].

No independence required. The expectation of a sum is always the sum of expectations, regardless of whether XXX and YYY are independent, correlated, or even deterministically linked.

Proof sketch (discrete):

E[X+Y]=∑x∑y(x+y) pX,Y(x,y)=∑xx∑ypX,Y(x,y)+∑yy∑xpX,Y(x,y)=∑xx pX(x)+∑yy pY(y)=E[X]+E[Y].\mathbb{E}[X + Y] = \sum_x \sum_y (x + y)\, p_{X,Y}(x, y) = \sum_x x \sum_y p_{X,Y}(x, y) + \sum_y y \sum_x p_{X,Y}(x, y) = \sum_x x\, p_X(x) + \sum_y y\, p_Y(y) = \mathbb{E}[X] + \mathbb{E}[Y].E[X+Y]=x∑​y∑​(x+y)pX,Y​(x,y)=x∑​xy∑​pX,Y​(x,y)+y∑​yx∑​pX,Y​(x,y)=x∑​xpX​(x)+y∑​ypY​(y)=E[X]+E[Y].

Worked example — dependent variables. Let XXX be the roll of a die, and Y=7−XY = 7 - XY=7−X (so YYY is completely determined by XXX). Then E[X]=3.5\mathbb{E}[X] = 3.5E[X]=3.5, E[Y]=3.5\mathbb{E}[Y] = 3.5E[Y]=3.5, and

E[X+Y]=E[X]+E[Y]=7.\mathbb{E}[X + Y] = \mathbb{E}[X] + \mathbb{E}[Y] = 7.E[X+Y]=E[X]+E[Y]=7.

Indeed X+Y=7X + Y = 7X+Y=7 always, so E[7]=7\mathbb{E}[7] = 7E[7]=7. Linearity works despite perfect dependence.

Binomial expectation via linearity: X=∑i=1nYiX = \sum_{i=1}^n Y_iX=∑i=1n​Yi​ where Yi∼Bernoulli(p)Y_i \sim \text{Bernoulli}(p)Yi​∼Bernoulli(p). Even if the YiY_iYi​ were correlated, E[X]=∑E[Yi]=np\mathbb{E}[X] = \sum \mathbb{E}[Y_i] = npE[X]=∑E[Yi​]=np (for i.i.d. they happen to be independent, but linearity does not need that).

Variance#

The variance measures spread — how far XXX tends to be from its mean:

Var(X)=E[(X−μ)2],where μ=E[X].\mathrm{Var}(X) = \mathbb{E}[(X - \mu)^2], \quad \text{where } \mu = \mathbb{E}[X].Var(X)=E[(X−μ)2],where μ=E[X].

The standard deviation is σX=Var(X)\sigma_X = \sqrt{\mathrm{Var}(X)}σX​=Var(X)​, which has the same units as XXX.

Computational formula (often easier to calculate):

Var(X)=E[X2]−(E[X])2.\mathrm{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2.Var(X)=E[X2]−(E[X])2.

Proof: E[(X−μ)2]=E[X2−2μX+μ2]=E[X2]−2μ E[X]+μ2=E[X2]−μ2\mathbb{E}[(X - \mu)^2] = \mathbb{E}[X^2 - 2\mu X + \mu^2] = \mathbb{E}[X^2] - 2\mu\,\mathbb{E}[X] + \mu^2 = \mathbb{E}[X^2] - \mu^2E[(X−μ)2]=E[X2−2μX+μ2]=E[X2]−2μE[X]+μ2=E[X2]−μ2.

Scaling and shifting:

Var(aX+b)=a2 Var(X).\mathrm{Var}(aX + b) = a^2 \, \mathrm{Var}(X).Var(aX+b)=a2Var(X).

The constant shift bbb adds no variance. Scaling by aaa scales variance by a2a^2a2 (since variance is in squared units).

Worked examples:

  • Bernoulli(p)(p)(p): Var(X)=p(1−p)\mathrm{Var}(X) = p(1-p)Var(X)=p(1−p). Maximum at p=0.5p = 0.5p=0.5.
  • Binomial(n,p)(n, p)(n,p): Var(X)=np(1−p)\mathrm{Var}(X) = np(1-p)Var(X)=np(1−p).
  • Uniform(a,b)(a, b)(a,b): Var(X)=(b−a)212\mathrm{Var}(X) = \frac{(b-a)^2}{12}Var(X)=12(b−a)2​.
  • Exponential(λ)(\lambda)(λ): Var(X)=1/λ2\mathrm{Var}(X) = 1 / \lambda^2Var(X)=1/λ2.

Worked computation — fair die. E[X]=3.5\mathbb{E}[X] = 3.5E[X]=3.5. From earlier, E[X2]=91/6≈15.167\mathbb{E}[X^2] = 91/6 \approx 15.167E[X2]=91/6≈15.167. So

Var(X)=916−(3.5)2=916−494=182−14712=3512≈2.917.\mathrm{Var}(X) = \frac{91}{6} - (3.5)^2 = \frac{91}{6} - \frac{49}{4} = \frac{182 - 147}{12} = \frac{35}{12} \approx 2.917.Var(X)=691​−(3.5)2=691​−449​=12182−147​=1235​≈2.917.

Standard deviation σ≈1.71\sigma \approx 1.71σ≈1.71.

Covariance and Correlation#

Covariance measures how two variables move together:

Cov(X,Y)=E[(X−μX)(Y−μY)].\mathrm{Cov}(X, Y) = \mathbb{E}[(X - \mu_X)(Y - \mu_Y)].Cov(X,Y)=E[(X−μX​)(Y−μY​)].

Computational form: Cov(X,Y)=E[XY]−E[X] E[Y]\mathrm{Cov}(X, Y) = \mathbb{E}[XY] - \mathbb{E}[X]\,\mathbb{E}[Y]Cov(X,Y)=E[XY]−E[X]E[Y].

  • Positive covariance: XXX above its mean tends to happen with YYY above its mean.
  • Negative covariance: opposite directions.
  • Zero covariance: no linear relationship detected.

Properties:

  • Cov(X,X)=Var(X)\mathrm{Cov}(X, X) = \mathrm{Var}(X)Cov(X,X)=Var(X).
  • Cov(X,Y)=Cov(Y,X)\mathrm{Cov}(X, Y) = \mathrm{Cov}(Y, X)Cov(X,Y)=Cov(Y,X) (symmetric).
  • Cov(aX+b,cY+d)=ac Cov(X,Y)\mathrm{Cov}(aX + b, cY + d) = ac \, \mathrm{Cov}(X, Y)Cov(aX+b,cY+d)=acCov(X,Y).
  • Var(X+Y)=Var(X)+Var(Y)+2 Cov(X,Y)\mathrm{Var}(X + Y) = \mathrm{Var}(X) + \mathrm{Var}(Y) + 2\,\mathrm{Cov}(X, Y)Var(X+Y)=Var(X)+Var(Y)+2Cov(X,Y).

The last formula shows why variance of a sum is not always sum of variances — the covariance term can add or subtract.

Correlation standardises covariance to the range [−1,1][-1, 1][−1,1]:

ρ(X,Y)=Cov(X,Y)σXσY.\rho(X, Y) = \frac{\mathrm{Cov}(X, Y)}{\sigma_X \sigma_Y}.ρ(X,Y)=σX​σY​Cov(X,Y)​.
  • ρ=1\rho = 1ρ=1: perfect positive linear relationship.
  • ρ=−1\rho = -1ρ=−1: perfect negative linear relationship.
  • ρ=0\rho = 0ρ=0: uncorrelated.

Critical warning — uncorrelated ≠\ne= independent. Independence implies uncorrelated, but uncorrelated does not imply independence. Example: let X∼Uniform(−1,1)X \sim \text{Uniform}(-1, 1)X∼Uniform(−1,1) and Y=X2Y = X^2Y=X2. Then Cov(X,Y)=E[X3]−E[X]E[X2]=0−0⋅(1/3)=0\mathrm{Cov}(X, Y) = \mathbb{E}[X^3] - \mathbb{E}[X]\mathbb{E}[X^2] = 0 - 0 \cdot (1/3) = 0Cov(X,Y)=E[X3]−E[X]E[X2]=0−0⋅(1/3)=0. They are uncorrelated but perfectly dependent — knowing XXX tells you YYY exactly.

Correlation only captures linear dependence. Non-linear relationships (quadratic, periodic) can have zero correlation but strong dependence.

Worked example — joint table. Suppose XXX and YYY have the following joint PMF:

| | Y=0Y = 0Y=0 | Y=1Y = 1Y=1 | |---|---|---| | X=0X = 0X=0 | 0.2 | 0.3 | | X=1X = 1X=1 | 0.1 | 0.4 |

Then E[X]=0.5\mathbb{E}[X] = 0.5E[X]=0.5, E[Y]=0.7\mathbb{E}[Y] = 0.7E[Y]=0.7, E[XY]=0⋅0⋅0.2+0⋅1⋅0.3+1⋅0⋅0.1+1⋅1⋅0.4=0.4\mathbb{E}[XY] = 0 \cdot 0 \cdot 0.2 + 0 \cdot 1 \cdot 0.3 + 1 \cdot 0 \cdot 0.1 + 1 \cdot 1 \cdot 0.4 = 0.4E[XY]=0⋅0⋅0.2+0⋅1⋅0.3+1⋅0⋅0.1+1⋅1⋅0.4=0.4.

Cov(X,Y)=0.4−(0.5)(0.7)=0.4−0.35=0.05.\mathrm{Cov}(X, Y) = 0.4 - (0.5)(0.7) = 0.4 - 0.35 = 0.05.Cov(X,Y)=0.4−(0.5)(0.7)=0.4−0.35=0.05.

Small positive covariance — X=1X = 1X=1 slightly increases the chance of Y=1Y = 1Y=1.

Conditional Expectation Intro#

The conditional expectation E[X∣Y=y]\mathbb{E}[X \mid Y = y]E[X∣Y=y] is the average of XXX over the subset of outcomes where Y=yY = yY=y. It is a function of yyy: for each possible value of YYY, we get a (potentially different) expected value of XXX.

Worked example — from the table above:

E[X∣Y=0]=0⋅0.2+1⋅0.10.3=13,E[X∣Y=1]=0⋅0.3+1⋅0.40.7=47.\mathbb{E}[X \mid Y = 0] = \frac{0 \cdot 0.2 + 1 \cdot 0.1}{0.3} = \frac{1}{3}, \quad \mathbb{E}[X \mid Y = 1] = \frac{0 \cdot 0.3 + 1 \cdot 0.4}{0.7} = \frac{4}{7}.E[X∣Y=0]=0.30⋅0.2+1⋅0.1​=31​,E[X∣Y=1]=0.70⋅0.3+1⋅0.4​=74​.

When Y=0Y = 0Y=0, XXX averages lower; when Y=1Y = 1Y=1, XXX averages higher — consistent with the positive covariance we found.

Law of total expectation: E[X]=EY[E[X∣Y]]\mathbb{E}[X] = \mathbb{E}_Y[\mathbb{E}[X \mid Y]]E[X]=EY​[E[X∣Y]] — the overall mean is the weighted average of conditional means. Week 4 deepens this.

Exercise · Fill in the blank

Let X take values 1, 2, 3 with probabilities 0.2, 0.5, 0.3. Compute E[X] as a decimal.


Knowledge check#

Question 1 of 4

Linearity of expectation holds only when the random variables are independent. True or false?

False — linearity holds always
True — independence is required
Only for discrete variables
Only for continuous variables

Browser lab#

Sample pairs from a known joint distribution, compute empirical mean/variance/covariance, and compare to formulas.

python · runs in browser
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(42)
n = 5000

joint = np.array([[0.1, 0.2, 0.1],
                   [0.05, 0.3, 0.05],
                   [0.0, 0.1, 0.1]])
x_vals = np.array([0, 1, 2])
y_vals = np.array([0, 1, 2])

flat = joint.ravel() / joint.sum()
indices = rng.choice(len(flat), size=n, p=flat)
xi = indices // 3
yi = indices % 3
xs = x_vals[xi]
ys = y_vals[yi]

E_x = np.sum(x_vals * joint.sum(axis=1))
E_y = np.sum(y_vals * joint.sum(axis=0))
E_xy = np.sum(np.outer(x_vals, y_vals) * joint)
Cov_xy = E_xy - E_x * E_y

E_x_emp = xs.mean()
E_y_emp = ys.mean()
Cov_emp = np.mean((xs - E_x_emp) * (ys - E_y_emp))

print(f"E[X]  true={E_x:.4f}  emp={E_x_emp:.4f}")
print(f"E[Y]  true={E_y:.4f}  emp={E_y_emp:.4f}")
print(f"Cov   true={Cov_xy:.4f}  emp={Cov_emp:.4f}")

px = joint.sum(axis=1)
py = joint.sum(axis=0)

fig, axes = plt.subplots(1, 3, figsize=(13, 4))
axes[0].bar(x_vals, px, color="tab:blue", alpha=0.7)
axes[0].set_title("Marginal P(X)")
axes[0].set_xlabel("x")
axes[1].bar(y_vals, py, color="tab:orange", alpha=0.7)
axes[1].set_title("Marginal P(Y)")
axes[1].set_xlabel("y")
axes[2].scatter(xs[:500], ys[:500], alpha=0.3, s=8)
axes[2].set_title("Joint samples (first 500)")
axes[2].set_xlabel("x"); axes[2].set_ylabel("y")
plt.tight_layout()
plt.show()
← Previous
Week 2: Random Variables and Distributions
Next →
Week 4: Conditioning and Independence
On this page
  • Expectation
  • Linearity of Expectation
  • Variance
  • Covariance and Correlation
  • Conditional Expectation Intro
  • Knowledge check
  • Browser lab