Expected value, variance and covariance: where a distribution lies, how wide it is and what goes with what
The expected value as a centre of mass, linearity, variance and standard deviation, Chebyshev's inequality, covariance and correlation — and why zero covariance does not yet mean independence.
Series AI basics Part 16 of 52
- Map of artificial intelligence: what is what and how it all fits together
- A brief history of artificial intelligence: from Turing to language models
- Data, model, training, inference: the basic terms on a single example
- How to read mathematical notation: symbols, indices, sums and quantifiers
- Sets and relations: the building blocks of definitions in AI
- Functions and mappings: why every AI model is a function
- Logic and proofs: the rules AI reasons by
- Graphs and trees: the structure that both search and neural networks stand on
- Linear algebra for machine learning: vectors, matrices and why AI mostly multiplies
- Derivatives, the gradient and the chain rule: how a model finds out which way to learn
- Gradient descent: how a model learns
- What probability is: from rolling dice to the Kolmogorov axioms
- Conditional probability and Bayes’ theorem: how to update your estimate when you learn something new
- Random variables and the cumulative distribution function: one function that tells you everything about a distribution
- Probability density and probability mass functions: why a continuous variable has zero probability at a point
- Expected value, variance and covariance: where a distribution lies, how wide it is and what goes with what
- Overview of probability distributions: Bernoulli, binomial, Poisson, uniform, normal and exponential
- Law of large numbers and central limit theorem: why averages behave so predictably
- Statistics and parameter estimation: maximum likelihood, MAP and confidence intervals
- Information theory: entropy, cross-entropy and KL divergence — where loss functions come from
- State-space search: BFS, DFS, UCS and A* with animations
- Games and adversaries: minimax, alpha-beta pruning and Monte Carlo tree search
- Constraint satisfaction: backtracking, constraint propagation and AC-3 on sudoku and a map of Czech regions
- Logic and knowledge systems: forward and backward chaining, resolution and expert systems
- Planning: STRIPS, the blocks world, heuristics from relaxation and planning in AI agents
- Local search and simulated annealing: a travelling salesman through the Czech regional capitals
- Genetic algorithms: population, crossover, mutation and selection on a backpack for a hike
- Evolution strategies and genetic programming: from the one-fifth rule to Kepler’s law
- Ant colony optimization: how a pheromone trail finds a short path
- Particle swarm optimisation: inertia, personal experience and the swarm’s experience
- Neuroevolution: evolving the weights and shape of neural networks, and the NEAT algorithm
- Fuzzy sets and membership functions: how to write down “somewhat warm”
- Fuzzy logic and probability: why “somewhat warm” does not mean “probably warm”
- Fuzzy logic: t-norms, implications and IF–THEN rules
- Fuzzy control: Mamdani and Sugeno from a fan to a pole on a cart
- Fuzzy clustering: the fuzzy c-means algorithm and points between clusters
- Neuro-fuzzy systems (ANFIS): fuzzy rules that learn from data
- Markov chains: the transition matrix, the stationary distribution and PageRank
- Hidden Markov models: the Viterbi algorithm and the dishonest casino
- Bayesian networks: why the grass is wet and what it means for the sprinkler
- The Kalman filter: how to estimate where a car is and how fast it goes from imprecise measurements
- Types of machine learning: supervised, unsupervised, semi-supervised and more
- Prompts, context and RAG: how to give a model the right information
- Variational autoencoders (VAE): new images from a continuous latent space
- Generative adversarial networks (GAN): the forger versus the detective
- Diffusion models: how pure noise becomes an image
- Markov decision processes: states, actions, rewards and the Bellman equations
- Q-learning and DQN: learning the value of actions, from a table to a neural network
- Policy gradient and PPO: learning the policy directly
- How an AI agent works: a language model in a loop with tools
- Programming with AI agents: the task, tests and review
- Evaluating and securing AI systems: how to measure and how to keep watch
Full series syllabus 52 of 83 topics published
- What the expected value is, why it is the centre of mass of a distribution and why expected values always add up.
- What variance and standard deviation measure, when variances add up and what Chebyshev's inequality guarantees.
- What covariance and correlation are, how they show up in data and why zero covariance does not mean independence.
Useful to know first: Probability density and probability mass functions: why a continuous variable has zero probability at a point
The distribution of a random variable tells you everything — but “everything” is often too much. To compare two distributions quickly, a few numbers are enough: where the distribution lies, how wide it is and whether two variables grow together or pull against each other. These numbers are called the expected value, the variance and the covariance, and you will meet them in every machine learning model — from the loss function to the Adam optimizer.
This part builds on Random variable and Probability density function.
1. Expected value
Definition — expected value of a discrete variable
Let X be a discrete random variable, H = {x1, x2, …} ⊆ ℝ an at most countable set of pairwise distinct values with P(X ∈ H) = 1 and p : ℝ → [0, 1] the probability mass function of X (see the parts Random variable and Probability density function). We say that X has a finite expected value if and only if ∑i |xi| · p(xi) < ∞ (the series converges absolutely). Then the expected value of X is the number
E[X] = ∑i xi · p(xi).
Absolute convergence guarantees that the sum does not depend on the order of the values xi. It is also written μ or μX. The expected value is thus the average of the values weighted by their probabilities.
A weighted average of the possible values, with probabilities as the weights. For a single die, 1 · 1/6 + 2 · 1/6 + … + 6 · 1/6 = 1 + 2 + … + 6/6 = 3.5 (colours as in the formula below). The expected value does not have to be a value that can actually come up — a die never shows 3.5. It is “the average you end up with in the long run”.
E[X] = ∑i xi · p(xi)
- E[X]
- the expected value — a single number meant to stand in for the whole distribution: where it lies “on average”
- ∑i
- over all possible values — none is left out, each one contributes to the average
- xi
- the value — the thing being averaged
- p(xi)
- the weight of the value — more frequent values should pull the average more; the weights add up to 1, so there is nothing to divide by at the end
Physically, the expected value is the centre of mass. Put the probabilities on a lever as weights: a pivot keeps it level only when it sits at the expected value. The picture shows the larger of the two numbers on a pair of dice — the maximum. Large numbers are more common (six is the maximum in 11 of 36 cases, one in just a single case), so the centre of mass lies to the right of the middle: E[max] = 161/36 ≈ 4.47.
Example — the expected value of the maximum of two dice and the balance of the lever
The maximum is at most k exactly when both dice are at most k: that is k² pairs. So it is exactly k in k² − (k − 1)² = 2k − 1 pairs.
p(1), …, p(6) = 1/36, 3/36, 5/36, 7/36, 9/36, 11/36
E[max] = 1 · 1/36 + 2 · 3/36 + 3 · 5/36 + 4 · 7/36 + 5 · 9/36 + 6 · 11/36 = 1 + 6 + 15 + 28 + 45 + 66/36 = 161/36 ≈ 4.47
(colours as in the formula above)
The lever: the moment about a point c is ∑ (k − c) · p(k) = E[max] − c, and it is zero exactly for c = E[max].
Definition — expected value of a continuous variable
Let X be a random variable with density f : ℝ → [0, ∞). We say that X has a finite expected value if and only if ∫−∞∞ |x| · f(x) dx < ∞ (the integral converges absolutely). Then the expected value of X is the number
E[X] = ∫−∞∞ x · f(x) dx.
It is the same weighted average as in the discrete case, except that the sum over values is replaced by an integral and the probability p(xi) by the “probability on a stretch” f(x) dx.
The same thing, except that instead of summing over points you integrate over the area under the density. For the e-mail wait from the previous part (density 0.5 · e−0.5x), E[X] = 2 minutes — exactly the average we built into the example.
Example — the expected waiting time for an e-mail
We compute the integral of the product by integration by parts, ∫ u v′ = [u v] − ∫ u′ v, with u = x and v′ = λ e−λx, i.e. v = −e−λx:
E[X] = ∫0∞ x · λ e−λx dx = [−x e−λx]0∞ + ∫0∞ e−λx dx
= (0 − 0) + [−e−λx/λ]0∞ = 0 + 1/λ = 1/λ
λ = 0.5 → E[X] = 1/0.5 = 2 minutes
The term x e−λx goes to zero as x → ∞, because the exponential falls faster than x grows.
2. Linearity of expectation
Theorem — linearity of expectation
Let (Ω, 𝓕, P) be a probability space, X, Y : Ω → ℝ random variables with finite expected values and a, b ∈ ℝ. Then the variables aX + b and X + Y (defined outcome by outcome: (X + Y)(ω) = X(ω) + Y(ω)) have finite expected values and
E[aX + b] = a · E[X] + b, E[X + Y] = E[X] + E[Y],
and this holds even when X and Y are not independent.
Proof (for a finite sample space Ω with 𝓕 = 2Ω, such as two dice). Write P(ω) = P({ω}). The expected value can be summed over outcomes instead of over values: E[X] = ∑ω ∈ Ω X(ω) P(ω) (the outcomes with the same value X(ω) = x together give x · p(x)). Then
E[X + Y] = ∑ω (X(ω) + Y(ω)) P(ω) = ∑ω X(ω) P(ω) + ∑ω Y(ω) P(ω) = E[X] + E[Y].
Nowhere did we need to know how X and Y are related — both are evaluated at the same outcome ω. The equality for aX + b follows in the same way from ∑ P(ω) = 1. The general case is proved by the same argument with integrals. ∎
The average of a sum is the sum of the averages. The sum of two dice has expected value 3.5 + 3.5 = 7 — no table needed. And if you roll just once and double the result, you also get 2 · 3.5 = 7. Linearity is the most useful property in all of probability: break a complicated variable into simple pieces and add up their averages.
Example — how many sixes come up on two dice
For an event A ∈ 𝓕, the indicator of A is the random variable 𝟙A : Ω → {0, 1}, 𝟙A(ω) = 1 for ω ∈ A and 𝟙A(ω) = 0 for ω ∉ A. We split the number of sixes N into two indicators: I1 = 𝟙A1 for A1 = “a six on the first die” and I2 = 𝟙A2 for A2 = “a six on the second die”. Then N = I1 + I2.
E[I1] = 1 · 1/6 + 0 · 5/6 = 1/6, E[I2] = 1/6 (colours as in the formula for the expected value above)
E[N] = E[I1] + E[I2] = 1/6 + 1/6 = 1/3
Checking by enumeration: two sixes in 1 of 36 cases, one in 10 of 36 → 2 · 1 + 1 · 10/36 = 12/36 = 1/3
The expected value of an indicator is the probability of its event — thanks to that, linearity lets you compute the average count of anything.
3. Variance and standard deviation
Definition — variance
Let X be a random variable with finite expected value μ = E[X]. If the random variable (X − μ)² has a finite expected value (which happens if and only if E[X²] < ∞), the variance of X is the number
Var(X) = E[(X − μ)²]
and we say that X has a finite variance. For a discrete X, E[(X − μ)²] = ∑i (xi − μ)² · p(xi); for X with density f it is ∫−∞∞ (x − μ)² · f(x) dx. The variance is thus the expected value of the squared deviation from the expected value; always Var(X) ≥ 0.
The average squared distance from the average. When values land close to the expected value, the variance is small; when they fly far away, it is large. Squaring makes sure that deviations up and down do not cancel out, and it punishes large deviations more than small ones.
Var(X) = E[(X − μ)²]
- Var(X)
- the variance — a single number for the width of the distribution
- X − μ
- the deviation from the centre — we measure the width from the expected value, so shifting the whole distribution does not change the variance
- ²
- the square — without it, deviations up and down would cancel out and the result would always be zero (E[X − μ] = μ − μ = 0); moreover, it counts one large deviation more than two half-size ones
- E
- the average over the whole distribution — frequent deviations weigh more than rare ones, just as with the expected value
Theorem — computational formula for variance
Let X be a random variable with finite variance. Then
Var(X) = E[X²] − (E[X])².
Proof. Write μ = E[X]. We expand and use linearity, μ being a constant: E[(X − μ)²] = E[X² − 2μX + μ²] = E[X²] − 2μ · E[X] + μ² = E[X²] − 2μ² + μ² = E[X²] − μ². ∎
“The mean of the squares minus the square of the mean.” For a single die, E[X²] = 1 + 4 + 9 + 16 + 25 + 36/6 = 91/6, so Var(X) = 91/6 − 3.5² = 35/12 ≈ 2.92. It follows from linearity: just expand (X − μ)².
Definition — standard deviation
Let X be a random variable with finite variance Var(X) ≥ 0. The standard deviation of X is the non-negative number
σ = σX = √Var(X).
Thus σ ≥ 0 and σ² = Var(X); σ has the same units as X.
Variance comes in odd units — for a wait measured in minutes, it is in minutes squared. The square root brings it back: σ is “the typical distance from the average” in the same units as the data. For a single die σ ≈ 1.71; for the e-mail wait the variance is 4 and σ = 2 minutes.
From data instead of from a distribution
Often we do not know the distribution, all we have are the measured numbers x1, …, xn. Then each of them gets the weight 1/n — as if it were a distribution in which every measurement is equally likely:
def prumer_a_rozptyl(data):
n = len(data)
mu = sum(data) / n # E[X] with weights 1/n
var = sum((x - mu) ** 2 for x in data) / n # E[(X − μ)²] with weights 1/n
return mu, var, var ** 0.5 # μ, variance, σ
Example — the mean and variance of eight numbers
data 2, 4, 4, 4, 5, 5, 7, 9; μ = 40/8 = 5
deviations xi − μ: −3, −1, −1, −1, 0, 0, 2, 4; squares 9, 1, 1, 1, 0, 0, 4, 16
Var = 9 + 1 + 1 + 1 + 0 + 0 + 4 + 16/8 = 32/8 = 4, σ = √4 = 2
(colours as in the breakdown of the variance above: the average E with weights 1/8 is division by eight)
When the data are only a sample from a larger whole and we estimate the variance of the whole, one often divides by n − 1 instead of n (the so-called sample variance, here 32/7 ≈ 4.57): we measure the deviations from the sample mean, which fits the data better than the true μ, and dividing by n would underestimate the variance on average.
4. Variance of a sum: why rolling twice pays off
Theorem — variance of a linear function and of a sum
Let X, Y be random variables on the same probability space with finite variance and a, b ∈ ℝ. Then
- Var(aX + b) = a² · Var(X);
- if X and Y are independent (defined in the section Covariance and correlation), then Var(X + Y) = Var(X) + Var(Y).
Proof of the first part. By linearity E[aX + b] = aμ + b, so the deviation from the centre is (aX + b) − (aμ + b) = a(X − μ) — the b cancels. Then Var(aX + b) = E[a²(X − μ)²] = a² Var(X). We prove the second part in the section on covariance. ∎
Shifting by b does not change the width; stretching a times makes it a² times larger. And the variances of independent variables add up — unlike the standard deviations.
Let us compare two games with the same expected value of 7. In the first, you roll two dice and add them up: Var = 35/12 + 35/12 = 35/6 ≈ 5.83, σ ≈ 2.42. In the second, you roll once and double the result: Var = 2² · 35/12 = 35/3 ≈ 11.67, σ ≈ 3.42. The average is the same, but the second game is much wilder — with two dice, bad luck on one is often offset by good luck on the other.
The same principle is behind diversifying investments and behind the fact that an average of several measurements fluctuates less than a single measurement. Exactly how much less is the subject of the next part, on the law of large numbers.
5. Chebyshev's inequality
Theorem — Chebyshev's inequality
Let X be a random variable with finite variance, μ = E[X] and σ = √Var(X) > 0. Then for every k ∈ ℝ, k > 0,
P(|X − μ| ≥ kσ) ≤ 1/k²,
where {|X − μ| ≥ kσ} = {ω ∈ Ω : |X(ω) − μ| ≥ kσ}.
There cannot be much probability far from the average — and this holds for every distribution. At most a quarter of the values land more than two standard deviations from the average, at most a ninth more than three. For the sum of two dice, 2σ ≈ 4.83, and only the sums 2 and 12 are that far from seven: in fact 2/36 ≈ 0.056, well below the guaranteed bound of 1/2² = 0.25 (colours as in the formula below).
P(|X − μ| ≥ kσ) ≤ 1/k²
- P
- the probability of a “runaway” value — this is what we want to bound from above without knowing the shape of the distribution
- |X − μ|
- the distance from the expected value — an absolute value, because we care about runaways in both directions
- kσ
- a threshold measured in standard deviations — that way one inequality holds for all distributions and all units; “far” means far relative to the typical distance
- 1/k²
- the bound — the square comes from the variance (the average square of the deviation): every value at least kσ from the centre contributes at least k²σ² times its probability to the variance; if together they had more than 1/k², the variance would come out larger than σ²
Theorem — Markov's inequality and the proof of Chebyshev's
Markov's inequality: Let Y be a random variable with finite expected value that is non-negative (Y(ω) ≥ 0 for every ω ∈ Ω), and a ∈ ℝ, a > 0. Then
P(Y ≥ a) ≤ E[Y]/a.
Proof. Always Y ≥ a · I, where I = 𝟙{Y ≥ a} is the indicator of the event {Y ≥ a} (1 when Y ≥ a, otherwise 0) (for Y ≥ a this reads Y ≥ a, otherwise Y ≥ 0). The expected value preserves the inequality: E[Y] ≥ a · E[I] = a · P(Y ≥ a).
Chebyshev. Apply Markov to Y = (X − μ)² ≥ 0 and a = k²σ² > 0:
P(|X − μ| ≥ kσ) = P((X − μ)² ≥ k²σ²) ≤ E[(X − μ)²]/k²σ² = σ²/k²σ² = 1/k². ∎
How loose the bound is can be seen on the sum of two dice (σ = √35/6 ≈ 2.42) for several values of k (colours as in the formula above):
| k | threshold kσ | sums that far from 7 | true P | bound 1/k² |
|---|---|---|---|---|
| 1 | 2.42 | 2, 3, 4, 10, 11, 12 | 12/36 ≈ 0.333 | 1 |
| 1.5 | 3.62 | 2, 3, 11, 12 | 6/36 ≈ 0.167 | 0.444 |
| 2 | 4.83 | 2, 12 | 2/36 ≈ 0.056 | 0.25 |
| 3 | 7.25 | none | 0 | 0.111 |
For the continuous e-mail wait (μ = σ = 2 minutes), the threshold for k = 2 is 4 minutes; downwards the wait cannot get that far, upwards it means waiting at least 6 minutes: P(X ≥ 6) = e−3 ≈ 0.050, again well below 0.25. The bound is crude because it has to hold even for the worst possible distribution.
6. Covariance and correlation
Definition — covariance
Let X, Y : Ω → ℝ be random variables on the same probability space (Ω, 𝓕, P) with finite variance and μX = E[X], μY = E[Y]. The covariance of X and Y is the number
Cov(X, Y) = E[(X − μX)(Y − μY)].
This expected value is finite, because |(X − μX)(Y − μY)| ≤ (X − μX)² + (Y − μY)²/2. The covariance is thus the expected value of the product of the deviations of both variables from their expected values.
When X is above its average, is Y usually above its average too? Then the product of the deviations is mostly positive and so is the covariance. When they pull against each other, it is negative. In the picture, each product is a rectangle from the point where the averages cross to the data point: green ones are positive, orange ones negative. The covariance is their average signed area.
Definition — correlation coefficient
Let X, Y be random variables on the same probability space with finite variance and standard deviations σX > 0, σY > 0. The correlation coefficient (correlation) of X and Y is the number
ρ(X, Y) = Cov(X, Y)/σX σY.
Always −1 ≤ ρ(X, Y) ≤ 1 (this follows from the Cauchy–Schwarz inequality |Cov(X, Y)| ≤ σX σY).
Covariance depends on the units — in centimetres it comes out 10,000× larger than in metres. Correlation divides it by both standard deviations and gets a unitless number: 1 means the points lie on a rising line, −1 on a falling one, and 0 means no linear relationship.
The first die and the sum grow together: Cov = 35/12 ≈ 2.92 and ρ ≈ 0.71. The first and second die are independent, and the rectangles cancel out exactly: Cov = 0. And the higher the first die, the smaller the difference “second minus first”: Cov ≈ −2.92, ρ ≈ −0.71.
Theorem — computational formula for covariance
Let X, Y be random variables on the same probability space with finite variance. Then
Cov(X, Y) = E[XY] − E[X] · E[Y].
Proof. Just as with the variance, we expand: E[XY − μYX − μXY + μXμY] = E[XY] − μYμX − μXμY + μXμY. For Y = X this is the computational formula for variance: Cov(X, X) = Var(X). ∎
Example — the first die and the sum
X = first die, Y = second, S = X + Y; E[XS] = E[X²] + E[XY] = 91/6 + 3.5 · 3.5 = 91/6 + 49/4 = 329/12
Cov(X, S) = 329/12 − 3.5 · 7 = 329/12 − 294/12 = 35/12 ≈ 2.92
ρ = 35/12/√35/12 · √35/6 = √35/12/√35/6 = √1/2 ≈ 0.71
We were allowed to use E[XY] = E[X] · E[Y] because the dice are independent (theorem below).
Definition — independent random variables
Let X, Y : Ω → ℝ be random variables on the same probability space (Ω, 𝓕, P) with distribution functions FX, FY. X and Y are independent if and only if
∀x, y ∈ ℝ : P(X ≤ x, Y ≤ y) = FX(x) · FY(y),
where {X ≤ x, Y ≤ y} = {X ≤ x} ∩ {Y ≤ y}; the events {X ≤ x} and {Y ≤ y} are thus independent for every pair x, y (for independent events see the part Conditional probability and Bayes’ theorem). For discrete X and Y this is equivalent to the condition
∀x, y ∈ ℝ : P(X = x, Y = y) = P(X = x) · P(Y = y).
Learning the value of one variable changes nothing about the chances of the other. For two dice: P(first 6, second 6) = 1/36 = 1/6 · 1/6. For the sum and the difference (first − second) this does not hold: a sum of 12 together with a difference of 1 cannot come up, the probability is 0, but P(sum 12) · P(difference 1) = 1/36 · 5/36 ≠ 0.
Theorem — variance of a sum in general
Let X, Y be random variables on the same probability space with finite variance. Then
Var(X + Y) = Var(X) + Var(Y) + 2 · Cov(X, Y).
If moreover X and Y are independent, then E[XY] = E[X] · E[Y], hence Cov(X, Y) = 0 and Var(X + Y) = Var(X) + Var(Y).
Proof. The deviation of the sum from its centre is (X − μX) + (Y − μY); its square is (X − μX)² + (Y − μY)² + 2(X − μX)(Y − μY), and taking the expected value by linearity gives the first equality. For independent discrete variables E[XY] = ∑x,y xy · P(X = x) P(Y = y) = (∑x x P(X = x)) · (∑y y P(Y = y)) = E[X] · E[Y], so Cov = 0; for variables with a density one proceeds in the same way with integrals. ∎
Var(X + Y) = Var(X) + Var(Y) + 2 · Cov(X, Y)
- Var(X + Y)
- how much the sum fluctuates — say a whole portfolio, or the sum of two measurement errors
- Var(X) + Var(Y)
- the fluctuation of each part on its own — if they were unrelated, this would be the whole result
- Cov(X, Y)
- the interplay — when both variables go up together, the deviations add up and the sum fluctuates more (positive); when they pull against each other, the deviations cancel (negative)
- 2
- the cross term 2ab from (a + b)² — the pairs (X, Y) and (Y, X) contribute equally
Variables that grow together increase the variance of the sum; variables that pull against each other reduce it — which is exactly what hedging does in investing. For independent variables the covariance term vanishes, leaving the rule from the previous section.
Careful, the converse does not hold: zero covariance does not mean independence. The difference of the dice D = first − second and their sum S have covariance Var(first) − Var(second) = 0. Yet they are dependent: if the sum is 12, the difference must be 0. Covariance measures only a linear relationship.
Theorem — covariance is bilinear
Let X, Y, Z be random variables on the same probability space with finite variance and a, b ∈ ℝ. Then
- Cov(X, Y) = Cov(Y, X) (symmetry);
- Cov(X, X) = Var(X);
- Cov(aX + bY, Z) = a · Cov(X, Z) + b · Cov(Y, Z) (linearity in the first argument; with symmetry also in the second).
It follows from the linearity of expectation, just as for the variance.
Example — why the difference and the sum have zero covariance
Cov(X − Y, X + Y) = Cov(X, X) + Cov(X, Y) − Cov(Y, X) − Cov(Y, Y)
= Var(X) − Var(Y) = 35/12 − 35/12 = 0
The cross terms cancel thanks to symmetry, leaving the difference of the variances — zero, because both dice are the same. This changes nothing about the dependence.
7. Expected value and variance in artificial intelligence
- Loss function. The mean squared error (MSE) is the expected value of the squared error, E[(Y − ŷ)²]. The best constant prediction is exactly the expected value of Y, and the error that remains is the variance.
- Input standardization. Before data go into a network, they are converted to z = x − μ/σ — expected value 0, standard deviation 1. Training then runs faster and more stably; batch normalization does the same inside the network.
- Covariance matrix and PCA. The covariances of all pairs of features form a matrix, and its eigenvectors (see the part on linear algebra) point in the directions in which the data vary the most.
- The Adam optimizer (from the part on gradient descent) keeps running estimates of the expected value of the gradient and of its square — and divides the step by the square root of the latter. Where the gradient fluctuates a lot, it takes more cautious steps.
- Bias and variance of a model. A model's error can be split into a systematic error (bias) and fluctuation between training sets (variance). Simple models have high bias, complex ones high variance.
Why the best constant prediction is the expected value
Theorem — decomposition of the mean squared error
Let Y be a random variable with finite variance and c ∈ ℝ any number. Then
E[(Y − c)²] = Var(Y) + (E[Y] − c)².
The function c ↦ E[(Y − c)²] therefore has a unique minimum at the point c = E[Y], and its value there is Var(Y).
Proof. Var(Y − c) = Var(Y) (a shift does not change the width), and the computational formula for the variance of Y − c gives E[(Y − c)²] = Var(Y − c) + (E[Y − c])² = Var(Y) + (E[Y] − c)². ∎
E[(Y − c)²] = Var(Y) + (E[Y] − c)²
- E[(Y − c)²]
- the mean squared error (MSE) of the prediction c — the loss the model minimises
- Var(Y)
- the irreducible error — c does not appear in it at all, so no constant prediction can reduce it; it is the noise of the data themselves
- (E[Y] − c)²
- the bias of the prediction, squared — the only thing the choice of c affects; it is zero exactly for c = E[Y]
Example — predicting a die roll and standardization
Prediction c = 3: E[(Y − 3)²] = 4 + 1 + 0 + 1 + 4 + 9/6 = 19/6 ≈ 3.17
By the decomposition: 35/12 + (3.5 − 3)² = 35/12 + 1/4 = 38/12 = 19/6 ✓; for c = 3.5 only 35/12 + 0 ≈ 2.92 (colours as in the formula above)
Standardizing a six: z = 6 − 3.5/1.71 ≈ 1.46 — a six lies 1.46 standard deviations above the average
8. Summary
- The expected value is a weighted average — the centre of mass of the distribution. It is linear, even for dependent variables.
- Variance is the average squared deviation from the average; the standard deviation is its square root, in the same units as the data.
- The variances of independent variables add up; that is why the sum of two dice fluctuates less than one die × 2.
- Chebyshev's inequality: at most 1/k² of the probability lies farther than kσ from the average.
- Covariance and correlation measure the linear relationship between two variables; zero covariance does not yet mean independence.
Do you see it differently? Write to support@coden-app.com.