KernelDensity.jl icon indicating copy to clipboard operation
KernelDensity.jl copied to clipboard

bug in InterpKDE

Open OmriTreidel opened this issue 7 years ago • 5 comments

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 avatar Jul 19 '16 00:07 OmriTreidel

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

OmriTreidel avatar Jul 19 '16 00:07 OmriTreidel

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

simonbyrne avatar Aug 24 '16 15:08 simonbyrne

I'm looking into it.

OmriTreidel avatar Aug 29 '16 03:08 OmriTreidel

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

AlexRobson avatar Dec 02 '21 11:12 AlexRobson

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.

fedario avatar Feb 05 '22 00:02 fedario