Very nice solution! I was wondering if some type of integer partition technique with padding would work e.g. [0, 3, 1], [0,0,4] etc. Your approach does just that.
The expression for the cdf in this source is difficult to read. Once I translated it into LaTeX, it was easier to read and I could see I misunderstood the constraint in P(…). The code Dan has is correct and mine is incorrect. Please disregard it.
The only improvement I can suggest is breaking up the oneliner into two functions that the padded integer partition (or better technical name) can be tested separately from the cdf calculation. For example:
padded_partition(c, s) = foldl((r,x) -> (r[x]+=1; r), s; init=zeros(Int,length(c)))
MultinomialCDF(D, c) = sum(s -> pdf(D, padded_partition(c, s)),
multiset_combinations(1:length(c), c, D.n)
)
Now we can look at the partitions and verify they sum to 5:
using Combinatorics
using Distributions
D = Multinomial(5, 3)
c = [2,3,4]
map(s -> padded_partition(c, s),
multiset_combinations(1:length(c), c, D.n))
output
11-element Vector{Vector{Int64}}:
[2, 3, 0]
[2, 2, 1]
[2, 1, 2]
[2, 0, 3]
[1, 3, 1]
[1, 2, 2]
[1, 1, 3]
[1, 0, 4]
[0, 3, 2]
[0, 2, 3]
[0, 1, 4]
@jar1, I think you can mark this as an intermediate solution.
@Dan, would you be interested in opening an issue on GitHub to add your solution, or allowing me to open an issue and give you credit? I think this would be useful for the community.