我需要计算两个函数重叠的区域.我在这个特定的简化示例中使用了正态分布,但我需要一个更通用的过程来适应其他函数.
请参阅下面的图片,了解我的意思,红色区域是我所追求的:
这是我到目前为止的MWE:
import matplotlib.pyplot as plt import numpy as np from scipy import stats # Generate random data uniformly distributed. a = np.random.normal(1., 0.1, 1000) b = np.random.normal(1., 0.1, 1000) # Obtain KDE estimates foe each set of data. xmin, xmax = -1., 2. x_pts = np.mgrid[xmin:xmax:1000j] # Kernels. ker_a = stats.gaussian_kde(a) ker_b = stats.gaussian_kde(b) # KDEs for plotting. kde_a = np.reshape(ker_a(x_pts).T, x_pts.shape) kde_b = np.reshape(ker_b(x_pts).T, x_pts.shape) # Random sample from a KDE distribution. sample = ker_a.resample(size=1000) # Compute the points below which to integrate. iso = ker_b(sample) # Filter the sample. insample = ker_a(sample) < iso # As per Monte Carlo, the integral is equivalent to the # probability of drawing a point that gets through the # filter. integral = insample.sum() / float(insample.shape[0]) print integral plt.xlim(0.4,1.9) plt.plot(x_pts, kde_a) plt.plot(x_pts, kde_b) plt.show()
我在哪里申请Monte Carlo
获得积分.
这种方法的问题在于,当我使用ker_b(sample)
(或ker_a(sample)
)评估任一分布中的采样点时,我得到的值直接放在 KDE线上.因此,即使是明显重叠的分布,也应该返回非常接近1的公共/重叠区域值.返回小值(任一曲线的总面积为1.因为它们是概率密度估计).
我如何修复此代码以获得预期的结果?
这就是我应用Zhenya的答案
# Calculate overlap between the two KDEs. def y_pts(pt): y_pt = min(ker_a(pt), ker_b(pt)) return y_pt # Store overlap value. overlap = quad(y_pts, -1., 2.)
ev-br.. 6
在图上的红色区域的积分min(f(x), g(x))
,其中f
和g
是你的两个功能,绿色和蓝色.为了评估积分,你可以使用任何积分器scipy.integrate
(当然quad
是默认的积分器) - 或MC集成器,但我不太明白这一点.
在图上的红色区域的积分min(f(x), g(x))
,其中f
和g
是你的两个功能,绿色和蓝色.为了评估积分,你可以使用任何积分器scipy.integrate
(当然quad
是默认的积分器) - 或MC集成器,但我不太明白这一点.