Skip to content

Gaps between random points

Posted on:

Drop nn points independently and uniformly on a circle of circumference 11. They cut the circle into nn arcs, the gaps, whose average length is exactly 1/n1/n. The longest gap is much longer than that. Its expected length is

E[Gmax]=Hnn,Hn=1+12++1nlnn+0.5772,\mathbb{E}[G_{\max}] = \frac{H_n}{n}, \qquad H_n = 1 + \frac12 + \cdots + \frac1n \approx \ln n + 0.5772,

so with a thousand points the longest gap is, on average, 7.57.5 times the average gap, while the shortest gap has expectation exactly 1/n21/n^2, a thousand times shorter than average. The derivation is one change of variables. The exact distribution of the longest gap is a finite alternating sum that Fisher wrote down in 1929 for a problem in time-series analysis. One application is consistent hashing: nn servers hashed to a ring each own the arc up to their next neighbor, so the busiest server carries about lnn\ln n times its fair share of the keys.

The gaps are exchangeable

Rotate the circle so that the first point sits at 00. The other n1n - 1 points are then i.i.d. uniform on [0,1)[0, 1), and sorted into U(1)<U(2)<<U(n1)U_{(1)} < U_{(2)} < \cdots < U_{(n-1)} they have joint density (n1)!(n-1)! on the region 0<u1<<un1<10 < u_1 < \cdots < u_{n-1} < 1. The gaps are

G1=U(1),G2=U(2)U(1),,Gn=1U(n1),G_1 = U_{(1)}, \quad G_2 = U_{(2)} - U_{(1)}, \quad \ldots, \quad G_n = 1 - U_{(n-1)},

and the map from (U(1),,U(n1))(U_{(1)}, \ldots, U_{(n-1)}) to (G1,,Gn1)(G_1, \ldots, G_{n-1}) is linear with determinant 11. So the gap vector is uniformly distributed on the simplex {gi0,  g1++gn=1}\{g_i \ge 0,\; g_1 + \cdots + g_n = 1\}, the Dirichlet distribution with all parameters equal to 11. Uniformity on the simplex is symmetric in the coordinates, so the gaps are exchangeable: every permutation of (G1,,Gn)(G_1, \ldots, G_n) has the same joint law. They are not independent, since they sum to 11, but any statement about one gap is a statement about every gap. Each has the marginal

P(Gi>x)=(1x)n1,\mathbb{P}(G_i > x) = (1 - x)^{n-1},

the probability that the other n1n - 1 points all avoid an arc of length xx, with mean 1/n1/n. This is the same symmetry that produces the German tank estimator: there, kk observed serial numbers cut a range into k+1k+1 exchangeable gaps, and the unobserved gap above the maximum is estimated by the average of the visible ones.

The gap that contains a fixed location on the circle, say the point diametrically opposite the origin, is not a typical gap: long gaps are more likely to contain any given location, in proportion to their length. Its mean is E[G2]/E[G]=2/(n+1)\mathbb{E}[G^2]/\mathbb{E}[G] = 2/(n+1), about twice the average gap. This is the inspection paradox, and it is the reason that, when buses arrive at random times, the interval you land in is longer than the average interval between buses.

Gaps are normalized exponentials

The uniform distribution on the simplex has a second description that makes order statistics easy. Let E1,,EnE_1, \ldots, E_n be i.i.d. exponential random variables with mean 11 and let S=E1++EnS = E_1 + \cdots + E_n. The joint density of the EiE_i is e(e1++en)e^{-(e_1 + \cdots + e_n)}, a function of the sum alone, so conditional on SS the vector (E1,,En)(E_1, \ldots, E_n) is uniform on the simplex of total SS; equivalently, (E1/S,,En/S)(E_1/S, \ldots, E_n/S) is uniform on the unit simplex and independent of SS. The gaps have the same law as Ei/SE_i/S.

Order statistics of exponentials follow from memorylessness. The minimum of nn exponentials with mean 11 is exponential with mean 1/n1/n; given the minimum, the excesses of the other n1n-1 variables over it are again i.i.d. exponential with mean 11, so the second smallest exceeds the smallest by an exponential with mean 1/(n1)1/(n-1), and so on up the order. This is Rényi’s representation (1953):

E(k)  =d  Z1n+Z2n1++Zknk+1,Zj i.i.d. exponential with mean 1.E_{(k)} \;\stackrel{d}{=}\; \frac{Z_1}{n} + \frac{Z_2}{n-1} + \cdots + \frac{Z_k}{n-k+1}, \qquad Z_j \text{ i.i.d. exponential with mean } 1.

Since G(k)=E(k)/SG_{(k)} = E_{(k)}/S is independent of SS, taking expectations gives E[E(k)]=E[G(k)]E[S]=nE[G(k)]\mathbb{E}[E_{(k)}] = \mathbb{E}[G_{(k)}]\,\mathbb{E}[S] = n\,\mathbb{E}[G_{(k)}], and therefore

E[G(k)]=1nj=1k1nj+1,E[Gmin]=1n2,E[Gmax]=Hnn.\mathbb{E}[G_{(k)}] = \frac{1}{n}\sum_{j=1}^{k} \frac{1}{n - j + 1}, \qquad \mathbb{E}[G_{\min}] = \frac{1}{n^2}, \qquad \mathbb{E}[G_{\max}] = \frac{H_n}{n}.

In units of the average gap 1/n1/n: the smallest gap is 1/n1/n of an average gap, the median gap (k=n/2k = n/2) is HnHn/2ln2=0.69H_n - H_{n/2} \approx \ln 2 = 0.69 average gaps, and the largest is HnH_n average gaps. The largest gap grows like lnn\ln n average gaps and the smallest shrinks like 1/n1/n of one. The expected largest gap, in units of the average gap:

nn101001,00010610^6
HnH_n2.935.197.4914.39

The exact distribution of the largest gap

All gaps are at most xx exactly when none exceeds xx, and inclusion–exclusion over the events {Gi>x}\{G_i > x\} needs only one fact: for any kk specified gaps,

P(Gi1>x,,Gik>x)=(1kx)+n1.\mathbb{P}(G_{i_1} > x, \ldots, G_{i_k} > x) = (1 - kx)_+^{\,n-1}.

Subtracting xx from each of the kk gaps leaves a vector uniform on a simplex of total 1kx1 - kx, and the volume of that simplex relative to the full one is (1kx)n1(1 - kx)^{n-1} (the simplex has dimension n1n - 1). Hence

P(Gmaxx)=k=01/x(1)k(nk)(1kx)n1.\mathbb{P}(G_{\max} \le x) = \sum_{k=0}^{\lfloor 1/x \rfloor} (-1)^k \binom{n}{k} (1 - kx)^{n-1}.

For x12x \ge \tfrac12 only the terms k=0,1k = 0, 1 survive, and P(Gmax>12)=n21n\mathbb{P}(G_{\max} > \tfrac12) = n\,2^{1-n} is the probability that all nn points fit in some semicircle, a classical problem. At the other end, P(Gmin>x)=(1nx)n1\mathbb{P}(G_{\min} > x) = (1 - nx)^{n-1} is the single term k=nk = n (every gap exceeds xx), so the probability that some two of the points lie within distance xx of each other is 1(1nx)n11en2x1 - (1 - nx)^{n-1} \approx 1 - e^{-n^2 x}, the continuous form of the birthday problem; in units of 1/n21/n^2 the smallest gap is asymptotically exponential.

For the largest gap, put x=(lnn+y)/nx = (\ln n + y)/n. Then (nk)nk/k!\binom{n}{k} \approx n^k/k! and (1kx)n1ek(lnn+y)=nkeky(1 - kx)^{n-1} \approx e^{-k(\ln n + y)} = n^{-k}e^{-ky}, so term by term the sum tends to k(1)keky/k!\sum_k (-1)^k e^{-ky}/k!:

P(nGmaxlnny)    exp(ey),\mathbb{P}\bigl(n\,G_{\max} - \ln n \le y\bigr) \;\longrightarrow\; \exp\bigl(-e^{-y}\bigr),

the Gumbel distribution, whose mean is Euler’s constant γ=0.5772\gamma = 0.5772, in agreement with HnlnnγH_n - \ln n \to \gamma. The largest gap is lnn\ln n average gaps plus a Gumbel-distributed number of average gaps: its mean grows with nn, but its fluctuations stay of the same order as a single average gap. The widget draws a sample on the left and, on the right, the exact density of nGmaxn\,G_{\max} from the alternating sum, with the Gumbel limit and an adjustable threshold:

points n = 100
threshold c = 3.0 × average gap
exact density of n·Gmax Gumbel limit, shifted by ln n largest gap in the sample
largest gap here: × average
expected largest gap = × averageexpected smallest gap = average / P(largest gap > c × average) =

Consistent hashing

Karger and coauthors (1997) proposed the following way to spread keys over servers so that adding or removing a server disturbs as little as possible. Hash each server to a point on a ring, hash each key to a point on the same ring, and assign each key to the first server clockwise from it. When a server joins or leaves, only the keys on one arc move. The share of keys a server receives is the length of the arc it owns, which is exactly one of the gaps above. So the busiest server’s share is GmaxG_{\max}: on average HnH_n times the fair share 1/n1/n, and the imbalance grows with nn. With 100100 servers the busiest holds 5.25.2 times its share on average, and the probability that some server holds more than three times its share is 0.99920.9992; with 1,0001{,}000 servers the busiest holds 7.57.5 times its share.

The remedy in the same paper is to hash each server to vv points instead of one, the virtual nodes of later systems. A server’s share is then the sum of vv of the nvnv gaps, and by exchangeability that sum has the beta distribution with parameters vv and (n1)v(n-1)v: mean 1/n1/n as before, and relative standard deviation (n1)/(nv+1)1/v\sqrt{(n-1)/(nv+1)} \approx 1/\sqrt{v}. A hundred virtual nodes per server bring the typical deviation from the fair share down to about 10%, and the largest of the nn shares to roughly 1+2lnn/v1 + \sqrt{2\ln n / v} times the average (a Gaussian approximation to the beta law, with a union bound over the servers), about 1.31.3 for a hundred servers with a hundred virtual nodes each. Karger and coauthors prove that O(logn)O(\log n) points per server suffice to bring the largest share within a constant factor of 1/n1/n with high probability, and production systems such as Dynamo assign each server many positions on the ring for this reason.

The theory of spacings between order statistics is surveyed in Pyke (1965), the general distribution theory for functions of the gaps goes back to Darling (1953), and Holst (1980) derives the limit laws for the jj-th longest piece from the exponential representation alone.

References



Next Post
The Ehrenfest urn