I am trying to implement algorithm 2 and would like to know a simple way to randomly sample points from the unit sphere in d-dimensional Euclidean space which I can code up in python.
Use a distibution that's symmetric around the origin and normalize the results so they lie on the sphere. E.g. you can use a Gaussian. Here's some code.
def sample_sphere_uniformly(d, n_samples):
"""Sample uniformly from d-sphere.
Args:
d: dimension of sphere
n: number of sample points to generate
Returns:
array of shape (n, d+1)
"""
import numpy as np
x = np.random.normal(0, 1, (n_samples, d+1))
return x / np.linalg.norm(x, axis=1, keepdims=True)