Suppose you want to randomly choose a direction. Maybe you’re a neutrophil, a type of immune cell, and you’re searching for a bacteria. Which direction should you choose? Maybe you’re coding, or building a mathematical model and you want to choose a random direction to move in. Let’s begin by asking a more specific question.
How do you uniformly choose a direction in 2-dimensions?
I think that the easiest thing to do is to uniformly pick an angle, , and then convert to a vector on the unit circle:
This method works! If you sample 100 points from a uniform distribution on , and plot the result, it looks pretty uniform on the unit circle:
The reason this works is that we want any little segment of arc length on the unit circle to have the same number of points. In terms of differentials (arc-length-elements and theta-elements), we have
because and .
Does the same method work in 3-dimensions?
Interestingly, no! If you were to randomly choose two angles and , and use spherical coordinates
then you would observe cluster of points at the poles. You can see this in the figure on the left, or by looking at a top down view, as on the right:
This method fails in 3D due to the fact that not all latitudes have the same circumference! In other words, our method breaks because the area element
is a function of . Note that you could choose a transformation other than spherical coordinates so that this works nicely if you wanted to (http://mathworld.wolfram.com/SpherePointPicking.html).
-dimensional uniform distributions on the unit sphere.
It turns out that one of the easiest ways generate a uniform distribution on the -dimension unit sphere is to actually use normal distributions…
The density function for a normally distributed random variable vector is
For the sake of example, consider the case where the covariance is identity, , and the mean is . Then
In general, we can conclude that a -dimensional Gaussian with mean and diagonal covariance matrix is the same as a collection of independent Gaussian random variables with mean and variance .
From this, we can sample uniformly random points on the -dimension unit sphere by
- Generate standard normal random variables ,
- and normalizing: .
On the unit sphere in -dimensions, ,
is spherically symmetric, i.e., the distribution is constant!
This works! No clustering!