From 52ddc5eb73c1b8263645a841040a5a447d9ec86c Mon Sep 17 00:00:00 2001 From: Xuan Date: Thu, 3 Jun 2021 18:05:08 -0400 Subject: [PATCH] Add workaround for mismatch between `eltype` and type of samples. The semantics of `eltype` are unresolved, as discussed in this issue: https://github.com/JuliaStats/Distributions.jl/issues/1071 --- src/distributions.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/distributions.jl b/src/distributions.jl index 8f08c91..f25e2fb 100644 --- a/src/distributions.jl +++ b/src/distributions.jl @@ -1,17 +1,20 @@ import SpecialFunctions: logfactorial +safe_eltype(d::Distributions.Distribution) = eltype(d) +safe_eltype(d::ContinuousDistribution) = float(eltype(d)) + "Wraps Distributions.jl distributions as Gen.jl distributions." struct WrappedDistribution{T,D <: Distributions.Distribution} <: Gen.Distribution{T} dist::D end WrappedDistribution(d::D) where {D <: UnivariateDistribution} = - WrappedDistribution{eltype(D),D}(d) + WrappedDistribution{safe_eltype(d),D}(d) WrappedDistribution(d::D) where {D <: MultivariateDistribution} = - WrappedDistribution{Vector{eltype(D)},D}(d) + WrappedDistribution{Vector{safe_eltype(d)},D}(d) WrappedDistribution(d::D) where {D <: MatrixDistribution} = - WrappedDistribution{Matrix{eltype(D)},D}(d) + WrappedDistribution{Matrix{safe_eltype(d)},D}(d) WrappedDistribution(d::Truncated{D}) where {D} = - WrappedDistribution{eltype(D),typeof(d)}(d) + WrappedDistribution{safe_eltype(d),typeof(d)}(d) (d::WrappedDistribution)(args...) = Gen.random(d, args...)