Multinomial accepts wrong parameters

Distributions.Multinomial seems to accept a vector of probabilities that doesn’t sum to one, even if in the code reported in the documentation it should have ckeck_args true by default. Why is that ?

using Distributions #0.21.6

julia> p_v = [0.1,0.4,0.3,0.8]
4-element Array{Float64,1}:
 0.1
 0.4
 0.3
 0.8
julia> a = Multinomial(10000, p_v)
Multinomial{Float64,Array{Float64,1}}(n=10000, p=[0.1, 0.4, 0.3, 0.8])
julia> b = rand(a)
4-element Array{Int64,1}:
 1010
 3980
 2977
 2033
julia> c = sum(b,dims=2) ./ 10000
4-element Array{Float64,1}:
 0.101 
 0.398 
 0.2977
 0.2033
julia> sum(c)
1.0
julia> isprobvec([0.1,0.4,0.3,0.8])
false

Looks like it changed in the the latest release (v0.21.6), at this commit. I’m not sure why the default for check_args isn’t being used here, but it does throw the expected error for check_args = true:

julia> using Distributions

julia> p_v = [0.1,0.4,0.3,0.8]
4-element Array{Float64,1}:
 0.1
 0.4
 0.3
 0.8

julia> isprobvec(p_v)
false

julia> Multinomial(10, p_v)
Multinomial{Float64,Array{Float64,1}}(n=10, p=[0.1, 0.4, 0.3, 0.8])

julia> Multinomial(10, p_v; check_args = true)
ERROR: ArgumentError: p = [0.1, 0.4, 0.3, 0.8] is not a probability vector.
Stacktrace:
 [1] #Multinomial#114(::Bool, ::Type{Multinomial}, ::Int64, ::Array{Float64,1}) at /home/jkbest/.julia/packages/Distributions/ehx56/src/multivariate/multinomial.jl:32
 [2] (::getfield(Core, Symbol("#kw#Type")))(::NamedTuple{(:check_args,),Tuple{Bool}}, ::Type{Multinomial}, ::Int64, ::Array{Float64,1}) at ./none:0
 [3] top-level scope at REPL[25]:1

Probably worth filing an issue.

Cc: @mbesancon

Thanks for reporting this, this is due to the default constructors in parameterized structures, it has already bitten me off in Distributions. I’m running the test and pushing a patch now.

EDIT the patch is merged, waiting for the new release:

2 Likes