A kernel density estimate smooths a sample into a density by placing a Gaussian bump of width h (the bandwidth) on every point:
Implement for 1-D data:
kde(sample, x, h): the density at each point of xsilverman(sample): Silverman's rule of thumb, 0.9 * min(std, IQR / 1.34) * n ** (-1/5) (population std; IQR from np.percentile)loo_log_likelihood(sample, h): the leave-one-out log-likelihood, , where each point's density is estimated from the other n − 1 pointsbest_bandwidth(sample, grid): the h in grid with the highest leave-one-out log-likelihood (ties: the smallest)