title: Python: Overlap between two functions (PDF of kde and normal)
body: <p>Short summary: <strong>Im trying to figure out how to calculate overlap between two functions. One is a gaussian, the other is a kernel density, based on data. Then, I would like to make a small algorithm that choose the mean and variance for the gaussian, which maximises overlap</strong></p>
  
  <p>Firstly, needed imports:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  import seaborn as sns
  from scipy.stats.kde import gaussian_kde
  import scipy
  </code></pre>
  
  <p>I have some data which is approximately normal (somewhat heavy right-tail). I calculate the kernel density, cdf and pdf of this data <em>(in this example, the data is drawn from a uniform, as i can't supply the real data)</em> like this: </p>
  
  <pre><code>def survivalFunction():
  
      data = np.random.normal(7,1,100) #Random data 
  
      p = sns.kdeplot(data, shade=False, lw = 3)
      x,y = p.get_lines()[0].get_data()
      cdf = scipy.integrate.cumtrapz(y, x, initial=0)
  
      plt.hist(data,50,normed = 1,facecolor='b',alpha = 0.3)
  </code></pre>
  
  <p>Then i have another function, which is just a simple gaussian:</p>
  
  <pre><code>def surpriseFunction(mu,variance):
  
      hStates = np.linspace(0,20,100)
      sigma = math.sqrt(variance)
  
      plt.plot(hStates,scipy.stats.norm.pdf(hStates, mu, sigma))
  </code></pre>
  
  <p>calling the functions </p>
  
  <pre><code>surpriseFunction(5,1)
  survivalFunction()
  </code></pre>
  
  <p>gives this plot</p>
  
  <p><img src="https://i.stack.imgur.com/F5bgX.jpg" alt="enter image description here"></p>
  
  <p>As you might have noticed, exchanging different values of mu, moves around the normal to overlap more or less with the kernel estimation. Now, my question is twofold: </p>
  
  <p><strong>1) How to I calculate the overlap between the two functions?</strong></p>
  
  <p><strong>2) How would I go about making a small algorithm, that choose a mean and variance for the gaussian, in such a way, as to maximise this overlap?</strong></p>