计算两个函数的重叠面积 [英] Calculate overlap area of two functions

查看:243
本文介绍了计算两个函数的重叠面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算两个功能重叠的区域.在此特定的简化示例中,我使用正态分布,但是我还需要一个更通用的过程来适应其他功能.

I need to calculate the area where two functions overlap. I use normal distributions in this particular simplified example, but I need a more general procedure that adapts to other functions too.

请参见下图,以了解我的意思,红色区域是我想要的位置:

See image below to get an idea of what I mean, where the red area is what I'm after:

这是我到目前为止拥有的MWE:

This is the MWE I have so far:

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以获得积分.

where I apply Monte Carlo to obtain the integral.

此方法的问题在于,当我使用ker_b(sample)(或ker_a(sample))评估任一分布中的采样点时,我会直接将值放置在KDE线上.因此,即使是明显重叠的分布,也应该返回非常接近1的公共/重叠区域值.而是返回较小的值(两条曲线的总面积均为1,因为它们是概率密度估计值).

The problem with this method is that when I evaluate sampled points in either distribution with ker_b(sample) (or ker_a(sample)), I get values placed directly over the KDE line. Because of this, even clearly overlapped distributions which should return a common/overlapped area value very close to 1. return instead small values (the total area of either curve is 1. since they are probability density estimates).

如何解决此代码以提供预期的结果?

How could I fix this code to give the expected results?

这就是我运用珍雅答案的方式

This is how I applied Zhenya's answer

# 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.) 

推荐答案

图中的红色区域是min(f(x), g(x))的整数,其中fg是您的两个函数,绿色和蓝色.要评估积分,您可以使用scipy.integrate(我会说quad是默认值)中的任何积分器-当然也可以使用MC积分器,但是我不太明白这一点其中.

The red area on the plot is the integral of min(f(x), g(x)), where f and g are your two functions, green and blue. To evaluate the integral, you can use any of the integrators from scipy.integrate (quad's the default one, I'd say) -- or an MC integrator, of course, but I don't quite see the point of that.

这篇关于计算两个函数的重叠面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆