JuliaStats / KernelDensity.jl

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

broadcast doesn't work for pdf calculation #63

Open afniedermayer opened 5 years ago

afniedermayer commented 5 years ago

broadcast doesn't work for pdf calculation, see e.g.

julia> using KernelDensity

julia> y=kde(randn(1000));

julia> pdf.(y, [.5,.6])
ERROR: MethodError: no method matching length(::UnivariateKDE{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}})
Closest candidates are:
  length(::Core.SimpleVector) at essentials.jl:582
  length(::Base.MethodList) at reflection.jl:728
  length(::Core.MethodTable) at reflection.jl:802
  ...
Stacktrace:
 [1] _similar_for(::UnitRange{Int64}, ::Type, ::UnivariateKDE{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}, ::Base.HasLength) at .\array.jl:532
 [2] _collect(::UnitRange{Int64}, ::UnivariateKDE{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}, ::Base.HasEltype, ::Base.HasLength) at .\array.jl:563
 [3] collect(::UnivariateKDE{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}) at .\array.jl:557
 [4] broadcastable(::UnivariateKDE{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}) at .\broadcast.jl:609
 [5] broadcasted(::Function, ::UnivariateKDE{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}, ::Array{Float64,1}) at .\broadcast.jl:1163
 [6] top-level scope at none:0

This is in contrast to other calculations of pdfs, e.g.

julia> using Distributions

julia> N=Normal()
Normal{Float64}(μ=0.0, σ=1.0)

julia> pdf.(N, [.5,.6])
2-element Array{Float64,1}:
 0.3520653267642995
 0.33322460289179967

The non-broadcasting version does work for KernelDensity (pdf(y,[.5,.6])), however, for this kind of usage seems to be not recommended and deprecated, see e.g.

julia> pdf(N, [.5,.6])
┌ Warning: `pdf(d::UnivariateDistribution, X::AbstractArray)` is deprecated, use `pdf.(d, X)` instead.
│   caller = top-level scope at none:0
└ @ Core none:0
2-element Array{Float64,1}:
 0.3520653267642995
 0.33322460289179967
simonbyrne commented 5 years ago

Good point. You can use pdf.(Ref(y), [.5,.6]) until this is fixed.