JuliaStats / KernelDensity.jl

Kernel density estimators for Julia
Other
177 stars 40 forks source link

KernelDensity with range #12

Closed skrisna closed 9 years ago

skrisna commented 9 years ago

I am not sure if this is an issue but I am having a hard time understanding the behavior of KernelDensity so please feel free to close it if it is not really an issue.

I am trying to get KernelDensity to work using range. This is what I did-

kr = kde_range((0.0,1.0),2048) k1 = kde(H1,kr) k0 = kde(H0,kr)

This function behaves properly except at the boundaries- screenclip

When I do not use the range parameter, things seem to die down at the boundaries but the boundaries extend beyond the range I am interested in. screenclip

I would like to have a range and guarantee that the function is 0 (dies down) at the boundaires. How can I setup KernelDensity properly to achieve this result?

simonbyrne commented 9 years ago

I would like to have a range and guarantee that the function is 0 (dies down) at the boundaires. How can I setup KernelDensity properly to achieve this result?

Short answer: you can't. The problem is that the convolution in KDEs is based on a Fourier transform, which essentially assumes that your domain is periodic: this means you need to pad the extent of each range so that this "wrap-around" effect is small.

What you could do is construct the kde, trim off what you don't want, and put it back together, e.g.:

X = rand(10)
r = linrange(-1.0,2.0,1024)
# construct "padded" kde
k = kde(X,r)

# lower and upper indices
i,j = searchsortedfirst(r,0), searchsortedlast(r,1)
# construct trimmed kde object
k2 = UnivariateKDE(k.x[i:j],k.density[i:j])
skrisna commented 9 years ago

That is great. Thanks for the exposition and the padding suggestion.