All courses

Variance of sums and dot products of random vectors

Show that the variance of a sum of n independent terms grows like n. Conclude that the dot product of two random d-dimensional vectors with unit-variance entries has standard deviation √d.

Builds on: Weights, parameters and layers

▶ Watch this lesson Start the whole course

What the animation says

How big is the sum?

Every layer of a network computes dot products: multiply d pairs of numbers, then add them all up. And d is often in the thousands. Suppose each product is a random number of about size one, as likely negative as positive. Does the total stay near one, grow like the square root of d, or grow like d? Let's start smaller. Flip a coin a hundred times: heads, step right; tails, step left. Where do you typically end up?

Variances add

About ten. Here are many hundred-step walks, side by side. Few end a hundred away, and few end at zero. Most land within about ten of the start. Why ten? Each step has variance one. For independent steps there are no cross terms, so variances simply add. A hundred steps: variance one hundred. But variance is measured in squared units. The typical distance is its square root: ten. Four times the steps gives only twice the spread.

A dot product, by hand

Now a dot product. Take two coin-flip vectors with four entries each. Multiply pair by pair: one, minus one, minus one, one. This time they sum to zero. Each product is plus or minus one: mean zero, variance one. Each product uses its own pair of entries, so they're independent. Variances add to four; the spread is two. The same holds for any entries with mean zero and variance one: the spread is the square root of d. Now stretch to ten thousand entries. Tap the typical size of the dot product.

Check by simulation

A hundred, far below the maximum of ten thousand. Let's check by simulation. These two lines draw two thousand random vector pairs, with Gaussian entries, mean zero, variance one. This line multiplies them entry by entry and sums each row: two thousand dot products. Then we print their measured spread next to the square root of d. Sixteen gives about four. Two fifty-six, about sixteen. Four thousand ninety-six, about sixty-four. The next layer wants values near one again. At width four thousand ninety-six, what should we divide by?

Divide by √d

Sixty-four: the square root of d. Divide by it, and the spread is back to one. Divide by d instead, and the values shrink toward zero as width grows. Divide by the square root of d, and the size stays near one at any width: sixteen, four thousand, ten thousand. The rule underneath: for independent terms, variances add. So spread grows like the square root of n, not like n.

How big is a sum of d random terms?

A layer computes $\sum_i u_i w_i$ again and again: it multiplies $d$ numbers pairwise and adds the results, with $d$ often in the thousands. Suppose each product is a random number of size about 1, as likely to be negative as positive. How big is the total: about 1, about $\sqrt d$, or about $d$? The answer tells you whether numbers stay a sensible size through a network or blow up.

Random signs partly cancel

Picture a walk of $n$ steps, each $+1$ or $-1$ by coin flip. You rarely end up $n$ away, because the steps partly cancel. They also don't cancel fully. The typical distance from the start is about $\sqrt n$. The reason: for independent terms, **variances add**, and variance is measured in squared units. So the spread, its square root, grows only like $\sqrt n$.

Variance of a sum, then of a dot product

$\mathrm{Var}(X+Y)=\mathrm{Var}X+\mathrm{Var}Y+2\,\mathrm{Cov}(X,Y)$. For $n$ terms, the variance of the sum is the sum of the variances plus $2\,\mathrm{Cov}$ for every pair. If the terms are independent, every $\mathrm{Cov}=0$. So for $n$ independent terms that each have variance $s^2$: $\mathrm{Var}\big(\sum_{i=1}^n X_i\big)=ns^2$ and $\mathrm{SD}=s\sqrt n$. Dot product: $u\cdot v=\sum_{i=1}^d u_iv_i$, with all entries independent, mean 0 and variance 1. Each product has mean $E[u_i]E[v_i]=0$, so $\mathrm{Var}(u_iv_i)=E[u_i^2v_i^2]=E[u_i^2]E[v_i^2]=1$. The $d$ products are independent, since each uses its own pair of entries. So $\mathrm{Var}(u\cdot v)=d$ and $\mathrm{SD}=\sqrt d$.

Coin-flip vectors, d = 4 and d = 10,000

Take $u=(1,-1,1,1)$ and $v=(1,1,-1,1)$, each entry a random $\pm1$. The products are $1,-1,-1,1$, which sum to $0$. Each product is $\pm1$ with mean 0 and variance 1, so the sum has variance $4$ and SD $2$. At $d=10{,}000$ the SD is $100$, far below the largest possible value of $10{,}000$. Dividing by $\sqrt d=100$ brings the SD back to 1.

Check it by simulation

This draws 2,000 random vector pairs for each $d$, with Gaussian entries of mean 0 and variance 1. The measured SD of their dot products should match $\sqrt d$: about 4, 16 and 64.
import numpy as np
rng = np.random.default_rng(0)
for d in [16, 256, 4096]:
    u = rng.standard_normal((2000, d))
    v = rng.standard_normal((2000, d))
    dots = (u * v).sum(axis=1)
    print(d, dots.std().round(1), np.sqrt(d))

Variances add; spreads grow like √n

For independent terms, variances add. So a sum of $n$ terms, each with SD $s$, has SD $s\sqrt n$, not $sn$. The dot product of two random $d$-dimensional vectors with independent, mean-0, unit-variance entries has SD $\sqrt d$. Dividing it by $\sqrt d$ keeps its size near 1 at any width.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.