Skip to content

Cannot sample from bounded distribution when using Dual types #2088

@itsdfish

Description

@itsdfish

Hi all,

I believe I found a bug while trying to generate posterior predictive distributions. As shown below, it is possible to sample from distributions unless they are bounded and more than one sample is drawn. The error occurs with other bounded distributions, such as Gamma. It looks like it could be an issue with either Turing or Distributions. Sorry if I filed to the wrong package.

works

using Turing 

@model function test_model()
    μ ~ Gamma(2, 2)
    σ ~ Gamma(2, 2)
    return rand(Normal(μ, σ))
end

chain = sample(test_model(), NUTS(1000, .85), MCMCThreads(), 1000, 1)

works

using Turing 

@model function test_model()
    μ ~ Gamma(2, 2)
    σ ~ Gamma(2, 2)
    return rand(Normal(μ, σ), 1)
end

chain = sample(test_model(), NUTS(1000, .85), MCMCThreads(), 1000, 1)

works

using Turing 

@model function test_model()
    μ ~ Gamma(2, 2)
    σ ~ Gamma(2, 2)
    return rand(InverseGaussian(μ, σ))
end

chain = sample(test_model(), NUTS(1000, .85), MCMCThreads(), 1000, 1)

Fails

using Turing 

@model function test_model()
    μ ~ Gamma(2, 2)
    σ ~ Gamma(2, 2)
    return rand(InverseGaussian(μ, σ), 1)
end

chain = sample(test_model(), NUTS(1000, .85), MCMCThreads(), 1000, 1)

truncated error

ERROR: TaskFailedException

    nested task error: TaskFailedException
    
        nested task error: MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 2})
        
        Closest candidates are:
          (::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat
           @ Base rounding.jl:207
          (::Type{T})(::T) where T<:Number
           @ Core boot.jl:792
          (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number}
           @ Base char.jl:50
          ...
        
        Stacktrace:
          [1] convert(#unused#::Type{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 2})
            @ Base ./number.jl:7
          [2] setindex!(A::Vector{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{Turing.TuringTag, Float64}, Float64, 2}, i1::Int64)
            @ Base ./array.jl:969
          [3] _rand!
            @ ~/.julia/packages/Distributions/wyOWr/src/univariates.jl:147 [inlined]
          [4] rand!
            @ ~/.julia/packages/Distributions/wyOWr/src/univariates.jl:142 [inlined]
          [5] rand
            @ ~/.julia/packages/Distributions/wyOWr/src/genericrand.jl:52 [inlined]
          [6] rand
            @ ~/.julia/packages/Distributions/wyOWr/src/genericrand.jl:24 [inlined]
          [7] rand
            @ ~/.julia/packages/Distributions/wyOWr/src/genericrand.jl:22 [inlined]

versions

  Julia 1.9.3
  [31c24e10] Distributions v0.25.102
  [366bfd00] DynamicPPL v0.23.18
  [fce5fe82] Turing v0.29.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions