Wednesday 12 August 2026

Quant interview

Why HH takes longer to arrive than TH

medium · Waiting times for patterns in coin tosses

A fair coin is tossed repeatedly and independently.

  1. Let NHH be the number of tosses until two heads have appeared in a row for the first time, and NTH the number until a tail is immediately followed by a head for the first time. Compute 𝔼[NHH] and 𝔼[NTH].
  2. The two answers differ even though both patterns have probability 1/4 in any two fixed consecutive tosses. Give the structural reason.
  3. Compute the probability that HH appears before TH.
Solution

1. Two first-step recursions

Track only what a pattern needs next, which makes each chain two states.

For HH, let a=𝔼[NHH] from a fresh start and b the expected further wait after a head. From a fresh start the next toss is a head (move to the b state) or a tail (start over):

a=1+12b+12a

From the b state a head finishes and a tail sends you back to the start:

b=1+12·0+12a

Substituting gives a=2+b and b=1+a/2, hence a=6 and b=4.

For TH, let a′ be the wait from a fresh start and b′ the wait once a tail has appeared. The difference is in the b′ state: a head finishes, and a tail leaves you exactly where you were.

a′=1+12b′+12a′,b′=1+12·0+12b′

So b′=2 and a′=4.

𝔼[NHH]=6,𝔼[NTH]=4

2. Where the asymmetry comes from

HH overlaps itself: after a head, a tail destroys the progress you had made and returns you to the start. TH cannot overlap itself in that way — once a tail has appeared you are permanently one head away, and a further tail keeps you one head away. Progress toward TH is never lost, progress toward HH is, so HH waits longer. This is exactly the correlation term in Conway's leading-number algorithm: a pattern's expected waiting time grows with its self-overlap.

3. Which comes first

HH appears before TH if and only if the first two tosses are both heads, so the probability is 1/4.

The forward direction is immediate. For the converse, suppose the first two tosses are not HH. If the first toss is a tail, then TH completes at the very first head that appears, and no HH can have occurred earlier because every toss before that first head is a tail. If the first toss is a head and the second a tail, the same argument applies from the second toss onward. Either way TH comes first.

ℙ(HH before TH)=14

Note. Compare with HT, where the answer is 1/2: after the first head, the next toss decides between HH and HT immediately. Pattern races are decided by overlap structure, not by the individual pattern probabilities, and swapping one letter changes the answer.

Statistics in machine learning

The exponential rate, where unbiasedness is not the last word

medium · Maximum likelihood: Fisher information, exact bias, MSE

Let X1,…,Xn be i.i.d. exponential with rate λ>0, density λe−λx for x>0.

  1. Find the maximum likelihood estimator λ^n and the Fisher information I(λ) for a single observation, and state the limiting distribution of n(λ^n−λ).
  2. Compute 𝔼[λ^n] exactly for n≥2 and give the exact bias. Is the estimator consistent?
  3. For n≥3, compare the mean squared error of λ^n with that of the exactly unbiased multiple of it, and then find the multiple c/∑iXi with the smallest MSE of all.
Solution

1. The estimator and its asymptotics

The log-likelihood is nlogλ−λ∑iXi, with derivative n/λ−∑iXi, so

λ^n=1X¯.

For one observation, logf=logλ−λx has ∂λ2logf=−1/λ2, a constant, so

I(λ)=−𝔼[∂λ2logf]=1λ2.

Standard MLE asymptotics then give

n(λ^n−λ)⇒N(0,I(λ)−1)=N(0,λ2).

2. Exact moments

Let S=∑iXi~Gamma(n,λ) (shape n, rate λ). For k<n,

𝔼[S−k]=λkΓ(n−k)Γ(n),

so 𝔼[S−1]=λ/(n−1) for n≥2 and 𝔼[S−2]=λ2/((n−1)(n−2)) for n≥3. Since λ^n=n/S,

𝔼[λ^n]=nλn−1,Bias=λn−1.

The MLE overestimates the rate, for the reason Jensen's inequality predicts: 1/X¯ is a strictly convex function of X¯, so its mean exceeds 1/𝔼[X¯]=λ. It is nonetheless consistent — the bias and the variance both vanish as n→∞.

3. Debiasing, and then doing better

Write λ~=n−1nλ^n=(n−1)/S, which is exactly unbiased. Using the moments above,

MSE(λ^n)=n2λ2(n−1)(n−2)−2nλ2n−1+λ2=(n+2)λ2(n−1)(n−2)

MSE(λ~)=Var(λ~)=(n−1)λ2n−2−λ2=λ2n−2=(n−1)λ2(n−1)(n−2)

Comparing numerators, n+2 against n−1, the unbiased version wins for every n≥3, by

MSE(λ^n)−MSE(λ~)=3λ2(n−1)(n−2)>0.

Now optimize over the whole family c/S. Its MSE is

c2λ2(n−1)(n−2)−2cλ2n−1+λ2,

a convex quadratic in c minimized at c=n−2, giving

MSE(n−2S)=λ2n−1,

smaller still. So the ranking is (n−2)/S, then (n−1)/S, then the MLE n/S — and note that the optimal c does not depend on the unknown λ, so this estimator is actually usable.

Note. Two lessons sit on top of each other here. The MLE's optimality is asymptotic, and at finite n a deterministic rescaling beats it; and unbiasedness is not the objective, since the MSE-optimal member of the family is biased low on purpose. The tempting wrong answer is that λ~ must be best because it is the unbiased one.


Two new problems every morning at 8am · every day so far