Overlapping probability of two normal distribution with scipy
Question: i have two scipy.stats.norm(mean, std).pdf(0) normal distribution curve and i
am trying to find out the differential (overlapping) of the two curves.

How do i calculate it with scipy in Python? Thanks


Answer: You can use the answer suggested by @duhalme to get the intersect and then use
this point to define the range of integral limits,

[![enter image description
here](http://i.stack.imgur.com/7xtWy.png)](http://i.stack.imgur.com/7xtWy.png)

Where the code for this looks like,

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.stats import norm
    norm.cdf(1.96)
    
    def solve(m1,m2,std1,std2):
      a = 1/(2*std1**2) - 1/(2*std2**2)
      b = m2/(std2**2) - m1/(std1**2)
      c = m1**2 /(2*std1**2) - m2**2 / (2*std2**2) - np.log(std2/std1)
      return np.roots([a,b,c])
    
    m1 = 2.5
    std1 = 1.0
    m2 = 5.0
    std2 = 1.0
    
    #Get point of intersect
    result = solve(m1,m2,std1,std2)
    
    #Get point on surface
    x = np.linspace(-5,9,10000)
    plot1=plt.plot(x,norm.pdf(x,m1,std1))
    plot2=plt.plot(x,norm.pdf(x,m2,std2))
    plot3=plt.plot(result,norm.pdf(result,m1,std1),'o')
    
    #Plots integrated area
    r = result[0]
    olap = plt.fill_between(x[x>r], 0, norm.pdf(x[x>r],m1,std1),alpha=0.3)
    olap = plt.fill_between(x[x<r], 0, norm.pdf(x[x<r],m2,std2),alpha=0.3)
    
    # integrate
    area = norm.cdf(r,m2,std2) + (1.-norm.cdf(r,m1,std1))
    print("Area under curves ", area)
    
    plt.show()
    

The cdf is used to obtain the integral of the Gaussian here, although symbolic
version of the Gaussian could be defined and `scipy.quad` employed (or
something else). Alternatively, you could use a Monte Carlo method like this
[link](http://rpsychologist.com/calculating-the-overlap-of-two-normal-
distributions-using-monte-carlo-integration) (i.e. generate random numbers and
reject any outside the range you want).

