3

I want to uniformly generate two $n$-dimensional orthogonal vectors $\mathbf a, \mathbf b \in \mathbb R^n$ on a unit $n$-dimensional sphere. In other words, the vectors should satisfy $$ \begin{cases} a_1^2 + a_2^2 + \dotsb + a_n^2 = 1, \\ b_1^2 + b_2^2 + \dotsb + b_n^2 = 1, \\ a_1 b_1 + a_2 b_2 + \dotsb a_n b_n = 0. \end{cases} $$

The problem for me is to generate them uniformly over their set.

If $n=2$, I would generate like this:

  1. Generate a uniform angle $\varphi \in [0, 2\pi]$.
  2. Set $\mathbf a = (\cos \varphi, \sin \varphi)$.
  3. Uniformly generate angle $\theta \in \{ \varphi + \pi/2, \varphi - \pi/2 \}$ (only two values to generate from).
  4. Set $\mathbf b = (\cos \theta, \sin \theta)$.

For $n=3$, the situation already becomes more complicated...

  • The first idea was to generate just two vectors $\mathbf a'$ and $\mathbf b'$ that are not parallel (which is almost for sure, otherwise we can just re-generate them) and then just apply Gram-Schmidt process on them. However, I am far from sure they will be uniformly distributed... – Yauhen Yakimenka Apr 24 '20 at 02:36
  • 2
    This is everything you need: http://extremelearning.com.au/how-to-generate-uniformly-random-points-on-n-spheres-and-n-balls/ – Quillo Apr 24 '20 at 04:49
  • Okay, this gives a lot indeed, @mrcntn Combined with angryavian, it indeed answers my question in full. – Yauhen Yakimenka Apr 24 '20 at 10:58

3 Answers3

2

I'm not sure if this suits your purposes but here's an idea. This works well if you are intending to implement a computer algorithm.

Choose a vector $v$ uniformly in a cube centered at the origin $[-1,1]^n$. This means that you only have to choose the Cartesian components independently and uniformly on $[-1,1]$. Check the magnitude of $v$. If it's less than 1, keep it. Else, throw it away and run the same procedure over and over until it is. Now, normalize $v$ onto the unit sphere. It is clear that this scheme works better for low-dimensional spheres since the ratio of the volume of the sphere to that of its circumscribed cube goes to $0$ as the dimension goes to infinity.

Run the same procedure for a vector $u$.

At this point, you have chosen two vectors uniformly on the unit sphere. Now we simply apply the Gram-Schmidt algorithm:

Keeping $v$ fixed, let $w=v-(v\cdot u)u$ and then normalize. $\{v,w\}$ should be what you want.

Anz
  • 144
  • I think I can normalize vector $\mathbf v$ even if norm (is norm=magnitude here?) less than 1. 2) I am not that sure the vectors generated in this way are indeed uniform. Do you have any idea on why that can be the case?
  • – Yauhen Yakimenka Apr 24 '20 at 10:41
  • 1
    @YauhenYakimenka This is a well-known technique, sometimes called a "rejection method." When you initially generate $v,$ the conditional distribution of vectors inside the sphere is uniform because you are conditioning on a subset of a uniform distribution; by rejecting anything that isn't inside the sphere you are left with a uniform distribution inside the sphere. But when $n$ is large you have to reject most of the random vectors in the cube. For $n=4$ you reject more than $69%$ of them, and for $n>4$ it is worse. So I think this is rarely used for more than $2$ or $3$ dimensions. – David K Apr 24 '20 at 11:44
  • 1
    The rejection method will generate vectors uniformly on the sphere. This is well known. My naive expectation is that the joint distribution of the vector and its orthogonal counterpart should be uniform since the entire situation is spherically symmetric (non-rigorous). – Anz Apr 24 '20 at 19:22