A simple way to sample a point uniformly in the unit -ball is to draw a point uniformly in the cube and accept it only if its norm is at most . Find the acceptance probability as a function of and describe how it behaves for .
Reveal solutionHide solution
#The acceptance probability
A point drawn uniformly in the cube lands inside the ball with probability equal to the ratio of their volumes,
#It collapses with dimension
The ball's volume eventually shrinks while the cube's volume keeps doubling, so races to zero. A clean recursion is , starting from .
#In code
import numpy as np
from scipy.special import gammaln
d = np.arange(1, 51)
log_p = (d / 2) * np.log(np.pi) - gammaln(d / 2 + 1) - d * np.log(2)
accept = np.exp(log_p)#Read it off
By the acceptance is already about , and by near . Past a handful of dimensions, rejection from the cube is hopeless, the curse of dimensionality in miniature, and one reaches instead for a direct method like normalizing a Gaussian vector.