数学杂谈:高维空间向量夹角小记

1. 问题描述

故事起源于long long ago的时候看到的苏剑林某一篇博客当中提到了一个结论:

  • 高维空间中两个随机向量大概率是相互正交的。

当时就对这个结论比较在意了,今天突然地又想到了这个问题,就想着趁着放假把这个结论验证一下。

显然,苏剑林在博客中提到的这个结论的表述是比较随意的,为了更好地定义我们的问题,我们将其细化一下:

  • 对于n维中间的单位球面上的两个随机向量,他们之间的夹角 θ \theta θ在n取大值时,趋近于90度。

要证明这个结论感觉比较难,但是要演示说明一下这个结果确实相对容易的,我们的第一反应就是蒙特卡洛模拟一下,实际生成 N N N n n n维空间上的均匀单位向量,然后考察一下他们之间的角度分布。

但是,要做到这件事,我们首先需要生成一下n维空间当中的均匀分布的单位向量。

2. n维空间中的均匀向量

1. 2维以及3维空间中的特殊情况

首先,我们来考察一些简单的情况,即2维和3维空间当中的情况。

1. 2维空间中的均匀分布向量

二维空间中的均匀分布向量其实就是单位圆上面的均匀分布向量,因此,我们只需要给出一个 0 0 0 π \pi π之间均匀分布的角度 ϕ \phi ϕ即可得到一个均匀分布的单位向量 v ⃗ = ( s i n θ , c o s θ ) \vec{v} = (sin\theta, cos\theta) v =(sinθ,cosθ)

3. 3维空间中的均匀分布向量

对于三维的情况,事实上我相信大部分读者只要熟悉坐标系变换的话就还是很轻易能够写出解答的。

我们极坐标系下的坐标如下:
{ 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=rsinθsinϕ=rsinθcosϕ=rcosθ

进而我们可以得到,单位体积元的表达公式为:
ρ = 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 ρ=Csinθdθdϕ=Cdcosθ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)

2. n维坐标系中的均匀向量

现在,我们来考察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=rcosθ1x2=rsinθ1cosθ2x3=rsinθ1sinθ2cosθ3...xn1=rsinθ1sinθ2...sinθn2cosθn1xn=rsinθ1sinθ2...sinθn2sinθn1

可以得到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,...,θn1))(x1,x2,...,xn)drdθ1dθ2...dθn1=detrx1rx2...rxnθ1x1θ1x2θ1xn.........θn1x1θn1x2θn1xndrdθ1dθ2...dθn1=rn1sinn2θ1sinn3θ2...sin2θn3sinθn2drdθ1dθ2...dθn1

由此,我们只需要令任意的 θ i \theta_i θi满足分布条件 s i n n − 1 − i θ i d θ i sin^{n-1-i} \theta_i d\theta_{i} sinn1iθidθi是均匀分布,即可得到全空间范围内的一个针对空间角均匀分布的随机向量。

当然,这件事并不容易实现就是了。

当然,如果可以令 x i x_i xi ( − ∞ , ∞ ) (-\infty, \infty) (,)范围内的均匀分布,那么事实上随机生成的向量也是在空间角上均匀的,但是这显然也同样难以实现。

3. 正态分布的巧妙应用

这里,我们给出一个黑科技,即虽然我们无法在 ( − ∞ , ∞ ) (-\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π 1e2xi2=(2π 1)nei=1nxi2/2=(2π 1)ner2/2

可以看到,这个空间体积元上的概率密度只与径向距离r有关,而与空间角无关,因此,上述方式构造的n维向量在n维空间角上是呈现均匀分布的。

而我们将其进行归一化处理,即可得到n维空间当中均匀分布的单位向量。

我们在二维和三维空间当中验证上述方法的有效性如下:

在这里插入图片描述

3. n维空间中两向量夹角考察

综上,我们即可对n维空间上的单位向量进行随机生成。

那么,我们就可以通过蒙特卡洛生成的方式来考察两个随机向量之间的夹角与维度n之间的变化关系。

我们给出结果图如下:

在这里插入图片描述

可以看到:

  • 对于任意维度n,两个随机向量之间的夹角 θ \theta θ的均值都是90度;
  • 随着维度n的增加,夹角 θ \theta θ的标准差分布逐渐减小,最终收敛到0,即在高维空间当中,两个向量的夹角将会非常接近于90度。

4. 总结 & 思考

综上,苏剑林在他的博客当中提到的这个结论就得到了证明,确实是一个非常有意思的结论。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值