两个磁盘交叉区域的统一采样

时间:2017-11-26 13:55:25

标签: math random geometry

鉴于2D统一变量,我们可以在单位磁盘中生成均匀分布,如here所述。

我的问题类似于我希望统一采样两个相交磁盘的交叉区域,其中一个磁盘始终是单元磁盘,另一个磁盘可以自由移动和调整大小,如here

enter image description here

我试图将该区域划分为两个区域(如上所述),并根据所考虑的磁盘对每个区域进行单独采样。我的方法基于上面引用的统一磁盘算法。为了对中心线右边的第一个区域进行采样,我将θ限制在两个交叉点内。接下来的r需要根据theta进行预测 这样点就会被推到我们的中线和磁盘半径之间的区域。可以找到python示例代码here

u = unifrom2D()
A;B; // Intersection points
for p in allPoints
    theta = u.x * (getTheta(A) - getTheta(B)) + getTheta(B)
    r = sqrt(u.y + (1- u.y)*length2(lineIntersection(theta)))  
    p = (r * cos(theta), r * sin(theta))

然而,这种方法相当昂贵,并且进一步无法保持一致性。只是为了澄清我不想使用拒绝抽样。

1 个答案:

答案 0 :(得分:3)

我不确定这是否比拒绝采样更好,但这里是一个对圆段(中心角<= pi)进行均匀采样的解决方案,涉及反函数的数值计算。 (两个圆的交点的均匀采样可以由段,扇区和三角形的采样组成 - 取决于交叉点如何分成更简单的数字。)

首先,我们需要知道如何使用给定的分布Z生成随机值F,即我们想要

P(Z < x) = F(x)                   <=>  (x = F^-1(y))
P(Z < F^-1(y)) = F(F^-1(y)) = y   <=>  (F is monotonous)
P(F(Z) < y) = y

这意味着:如果Z具有请求的分发F,那么F(Z)将统一分发。反过来说:

Z = F^-1(Y), 

其中Y[0,1]中统一分配,具有请求的分发。

如果F的格式为

       / 0,                             x < a
F(x) = | (F0(x)-F0(a)) / (F0(b)-F0(a)), a <= x <= b
       \ 1,                             b < x

然后我们可以在Y0中统一选择[F(a),F(b)]并设置Z = F0^-1(Y0)

我们选择按(theta,r)对细分进行参数化,其中中心角theta是从一个细分侧测量的。当片段的中心角为alpha时,片段的区域与片段开始处的角度theta的扇区相交(对于theta中的单位圆[0,alpha/2] })

F0_theta(theta) = 0.5*(theta - d*(s - d*tan(alpha/2-theta)))

enter image description here 其中s = AB/2 = sin(alpha/2)d = dist(M,AB) = cos(alpha/2)(圆心与细分的距离)。 (案例alpha/2 <= theta <= alpha是对称的,此处不予考虑。) 我们需要随机theta P(theta < x) = F_theta(x)F_theta的倒数不能用符号计算 - 它必须由某种优化算法(例如Newton-Raphson)确定。

修复theta后,我们需要

范围内的随机半径r
[r_min, 1], r_min = d/cos(alpha/2-theta).

对于x中的[0, 1-r_min],分发必须为

F0_r(x) = (x+r_min)^2 - r_min^2 = x^2 + 2*x*r_min.

这里可以用符号计算逆:

F0_r^-1(y) = -r_min + sqrt(r_min^2+y)

这是Python中用于概念验证的实现:

from math import sin,cos,tan,sqrt
from scipy.optimize import newton

# area of segment of unit circle
# alpha: center angle of segment (0 <= alpha <= pi)
def segmentArea(alpha):
  return 0.5*(alpha - sin(alpha))

# generate a function that gives the area of a segment of a unit circle
# intersected with a sector of given angle, where the sector starts at one end of the segment. 
# The returned function is valid for [0,alpha/2].
# For theta=alpha/2 the returned function gives half of the segment area.
# alpha: center angle of segment (0 <= alpha <= pi)
def segmentAreaByAngle_gen(alpha):
  alpha_2 = 0.5*alpha
  s,d = sin(alpha_2),cos(alpha_2)
  return lambda theta: 0.5*(theta - d*(s - d*tan(alpha_2-theta)))

# generate derivative function generated by segmentAreaByAngle_gen
def segmentAreaByAngleDeriv_gen(alpha):
  alpha_2 = 0.5*alpha
  d = cos(alpha_2)
  return lambda theta: (lambda dr = d/cos(alpha_2-theta): 0.5*(1 - dr*dr))()

# generate inverse of function generated by segmentAreaByAngle_gen
def segmentAreaByAngleInv_gen(alpha):
  x0 = sqrt(0.5*segmentArea(alpha)) # initial guess by approximating half of segment with right-angled triangle
  return lambda area: newton(lambda theta: segmentAreaByAngle_gen(alpha)(theta) - area, x0, segmentAreaByAngleDeriv_gen(alpha))

# for a segment of the unit circle in canonical position
# (i.e. symmetric to x-axis, on positive side of x-axis)
# generate uniformly distributed random point in upper half
def randomPointInSegmentHalf(alpha):
  FInv = segmentAreaByAngleInv_gen(alpha)
  areaRandom = random.uniform(0,0.5*segmentArea(alpha))
  thetaRandom = FInv(areaRandom)
  alpha_2 = 0.5*alpha
  d = cos(alpha_2)
  rMin = d/cos(alpha_2-thetaRandom)
  secAreaRandom = random.uniform(0, 1-rMin*rMin)
  rRandom = sqrt(rMin*rMin + secAreaRandom)
  return rRandom*cos(alpha_2-thetaRandom), rRandom*sin(alpha_2-thetaRandom)

可视化似乎验证了均匀分布(中心角为pi/2的段的上半部分):

import matplotlib.pyplot as plot
segmentPoints = [randomPointInSegmentHalf(pi/2) for _ in range(500)]
plot.scatter(*zip(*segmentPoints))
plot.show()

enter image description here