Expectation and variance
Quick answer
Expectation is the probability-weighted average of a random variable; variance measures average squared distance from that expectation. Covariance measures joint variation of two variables. These quantities help ML practitioners reason about prediction error, feature scale and uncertainty, but no single moment describes an entire distribution.
Compute mean and variance from masses
For finite X with values x and masses p(x), μ=E[X]=Σ x p(x). Variance is E[(X−μ)²], also E[X²]−μ² when second moments exist. Standard deviation is the square root of variance and has the same units as X. For a Bernoulli(p), E[X]=p and Var(X)=p(1−p). For an independent binomial count, E[X]=np and Var(X)=np(1−p). Consider the count of heads from two independent fair flips.
xs = [0,1,2]ps = [0.25,0.5,0.25]mu = sum(x*p for x,p in zip(xs,ps))var = sum((x-mu)**2*p for x,p in zip(xs,ps))print(mu, var, round(var**0.5, 4))Expected output (Python 3.13.5):
1.0 0.5 0.7071The expected count is one, variance is half a head-count squared, and standard deviation is about 0.7071 in count units. Expectation need not be a possible outcome.
Measure joint variation
Cov(X,Y)=E[(X−E[X])(Y−E[Y])], when the relevant moments exist. Positive covariance means deviations tend to have the same sign in the weighted average; negative means opposite. For two variables with four equally likely paired outcomes, compute the mean centered product. These are model probabilities, not sample variance estimates. Linearity E[X+Y]=E[X]+E[Y] does not require independence; Var(X+Y)=Var(X)+Var(Y)+2Cov(X,Y).
pairs = [(1,4),(2,3),(3,2),(4,1)]mx = sum(x for x,y in pairs)/4my = sum(y for x,y in pairs)/4cov = sum((x-mx)*(y-my) for x,y in pairs)/4print(mx, my, cov)Expected output (Python 3.13.5):
2.5 2.5 -1.25Here the variables move in opposite directions. Covariance has product units, and zero covariance does not in general prove independence.
Common mistakes
Do not divide by n−1 when computing the variance of a fully specified uniform finite distribution. Estimating population variance from sampled observations is a separate problem in the next chapter. Prefer centered squared deviations to subtracting two nearly equal large values in floating-point calculations. Expectation is not a guarantee for one trial.
Practice and next steps
Compute expectation and variance for X=0 with probability 0.75 and X=4 with probability 0.25. Check whether the expected value is itself possible. Next, compare theory with repeated sampling.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Expectation and variance — knowledge check
Apply probability concepts to original finite and simulated examples.
What is E[X] for a Bernoulli variable with p=0.3?
What is Var(X) for a fair Bernoulli variable?
Which quantity has the same physical units as X?
What can be concluded from zero covariance alone?
What is the variance of a variable always equal to 7?