Search code examples
pythonintegrationprobability-densitykernel-density

How to integrate kernel density estimation


I have data

from scipy.stats.kde import gaussian_kde
import numpy as np
from scipy import integrate

data1 = np.linspace(0,1,50)
data2 = np.linspace(0.1,0.9,50)
data3 = np.linspace(0,0.7,50)
data4 = np.linspace(0.1,1,50)

And I need to integrate density multiplication over all variables

kde1 = gaussian_kde(data1)
kde2 = gaussian_kde(data2)
kde3 = gaussian_kde(data3)
kde4 = gaussian_kde(data4)


print(integrate.nquad(lambda x1,x2,x3,x4: kde1(x1)*kde2(x2)*kde3(x3)*kde4(x4),
                            [[-1,1],[-1,1],[-1,1],[-1,1]])[0])

I think that is true solution but it work very slow(more than 10min). Is it possible to make it more faster?


Solution

  • Problem can be solved with Monte-Carlo integration method

    for example

    from skmonaco import mcquad
    mcquad(lambda x_y: x_y[0]*x_y[1], # integrand
         xl=[0.,0.],xu=[1.,1.], # lower and upper limits of integration
         npoints=100000 # number of points
         )
    

    result: (0.24959359250821114, 0.0006965923631156234)