# The expected distance to the nearest neighbor when N points are placed randomly on a line

In a prior post about the expected distance between two randomly selected points on a line of length L, this answer was given link. The visual representation for the answer $$\frac{L}{3}$$ was useful, and led me to think about the answer to the more general problem:

If $$N\geq 2$$ random points are placed along the line segment stated above, what is the expected distance between any dot selected at random, and its nearest neighbor?

Is the answer as simple as $$\frac{L}{N + 1}$$?

• $N$ points means $N(N-1)/2$ distances. Which one of those distances are you asking about? – Gerry Myerson Sep 26 '18 at 6:53
• I am asking about all these distances, and more specifically, the average of all these distances. – user120911 Sep 26 '18 at 7:06
• @GerryMyerson, you got me thinking. That was a good question. I suppose what I am really asking about is the expected distance to the nearest neighbor for any randomly selected point. Because if I am asking about the average distance between two dots selected at random, $\frac{L}{3}$ appears to be that answer, irrespective of of many points I can select from. But the expected distance to the nearest neighbor would surely depend on $N$. – user120911 Sep 26 '18 at 7:13
• en.wikipedia.org/wiki/… The $k$-th ordered uniform of sample size $n$ has a mean $\displaystyle \frac {k} {n+1}$ (you can multiply by $L$ to scale it as you like). So you guess should be correct. – BGM Sep 26 '18 at 7:47

Fix an integer $$n\geq 2$$. Let $$X_1,X_2,\ldots,X_n$$ be independent and identically distributed random variable with distribution function $$F$$ and probability density $$f$$. Write $$Y_1,Y_2,\ldots,Y_n$$ for a rearrangement of $$X_1,X_2,\ldots,X_n$$ so that $$Y_1\leq Y_2\leq \ldots\leq Y_n$$. Then, prove that the probability density $$g_k$$ of $$Y_k$$ is given by $$g_k(y)=n\binom{n-1}{k-1}\,F(y)^{k-1}\,\big(1-F(y)\big)^{n-k}\,f(y)\text{ for all }y\in\mathbb{R}\,.$$ In particular, if $$X_1,X_2,\ldots,X_n$$ are uniformly distributed on $$[0,L]$$, then we have $$f(x)=\frac{1}{L}\text{ and }F(x)=\frac{x}{L}\text{ for }x\in[0,L]\,.$$ Therefore, $$g_k(y)=n\binom{n-1}{k-1}\,\left(\frac{y}{L}\right)^{k-1}\,\left(1-\frac{y}{L}\right)^{n-k}\,\frac{1}{L}\text{ for }y\in[0,L]\,.$$ This means $$\mathbb{E}[Y_k]=n\binom{n-1}{k-1}\,\int_0^L\,\left(\frac{y}{L}\right)^{k-1}\,\left(1-\frac{y}{L}\right)^{n-k}\,\frac{y}{L}\,\text{d}y\,.$$ Letting $$t:=\dfrac{y}{L}$$, we get $$\mathbb{E}[Y_k]=n\binom{n-1}{k-1}\,L\,\int_0^1\,t^{k}\,(1-t)^{n-k}\,\text{d}t=n\binom{n-1}{k-1}\,L\,\frac{k!\,(n-k)!}{(n+1)!}=\frac{kL}{n+1}\,.$$ (This integral is a beta integral. See the definition of the beta function if you need to.) Thus, $$\mathbb{E}[Y_{k+1}-Y_k]=\mathbb{E}[Y_{k+1}]-\mathbb{E}[Y_k]=\frac{(k+1)L}{n+1}-\frac{kL}{n+1}=\frac{L}{n+1}\,.$$ Taking the average for $$k=1,2,\ldots,n-1$$, we get $$\frac{1}{n-1}\,\sum_{k=1}^{n-1}\,\mathbb{E}[Y_{k+1}-Y_k]=\frac{L}{n+1}\,.$$

• This answers a possible interpretation of the original unclear question, but it doesn't answer the clarified question. The question you're answering can be answered with much less effort using symmetry: Distribute $n+1$ points independently uniformly on a circle of length $L$; then uniformly pick one of them at which to split the circle into a line segment. By symmetry, each of the $n+1$ segments has expected length $\frac L{n+1}$. – joriki Nov 23 '19 at 16:24

The solution is slightly encumbered by the boundaries. I'll solve the similar question for the unit circle, and you can add the complications due to the boundaries yourself.

Fix one point $$P$$ and distribute the other $$n-1$$ points independently uniformly on the unit circle. Let $$D$$ be the arc distance from $$P$$ to its nearest neighbour. Then $$P(D\ge d)=\left(1-\frac d\pi\right)^{n-1}$$, and thus

$$E[D]=\int_0^\pi\mathrm dsP(D\ge s)=\int_0^\pi\mathrm ds\left(1-\frac s\pi\right)^{n-1}=\frac\pi n$$

Thus, the expected distance to the nearest neighbour is exactly half the expected distance to either neighbour (clockwise or counterclockwise).

Scaling to a line segment of length $$L$$ and ignoring boundary effects, we can estimate that the expected distance to the nearest neighbour is roughly $$\frac L{2n}$$.