JuliaStats / Distributions.jl

A Julia package for probability distributions and associated functions.
Other
1.08k stars 410 forks source link

Adding gradlogpdf for AbstractMixture #1788

Open sethaxen opened 8 months ago

sethaxen commented 8 months ago

AbstractMixture has no implementation of gradlogpdf, but this is straightforward to implement in terms of gradlogpdf of the components, e.g.

function Distributions.gradlogpdf(d::AbstractMixtureModel, x)
    ps = probs(d)
    ds = components(d)
    inds = filter(i -> !iszero(ps[i]), eachindex(ps, ds))
    ws = pweights(softmax!([log(ps[i]) + logpdf(ds[i], x) for i in inds]))
    g = mean(map(i -> gradlogpdf(ds[i], x), inds), ws)
    return g
end