Friday 28 August 2026

Quant interview

The gap that catches your point

medium · Order statistics and uniform spacings

Drop n points independently and uniformly on the interval [0,1]. Let the order statistics be 0=U(0)<U(1)<⋯<U(n)<U(n+1)=1, and define the n+1 spacings Di=U(i)−U(i−1),i=1,…,n+1.

  1. Find the marginal distribution of a single spacing Di and its mean.

  2. Fix a deterministic point x∈(0,1). Let L(x) be the length of the spacing that contains x (i.e. the gap (U(i),U(i+1)) with U(i)<x<U(i+1), with the convention that the endpoints 0 and 1 serve as boundaries when no sample point lies below or above x). Compute 𝔼[L(x)] in closed form.

  3. Now let X~Uniform(0,1) be drawn independently of the n points, and let L(X) be the length of the spacing containing X. Compute 𝔼[L(X)], and reconcile the answer with the mean spacing found in part 1.

Solution

Part 1: distribution of one spacing

By exchangeability of the n+1 spacings (the vector (D1,…,Dn+1) is uniformly distributed on the simplex {di≥0,∑di=1}), all Di share one marginal law. Take D1=U(1), the minimum of n i.i.d. uniforms: ℙ(D1>t)=ℙ(all n points>t)=(1−t)n,0≤t≤1. Differentiating, Di has density n(1−t)n−1, i.e. Di~Beta(1,n), with 𝔼[Di]=1n+1.

Part 2: the gap containing a fixed point

Write L(x)=A+B, where A=x−max{Uj:Uj<x} (=x if none),B=min{Uj:Uj>x}−x (=1−x if none). The step candidates miss is that L(x) is not a typical spacing: conditioning on containing x length-biases the gap, and there is a boundary correction. The clean route is the tail-integral formula 𝔼[A]=∫0∞ℙ(A>a)da.

For 0≤a≤x, the event {A>a} means no sample point falls in the interval (x−a,x) of length a: ℙ(A>a)=(1−a)n. Since A≤x always (there is an atom of mass (1−x)n at A=x, automatically included by the tail integral), 𝔼[A]=∫0x(1−a)nda=1−(1−x)n+1n+1. Symmetrically, ℙ(B>b)=(1−b)n for 0≤b≤1−x, so 𝔼[B]=∫01−x(1−b)ndb=1−xn+1n+1. Adding, 𝔼[L(x)]=2−(1−x)n+1−xn+1n+1.

Sanity check at x=12: 𝔼[L]=2(1−2−(n+1))n+1≈2n+1, roughly twice the mean spacing 1n+1 — the inspection paradox: a fixed point is more likely to fall inside a long gap.

Part 3: the gap containing an independent uniform point

Integrate the part-2 result over x~Uniform(0,1): 𝔼[L(X)]=∫012−(1−x)n+1−xn+1n+1dx=1n+1(2−1n+2−1n+2). 𝔼[L(X)]=1n+1·2(n+1)n+2=2n+2.

Reconciliation (length-biased sampling). Given the spacings, the probability that X lands in gap i is exactly Di. Hence 𝔼[L(X)]=𝔼[∑i=1n+1Di·Di]=(n+1)𝔼[Di2]=𝔼[Di2]𝔼[Di], the length-biased mean. For Beta(1,n), 𝔼[Di2]=2(n+1)(n+2), so 𝔼[L(X)]=2/((n+1)(n+2))1/(n+1)=2n+2, agreeing with the integral. The random point sees gaps weighted by their length, so its expected gap is the size-biased mean, strictly larger than the plain mean 1n+1 whenever n≥1.

Answers

  1. Di~Beta(1,n), mean 1n+1.
  2. 𝔼[L(x)]=2−(1−x)n+1−xn+1n+1.
  3. 𝔼[L(X)]=2n+2, the length-biased mean 𝔼[D2]/𝔼[D].

Tempting wrong answer: quoting 1n+1 (the plain spacing mean) for either part 2 or part 3 — this ignores that requiring a gap to contain a given point biases toward longer gaps.

Statistics in machine learning

When the sweep order breaks reversibility

medium · Metropolis-Hastings, Gibbs sampling and detailed balance

Let π be a probability distribution on the finite state space {0,1}2, with states written (x1,x2) and probabilities

state (0,0) (0,1) (1,0) (1,1)
π 0.1 0.2 0.3 0.4

Consider the two single-coordinate Gibbs kernels. P1 leaves x2 fixed and redraws x1 from the exact conditional π(·∣x2); P2 leaves x1 fixed and redraws x2 from π(·∣x1).

  1. Show that each Pi satisfies detailed balance with respect to π, and deduce that the random-scan Gibbs kernel Prs=12P1+12P2 is π-reversible.

  2. Let Psys=P1P2 be the systematic-scan kernel that updates coordinate 1 and then coordinate 2. Using the numbers above, compute π(A)Psys(A→B) and π(B)Psys(B→A) for A=(0,0), B=(1,1), and conclude that Psys is not π-reversible. Confirm nonetheless that πPsys=π.

  3. Prove the general criterion behind part 2: for two kernels that are each π-reversible, the composition P1P2 is π-reversible if and only if P1P2=P2P1. Use it to name one deterministic scan order that is guaranteed to be reversible.

Solution

Throughout, write Π=diag(π) and regard a kernel P as a matrix. The identity that organizes everything: P is π-reversible (detailed balance) ⟺ the matrix ΠP is symmetric ⟺ P is self-adjoint on L2(π) with ⟨f,g⟩π=∑xπ(x)f(x)g(x).

Part 1: each coordinate update is reversible

P1 only connects states sharing the same x2, with P1((x1,x2)→(x1′,x2))=π(x1′∣x2)=π(x1′,x2)π(x2). Hence π(x1,x2)P1((x1,x2)→(x1′,x2))=π(x1,x2)π(x1′,x2)π(x2), which is symmetric under swapping x1↔x1′. For transitions that change x2 both sides are 0. So P1 satisfies detailed balance; by symmetry so does P2.

Detailed balance is preserved by convex combinations: if π(x)Pi(x,y)=π(y)Pi(y,x) for i=1,2, then averaging the two identities gives π(x)Prs(x,y)=π(y)Prs(y,x). Thus Prs is π-reversible.

Part 2: the systematic scan breaks detailed balance

First the conditionals. Marginals of x2: π(x2=0)=0.1+0.3=0.4, π(x2=1)=0.6; marginals of x1: π(x1=0)=0.3, π(x1=1)=0.7. Then π(x1=1∣x2=0)=0.30.4=0.75,π(x2=1∣x1=1)=0.40.7=47, π(x1=0∣x2=1)=0.20.6=13,π(x2=0∣x1=0)=0.10.3=13.

Under Psys=P1P2 we redraw x1 from π(·∣x2old), then redraw x2 from π(·∣x1new), so Psys((x1,x2)→(x1′,x2′))=π(x1′∣x2)π(x2′∣x1′).

For A=(0,0)→B=(1,1): Psys(A→B)=π(x1=1∣x2=0)π(x2=1∣x1=1)=0.75·47=37, π(A)Psys(A→B)=0.1·37=0.37≈0.042857.

For B=(1,1)→A=(0,0): Psys(B→A)=π(x1=0∣x2=1)π(x2=0∣x1=0)=13·13=19, π(B)Psys(B→A)=0.4·19=0.49≈0.044444.

Since 0.042857≠0.044444, detailed balance fails: Psys is not π-reversible.

Stationarity still holds, however. π is a left eigenvector of each factor (πP1=π because a full conditional resample leaves the joint invariant, likewise πP2=π), so πPsys=πP1P2=πP2=π. This is the point candidates most often miss: a systematic-scan Gibbs sampler has the correct invariant distribution yet is generically not reversible.

Part 3: the commutation criterion

Reversibility is self-adjointness on L2(π). If P1,P2 are each self-adjoint, then (P1P2)*=P2*P1*=P2P1, using (AB)*=B*A* and Pi*=Pi. Hence P1P2 is self-adjoint (i.e. π-reversible) iff P1P2=P2P1.

In the numeric example the two updates do not commute, which is exactly why detailed balance broke.

A scan order guaranteed reversible: the palindromic sweep P1P2P1 (update 1, then 2, then 1 again). Indeed (P1P2P1)*=P1*P2*P1*=P1P2P1, so it is self-adjoint regardless of whether P1 and P2 commute. (One must halve the effective work of the repeated block, but reversibility is restored.)

Answer

  1. Each Pi satisfies detailed balance; Prs is π-reversible as a convex combination.
  2. π(A)Psys(A→B)=0.3/7≈0.04286 while π(B)Psys(B→A)=0.4/9≈0.04444; unequal, so Psys is not reversible, though πPsys=π.
  3. P1P2 is π-reversible iff P1P2=P2P1; the palindromic scan P1P2P1 is always reversible.

Note. A tempting error is to reason "detailed balance for each block ⇒ detailed balance for the sweep." Convex combinations inherit detailed balance (part 1), but compositions do not, because self-adjoint operators need not have self-adjoint products. Reversibility is not required for a valid MCMC sampler — only invariance is — but many CLT and spectral-gap results assume it, which is one practical reason the palindromic (or random) scan is preferred.


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