I'm caring monte-carlo simulations and I am checking my code for some faults. I have just realized (the hard way) that to generate a unit vector pointing in a random direction I cannot simply pick 3 (or any other number) random number and normalize them. I didn't understand why I can't do this apart from the claim that the generated vector doesn't have even probability to point at any direction. I can't find the math behind there and I suspect that this is mainly due to lack of proper term to search. What should I search for? Where can I find explanation? Preferably without assumption of prior knowledge.
|
migrated from physics.stackexchange.com Dec 19 '12 at 19:47
|
A good way to get a random direction on a 2-sphere might be to choose $z$ uniform in $[-1,1]$, and $\theta$ uniform in $[0,2\pi]$. Then take the point $$ (\sqrt{1-z^2} \cos \theta, \sqrt{1-z^2} \sin \theta, z).$$ I won't do the math to show why these give points uniformly on a sphere. It's not hard. For large dimensions, the best way is probably to choose $n$ samples from a Gaussian distribution, $x_1 \ldots, x_n$, and normalize the resulting vector $(x_1, x_2, \ldots,x_n)$. You can see why this works by looking at the probability density function. The function for a Gaussian is $$\frac{1}{\sqrt{2 \pi}} e^{-\frac{1}{2}x^2},$$ so when you multiply $n$ of them you get $$\frac{1}{(2 \pi)^{n/2}} e^{-\frac{1}{2}\sum_i x_i^2},$$ which is clearly spherically symmetric. |
||||
|
|
|
If you choose 3 independent random numbers between -1 and 1, you are basically choosing a point inside a cube. From that image, you can easily see that, after normalization, the likelihood of your vector pointing along the cube main diagonal is $\sqrt{3}$ larger than that of it pointing along the $x$ axis, simply because there are $\sqrt{3}$ more points inside the cube along the main diagonal than along the direction of any coordinate axis. As Jerry Schirmer points out, you can take two angles, and build your vector from there, basically using spherical coordinates. The idea is also developed here. Alternatively, you could still generate your three uniform random numbers, and before normalizing them, get rid of them if $x^2 + y^2 + z^2 > 1$, thus effectively limiting your 3-D point to within a sphere. As for general reading on the subject, you want to look for sampling an arbitrary distribution, although most of what you will find will be for one-dimensional distributions, probably the most general method being inverse transform sampling. |
|||||||||||
|