Random Points on a Sphere
jasondavies.com
jasondavies.com
This works because the normal distribution is rotationally symmetric, so each sample is essentially a random unit vector scaled by a random (normally distributed) radius. Once you remove the variation in radius you just get back the random unit vector, i.e. a point on the unit sphere.
@ginko's comment is correct - you can fix the algorithm by throwing out any points that lie outside the sphere before normalizing.
But that pdf is also the joint pdf of N i.i.d. gaussians, evident by decomposing f=a * exp(-x1^2) * ... * exp(-xN^2) [2], which is the joint pdf x1,...xN s.t. fx1=c * exp(-x1^2), ..., fxn=c * exp(-xN^2).
[1] Since exp(-R^2) does not depend on direction but only on distance from the origin
[2] The fact that f(x1,...xN)=f1 * ... * fN if x1,...xN are independent follows directly from the fact that P(A & B) = P(A)*P(B) if A and B are independent events.
One of the advantages of the normal-sampling route over @ginko's rejection-based method is that in high dimensions almost all of the volume of the unit cube is situated outside of the unit sphere (the volume of the unit cube is always 1, whereas the volume of the unit sphere decreases exponentially with dimension). So the rejection method becomes exponentially slow in high dimensions, while the Gaussian method still works just fine.
Other simple distributions tend to give biases towards the corners or axis. Perhaps the Gaussian is unique in this regard? I'm not sure.
I think I found one, but I'm not sure:
Without loss of generality, take f(xi) = k1 * exp(-g(xi)) [1], for some g. Then we need the joint pdf to satisfy f(x1,...,xn) = k2 * exp(-h(R^2)), R=sum(xi^2)^1/2 (the R^2 and h(.) is w.l.g. too). So we get g(x1)+g(x2)=h(x1^2+x2^2). Then assuming the functions g and h analytic we end up needing g(x)= k * x^2, otherwise we get cross terms in the Taylor expansion that can't be cancelled out for all xi. Sounds good?
[1] The function f trivially needs to be symmetric, justifying no loss of generality.
I wanted to make sure I got a proof, since I didn't really find this elsewhere.
You need to work harder than that, as most of the other comments are pointing out.
V_{2k} = pi^k/k!
For 10 dimensions, the hypersphere is of volume 2.55, but the enclosing box is of volume 2^10 = 1024. So roughly one in 400 trials gets you a point inside the sphere. Not good odds.In terms of CPU power, calculating the length of the vector and doing a branch, I think, is going to be slower then the proper way to do it.
So your solution ends up pretty much using the same math (while probably calling cos a few more times) and throwing away at least one random number and possibly half of them (depending on the implementation of randn).
For example:
https://www.gnu.org/software/gsl/manual/html_node/Spherical-...
[0] http://mathworld.wolfram.com/SpherePointPicking.html [1] http://stats.stackexchange.com/questions/7977/how-to-generat...
This is false. Random variables can be uniform even if they are not independent.
For example, let X be uniform(0, 1), and let Y = X + 0.1 mod 1. Clearly they are not independent. Clearly X is uniform. When you realize that you could redefine the variables as Y = uniform(0, 1) and X = Y - 0.1 mod 1, then it becomes obvious that Y is also uniform.
It is a common problem to know a variable's distribution without knowing whether it is independent.
Let X be uniform(0,1) and let Y=X. Clearly the are not independent. Clearly X is uniform. And it's obvious that Y is also uniform.
The comment you quote means "the points are no longer independent of each other, hence each point is no longer uniformly distributed given the others". In you example, Y doesn't have a uniform distribution for X fixed.
This is how randomness should look. If you randomly generate (using a high-quality random engine) uniformly distributed points on a square plane or even a line, you'll see the same apparent clustering.
Put another way, if you plot every point on a coordinate grid with integer coordinates, you'll have a uniform distribution, but it will hardly be "random."
> One solution is to pick λ ∈ [-180°, 180°) as before and then set φ = cos^(-1)(2x - 1), where x is uniformly distributed and x ∈ [0, 1).
There is a fascinating fact hidden in the above sentence: if a point (x,y,z) is uniformly distributed on the surface of a sphere -- say, of radius 1, centered at (0,0,0) -- then each of x, y, and z is uniformly distributed over the interval [-1, 1].
Honestly, that's amazing.
It's peculiar to the 2-sphere ("ordinary" spheres).
I imagine there is a trend where, in higher dimensions, coordinates have a greater tendency to be near zero?
In a 0-sphere, x is either -1 or +1. In a 1-sphere, each of x, y, is (informally speaking) more likely to be near +/- 1 than near 0. A 2-sphere gives us uniform distribution for each coordinate. So I suppose that the coordinates of a 3-sphere are more likely to be near 0 than near +/- 1, and this tendency is more pronounced, the higher the dimension gets. (?)
n-spheres are funny things. Intuition about them is often misleading. (See, for example, the comments expressing skepticism about my original uniform-distribution observation.)
----
EDIT. Ooo, some interesting questions here. What is the limiting behavior of the distribution of x on the n-sphere, as n -> +infinity? I imagine the graph looks like a narrower & narrower spike at x = 0.
Now, suppose we scale the graph of the distribution horizontally by some appropriate function of n. That is, let f_n be the probability distribution of x on the n-sphere. Is there some function g:Z -> R so that the function x -> f_n(x / g(n)) has a limit as n -> +infinity? Does it approach (wild guess) a normal distribution? If so, is this fact (even wilder guess) a special case of some kind of central-limit-theorem-ish statement that holds for geometrical objects?
Thanks for the paper link.
In fact the simplex seems to be the worst case for convex objects, in terms of concentration of distribution near the center. And the best case should be the sphere. Which plays out nicely since the simplex seems to be the most "concavey" convex shape of a given 'diameter' is the sphere is the most "convexey" convex shape of a given 'diameter', no?
One consequence is that yet another way to pick a point uniformly on the sphere is to choose z ∈ [-1, 1] uniformly, then choose λ ∈ [-180°, 180°) uniformly and use the point
(cos(λ)*sqrt(1-z*z), sin(λ)*sqrt(1-z*z), z)[1] https://www.cse.cuhk.edu.hk/~ttwong/papers/udpoint/udpoint.p...
("choose a random chord intersecting a circle")
If someone is familiar with this kind of thing, I'd like to know a little more about the Jaynes solution described. It seems to me that if the circle were translated off to some distant portion of the plane, it would be very unlikely that any method of choosing chords would have the same distributional properties on the original circle and on the translated circle, because the chords are all originally restricted to intersect the first circle and, given two circles in a plane, there are many lines that intersect one but not both circles. Will Jaynes' method really shade the translated circle the same way it will shade the original? My intuition says the translated one should be shaded more heavily in a sort of "band" parallel to the line connecting the origins of the two circles, or possibly in a crescent pattern with more shading closer to the original.
> Choose a diameter of the circle, choose a point on the diameter and construct the chord through this point and perpendicular to the diameter.
In the general case, choose a line going through the origin.
=> In the circle, there is a diameter parallel to this line.
Then choose a point on the line, and construct the perpendicular line at that point. (To be able to choose a point uniformly, you have to consider a finite segment but this is not a problem as long as the location/size of the circle is bounded).
=> This perpendicular line might not intersect the circle, but when it does it determines a chord that goes through a point chosen uniformly on the diameter.
This generalized method can be used to cover uniformly multiple circles at the same time, but obviously not every line will intersect every circle.
http://www.openprocessing.org/sketch/41142
n = number of points
phi = (sqrt(5)+1)/2 - 1
ga = phi * 2 * PI
for each point i (1..n)
longitude = ga*i
latitude = asin(-1 + 2*i/n)https://en.wikipedia.org/wiki/Von_Mises%E2%80%93Fisher_distr...
And your method is extremely complicated. You can solve this problem with 5 lines of code and no data structures.