Distributions.jl icon indicating copy to clipboard operation
Distributions.jl copied to clipboard

Add CDF for Multinomial Distribution

Open itsdfish opened this issue 1 year ago • 2 comments

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)

itsdfish avatar Sep 17 '23 14:09 itsdfish

That looks great! Could you make a PR?

ParadaCarleton avatar Oct 03 '23 17:10 ParadaCarleton

@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.

itsdfish avatar Oct 03 '23 18:10 itsdfish