JuliaStats / KernelDensity.jl

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

bug in InterpKDE #29

Open OmriTreidel opened 8 years ago

OmriTreidel commented 8 years ago

I have a kde::UnivariateKDE which is non-negative. Constructing ik = InterpKDE(kde) results in undefined grid. Even worse, calling pdf(ik, x) sometimes return negative values.

OmriTreidel commented 8 years ago

I've traced the undefined grid to the default boundary condition bc::Type{BC}=BCnan. Is that really the most sensible default?

Also, the negative pdf results from the quadratic interpolation. There are several ways this can be fixed:

  1. pdf(ik::InterpKDE,x...) = max(0.0, ik.grid[x...])
  2. Default the interpolation in InterpKDE to Linear
simonbyrne commented 7 years ago

I've changed KernelDensity to use Interpolations instead of Grid. Would you mind seeing if this is still an issue?

OmriTreidel commented 7 years ago

I'm looking into it.

AlexRobson commented 2 years ago

Old thread, but I believe that I'm also hitting a variant of this when deploying in the wild with some heavy tailed data:

MWE:

rx = [randn(95,); 10^1; 10^2; 10^3; 10^4; 10^5]
ry = randn(100,)

kxy = KernelDensity.kde((rx, ry))
kx = KernelDensity.kde(rx)

display(pdf(kx, kxy.x)[1:10])
10-element Vector{Float64}:
  0.018119920780898854
  2.824161390572578e-10
 -1.52690720751668e-8
 -1.3976374893253972e-7
  1.3678176032328834e-11
  1.7180241491468265e-11
  1.914539912867148e-11
  2.0346765855877925e-11
  2.1132222284907084e-11
  2.1673349260840836e-11
fedario commented 2 years ago

Is this related? https://github.com/JuliaPlots/AlgebraOfGraphics.jl/issues/334

If so, there is at least a data set and two minimal working examples to reproduce this.