Let
x = rand(100); #some data
PDF of x can be estimated using KDE:
using KernelDensity
KDE_x = kde(x)
ik = InterpKDE(KDE_x)
KDE_pdf(x) = pdf(ik, x)
How can the KDE_pdf function be used in to construct a custom distribution?
Here’s my partial first try, still have to implement rand for this distribution:
I want to use the KDEDist in a Turing.jl model
using Distributions, KernelDensity
struct KDEDist <: ContinuousUnivariateDistribution
data::Vector{Float64}
end
function Distributions.pdf(d::KDEDist, x::Real)
KDE_fit = kde(d.data)
ik = InterpKDE(KDE_fit)
return pdf(ik, x)
end
As we can see that when the pdf function is called the KDE is fitted every time which is wasteful. Is there a way to just the pdf(ik, x) function into the Distributions.pdf function to avoid wasteful computation?