Search code examples
pythonscikit-learnkernel-density

How to stack a kernel above each point on a regular grid with Python?


The idea is to stack a kernel above each point scattered along 1D. Specifically, the peak of the kernel is align/ centered to x-axis point each of the point. This is akin to Kernel Density Estimation except only half of the kernel is stacked at each of the point as shown at the picture below.

enter image description here

Ultimately, the summation of each of density will be calculated and will yield a single curve (i.e., gray line) as shown below.

As a starting point, I had dig around the Kernel Density Estimation of scikit learn module for an idea. However, I fail to find any line on how/where the stack they kernel on top each of point.

Really appreciate if someone can provide a good reading material for me to achieve this objective.

enter image description here


Solution

  • Does this do what you want? Sorry the graphs are not half as pretty as yours but did not think that was the point

    import numpy as np
    from scipy.stats import norm
    
    # define a half-kernel function. We normalize to have integral(half_kernel) = 1 if required
    def half_kernel(x, center, width = 1, normalize = True):
        kernel = (x>=center)*norm.pdf(x, center, width)
        if normalize:
            kernel *= 2
        return kernel
    
    # this are the points where we center our kernels -- random for testing
    centers = np.random.normal(0.0,2.0,7)
    
    # Grid on which we look at the results
    x = np.linspace(-3.0,3.0,101)
    
    # get the results here, each column is one of the kernels 
    discr_kernels = np.zeros((len(x),len(centers)))
    for n in range(len(centers)):
        discr_kernels[:,n] = half_kernel(x, centers[n])
    y = discr_kernels.sum(axis= 1)
    
    plt.plot(x,discr_kernels,'--')
    plt.plot(x,y, '.-', label = 'total')
    plt.legend(loc = 'best')
    

    result