# Epanechnikov kernel density

**URL:** https://discourse.julialang.org/t/epanechnikov-kernel-density/23818
**Category:** General Usage
**Created:** [May 3, 2019, 3:36pm UTC](https://discourse.julialang.org/t/epanechnikov-kernel-density/23818 "2019-05-03T15:36:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [May 3, 2019, 3:36pm UTC](https://discourse.julialang.org/t/epanechnikov-kernel-density/23818/1 "2019-05-03T15:36:46Z")

</div>

I am trying to use a Epanechnikov kernel density estimator in KernelDensity.jl. Surprisingly, this is not implemented, but it should be possible to accomplish by extending the function `kernel_dist`. I have been unable to match the results from R. It is not clear to me whether this is because I extended `kernel_dist` incorrectly or because the interpolation functions differ. So I was hoping that someone who has more familiarity with the topic might provide some help.

Julia code:

```julia
using KernelDensity,Distributions
import KernelDensity: kernel_dist
kernel_dist(::Type{Epanechnikov},w::Real) = Epanechnikov(0.0,w)
data = [0.952,0.854,0.414,0.328,0.564,0.196,0.096,0.366,0.902,0.804]
kd = kde(data;kernel=Epanechnikov)
dist = InterpKDE(kd)
pdf(dist,.3)

```

Result = 1.3029

R code:

```julia
data = c(0.952,0.854,0.414,0.328,0.564,0.196,0.096,0.366,0.902,0.804)
kd = density(data,kernel = "epanechnikov")
f = approxfun(kd)
f(.3)

```

Result = .9670

I repeated the comparison for a standard normal with 10^5 samples and found a discrepancy, albeit a smaller one: .3818 for R which is very close to the theoretical true value and .3782 for Julia.

---

<div class="post-metadata">

### Author: ![aharoun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aharoun/32/6887_2.png) [@aharoun](https://discourse.julialang.org/u/aharoun)
#### Post date: [May 3, 2019, 10:10pm UTC](https://discourse.julialang.org/t/epanechnikov-kernel-density/23818/3 "2019-05-03T22:10:05Z")

</div>

For small sample, implementation differences may create significant deviations. For example by fixing the bandwidth, grid points and interpolation, you can get pretty similar results:

```julia
using KernelDensity,Distributions,Interpolations
import KernelDensity: kernel_dist
kernel_dist(::Type{Epanechnikov},w::Real) = Epanechnikov(0.0,w)
data = [0.952,0.854,0.414,0.328,0.564,0.196,0.096,0.366,0.902,0.804]
kd = kde(data, range(-5.0, 5.0,length=2048); kernel=Epanechnikov, bandwidth=1.0*sqrt(5))
dist = InterpKDE(kd, BSpline(Linear()))

julia> pdf(dist,.3)
0.32542324675955636

```

In R:

```nohighlight
> data = c(0.952,0.854,0.414,0.328,0.564,0.196,0.096,0.366,0.902,0.804)
> kd = density(data,bw = 1.0, kernel = "epanechnikov",from=-5.0,to=5.0, n=2048)
> f = approxfun(kd)
> f(.3)
[1] 0.3254264361

```

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [May 3, 2019, 10:15pm UTC](https://discourse.julialang.org/t/epanechnikov-kernel-density/23818/4 "2019-05-03T22:15:47Z")

</div>

Thanks. I had a suspicion that there were implementation differences lurking somewhere.
