计算两个函数的重叠区域

 徘徊在堕落边缘的魔鬼 发布于 2023-02-12 19:43

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

请参阅下面的图片,了解我的意思,红色区域是我所追求的:

在此输入图像描述

这是我到目前为止的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)),其中fg是你的两个功能,绿色和蓝色.为了评估积分,你可以使用任何积分器scipy.integrate(当然quad是默认的积分器) - 或MC集成器,但我不太明白这一点.

1 个回答
  • 在图上的红色区域的积分min(f(x), g(x)),其中fg是你的两个功能,绿色和蓝色.为了评估积分,你可以使用任何积分器scipy.integrate(当然quad是默认的积分器) - 或MC集成器,但我不太明白这一点.

    2023-02-12 19:45 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有