Skip to content

13.1 Random variables, expectation, variance, and normal draws by Box-Muller

By the end of the previous chapter you had a reproducible generator, PCG32, that produces uniform numbers in [0,1)[0, 1). Neural networks are not initialized with uniform numbers. Almost every weight in AI Engineering, from the first Linear layer in Module system and basic layers to the Llama-family model, starts as a draw from a normal distribution with a carefully chosen spread. The first place this book needs one is orthogonal initialization, which is the QR factorization of a Gaussian matrix.

Concepts from other sections (7)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

  • Unit circle: the circle of radius 1 about the origin; the point at angle θ\theta is (cos⁡θ,sin⁡θ)(\cos\theta, \sin\theta). Read more: 6.2 Trigonometry, rotations, complex numbers, and Euler’s formula
  • Standard deviation: Var⁡(X)\sqrt{\operatorname{Var}(X)}, the typical distance of a random variable from its mean, in the same units as the variable itself. Read more: Chapter 13: Probability and Statistics
  • Normal distribution / Gaussian distribution: the bell-shaped density N(μ,σ2)N(\mu, \sigma^2) with mean μ\mu and variance σ2\sigma^2; about 68%, 95%, and 99.7% of its mass lies within one, two, and three standard deviations of the mean, and the central limit theorem makes it the limit of sample means. Read more: Chapter 13: Probability and Statistics
  • Cumulative distribution function (CDF): F(x)=P(X≤x)F(x) = P(X \le x), defined for every random variable, discrete or continuous; it rises from 0 to 1, and for a continuous variable its derivative is the density. Read more: Chapter 13: Probability and Statistics
  • Polar coordinates: describing a point by its distance rr from the origin and angle θ\theta, with x=rcos⁡θx = r\cos\theta and y=rsin⁡θy = r\sin\theta; a polar curve r(θ)r(\theta) sweeps area 12∫αβr(θ)2 dθ\frac12\int_\alpha^\beta r(\theta)^2\,d\theta. Read more: Chapter 8: Calculus 2
  • Box-Muller: the transform r=−2ln⁡(1−u1)r = \sqrt{-2\ln(1-u_1)}, θ=2πu2\theta = 2\pi u_2 that turns two uniforms into two independent standard normals. Read more: 13.9 Probability problem set, part a: axioms, Bayes, random variables, Box-Muller
  • Standard error: s/ns/\sqrt{n}, the estimated standard deviation of the sample mean. Read more: 13.5 LLN, CLT, confidence intervals, bootstrap

Choosing that spread is a variance calculation, the subject of Variance propagation and initialization: too large and activations explode through 20 layers, too small and they vanish. This section lays the groundwork. It defines random variables, expectation, and variance from the beginning, shows how to compute a variance without destroying its digits, and turns uniforms into normals with the Box-Muller transform, in exactly the order the Rust and Go ports use, so that one seed means the same weights everywhere.

You will need the generator from Modular arithmetic, hashing, and PCG32 (the solution’s rng.py), cosine, sine, and the unit circle from Trig, rotations, and Euler’s formula, and, for background, the Gaussian integral from Calculus 2, which its problem set works through. The reference implementation is course/solutions/python/tinyllm/prob/rv.py, its contract is rv.pyi, and the cross-language draw order is fixed by spec/pcg32.md.

Learning objectives

  • Compute the expectation and variance of a discrete distribution, and validate that a table really is a distribution.
  • Derive Var⁡[aX+c]=a2Var⁡[X]\operatorname{Var}[aX + c] = a^2 \operatorname{Var}[X] and explain why independent variances add.
  • Explain why E[X2]−E[X]2E[X^2] - E[X]^2 fails numerically and compute the variance in two passes instead.
  • Derive the Box-Muller transform from the polar form of two independent normals and implement it in the spec’s draw order.

The section uses the following symbols throughout.

SymbolMeaningType
XXa random variable: a quantity whose value is drawn at random
xi,pix_i, p_ithe values of a discrete XX and their probabilities, pi≥0p_i \ge 0, ∑ipi=1\sum_i p_i = 1float64[k] each
E[X]=μE[X] = \muexpectation (mean)scalar
Var⁡[X]=σ2\operatorname{Var}[X] = \sigma^2variance; σ\sigma is the standard deviationscalar
UUa uniform random variable on [0,1)[0, 1)
f(z)f(z)a probability density: Pr⁡[a≤Z≤b]=∫abf(z) dz\Pr[a \le Z \le b] = \int_a^b f(z)\,dz
N(μ,σ2)\mathcal{N}(\mu, \sigma^2)the normal distribution; N(0,1)\mathcal{N}(0, 1) is the standard normal
Φ(z)\Phi(z)the standard normal CDF, Pr⁡[Z≤z]=12(1+erf⁡(z/2))\Pr[Z \le z] = \tfrac12\big(1 + \operatorname{erf}(z/\sqrt2)\big)
u1,u2u_1, u_2two uniform drawsfloat
r,θr, \thetapolar radius and anglefloat

A random variable assigns a number to each outcome of a random experiment. A discrete one takes values x1,…,xkx_1, \dots, x_k with probabilities p1,…,pkp_1, \dots, p_k, where every pi≥0p_i \ge 0 and ∑ipi=1\sum_i p_i = 1; that table is its distribution. A table whose probabilities do not sum to 1 (say, raw counts) or contain a negative entry describes no random variable, and the functions in this section reject it rather than average with it.

A continuous random variable has a density f≥0f \ge 0 with ∫f=1\int f = 1; probabilities are areas under ff, and any single value has probability 0. UU, uniform on [0,1)[0, 1), has f=1f = 1 on that interval.

Once you have a distribution, the first two things to ask of it are where it sits and how widely it spreads. The expectation answers the first. It is the probability-weighted average of the values,

E[X]=∑ipi xi(continuous: ∫zf(z) dz),E[X] = \sum_i p_i\, x_i \qquad \Big(\text{continuous: } \int z f(z)\,dz\Big),

the long-run average of many independent draws. It is linear: E[aX+c]=aE[X]+cE[aX + c] = aE[X] + c, and E[X+Y]=E[X]+E[Y]E[X + Y] = E[X] + E[Y] for any X,YX, Y.

The variance answers the second, as the expected squared distance from the mean:

Var⁡[X]=E[(X−μ)2]=∑ipi (xi−μ)2,μ=E[X].\operatorname{Var}[X] = E\big[(X - \mu)^2\big] = \sum_i p_i\,(x_i - \mu)^2, \qquad \mu = E[X].

From linearity, Var⁡[aX+c]=E[(aX+c−aμ−c)2]=a2Var⁡[X]\operatorname{Var}[aX + c] = E[(aX + c - a\mu - c)^2] = a^2 \operatorname{Var}[X]. A shift moves the distribution without spreading it; a scale by aa spreads it by ∣a∣\lvert a \rvert, so the variance grows by a2a^2. For independent XX and YY, variances add: Var⁡[X+Y]=Var⁡[X]+Var⁡[Y]\operatorname{Var}[X + Y] = \operatorname{Var}[X] + \operatorname{Var}[Y]. A neuron ∑j=1nwjxj\sum_{j=1}^{n} w_j x_j with independent zero-mean terms therefore has variance nVar⁡[w]Var⁡[x]n \operatorname{Var}[w]\operatorname{Var}[x], which is why Variance propagation and initialization scales initial weights by 1/n1/\sqrt{n}. Every initialization in the book is sized with this rule.

Two examples recur everywhere. A Bernoulli(pp) variable (1 with probability pp, else 0) has E=pE = p and Var⁡=p(1−p)\operatorname{Var} = p(1 - p): every accuracy you will ever measure is an average of these. A point mass (one value with probability 1) has variance exactly 0.

Computing the variance without cancellation

Section titled “Computing the variance without cancellation”

The definition is clear; the arithmetic is where implementations go wrong. Expanding the square gives the textbook shortcut Var⁡[X]=E[X2]−E[X]2\operatorname{Var}[X] = E[X^2] - E[X]^2, which is algebraically equal and numerically dangerous. Take X=109+BX = 10^9 + B with BB a fair coin: the true variance is 14\tfrac14. But E[X2]≈1018E[X^2] \approx 10^{18} and E[X]2≈1018E[X]^2 \approx 10^{18}, and float64 numbers near 101810^{18} are spaced 128 apart, so their difference is a multiple of 128 near zero: the answer comes out 0 or −64-64, every digit wrong, sometimes negative.

The two-pass method computes μ\mu first, then ∑ipi(xi−μ)2\sum_i p_i (x_i - \mu)^2. The deviations xi−μx_i - \mu are small numbers computed with small errors, and the squares are all non-negative. Stable numerics returns to this problem with Welford’s and Kahan’s algorithms.

With expectation and variance in hand, you can describe the distribution the rest of the section is about. The standard normal N(0,1)\mathcal{N}(0, 1) has density f(z)=12πe−z2/2f(z) = \frac{1}{\sqrt{2\pi}} e^{-z^2/2}: mean 0, variance 1, symmetric, with about 68% of its mass within one standard deviation and 99.7% within three. μ+σZ\mu + \sigma Z is N(μ,σ2)\mathcal{N}(\mu, \sigma^2). The constant 12π\frac{1}{\sqrt{2\pi}} comes from the Gaussian integral ∫−∞∞e−z2/2 dz=2π\int_{-\infty}^{\infty} e^{-z^2/2}\,dz = \sqrt{2\pi}, computed by squaring it and switching to polar coordinates, and the same trick in reverse is how normals are generated.

Two independent standard normals (Z0,Z1)(Z_0, Z_1) form a point in the plane whose density 12πe−(z02+z12)/2\frac{1}{2\pi} e^{-(z_0^2 + z_1^2)/2} depends only on the distance from the origin. In polar coordinates (r,θ)(r, \theta), two facts follow:

  • the angle θ\theta is uniform on [0,2π)[0, 2\pi), by the rotational symmetry;
  • the radius satisfies Pr⁡[R>r]=e−r2/2\Pr[R > r] = e^{-r^2/2} (integrate the density outside the circle of radius rr), so R2/2R^2/2 is exponential with mean 1.

To sample RR, invert its survival function: if VV is uniform on (0,1](0, 1], R=−2ln⁡VR = \sqrt{-2 \ln V} has exactly that distribution. With two uniforms u1,u2∈[0,1)u_1, u_2 \in [0, 1):

r=−2ln⁡(1−u1),θ=2πu2,(z0,z1)=(rcos⁡θ, rsin⁡θ).r = \sqrt{-2 \ln(1 - u_1)}, \qquad \theta = 2\pi u_2, \qquad (z_0, z_1) = (r\cos\theta,\ r\sin\theta).

Using 1−u11 - u_1 instead of u1u_1 matters at the edge. uniform() can return exactly 0.0 (probability 2−532^{-53}, but over 10910^9 draws not never), and ln⁡0=−∞\ln 0 = -\infty would produce an infinite “normal”; 1−u11 - u_1 lies in (0,1](0, 1], so the log is finite and rr is finite. Both outputs are independent standard normals; throwing the sine away wastes half the work.

The order is a contract. spec/pcg32.md fixes it: for each pair, draw u1u_1 then u2u_2, emit z0z_0 (cosine) then z1z_1 (sine). normal(rng, n) fills nn values pair by pair; for odd nn it still draws a whole last pair and drops the final sine. (The spec’s generator-level normal() keeps that sine as a spare for the next call; normal(rng, n) is a free function over any generator, so it keeps no state between calls.) Every other port reproduces this order, which is how normals can be compared across languages.

Before writing code, it helps to have three answers you can check it against.

A fair die. Values 1,…,61, \dots, 6, each with probability 16\tfrac16:

E[X]=1+2+3+4+5+66=216=3.5.E[X] = \frac{1 + 2 + 3 + 4 + 5 + 6}{6} = \frac{21}{6} = 3.5 .

The deviations from 3.5 are ±2.5,±1.5,±0.5\pm 2.5, \pm 1.5, \pm 0.5, so

Var⁡[X]=2 (6.25+2.25+0.25)6=17.56=3512≈2.9167.\operatorname{Var}[X] = \frac{2\,(6.25 + 2.25 + 0.25)}{6} = \frac{17.5}{6} = \frac{35}{12} \approx 2.9167 .

Box-Muller by hand. Choose u1=1−e−2≈0.8647u_1 = 1 - e^{-2} \approx 0.8647 and u2=18u_2 = \tfrac18. Then 1−u1=e−21 - u_1 = e^{-2}, so r=−2ln⁡e−2=4=2r = \sqrt{-2 \ln e^{-2}} = \sqrt{4} = 2; and θ=2π/8=π/4\theta = 2\pi/8 = \pi/4. The pair is (2cos⁡π4, 2sin⁡π4)=(2,2)≈(1.41421,1.41421)(2\cos\frac{\pi}{4},\ 2\sin\frac{\pi}{4}) = (\sqrt2, \sqrt2) \approx (1.41421, 1.41421).

The first normals of seed 0. PCG32(0) gives u1=0.2803122753265841u_1 = 0.2803122753265841 and u2=0.4892241428740438u_2 = 0.4892241428740438 (worked by hand in the PCG32 section). Then ln⁡(1−u1)=ln⁡0.71969=−0.328938\ln(1 - u_1) = \ln 0.71969 = -0.328938, r=0.657876=0.811095r = \sqrt{0.657876} = 0.811095, θ=3.073886\theta = 3.073886 (just under π\pi, so the point lies left of the origin, slightly up), cos⁡θ=−0.997709\cos\theta = -0.997709, sin⁡θ=0.067655\sin\theta = 0.067655, and the first two normals are z0=−0.809237z_0 = -0.809237 and z1=0.054875z_1 = 0.054875: the first two entries of normal["0"] in spec/pcg32.vectors.json.

Four functions cover the section:

def expectation(values, probs) -> float: ... # sum p_i x_i; ValueError unless a distribution
def variance(values, probs) -> float: ... # sum p_i (x_i - mu)^2, two passes
def box_muller(u1: float, u2: float) -> tuple[float, float]: ... # (r cos t, r sin t)
def normal(rng, n: int) -> NDArray: ... # float64 [n]; u1, u2 per pair; cosine first

rng is anything with a uniform() method returning the spec’s 53-bit doubles; in this system that is tinyllm.num.rng.PCG32. Any implementation of the spec gives the same uniforms bit for bit.

The implementation follows from five observations, each of which you should try to predict before reading on.

  1. A distribution is a validated table. Values and probabilities must be 1-D, the same non-zero length, finite, with pi≥0p_i \ge 0 and ∑pi=1\sum p_i = 1. Anything else (raw counts, a negative entry) is not a random variable, so both expectation and variance share one validator that raises instead of averaging. The sum test needs a tolerance (10−910^{-9}) because probabilities computed in floating point need not sum to exactly 1.
  2. The expectation is a dot product. E[X]=∑ipixi=x⋅pE[X] = \sum_i p_i x_i = x \cdot p. Weighting by pp (not dividing by kk) is what makes it right for non-uniform tables.
  3. The variance centres first, then squares. E[X2]−E[X]2E[X^2] - E[X]^2 cancels catastrophically near 101810^{18}. Computing μ\mu once, then d=x−μd = x - \mu and ∑ipidi2\sum_i p_i d_i^2, keeps every term small and non-negative, so 109+{0,1}10^9 + \{0, 1\} gives exactly 14\tfrac14 and no variance can come out negative.
  4. Box-Muller samples the polar form. r=−2ln⁡Vr = \sqrt{-2\ln V} with VV in (0,1](0, 1] and θ=2πu2\theta = 2\pi u_2. Taking V=1−u1V = 1 - u_1 maps the generator’s [0,1)[0, 1) onto (0,1](0, 1], so u1=0u_1 = 0 gives r=0r = 0 instead of ∞\infty.
  5. The draw order is part of the answer. Per pair, u1u_1 then u2u_2; emit cosine then sine. A loop over pairs that writes z0 always and z1 only when there is room implements both the even and odd cases, and an odd nn still consumes two uniforms for the last pair, so the generator ends at the same position as in every other port.

The validator comes first:

def _table(values: ArrayLike, probs: ArrayLike) -> tuple[NDArray, NDArray]:
"""values and probs as float64 vectors, checked to be a distribution."""
x = np.asarray(values, dtype=np.float64)
p = np.asarray(probs, dtype=np.float64)
# One shape check covers 2-D input, mismatched lengths, and the empty table.
if x.ndim != 1 or p.ndim != 1 or x.shape != p.shape or x.size == 0:
raise ValueError(f"values and probs must be 1-D of one non-zero length, got {x.shape} and {p.shape}")
if not np.all(np.isfinite(x)):
raise ValueError("values must be finite")
if np.any(p < 0) or not np.all(np.isfinite(p)):
raise ValueError("probs must be finite and >= 0")
# A tolerance, not ==: probabilities computed in float64 (a softmax, a
# normalized count) are only accurate to rounding.
if abs(float(p.sum()) - 1.0) > 1e-9:
raise ValueError(f"probs must sum to 1, got {float(p.sum())!r}")
return x, p

Raw counts such as [3, 1] sum to 4 and are rejected here, which is the point: a caller who forgot to normalize hears about it at once rather than getting an expectation four times too large.

With a validated table, the two moments are short:

def expectation(values: ArrayLike, probs: ArrayLike) -> float:
x, p = _table(values, probs)
return float(x @ p) # sum_i p_i x_i
def variance(values: ArrayLike, probs: ArrayLike) -> float:
x, p = _table(values, probs)
mu = float(x @ p) # pass 1: the mean
d = x - mu # pass 2: small deviations, computed accurately
return float((d * d) @ p) # sum_i p_i (x_i - mu)^2, every term >= 0

On the die, mu = 3.5, d is ±2.5,±1.5,±0.5\pm 2.5, \pm 1.5, \pm 0.5, and the weighted sum of squares is 17.5/6=35/1217.5/6 = 35/12, exactly the hand calculation. On 109+{0,1}10^9 + \{0, 1\} the deviations are exactly ±12\pm\tfrac12, so the result is exactly 14\tfrac14.

The transform and the sampler that drives it complete the module:

def box_muller(u1: float, u2: float) -> tuple[float, float]:
# 1 - u1 lies in (0, 1], so the log is finite and r is in [0, inf).
r = math.sqrt(-2.0 * math.log(1.0 - u1)) # R^2 / 2 is exponential(1)
theta = 2.0 * math.pi * u2 # a full turn, not half
return r * math.cos(theta), r * math.sin(theta) # cosine first: the spec's order
def normal(rng: Any, n: int) -> NDArray:
if n < 0:
raise ValueError(f"normal: n must be >= 0, got {n}")
out = np.empty(n, dtype=np.float64)
for i in range(0, n, 2): # one Box-Muller pair per iteration
u1 = rng.uniform() # u1 then u2: the cross-language draw order
u2 = rng.uniform()
z0, z1 = box_muller(u1, u2)
out[i] = z0
if i + 1 < n: # odd n: the last sine is dropped, but its
out[i + 1] = z1 # uniform was still consumed
return out

normal keeps no spare between calls: it is a free function over any object with uniform(), so the only state is the generator’s. n = 0 returns an empty array and draws nothing. The full solution is rv.py.

The costs are modest. expectation is one pass over the table and variance two, both O(k)O(k) time and O(k)O(k) extra space for d. normal(rng, n) makes 2⌈n/2⌉2\lceil n/2 \rceil uniform draws and one log, sqrt, cos, sin per pair.

The two-pass variance beats the shortcut outright. E[X2]−E[X]2E[X^2] - E[X]^2 also needs two sums and saves nothing in a setting where the whole table is in memory; it only loses digits. Welford’s one-pass update (see Further reading) is the right choice for streams, which is not this function’s job.

Box-Muller wins over its alternatives for a reason specific to this system. The Ziggurat algorithm is faster but needs tables and a rejection loop whose number of draws per normal varies, which makes cross-language reproduction harder. The polar (Marsaglia) method also rejects. Box-Muller uses exactly two uniforms per pair, so the stream position after normal(rng, n) is a simple function of nn, which is what the Rust and Go ports rely on.

The tradeoff accepted is speed: the Python loop is slow for millions of normals (one Python iteration per pair). Initializers in the book draw at most a few million values, and bit-for-bit agreement with the spec matters more than speed here.

A correct implementation has to pass checks of several kinds, from hand-worked unit cases to statistical tests of the output’s shape. The table lists them along with why each matters later.

CaseKind of checkChecksWhy it matters downstream
the fair dieunitE=7/2E = 7/2, Var⁡=35/12\operatorname{Var} = 35/12everyone agrees on the definitions
Bernoulli and point massunitE=pE = p, Var⁡=p(1−p)\operatorname{Var} = p(1 - p); a point mass has variance 0every accuracy metric is a Bernoulli mean (confidence intervals)
no catastrophic cancellationboundary109+{0,1}10^9 + \{0, 1\} gives exactly 14\tfrac14statistics of large-mean quantities (token counts, timestamps)
shift and scale lawspropertyVar⁡[aX+c]=a2Var⁡[X]\operatorname{Var}[aX + c] = a^2\operatorname{Var}[X] and E[aX+c]=aE[X]+cE[aX + c] = aE[X] + c on 50 random tablesthe scaling rule of initialization
tables that are not distributionsboundarysums other than 1, negative entries, shape mismatches, empty and 2-D tables raiseunnormalized counts are caught upstream
Box-Muller by handunit(2,2)(\sqrt2, \sqrt2), and (0,2)(0, 2) at θ=π/2\theta = \pi/2the transform itself
u1=0u_1 = 0 stays finiteboundaryu1=0u_1 = 0 gives radius 0; u1u_1 just below 1 gives a large finite valueno infinite weights
the spec vectorsgoldenthe first 8 normals of seeds 0, 1, 2632^{63} within 4 ulpthe Rust and Go ports draw the same normals
odd nnunitnormal(rng, 3) uses 4 uniforms and equals the first 3 of normal(rng, 4); n = 0 and n < 0the stream position after a call
the momentsstatistical20 000 draws: mean and variance within 3 standard errors of 0 and 1a missing factor 2 halves every initial variance
the shapestatistical20 bins equally likely under Φ\Phi: Pearson statistic below 43.82 (19 degrees of freedom, p>10−3p > 10^{-3})the right moments are not the right shape
the two halves of a pairstatisticalthe cosine and sine halves have correlation within 3 standard errors of 0independent draws

The mistakes that break these checks are few and recognizable. Averaging the values without the probabilities is right only for uniform tables; the solution weights each value with x @ p. Returning the standard deviation as the variance gives 35/12\sqrt{35/12} instead of 35/1235/12; the solution returns the weighted sum of squares with no square root. Using E[X2]−E[X]2E[X^2] - E[X]^2 gives a variance of 0 or a negative number for large means; the solution centres first with d = x - mu. Skipping validation turns unnormalized counts into silent nonsense; _table checks shape, finiteness, sign, and sum.

The Box-Muller mistakes are subtler, because several of them still produce plausible-looking numbers:

Warning: ln⁡u1\ln u_1 instead of ln⁡(1−u1)\ln(1 - u_1) produces an infinite value once in 2532^{53} draws and different values from every other port. Dropping the 2 in −2ln⁡(⋅)\sqrt{-2\ln(\cdot)} gives variance 12\tfrac12, so every initial weight is too small by 2\sqrt2. An angle of πu2\pi u_2 instead of 2πu22\pi u_2 makes the sine half always positive, the wrong shape. Taking both halves from the cosine gives perfectly correlated pairs.

Emitting the sine first is statistically fine but matches no other language; the solution returns (r * cos, r * sin) and writes z0 first.

The normals built here feed two sections directly. Orthogonality, Householder QR, and orthogonal initialization factors normal(rng, rows * cols) in orthogonal_init, and Variance propagation and initialization builds normal_init, Xavier and Kaiming normals, and the propagation rule nVar⁡[w]Var⁡[x]n\operatorname{Var}[w]\operatorname{Var}[x] on top of them. The next section, Categorical sampling, builds on the same uniforms to draw tokens.

  • Blitzstein and Hwang, Introduction to Probability, chapters 3 to 5 and 7.5 (Box-Muller).
  • Grinstead and Snell, Introduction to Probability, chapter 6.
  • Box and Muller, “A Note on the Generation of Random Normal Deviates” (1958).
  • Welford, “Note on a Method for Calculating Corrected Sums of Squares and Products” (1962); torch.var_mean and Chan et al.’s parallel merging extend it to one pass over a stream, mergeable across workers.
  • The Ziggurat algorithm (numpy’s standard_normal, Rust rand_distr): almost always one uniform and one table lookup per normal, no log, sin, or cos. See random_standard_normal in numpy’s numpy/random/src/distributions/distributions.c.
  • Marsaglia and Bray (1964), the polar method: rejection inside the unit disc replaces sin⁡\sin and cos⁡\cos.
  • torch.nn.init.normal_ (torch/nn/init.py) and jax.random.normal (jax/_src/random.py): per-device counter-based generators, many values per call.
  • Random variable: a number assigned to each outcome of a random experiment; a discrete one is a table of values and probabilities.
  • Expectation: the probability-weighted mean of a random variable’s values.
  • Variance: the expected squared distance from the mean; its square root is the standard deviation.
  • Catastrophic cancellation: the loss of all significant digits when two large, nearly equal floating-point numbers are subtracted.
  • Two-pass variance: computing the mean first, then the weighted sum of squared deviations from it.
  • Standard normal: the distribution N(0,1)\mathcal{N}(0, 1) with density 12πe−z2/2\frac{1}{\sqrt{2\pi}} e^{-z^2/2}.
  • Box-Muller transform: a map from two independent uniforms to two independent standard normals via a radius −2ln⁡(1−u1)\sqrt{-2\ln(1-u_1)} and angle 2πu22\pi u_2.
  • A discrete random variable is a table of values and probabilities; its expectation is the probability-weighted mean and its variance the expected squared distance from that mean.
  • Var⁡[aX+c]=a2Var⁡[X]\operatorname{Var}[aX + c] = a^2 \operatorname{Var}[X]: shifting does nothing, scaling scales by the square. Every initialization is sized with this rule.
  • Compute the variance in two passes (mean first, then squared deviations). E[X2]−E[X]2E[X^2] - E[X]^2 subtracts two huge, nearly equal numbers and can return 0 or a negative variance.
  • Box-Muller turns two independent uniforms into two independent standard normals: a radius −2ln⁡(1−u1)\sqrt{-2\ln(1-u_1)} and an angle 2πu22\pi u_2. Drawn in the spec’s order, the same seed gives the same normals in Python, Rust, and Go.
  1. Why does variance compute d = x - mu before squaring instead of returning (x * x) @ p - mu * mu?

    Answer

    For large means both E[X2]E[X^2] and μ2\mu^2 are near 101810^{18}, where float64 spacing is 128, so their difference is a multiple of 128 near zero: 0 or −64-64 for a true variance of 14\tfrac14. The deviations xi−μx_i - \mu are small and exact (or nearly), and their squares are non-negative, so the two-pass sum is accurate and cannot go negative.

  2. Why math.log(1.0 - u1) and not math.log(u1)? Both are uniform on the unit interval.

    Answer

    Distributionally they are equivalent, but the generator returns values in [0,1)[0, 1): u1=0u_1 = 0 is possible and ln⁡0=−∞\ln 0 = -\infty gives an infinite radius. 1−u11 - u_1 lies in (0,1](0, 1], so the log is finite. It also fixes which value every port computes from a given u1u_1, so the normals agree across languages.

  3. For odd nn, why draw a whole pair and throw the sine away instead of drawing one uniform?

    Answer

    Box-Muller needs both uniforms to make even one normal (the radius uses u1u_1, the angle u2u_2). And the spec defines the stream position: normal(rng, 3) must consume 4 uniforms and equal the first 3 of normal(rng, 4), so that the next draw from the generator is the same in Python, Rust, and Go.

  4. What would happen to the initial weights of Variance propagation and initialization if the radius were −ln⁡(1−u1)\sqrt{-\ln(1 - u_1)}, without the 2?

    Answer

    R2R^2 would be exponential with mean 1 instead of 2, so each coordinate would have variance 12\tfrac12. Every weight scaled by a carefully derived σ\sigma would actually have standard deviation σ/2\sigma/\sqrt2, and activations would shrink layer after layer. The mean is still 0, so only a variance check notices.

  5. Why does _table reject a table whose probabilities sum to 4 instead of normalizing it?

    Answer

    A table that does not sum to 1 is almost always a bug upstream (raw counts passed where probabilities were meant). Silently normalizing would hide it in this function and leave it in every other caller that trusts the same array. Raising makes the error visible where it is cheapest to fix.