JuliaStats / Distributions.jl

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

product_distribution of discrete and contineous distributions #1136

Open karlwessel opened 4 years ago

karlwessel commented 4 years ago

If I try to create a product distribution from a discrete distribution and a continuous distribution I get the following error:

julia> using Distributions

julia> discrete = DiscreteUniform(1, 5)
DiscreteUniform(a=1, b=5)

julia> continuous = Uniform(1, 5)
Uniform{Float64}(a=1.0, b=5.0)

julia> product_distribution([discrete, continuous])
ERROR: MethodError: no method matching Product(::Array{Distribution{Univariate,S} where S<:ValueSupport,1})
Closest candidates are:
  Product(::V) where {S<:ValueSupport, T<:Distribution{Univariate,S}, V<:AbstractArray{T,1}} at /home/karl/.julia/packages/Distributions/ZJ0hQ/src/multivariate/product.jl:23
Stacktrace:
 [1] product_distribution(::Array{Distribution{Univariate,S} where S<:ValueSupport,1}) at /home/karl/.julia/packages/Distributions/ZJ0hQ/src/multivariate/product.jl:46
 [2] top-level scope at REPL[4]:1

The documentation only says that the distributions have to be univariate, but not that they have to have the same ValueSupport.

Is this a missing feature or something that is not possible (or mathematically sensible) in general?

Jan-van-Waaij commented 1 year ago

Mathematically it is fine. I ran into the same question. Hence I am here. If you define d1 = Product([DiscreteUniform(1, 5), DiscreteUniform(1, 5)]) and d2 = Product([Uniform(), Uniform()]), then rand(d1) produces a vector of Int's, while rand(d2) produces a vector of Float64's. So d3 = Product([DiscreteUniform(1, 5), Uniform()]); rand(d3) should produce a vector with an Int and a Float64, which should then be of type Vector{Real}, which is known to be inefficient. Converting Int's to Float64's might also cause trouble. But I am not a developer. It is just my guess. You can, of course, define your own Product type, if appropriate.