【发布时间】:2018-04-10 08:20:24
【问题描述】:
我想从 ∥x∥ ≤ 1 的均匀分布中采样一个二维向量 x。我知道我可以从均匀分布中采样为 p>
numpy.random.uniform(0.0, 1.0, 2)
但我怎样才能确保 ∥x∥ ≤ 1?
【问题讨论】:
标签: python uniform-distribution
我想从 ∥x∥ ≤ 1 的均匀分布中采样一个二维向量 x。我知道我可以从均匀分布中采样为 p>
numpy.random.uniform(0.0, 1.0, 2)
但我怎样才能确保 ∥x∥ ≤ 1?
【问题讨论】:
标签: python uniform-distribution
可以通过随机化角度和长度并将它们转换为笛卡尔坐标来完成:
import numpy as np
length = np.sqrt(np.random.uniform(0, 1))
angle = np.pi * np.random.uniform(0, 2)
x = length * np.cos(angle)
y = length * np.sin(angle)
编辑:iguarna 对原始答案的评论是正确的。为了均匀地绘制一个点,length 需要是绘制的随机数的平方根。
可以在此处找到对它的引用:Simulate a uniform distribution on a disc。
举例来说,这是没有平方根的随机结果的结果:
并使用平方根:
【讨论】:
像上面那样对角度和长度进行采样并不能保证从圆中进行均匀采样。在这种情况下,P(a) > P(b) if ||a|| > ||b||。为了从一个圆圈中均匀采样,请执行以下操作:
length = np.random.uniform(0, 1)
angle = np.pi * np.random.uniform(0, 2)
x = np.sqrt(length) * np.cos(angle)
y = np.sqrt(length) * np.sin(angle)
【讨论】: