Random Chord Length
Posted on Sat 25 March 2023 in Posts
Here is a question: pick two points on a circle of radius \(r\) independently uniformly at random.
What is the average distance between these two points?
Analysis
Let's to analyze this question using probabilistic mathematical reasoning before doing simulations.
Sampling points on a circle
To sample a point \(P=(X,Y)\) on a circle, we require
Instead of dealing with two random variables, let's simplify things by using polar coordinates. We fix the radius at \(r\) and vary the angle \(\theta\) between \(0\) and \(2\pi\) uniformly:
For two independent points on a circle, \(P_1=(r,\,\theta_1),~P_2=(r,\,\theta_2)\), we can calculate the distance as follows
Our coordinates lie on same circle, hence \(r_1=r_2\).
Simplifying a bit more, we get
We seek the expected distance \(\mathbb{E}[d]\) for two independent distributions of \(\theta_1, \theta_2\). The expected value is written as
The main point of contention here is integrating the \(\left\lvert\sin\left(\frac{\theta_1-\theta_2}{2}\right)\right\rvert\) function. The function splits in two, based on the sign of \(\theta_1-\theta_2\). Without loss of generality, we can always assume that \(\theta_1 < \theta_2\), hence we arrive at
For \(r=1\), we get
Monte-Carlo simulation code
We will define a function that computes an array of distances for random points. The size of array is given by parameter size
and the radius can be specified via r
.
import numpy as np
def dist(r, size=100):
theta_1 = np.random.uniform(0,2*np.math.pi, size=size)
theta_2 = np.random.uniform(0,2*np.math.pi, size=size)
return 2*r*np.sin(np.abs((theta_1 - theta_2)/2))
Let us verify for r=1
and size=10000
by computing the difference between 4/pi
and the call to dist
:
print(abs(4/np.math.pi, dist(1, 10000).mean()))
#0.0008801268016318531
Bertrand's paradox
From colleagues at work, I discovered that this problem is closely related to a well-known paradox called Bertrand's paradox12 which I may write about in a later post.