Integrate 2D kernel density estimate

前端 未结 3 1513
梦如初夏
梦如初夏 2020-12-06 02:50

I have a x,y distribution of points for which I obtain the KDE through scipy.stats.gaussian_kde. This is my code and how the output looks (the

3条回答
  •  死守一世寂寞
    2020-12-06 03:41

    Here is a way to do it using monte carlo integration. It is a little slow, and there is randomness in the solution. The error is inversely proportional to the square root of the sample size, while the running time is directly proportional to the sample size (where sample size refers to the monte carlo sample (10000 in my example below), not the size of your data set). Here is some simple code using your kernel object.

    #Compute the point below which to integrate
    iso = kernel((x1,y1))
    
    #Sample from your KDE distribution
    sample = kernel.resample(size=10000)
    
    #Filter the sample
    insample = kernel(sample) < iso
    
    #The integral you want is equivalent to the probability of drawing a point 
    #that gets through the filter
    integral = insample.sum() / float(insample.shape[0])
    print integral
    

    I get approximately 0.2 as the answer for your data set.

提交回复
热议问题