Choosing 3M points on a 300-dimensional sphere
mikesmathpage.wordpress.com
mikesmathpage.wordpress.com
Chebyshev's inequality shows that the probability that the dot product be greater than 0.3 in absolute value is less than 4%.
x = np.random.randn(300)
x /= np.sqrt(np.sum(x*x))
To see why consider that the joint pdf depends solely on the vector's distance from the origin.Edit: yeah oops, I goofed and I'm wrong. I didn't notice it was randn - normally distributed. No culling necessary.
Or do you mean more that it sucks pretty bad to generate 300 rands and then throw them away, even once?
You are going to choose 300 random numbers between -1 and 1, and then throw away this batch if their norm is greater than 1. As you increase dimension (in this case 300), you are including more numbers that give a non-negative contribution to this norm, and so you are increasing your chance of rejection with each dimension.
Re-reading your comment, I notice you made a higher-level note regarding the volume of the hypercube vs. hypersphere. While my comment was right, I now think you are right that the volume ratio of hyper sphere to hypercube is, in fact, always over 0.5. I'm gonna run a quick simulation anyway, but you can convince yourself of this by considering that the line segment connecting (1,0) and (0,1) in 2D lies within the unit circle, but a similar construction will always work for high dimensions (I think).
Jeez, my intuition stinks. I initially thought it was obvious why the 3D sphere to unit cube ratio is > 0.5, by analogy to the 2d diamond embedded in a square, but in 3D the embedded octagon volume isn't 0.5. Obvious now, but that'll teach me not to guess.
Indeed, rejection sampling the 300 dimensional unit sphere will suck really bad.
Say we forget about dimensions and hyperspheres for a moment and just talk about quizzes. Specifically, a quiz of 300 true-false questions.
We have two students fill out the quiz at random, by flipping a coin. What's the probability that they answer all the questions the same way? 2^300 - vanishingly small. Similarly for answering all the questions in exactly the opposite way.
If we only have two or three dimensions, that's like a quiz with only two or three questions. If the possible answers to the questions are constrained, it's fairly likely that some students would get similar results just by chance. But the number of combinations goes up exponentially as we add more questions.
Secondly, I don't understand by which measure the distribution of these vectors would be uniformly distributed or random. Normalizing the vectors to get the coordinates on the unit hypersphere is only ever going to land on 2^n different coordinates. Clearly in low dimension this wouldn't be a good way to select random points on the sphere…
Does his approach make sense only because 2^300 is so much larger than 3 million?
The points are going to be on a hypersphere, just not the unit one. The radius will be about 17, right?
> Does his approach make sense only because 2^300 is so much larger than 3 million?
It has to be larger, I don't think it needs to be crushingly larger. As long as you're still sparse, all those extra digits on your random numbers don't matter. You can round it all the way to a single bit. Each dimension is only a tiny factor on your position on the sphere, so you get perfectly good coverage for something like dot products.
The sphere part is a little confusing at first, but it's really just a fancy (and yes, elegant) way of specifying a constraint on the n-tuple. Given some radius R, it defines the constraint sum(n_i^2) = R^2 for i=1..300.
It might be useful to normalize R, and it is convenient to select R=300 so that the average contribution of each n_i^2 term is 1. At this point, you just have to pick 3M n-tuples. I would probably use a random number generator to a) fill in the n-tuple, b) adjust a constant factor to fit with real world R, and c) repeat 3M times.