JuliaStats / Distributions.jl

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

Add CDF for Multinomial Distribution #1779

Open itsdfish opened 1 year ago

itsdfish commented 1 year ago

Hi,

Currently, Distributions does not have a method for the CDF of the Multinomial distribution. Another user on Discourse proposed a solution which I reproduce below. If this would be acceptable, I can submit a PR and add some tests.

using Distributions 
using Combinatorics

padded_partition(c, s, tmp) = foldl((r,x) -> (r[x]+=1; r), s; init=((tmp .= 0;tmp)))

function MultinomialCDF(D, c)
    k = length(c)
    tmp = zeros(Int, k)
    return sum(s -> pdf(D, padded_partition(c, s, temp)),
      multiset_combinations(1:k, c, D.n))
end

D = Multinomial(5, 3)
# upper bounds on n
c = [2,3,4]
MultinomialCDF(D,  c)
ParadaCarleton commented 1 year ago

That looks great! Could you make a PR?

itsdfish commented 1 year ago

@ParadaCarleton, I'd be happy to make a PR later this week. After I posted this issue, another user posted a more efficient solution. It might be worth considering that. The only thing I would need is guidance on licensing.