I need to calculate the area where two functions overlap. I use regular distributions in this simplified example, but I need a more general procedure that also adapts to other functions.
See the image below to understand what I mean, where the red area is what I want:

This is the MWE I have so far:
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
a = np.random.normal(1., 0.1, 1000)
b = np.random.normal(1., 0.1, 1000)
xmin, xmax = -1., 2.
x_pts = np.mgrid[xmin:xmax:1000j]
ker_a = stats.gaussian_kde(a)
ker_b = stats.gaussian_kde(b)
kde_a = np.reshape(ker_a(x_pts).T, x_pts.shape)
kde_b = np.reshape(ker_b(x_pts).T, x_pts.shape)
sample = ker_a.resample(size=1000)
iso = ker_b(sample)
insample = ker_a(sample) < iso
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()
where I apply Monte Carloto get the integral.
, ker_b(sample) ( ker_a(sample)), , KDE. - , / , 1. ( 1., ).
, ?
def y_pts(pt):
y_pt = min(ker_a(pt), ker_b(pt))
return y_pt
overlap = quad(y_pts, -1., 2.)