【问题标题】:Sampling Uniformly within the Unit Circle在单位圆内均匀采样
【发布时间】:2018-04-10 08:20:24
【问题描述】:

我想从 ∥x∥ ≤ 1 的均匀分布中采样一个二维向量 x。我知道我可以从均匀分布中采样为 p>

numpy.random.uniform(0.0, 1.0, 2)

但我怎样才能确保 ∥x∥ ≤ 1?

【问题讨论】:

    标签: python uniform-distribution


    【解决方案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。 举例来说,这是没有平方根的随机结果的结果: 并使用平方根:

    【讨论】:

    • 这不是统一的。转换不是线性的。
    • @iguarna,你是对的。我已经编辑了答案。谢谢!
    • 如何将其扩展到 n 维?
    【解决方案2】:

    像上面那样对角度和长度进行采样并不能保证从圆中进行均匀采样。在这种情况下,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)
    

    【讨论】:

      猜你喜欢
      • 2011-03-01
      • 2012-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多