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?
using Distributions, KernelDensity
struct KDEDist{D, K} <: ContinuousUnivariateDistribution
data::D
kde::K
end
function KDEDist(data)
KDE_fit = kde(d.data)
ik = InterpKDE(KDE_fit)
return KDEDist(data, ik)
end
function Distributions.pdf(d::KDEDist, x::Real)
return pdf(d.kde, x)
end