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
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.
Random signs partly cancel
Variance of a sum, then of a dot product
Coin-flip vectors, d = 4 and d = 10,000
Check it by simulation
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
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.
Bytewise
All courses