单位圆内均匀采样
Sampling Uniformly within the Unit Circle
我想从具有∥x∥≤1的均匀分布中采样一个二维向量x。我知道我可以来自均匀分布的样本
numpy.random.uniform(0.0, 1.0, 2)
但是我怎样才能确保∥x∥≤1?
可以通过随机化角度和长度并将它们转换为笛卡尔坐标来完成:
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) 如果 ||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)
我想从具有∥x∥≤1的均匀分布中采样一个二维向量x。我知道我可以来自均匀分布的样本
numpy.random.uniform(0.0, 1.0, 2)
但是我怎样才能确保∥x∥≤1?
可以通过随机化角度和长度并将它们转换为笛卡尔坐标来完成:
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) 如果 ||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)