skip to content

Random Variables & Moments

Discrete and continuous variables described by PMFs, PDFs and CDFs, then summarised by expectation, variance, covariance and entropy, or transformed into new ones. The vocabulary later topics assume.

on this pageshow

explore

questions

27

What does Shannon entropy measure for a discrete distribution, in bits?

level: juniorimportance: must knowfreq 70%

answer

  1. average surprise, not a single draw
  2. expected yes/no questions per draw
  3. log base 2 gives bits, ln gives nats
  4. flat distribution is the maximum
  5. -sum p log2 p

basics

~20 s

Shannon entropy is a distribution's average uncertainty: H = -sum p log2 p, the expected number of yes/no questions needed to pin down one draw. It is largest for equally likely outcomes and zero when one outcome is certain.

solid answer

~50 s

Shannon entropy of a discrete distribution is `H(X) = -sum_i p_i * log2(p_i)`, measured in bits when the log is base 2. It is the expected surprise per draw, where the surprise of an outcome with probability `p` is `-log2(p)` bits — rare outcomes are more surprising. Operationally it is the expected number of well-chosen yes/no questions, or the shortest achievable average code length, needed to communicate one draw. For a 4-symbol alphabet with probabilities 1/2, 1/4, 1/8, 1/8 the entropy is 1.75 bits, below the 2 bits a uniform alphabet of the same size would need, and the codewords 0, 10, 110, 111 hit that 1.75 average exactly. Entropy depends only on the probabilities, never on the labels or numeric values of the outcomes, and it is maximal for the uniform distribution and zero when some outcome has probability 1.

go deeper

for a junior

Be ready to write -sum p log2 p, say the unit is bits, and state the two extremes: uniform is maximal, a certain outcome is zero.

for a middle

Explain the surprise-and-expectation derivation and the coding reading, including why a skewed 4-symbol alphabet can average 1.75 bits per symbol instead of 2.

for a senior

Show judgment about estimation: entropy computed from counts is biased low on small samples because unseen outcomes contribute nothing, so plug-in entropy on a long tail understates uncertainty.

for a principal

Own the framing choice: argue when an information-theoretic summary is the right currency for a decision versus a task-specific metric, and insist units and normalisation are fixed before numbers get compared across teams.

## The quantity For a discrete random variable `X` taking values `x_1, ..., x_k` with probabilities `p_1, ..., p_k`, the Shannon entropy is ``` H(X) = -sum_i p_i * log(p_i) ``` with the convention that a term with `p_i = 0` contributes 0 (because `p log p -> 0` as `p -> 0`). Entropy is a functional of the *distribution*, not of the outcomes themselves: relabelling the outcomes, or rescaling numeric values, leaves it unchanged. That is the first thing that separates it from variance, which very much depends on the numeric values. ## Units: bits versus nats The base of the logarithm sets the unit. Base 2 gives **bits**, the natural log gives **nats**. They differ only by a constant factor: 1 nat = 1 / ln(2) ≈ 1.4427 bits, and 1 bit = ln(2) ≈ 0.693 nats. Any statement about which of two distributions has more entropy is unit-independent; only the numbers change. Interview answers should name the unit, because "the entropy is 0.69" is ambiguous — that is one bit expressed in nats. ## Surprise and expectation Define the surprise (self-information) of an outcome with probability `p` as `-log2(p)` bits. A certain outcome (`p = 1`) carries 0 bits of surprise; an outcome with `p = 1/8` carries 3 bits; a vanishingly rare outcome carries arbitrarily many. Entropy is then simply the *expected* surprise, `E[-log2 p(X)]`. This is why entropy is an average property of the distribution and not a property of any single observed draw: a single draw has a surprise value, the distribution has an entropy. ## The coding / twenty-questions reading The reason entropy is measured in bits is Shannon's source coding result: the expected number of bits per symbol of any uniquely decodable binary code for i.i.d. draws from the distribution is at least `H(X)`, and codes exist that approach it. Equivalently, playing twenty questions optimally against the distribution, the expected number of yes/no questions is about `H(X)`. Take a four-symbol alphabet A, B, C, D with probabilities 1/2, 1/4, 1/8, 1/8. Then ``` H = 0.5*1 + 0.25*2 + 0.125*3 + 0.125*3 = 1.75 bits ``` A prefix code assigning A -> 0, B -> 10, C -> 110, D -> 111 has expected length 0.5*1 + 0.25*2 + 0.125*3 + 0.125*3 = 1.75 bits per symbol — it meets the bound exactly, because every probability here is a power of 1/2. A fixed-length code would spend 2 bits per symbol. The 0.25-bit gap is exactly the compressibility that the skew in the distribution buys you. ## Bounds and the two extremes For a distribution over `k` outcomes, `0 <= H(X) <= log2(k)` bits. - The **maximum** `log2(k)` is attained only by the uniform distribution: maximal uncertainty, nothing to exploit, every symbol needs its full `log2(k)` bits. - The **minimum** 0 is attained only when some outcome has probability 1: no uncertainty, nothing to transmit. For a Bernoulli variable with success probability `p`, `H(p) = -p log2(p) - (1-p) log2(1-p)`. This curve is concave, symmetric about `p = 0.5`, peaks at exactly 1 bit when `p = 0.5`, and falls to 0 at both `p = 0` and `p = 1`. It is also flat near the top and steep near the ends: `H(0.4) ≈ 0.971` bits, barely below the maximum, while `H(0.9) ≈ 0.469` bits and `H(0.99) ≈ 0.081` bits. A heavily imbalanced binary outcome carries very little information per observation — a fact worth remembering whenever you are told a label is "rare". ## Common confusions worth heading off - **Entropy is not variance.** A variable taking values 0 and 1000 with probability 1/2 each and a variable taking values 0 and 1 with probability 1/2 each have the same entropy (1 bit) and wildly different variances. - **Entropy is not a probability**, so it is not bounded by 1; only the *binary* case caps at 1 bit. - **It is a property of the model, not of the sample.** Estimating entropy from counts on a small sample is biased downward, because unobserved outcomes get probability 0 and contribute nothing. - **Continuous variables need care.** The analogous differential entropy `-integral f(x) log f(x) dx` can be negative and is not invariant under a change of units, so it is not a drop-in replacement for the discrete quantity. In an interview, the strongest short answer states the formula, names the unit, gives the coding or twenty-questions interpretation, and cites the uniform-maximum / certain-minimum extremes.

  • What is the entropy of a Bernoulli variable with p = 0.5, and how does it change as p moves toward 0 or 1?
    At `p = 0.5` it is exactly 1 bit, the maximum for a binary outcome. `H(p) = -p log2 p - (1-p) log2(1-p)` is concave and symmetric about 0.5, falling to 0 at both `p = 0` and `p = 1`. The fall is slow near the middle and fast near the ends: `H(0.4) ≈ 0.97` bits but `H(0.9) ≈ 0.47` bits. Heavily imbalanced binary outcomes carry little information per observation.
  • Can entropy ever exceed log2 of the number of possible outcomes?
    No. For `k` outcomes, `H <= log2(k)`, with equality only for the uniform distribution. Any skew away from uniform strictly reduces entropy, because concentrating probability makes some outcomes more predictable. This upper bound is what lets you normalise entropy to a 0-to-1 scale by dividing by `log2(k)` when comparing alphabets of different sizes.
  • Two variables have the same entropy but very different variances. How is that possible?
    Entropy depends only on the probability vector, not on the outcome values. A coin paying 0 or 1 and a coin paying 0 or 1000, both fair, each have entropy 1 bit but variances of 0.25 and 250000. Entropy measures how hard the outcome is to guess; variance measures how far the numeric values spread. Neither implies the other.

It is the average length of the shortest message you could design to report one draw: predictable sources compress, unpredictable ones do not.

saying these in an interview costs you the question

  • Says entropy is a property of a single observed outcome
  • Confuses entropy with variance or with spread of values
  • Claims entropy is capped at 1 for any distribution
  • Reports a number without saying bits or nats
  • Thinks entropy depends on the numeric labels of outcomes

context

open as a page

How do you compute the expected value of a $1 bet on a single roulette number?

level: juniorimportance: must knowfreq 84%

basics

~20 s

Multiply each outcome by its probability and add the pieces up. On a 38-pocket wheel a $1 straight-up bet nets +$35 with probability 1/38 and -$1 with probability 37/38, giving about -$0.053 per dollar staked.

open as a page

For a continuous latency variable, what is the probability that response time is exactly 200.000 ms?

level: juniorimportance: must knowfreq 68%

basics

~10 s

Exactly zero. Under a continuous model, any single point has zero width and therefore zero area under the density, so only intervals carry probability. Ask instead for P(199.5 < X <= 200.5).

open as a page

What is the KL divergence KL(P||Q), and why is it not a distance metric?

level: middleimportance: must knowfreq 66%

basics

~20 s

KL(P||Q) = sum P(x) log(P(x)/Q(x)) is the cost of describing draws from P as if they came from Q. It is never negative and zero only when P equals Q, but it is asymmetric, so it is a divergence, not a metric.

open as a page

Why does linearity of expectation hold even when the random variables are dependent?

level: middleimportance: must knowfreq 76%

basics

~20 s

Linearity of expectation is proved by regrouping a sum over the joint distribution, never by multiplying probabilities. E[X + Y] = E[X] + E[Y] holds for any variables with finite means. Independence is needed for products, not sums.

open as a page

Why does the magnitude of Cov(X,Y) say little about the strength of association?

level: middleimportance: must knowfreq 78%

basics

~10 s

Covariance carries the units of X times the units of Y, so rescaling either variable rescales it. Its sign shows the direction of linear association; only the correlation Cov(X,Y)/(sd(X)*sd(Y)), bounded in [-1,1], measures strength.

open as a page

Why does Cov(X,Y) = 0 fail to guarantee that X and Y are independent?

level: middleimportance: must knowfreq 72%

basics

~20 s

Covariance detects only linear association. A pair can be perfectly dependent through a curved relationship and still have zero covariance: with X uniform on (-1,1) and Y = X^2, Cov(X,Y) = 0 even though Y is a function of X.

open as a page

Why can a probability density function take values greater than 1 when a probability cannot?

level: middleimportance: must knowfreq 62%

basics

~20 s

A density is probability per unit of x, not a probability. Probability is the area under the curve, so a tall density over a narrow interval still has total area 1. The uniform density on [0, 0.5] equals 2 everywhere.

open as a page

How do you find the distribution of the sum of two independent random variables?

level: middleimportance: must knowfreq 62%

basics

~10 s

By convolution: enumerate every split of the target value and multiply the two probabilities, which independence permits. Discretely, P(X+Y=s) = sum over k of P(X=k)*P(Y=s-k); continuously, f_S(s) = integral of f_X(x)*f_Y(s-x) dx.

open as a page

Why is the sum of two independent normal random variables exactly normal?

level: middleimportance: must knowfreq 55%

basics

~20 s

Moment generating functions multiply for independent variables, and the product of two normal moment generating functions is again a normal one. The sum is normal with the two means added and the two variances added.

open as a page

From a joint table of device and plan tier, how do you compute a marginal and a conditional distribution?

level: juniorimportance: should knowfreq 58%

basics

~20 s

Sum a row or column of joint probabilities to get a marginal: P(mobile) = P(mobile, free) + P(mobile, pro). Divide a single joint cell by that marginal to get a conditional: P(pro | mobile) = P(mobile, pro) / P(mobile).

open as a page

How do you find the distribution of the maximum of n i.i.d. random variables?

level: juniorimportance: should knowfreq 48%

basics

~20 s

Go through the CDF. The maximum is at most x exactly when every draw is at most x, so F_max(x) = F(x)^n for n independent draws with CDF F. The minimum instead gives F_min(x) = 1 - (1 - F(x))^n.

open as a page

How do entropy, cross-entropy and KL divergence relate to one another?

level: middleimportance: should knowfreq 52%

basics

~20 s

Cross-entropy splits into entropy plus divergence: H(P,Q) = H(P) + KL(P||Q). Entropy is the unavoidable cost of describing draws from P, KL is the extra cost of using Q instead, and cross-entropy is the total.

open as a page

What does mutual information between acquisition channel and conversion actually measure?

level: middleimportance: should knowfreq 36%

basics

~10 s

It measures how many bits knowing the channel removes from the uncertainty about conversion: I(X;Y) = H(Y) - H(Y|X). It is symmetric, never negative, and exactly zero when channel and conversion are independent.

open as a page

How does the variance of a temperature reading change when you convert Celsius to Fahrenheit?

level: middleimportance: should knowfreq 52%

basics

~20 s

Variance is multiplied by 1.8 squared, which is 3.24, and the plus-32 shift changes nothing. In general Var(aX + b) = a^2 * Var(X). The standard deviation is multiplied by 1.8, so 2 degrees Celsius becomes 3.6 degrees Fahrenheit.

open as a page

How do you derive Var(X) = E[X^2] - (E[X])^2 from the definition of variance?

level: middleimportance: should knowfreq 62%

basics

~10 s

Expand the squared deviation inside the expectation. Var(X) = E[(X - mu)^2] becomes E[X^2] - 2muE[X] + mu^2, and since E[X] = mu the last two terms collapse to -mu^2, leaving E[X^2] - (E[X])^2.

open as a page

How do you read the median and the p90 latency off a theoretical CDF F(t)?

level: middleimportance: should knowfreq 52%

basics

~10 s

Invert the CDF rather than reading heights: the median is the smallest t with F(t) at least 0.5, and the p90 the smallest t with F(t) at least 0.9.

open as a page

What normalising constant c makes f(x) = c*x a valid density on [0, 2]?

level: middleimportance: should knowfreq 45%

basics

~20 s

c = 1/2. The area under c*x from 0 to 2 is c times 2, and a density's total area must equal 1, so c = 1/2. The sign is fixed by requiring the density to be non-negative.

open as a page

Why does the density of Y = e^X carry a factor of 1/y when X is normal?

level: middleimportance: should knowfreq 38%

basics

~20 s

A density must be rescaled by the derivative of the inverse map. The inverse of Y = e^X is X = ln y, whose derivative is 1/y, so f_Y(y) = f_X(ln y)/y for y > 0.

open as a page

Why can KL divergence between last month's and this month's traffic mix come out infinite?

level: seniorimportance: should knowfreq 40%

basics

~20 s

KL is infinite whenever the current month has mass in a category the reference month gives zero probability. A brand-new channel or an empty bin makes the log ratio diverge, so the score explodes for a reason unrelated to drift size.

open as a page

Why does the mean of 1/X exceed 1 divided by the mean of X for a random latency X?

level: seniorimportance: should knowfreq 38%

basics

~10 s

Because the reciprocal function is strictly convex on positive values, Jensen's inequality gives E[1/X] >= 1/E[X], strict unless X is constant. Averaging per-item rates therefore overstates the rate implied by the average latency.

open as a page

For two equal-variance return streams, how does their correlation change the variance of a 50/50 blend?

level: seniorimportance: should knowfreq 46%

basics

~10 s

With both streams at variance s^2 and correlation rho, the blend has variance 0.5s^2(1 + rho). Perfectly correlated streams give no reduction, uncorrelated ones halve the variance, and rho = -1 cancels it entirely.

open as a page

How many random packs do you expect to buy to complete a 50-sticker collection?

level: seniorimportance: nice to knowfreq 22%

basics

~20 s

About 225 packs, if each pack holds one sticker drawn uniformly at random. Split the hunt into 50 stages with expected lengths 50/50, 50/49, ..., 50/1, which sum to 50 times the 50th harmonic number.

open as a page

In a bivariate normal distribution, what does conditioning on X = x do to the distribution of Y?

level: seniorimportance: nice to knowfreq 26%

basics

~20 s

Y given X = x is again normal, with mean mu_Y + rho*(sd_Y/sd_X)(x - mu_X) and variance sd_Y^2(1 - rho^2). The mean moves linearly with x, and the spread does not depend on x at all.

open as a page

How do you describe a customer spend variable with a point mass at 0 and continuous spend above it?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

As a mixed distribution: no single PMF or PDF describes it. Use the CDF, which jumps by the non-buyer share at 0 then rises smoothly, or a mixture of an atom and a spend distribution.

open as a page

How is the chi-square distribution built from standard normal random variables?

level: seniorimportance: nice to knowfreq 28%

basics

~20 s

It is a sum of squares: if Z1 through Zk are independent standard normals, then Z1^2 + ... + Zk^2 is chi-square with k degrees of freedom, never negative, with mean k and variance 2k.

open as a page

How do you decide whether to minimise KL(P||Q) or KL(Q||P) when approximating a distribution?

level: principalimportance: nice to knowfreq 24%

basics

~20 s

Pick the direction by which error you can least afford. Minimising KL(P||Q) is mass-covering: the approximation stretches to cover everything the target produces. Minimising KL(Q||P) is mode-seeking: it locks onto one region and ignores the rest.

open as a page