故事起源于long long ago的时候看到的苏剑林某一篇博客当中提到了一个结论:
当时就对这个结论比较在意了,今天突然地又想到了这个问题,就想着趁着放假把这个结论验证一下。
显然,苏剑林在博客中提到的这个结论的表述是比较随意的,为了更好地定义我们的问题,我们将其细化一下:
要证明这个结论感觉比较难,但是要演示说明一下这个结果确实相对容易的,我们的第一反应就是蒙特卡洛模拟一下,实际生成 N N N组 n n n维空间上的均匀单位向量,然后考察一下他们之间的角度分布。
但是,要做到这件事,我们首先需要生成一下n维空间当中的均匀分布的单位向量。
首先,我们来考察一些简单的情况,即2维和3维空间当中的情况。
二维空间中的均匀分布向量其实就是单位圆上面的均匀分布向量,因此,我们只需要给出一个 0 0 0到 π \pi π之间均匀分布的角度 ϕ \phi ϕ即可得到一个均匀分布的单位向量 v ⃗ = ( s i n θ , c o s θ ) \vec{v} = (sin\theta, cos\theta) v=(sinθ,cosθ)。
对于三维的情况,事实上我相信大部分读者只要熟悉坐标系变换的话就还是很轻易能够写出解答的。
我们极坐标系下的坐标如下:
{ x = r ⋅ s i n θ ⋅ s i n ϕ y = r ⋅ s i n θ ⋅ c o s ϕ z = r ⋅ c o s θ \left \{ \begin{aligned} x & = r\cdot sin\theta \cdot sin\phi \\ y & = r\cdot sin\theta \cdot cos\phi \\ z & = r\cdot cos\theta \end{aligned} \right. ⎩⎪⎨⎪⎧xyz=r⋅sinθ⋅sinϕ=r⋅sinθ⋅cosϕ=r⋅cosθ
进而我们可以得到,单位体积元的表达公式为:
ρ = d x d y d z = r 2 s i n θ d r d θ d ϕ \begin{aligned} \rho & = dxdydz \\ & = r^2sin\theta drd\theta d\phi \end{aligned} ρ=dxdydz=r2sinθdrdθdϕ
可以看到,对于单位面元而言,其具体的表达式为 ρ = C ⋅ s i n θ d θ d ϕ = C ′ ⋅ d c o s θ ⋅ d ϕ \rho = C \cdot sin\theta d\theta d\phi = C' \cdot dcos\theta \cdot d\phi ρ=C⋅sinθdθdϕ=C′⋅dcosθ⋅dϕ。因此,要生成一个均匀分布,我们只需要按照 c o s θ cos\theta cosθ的分布生成一个 θ \theta θ,然后生成一个 0 0 0到 2 π 2\pi 2π上面均匀分布的 ϕ \phi ϕ即可。
给出具体的python实现如下:
import numpy as np
def dummy():
theta = np.arccos(np.random.uniform(-1, 1))
phi = np.random.uniform() * 2 * np.pi
x = np.sin(theta) * np.sin(phi)
y = np.sin(theta) * np.cos(phi)
z = np.cos(theta)
return (x, y, z)
现在,我们来考察n维空间中的情况。
我们仿照3维空间的情况,只要先给出体积元的极坐标表达式,然后考察其中空间角的表达式即可。
给出n维空间下的极坐标转换如下:
{ x 1 = r ⋅ c o s θ 1 x 2 = r ⋅ s i n θ 1 ⋅ c o s θ 2 x 3 = r ⋅ s i n θ 1 ⋅ s i n θ 2 ⋅ c o s θ 3 . . . x n − 1 = r ⋅ s i n θ 1 ⋅ s i n θ 2 ⋅ . . . ⋅ s i n θ n − 2 ⋅ c o s θ n − 1 x n = r ⋅ s i n θ 1 ⋅ s i n θ 2 ⋅ . . . ⋅ s i n θ n − 2 ⋅ s i n θ n − 1 \left\{ \begin{aligned} & x_1 = r \cdot cos\theta_1 \\ & x_2 = r \cdot sin\theta_1 \cdot cos\theta_2 \\ & x_3 = r \cdot sin\theta_1 \cdot sin\theta_2 \cdot cos\theta_3 \\ & ... \\ & x_{n-1} = r \cdot sin\theta_1 \cdot sin\theta_2 \cdot ... \cdot sin\theta_{n-2} \cdot cos\theta_{n-1} \\ & x_n = r \cdot sin\theta_1 \cdot sin\theta_2 \cdot ... \cdot sin\theta_{n-2} \cdot sin\theta_{n-1} \end{aligned} \right. ⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧x1=r⋅cosθ1x2=r⋅sinθ1⋅cosθ2x3=r⋅sinθ1⋅sinθ2⋅cosθ3...xn−1=r⋅sinθ1⋅sinθ2⋅...⋅sinθn−2⋅cosθn−1xn=r⋅sinθ1⋅sinθ2⋅...⋅sinθn−2⋅sinθn−1
可以得到n维空间上的体积元:
d x 1 d x 2 . . . d x n = ∂ ( x 1 , x 2 , . . . , x n ) ∂ ( r , θ 1 , θ 2 , . . . , θ n − 1 ) ) ⋅ d r d θ 1 d θ 2 . . . d θ n − 1 = d e t ∣ ∂ x 1 ∂ r ∂ x 1 ∂ θ 1 . . . ∂ x 1 ∂ θ n − 1 ∂ x 2 ∂ r ∂ x 2 ∂ θ 1 . . . ∂ x 2 ∂ θ n − 1 . . . ∂ x n ∂ r ∂ x n ∂ θ 1 . . . ∂ x n ∂ θ n − 1 ∣ ⋅ d r d θ 1 d θ 2 . . . d θ n − 1 = r n − 1 s i n n − 2 θ 1 s i n n − 3 θ 2 . . . s i n 2 θ n − 3 s i n θ n − 2 ⋅ d r d θ 1 d θ 2 . . . d θ n − 1 \begin{aligned} dx_1dx_2...dx_n & = \frac{\partial(x_1, x_2, ..., x_n)}{\partial(r, \theta_1, \theta_2, ..., \theta_{n-1}))} \cdot drd\theta_1d\theta_2...d\theta_{n-1} \\ \\ & = det \begin{vmatrix} \frac{\partial x_1}{\partial r} & \frac{\partial x_1}{\partial \theta_1} & ... & \frac{\partial x_1}{\partial \theta_{n-1}} \\ \frac{\partial x_2}{\partial r} & \frac{\partial x_2}{\partial \theta_1} & ... & \frac{\partial x_2}{\partial \theta_{n-1}} \\ ... \\ \frac{\partial x_n}{\partial r} & \frac{\partial x_n}{\partial \theta_1} & ... & \frac{\partial x_n}{\partial \theta_{n-1}} \end{vmatrix} \cdot drd\theta_1d\theta_2...d\theta_{n-1} \\ \\ & = r^{n-1}sin^{n-2}\theta_1sin^{n-3}\theta_2...sin^2\theta_{n-3}sin\theta_{n-2} \cdot drd\theta_1d\theta_2...d\theta_{n-1} \end{aligned} dx1dx2...dxn=∂(r,θ1,θ2,...,θn−1))∂(x1,x2,...,xn)⋅drdθ1dθ2...dθn−1=det∣∣∣∣∣∣∣∣∣∂r∂x1∂r∂x2...∂r∂xn∂θ1∂x1∂θ1∂x2∂θ1∂xn.........∂θn−1∂x1∂θn−1∂x2∂θn−1∂xn∣∣∣∣∣∣∣∣∣⋅drdθ1dθ2...dθn−1=rn−1sinn−2θ1sinn−3θ2...sin2θn−3sinθn−2⋅drdθ1dθ2...dθn−1
由此,我们只需要令任意的 θ i \theta_i θi满足分布条件 s i n n − 1 − i θ i d θ i sin^{n-1-i} \theta_i d\theta_{i} sinn−1−iθidθi是均匀分布,即可得到全空间范围内的一个针对空间角均匀分布的随机向量。
当然,这件事并不容易实现就是了。
当然,如果可以令 x i x_i xi是 ( − ∞ , ∞ ) (-\infty, \infty) (−∞,∞)范围内的均匀分布,那么事实上随机生成的向量也是在空间角上均匀的,但是这显然也同样难以实现。
这里,我们给出一个黑科技,即虽然我们无法在 ( − ∞ , ∞ ) (-\infty, \infty) (−∞,∞)范围内生成一个均匀随机分布,但是我们可以退而求其次,如果对于一个n维向量,他的每一个维度上的值都满足正态分布 N ( 0 , 1 ) N(0, 1) N(0,1),那么这样随机生成的向量在任意的n维空间角上也是均匀分布的。
我们考察其在任意n维空间体积元内的概率密度如下:
ρ = Π i = 1 n 1 2 π e − x i 2 2 = ( 1 2 π ) n ⋅ e ∑ i = 1 n x i 2 / 2 = ( 1 2 π ) n ⋅ e r 2 / 2 \begin{aligned} \rho & = \Pi_{i=1}^{n} \frac{1}{\sqrt{2\pi}} e^{-\frac{x_i^2}{2}} \\ & = (\frac{1}{\sqrt{2\pi}})^n \cdot e^{\sum_{i=1}^{n} x_i^2 / 2} \\ & = (\frac{1}{\sqrt{2\pi}})^n \cdot e^{r^2 / 2} \\ \end{aligned} ρ=Πi=1n2π1e−2xi2=(2π1)n⋅e∑i=1nxi2/2=(2π1)n⋅er2/2
可以看到,这个空间体积元上的概率密度只与径向距离r有关,而与空间角无关,因此,上述方式构造的n维向量在n维空间角上是呈现均匀分布的。
而我们将其进行归一化处理,即可得到n维空间当中均匀分布的单位向量。
我们在二维和三维空间当中验证上述方法的有效性如下:
综上,我们即可对n维空间上的单位向量进行随机生成。
那么,我们就可以通过蒙特卡洛生成的方式来考察两个随机向量之间的夹角与维度n之间的变化关系。
我们给出结果图如下:
可以看到:
综上,苏剑林在他的博客当中提到的这个结论就得到了证明,确实是一个非常有意思的结论。