7

Fix a dimension $d$. Write $S^{d-1}$ for the surface of a hypersphere in $\mathbb{R}^d$, namely set of all $x = (x_1, \ldots, x_d) \in \mathbb{R}^d$ such that $|x|^2 = x_1^2 + \cdots + x_d^2 = 1$. I would like a post to finish the discussion once and for all about :

  1. What most commonly do people mean when they "sample uniformly" on $S^{d-1}$, especially in an "applied" context (That is, I assume people talk about this subject because they really need to code and generate observations to be used in some algorithms)?
  2. Assume that we can generate standard uniform distribution. Compactible with question 1, what are some of the easiest, mathematically justified, ways to do it?

This question has been heavily discussed. See for example :

  1. https://stats.stackexchange.com/questions/331253/draw-n-dimensional-uniform-sample-from-a-unit-n-1-sphere-defined-by-n-1-dime?noredirect=1&lq=1
  2. How to write a proper definition of the uniform distribution on unit sphere?
  3. https://mathworld.wolfram.com/HyperspherePointPicking.html
  4. https://stats.stackexchange.com/questions/77440/sampling-uniformly-from-n-sphere-using-spherical-coordinates
  5. https://mathoverflow.net/questions/24688/efficiently-sampling-points-uniformly-from-the-surface-of-an-n-sphere
  6. https://projecteuclid.org/journals/annals-of-mathematical-statistics/volume-43/issue-2/Choosing-a-Point-from-the-Surface-of-a-Sphere/10.1214/aoms/1177692644.full
  7. Even the wikipedia has a little section about it : https://en.wikipedia.org/wiki/N-sphere.

The trouble I am getting is that, question 2 really seems to have a lot of solutions, but most are not preceded by any discussion on question 1 whatsoever (and the solutions is often not very mathematically justified either). Even published papers do not seem to be very clear about this issue.

In my opinion, the uniform distribution for a finite measure space $(X, F_X, \nu)$ where $\nu(X) > 0$ and every singleton is measurable should be the probability measure $P$ such that its density $f$ with respect to $\nu$ is constant. For example, the non-disputed $d=1$ case satisfies $X = \{1,-1\}$, $\nu$ equals the counting measure, and $f(x) \equiv \frac{1}{2}$ for every $x$. The uniform distribution on an interval has $f$ constantly equal to inverse of the length with $\nu$ being the Lebesgue measure. (Although, sometimes, when the space is $\sigma$-finite like $X = \mathbb{R}$ and $\nu$ equals the Lebesgue measure, there are cases where we call $f\equiv 1$ uniform as well. I guess this does not apply to this problem. If $\nu(X)=0$, the uniform distribution should be left undefined.)

Therefore, the problem should lie on what is $\nu$ when $X = S^{d-1}$. The most popular choice still seems to be the surface measure (defined say on Folland's Analysis theorem 2.49). After all, this is the measure that is always used for polar coordinates in $\mathbb{R}^d$. In this case:

\begin{equation*} \nu(S^{d-1}) = \frac{2 \pi^{\frac{d}{2}}}{\Gamma(\frac{d}{2})} \end{equation*}

The uniform distribution would be :

\begin{equation*} f(x) \equiv \frac{\Gamma(\frac{d}{2})}{2 \pi^{\frac{d}{2}}} \end{equation*}

This all sounds reasonable and conventional (if this is not the case, please point out). However, the several most popular solutions do not seem to be using it. Assume from now on that $d \geqslant 2$. I give some solutions I am most interested in, be them right or wrong :

  1. Use spherical coordinates.

\begin{align*} x_1 &= \cos(\phi_1)\,,\\ x_2 &= \sin(\phi_1)\cos(\phi_2)\,,\\ &\quad\quad\vdots\\ x_{d-1} &= \sin(\phi_1)\cdots\sin(\phi_{d-2})\cos(\phi_{d-1})\,,\\ x_d&=\sin(\phi_1)\cdots\sin(\phi_{d-2})\sin(\phi_{d-1})\,,\\ \end{align*}

Here $\phi_{d-1} \in [0,2\pi)$ and all the rest are in $[0, \pi]$. Generate $\phi_{d-1}$ from $\text{Uniform}[0,2\pi)$ and the rest from $\text{Uniform}[0,\pi]$ all independently, and use the formula above to get a sample of $x$. People seem to criticize this for $d \geqslant 3$ only (e.x https://angms.science/doc/RM/randUnitVec.pdf). If we use surface measure, this seem to be a good way?

  1. The same as in solution 1, but proceed as in the answer in the very first reference. I am not sure where that method comes from.

From the surface measure, one might get the solution 1 or 2 I just wrote about, although I have difficulty filling in the details. But if the problem is already solved with 1 or 2, why so many methods and discussions?

  1. Generate $Y_1, \ldots, Y_d$ independent identically distributed from $N(0,1)$, and put, for each $i$ :

\begin{equation*} X_i = \frac{Y_i}{\sqrt{Y_1^2 + \cdots + Y_d^2}} \end{equation*}

Then $X = (X_1, \ldots, X_d)$ will be a uniform sample on $S^{d-1}$. The symmetry seems intuitive, but I do not know how to justify it or whether it is compactible with the surface measure definition.

I am even more lost at other methods.


Thank you for all your answers.

Conclusion :

The default uniform distribution on $S^{d-1}$ is the https://en.wikipedia.org/wiki/Spherical_measure . Despite many names along with the implied different constructions ( for instance normalized surface measure, normalized $d-1$ dimensional Hausdorff measure or Haar measure on a hypersphere ), they are one and the same, because there is only one unique probability measure among all measures on $S^{d-1}$ that is invariant under action of $d \times d$ orthogonal group, see for instance Uniqueness of the uniform spherical distribution . Pursuing the invariance direction, solution 3 provides a probability measure also invariant under orthogonal group, and is thus immediately seen correct by uniqueness. By pursuing along the direction of generating normalized surface measure, solution 2 does the correct computation and is acceptable. Solution 1 forgets about multiplying Jacobian and is incorrect when $d \geqslant 3$.

温泽海
  • 3,335

4 Answers4

12

The uniform distribution would be : \begin{equation*} f(x) \equiv \frac{\Gamma(\frac{d}{2})}{2 \pi^{\frac{d}{2}}} \end{equation*} This all sounds reasonable and conventional

You haven't defined a distribution yet. What does this mean? You are trying to define a probability density function. But those have to be integrated with respect to a measure. What measure?


This is the answer: $S^{d-1}$ has a unique probability measure which is invariant under the action of the group $O(d)$ of rotations and reflections; this measure is called spherical measure and is closely related to Haar measure on $O(d)$.

The uniform distribution on a finite set $X$ has a similar invariance property: namely, it is the unique probability measure which is invariant under all bijections $X \to X$.

(Edit: I thought I had a straightforward plausibility argument about the uniqueness of spherical measure but I don't think it actually works.)

Now, since the distribution of a tuple $(Y_1, \dots Y_d)$ of iid $N(0, 1)$ Gaussians is orthogonally invariant (this follows from the fact that their covariance matrix is orthogonally invariant and that any linear transform of a Gaussian is also a Gaussian), normalizing this distribution so it lands in the unit sphere must produce an orthogonally invariant probability measure on $S^{d-1}$, which by uniqueness must be spherical measure.

By contrast I don't see any reason to expect polar coordinates to produce an orthogonally symmetric measure.

Qiaochu Yuan
  • 468,795
2
  1. Given any $d$-dimensional surface $S \subset \mathbb{R}^n$ (or even any Riemannian manifold $S$), there is a surface measure $\mu$ on $S$, which is defined in local coordinates by $\mu(dx) = \sqrt{g(x)}\,dx$, where $g(x) = \det G(x)$ is the determinant of the metric tensor $G(x)$. More precisely, suppose $\phi : U \to V$ is a coordinate chart with $O$ open in $\mathbb{R}^d$ and $U$ open in $S$. If $A \subset U$, then $$\mu(A) = \int_{O}1_A(\phi(x))\sqrt{g(x)}\,dx.$$ For example, the "surface area" $4\pi$ of $S^2$ computed in calculus is $\mu(S^2)$. The uniform measure $\nu$ on $S$ is just the normalization of $\mu$ to a probability measure, i.e. $\nu(A) = \frac{1}{\mu(S)}\mu(A)$. I don't have a good reference, but one reference is page 119 to 126 of https://mtaylor.web.unc.edu/wp-content/uploads/sites/16915/2018/04/analmv.pdf
Mason
  • 12,787
1

Your first method does not give a correct uniform sampling of the hypersphere. By this, I mean if you have a generic function $f$ defined on the sphere: $$ \int fd\mu \neq \langle f(x)\rangle $$ with $\mu$ the uniform measure that you want to sample. Instead, you need to add the factor: $$ \int fd\mu = \langle f(x_1,...,x_{d-1})\sin^{d-2}\phi_1\sin^{d-3}\phi_2...\sin\phi_{d-2}\rangle $$ You can check this for the case of a constant, $f=1$: $$ \int fd\mu = \frac{\Gamma(d/2)}{2\pi^{d/2}}\\ \langle f(x_1,...,x_{d-1})\rangle = 2\pi^{d-1} $$

You can build intuition already in 3D. The correct measure is: $$ d\mu = \sin\theta d\theta d\phi \\ d\mu \neq d\theta d\phi $$ the latter being the naive measure.

In general, given the coordinates $u_1,...,u_{d-1}$ of your surface with corresponding basis vector: $$ e_k = \frac{\partial x}{\partial u_k} $$ then the area measure is: $$ d\mu = |\det(e_1,...,e_{d-1},n)|du_1...du_{d-1} $$ with $n$ the unit normal vector. In terms of sampling, this means that: $$ \int fd\mu \neq \langle f(x)|\det(e_1,...,e_{d-1},n)|\rangle $$ with $x(u_1,...u_{d-1})$ is given by the parametrisation of the surface and the $u_1,...,u_{d-1}$ are sampled uniformly according to the Lebesgue measure on their domain $U\subset\mathbb R^{d-1}$.

This was the extrinsic approach, you can also have intrinsic approaches say if you are given only the metric of Riemann manifold for example. In the case of symmetric surfaces like the sphere, hyperbolic plane etc., the extrinsic approach is redundant but keeps the symmetries apparent and are more convenient for actual computations.

LPZ
  • 6,049
1

Regarding the definition

This is a very natural question to ask, because the word "uniform" has multiple meanings. For instance, the nature of uniformity when considering a set of n points, a segment or a bounded surface are different. The underlying idea is the same in the three cases: there is some "natural" way of measuring (counting, measuring lengths and measuring areas respectively), and you want your probability distribution to align proportionally to this measure. For instance, sampling latitude and longitude uniformly from their respective intervals is not uniform on the sphere, because even if there is an "argument", this distribution does not align linearly with the measure of area.

So it all boils down to defining what a natural measure can be, and the answer to this comes from two parts. First, the euclidean metric is the natural metric by all means. Second, the concept of Hausdorff measure and dimension lets you define a metric-based measure on arbitrary metric spaces. For the case of your hollow sphere, the Hausdorff dimension is an integer ($d-1$), in which case the measure coincides with the $d-1$ Lebesgue measure, i.e. counting points if $d-1=0$, measuring lengths if $d-1=1$, etc.

Regarding the algorithm

There are many algorithms for generating the exact same distribution. What people mean when they "sample from the sphere uniformly" is to generate the samples with any of the equivalent methods that there exist. I wrote a paper that explains the method based on the Gaussian distribution, but it also shows yet another one, which is perhaps more natural: Pick one point uniformly from the hollow volume inside the hypersphere and project it to the surface. For sampling the point inside the sphere uniformly, you can sample repeatedly from a larger hypercube until you obtain a sample that falls inside. This method is also equivalent to the others, but it differs and is more sound (surprisingly) than the others when you change to other, less natural, p-norms.