Monday 24 August 2026

Quant interview

How long does it take to win at gambler's ruin?

medium · Random walks and gambler's ruin

A trader plays a sequence of independent fair rounds. In each round his bankroll goes up by $1 with probability 12 and down by $1 with probability 12. He starts with $20 and stops the moment his bankroll first hits either $0 (broke) or $50 (target). Let T be the number of rounds played until he stops, and let A be the event that he stops at $50.

  1. Compute ℙ(A).
  2. Compute 𝔼[T].
  3. Compute 𝔼[T∣A]: given that he does reach $50, how many rounds does that take on average? State the general closed form (start a, boundaries 0 and N) and then the number.
Solution

Write Xn for the bankroll after n rounds, X0=a=20, boundaries 0 and N=50. The increments are i.i.d. mean-zero, so Xn is a martingale, and T (first exit from {1,…,N−1}) is an a.s. finite stopping time with 𝔼[T]<∞, so optional stopping applies at T.

Part 1: hitting probability

Optional stopping on the martingale Xn:

a=𝔼[X0]=𝔼[XT]=0·(1−p)+N·p,p:=ℙ(A).

Hence p=a/N=20/50=25.

Part 2: unconditional duration

Use the compensated martingale Mn=Xn2−n (mean-zero unit-variance increments). Optional stopping:

a2=𝔼[XT2]−𝔼[T].

Since XT∈{0,N}, 𝔼[XT2]=N2p=N2·aN=Na. Therefore

𝔼[T]=Na−a2=a(N−a)=20·30=600.

Part 3: duration conditioned on winning

The step a candidate misses: you cannot recurse directly on the conditional expectation ma=𝔼a[T∣A], because conditioning on A retilts the step probabilities away from 12,12 (the walk is no longer symmetric once you condition on its endpoint). The clean fix is to work with the unnormalized quantity

ga:=𝔼a[T1A],

which obeys an ordinary first-step recursion under the true (symmetric) dynamics, and only divide by pa=a/N at the very end.

First-step analysis. From a, after one round we are at a±1 and T increases by 1:

ga=12𝔼a+1[(1+T′)1A]+12𝔼a−1[(1+T′)1A].

ga=12(ga+1+ga−1)+12(pa+1+pa−1).

Because pa=a/N is linear, pa+1+pa−1=2pa=2a/N, so the source term is a/N and the recursion becomes the discrete Poisson equation

ga+1−2ga+ga−1=−2aN,

with boundary data g0=0 and gN=0 (at N, A occurs but T=0; at 0, 1A=0).

A cubic kills the right side: since (a+1)3−2a3+(a−1)3=6a, take particular solution −a3/(3N), then add the linear homogeneous solution α+βa. Boundaries give α=0, β=N/3:

ga=a(N2−a2)3N.

Divide by pa=a/N:

𝔼[T∣A]=gapa=N2−a23.

With N=50, a=20:

𝔼[T∣A]=2500−4003=21003=700.

Summary

Quantity Formula Value
ℙ(A) a/N 2/5
𝔼[T] a(N−a) 600
𝔼[T∣A] (N2−a2)/3 700

Sanity check. By the reflection a↦N−a, the duration conditioned on ruin is (N2−(N−a)2)/3=(2Na−a2)/3=1600/3. Weighting by p and 1−p:

25·700+35·16003=280+320=600=𝔼[T].✓

Note. Conditional on winning, the expected number of rounds (700) exceeds the unconditional average (600): reaching the far boundary takes longer than a typical exit. The slicker route to Part 3 is Doob's h-transform — conditioning on A turns the walk into a Markov chain with step probabilities P(a→a+1)=a+12a, P(a→a−1)=a−12a (using ha=a/N), and computing the expected time to hit N in that chain reproduces (N2−a2)/3.

Statistics in machine learning

The logarithm that chaining removes

hard · VC dimension and Rademacher complexity

Let 𝒳 be an arbitrary set and let ℋ⊆{0,1}𝒳 have Vapnik–Chervonenkis dimension d≥1. Fix points x1,…,xn∈𝒳 with n≥d, and let σ1,…,σn be independent Rademacher signs (ℙ(σi=±1)=12). Write the empirical Rademacher complexity R^n(ℋ)=𝔼σ[suph∈ℋ1n∑i=1nσih(xi)]. For f,g:𝒳→ℝ let ‖f−g‖L2(Pn)=(1n∑i=1n(f(xi)−g(xi))2)1/2 denote the empirical L2 metric, and let N(ϵ,ℋ,L2(Pn)) be the corresponding covering number.

You may use the following named facts without proof.

(a) Prove the Sauer–Shelah/Massart bound R^n(ℋ)≤2dlog(en/d)n.

(b) Show that in fact R^n(ℋ)≤Cdn for a universal constant C (independent of d, n, and H), removing the logarithmic factor of part (a). Identify explicitly the step where the logarithm disappears.

(c) Show the rate in (b) is optimal: for every d≥1 and every n that is a multiple of d, exhibit a class H of VC dimension d and points x1,…,xn with R^n(ℋ)≥cdn,c=123.

Solution

(a) The Sauer–Shelah/Massart bound

Restrict each h to the sample: let A={(h(x1),…,h(xn)):h∈H}⊆{0,1}n. The supremum defining R^n depends on h only through its restriction, so R^n(ℋ)=𝔼σsupa∈A1n∑i=1nσiai. Every a∈A has entries in {0,1}, so ‖a‖2≤n; take R=n in Massart's lemma. By Sauer–Shelah, |A|=ΠH(n)≤(en/d)d, hence log|A|≤dlog(en/d). Massart gives R^n(ℋ)≤n2log|A|n=2log|A|n≤2dlog(en/d)n. Since log(en/d)=1+log(n/d), this carries an extra factor 1+log(n/d) over the target rate — the logarithm we now remove.

(b) Chaining removes the logarithm

The loss in (a) comes from bounding the whole class at the single scale ϵ=1 (a global cardinality bound). Dudley's integral instead pays for the class at every resolution, and the metric entropy of a VC class grows only like dlog(1/ϵ), whose square root is integrable near 0.

Converting Haussler to L2. For {0,1}-valued h,h′ one has (h(xi)−h′(xi))2=|h(xi)−h′(xi)|, so ‖h−h′‖L2(Pn)2=‖h−h′‖L1(Pn). Thus an L1(Pn)-cover at radius τ=ϵ2 is an L2(Pn)-cover at radius ϵ. Applying Haussler with Q=Pn and τ=ϵ2, N(ϵ,ℋ,L2(Pn))≤e(d+1)(2eϵ2)d,0<ϵ≤1. Taking logs, logN(ϵ)≤1+log(d+1)+dlog(2e)+2dlog(1/ϵ)≤d(C1+2log(1/ϵ)), where C1:=1+log(d+1)d+log(2e) is bounded by the absolute constant 1+log2+log(2e) for all d≥1 (using log(d+1)≤dlog2).

The integral. For ϵ>1 the diameter of H in L2(Pn) is at most 1, so N(ϵ)=1 and the integrand vanishes. Hence the Dudley integral runs only over (0,1]: ∫01logN(ϵ)dϵ≤d∫01C1+2log(1/ϵ)dϵ. Using a+b≤a+b and the exact value ∫01log(1/ϵ)dϵ=∫0∞ue−udu=Γ(3/2)=π2, ∫01C1+2log(1/ϵ)dϵ≤C1+2·π2=:C2, a finite absolute constant. This convergence at ϵ→0 is exactly the step (a) could not exploit. Therefore R^n(ℋ)≤12nC2d=Cdn,C=12C2.

(c) Matching lower bound

Let z1,…,zd∈X be distinct and set H={h:h|{z1,…,zd} arbitrary, h≡0 elsewhere}, i.e. all 2d patterns on the zj. This class shatters {z1,…,zd} and shatters no set of size d+1, so its VC dimension is exactly d.

Choice of sample — the key move. Do not take n=d (which gives a constant) and do not pad with a fixed point (which gives d/n). Instead spread the shattered points evenly: with m=n/d, let each zj appear exactly m times among x1,…,xn.

For a sign vector σ, group the indices by their point and write Tj=∑i:xi=zjσi, a sum of m independent Rademacher signs. Since h(zj)∈{0,1} can be chosen independently for each j, suph∈H∑i=1nσih(xi)=∑j=1dsuph(zj)∈{0,1}h(zj)Tj=∑j=1d(Tj)+. By symmetry of Tj,  𝔼(Tj)+=12𝔼|Tj|. Lower-bound 𝔼|Tj| by interpolation: ‖T‖2≤‖T‖11/3‖T‖42/3 (Hölder with exponents matching 12=13·1+23·14), so 𝔼|Tj|=‖Tj‖1≥‖Tj‖23‖Tj‖42=m3/2𝔼Tj4. For a Rademacher sum, 𝔼Tj2=m and 𝔼Tj4=3m2−2m≤3m2, hence 𝔼|Tj|≥m3/2/(3m)=m/3. Therefore 𝔼σsuph∑i=1nσih(xi)=∑j=1d12𝔼|Tj|≥d2m3. Dividing by n=md and using m=n/d, R^n(H)≥1n·d2m3=123·dmmd=123·1m=123dn.

Conclusion

For every VC class of dimension d and every sample of size n≥d, cdn ≤ supH,x1:nR^n(H) ≤ Cdn,c=123,  C=12(1+log2+log(2e)+2π2). So R^n(H)=Θ(d/n), dimension-free in the ambient space and free of the extra logarithm.

Closing note. The tempting wrong conclusion is that the log(n/d) factor in part (a) is intrinsic — it is an artifact of controlling the class at one scale. Chaining trades one global bound for a geometric sum of local ones, and ∫0log(1/ϵ)dϵ<∞ is what makes the trade profitable. On the lower-bound side, the classic trap is to evaluate at n=d (giving the constant 12) or to pad with an inert point (giving d/2n, which is smaller than d/n); replicating each shattered point n/d times, so that each coordinate group contributes 𝔼(Tj)+~m, is what produces the correct dn scaling.


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