TuringLang / Turing.jl

Bayesian inference with probabilistic programming.
https://turinglang.org
MIT License
2.03k stars 218 forks source link

NaN violates Normal condition σ >= zero(σ) #868

Closed itsdfish closed 5 years ago

itsdfish commented 5 years ago

Hi-

For some reason the sigma parameter (s) takes the value of NaN and triggers an inequality violation. I found this in several models, but this is the simplest model I found that produces the error.

using Turing,Random 

Random.seed!(1101)

@model model(y) = begin
    N = length(y)
    μ = Vector{Real}(undef,N)
    μ ~ [Normal(0,1)]
    s ~ Truncated(Cauchy(0,1),0,Inf)
    if !isa(s,Float64)
        println(s.value)
    end
    for i in 1:N
        for j in 1:length(y[i])
            y[i][j] ~ Normal(μ[i],s)
        end 
    end
end
μ = rand(Normal(0,1),10)
y = [rand(Normal(i,1),10) for i in μ]

chain = sample(model(y),NUTS(2000,1000,.8))
println(chain)

System Info:

Julia 1.1.0

(v1.1) pkg> st Turing
    Status `~/.julia/environments/v1.1/Project.toml`
  [0bf59076] AdvancedHMC v0.2.1
  [31c24e10] Distributions v0.21.0
  [c7f686f2] MCMCChains v0.3.10
  [4c63d2b9] StatsFuns v0.8.0
  [fce5fe82] Turing v0.6.22
  [e88e6eb3] Zygote v0.3.2

(v1.1) pkg> st AdvancedHMC
    Status `~/.julia/environments/v1.1/Project.toml`
  [0bf59076] AdvancedHMC v0.2.1
  [d96e819e] Parameters v0.10.3
  [2913bbd2] StatsBase v0.31.0
jonasmac16 commented 5 years ago

I have also encountered the same error using a truncated normal on sigma recently but have a hard time to reproduce it. Just wanted to report it hear.

itsdfish commented 5 years ago

Thanks, @jonasmac16. I was able to reproduce the error by replacing s with s ~ TruncatedNormal(0,1,0,Inf) in the code above. On my system, the error systems seems to be fairly robust to the prior on s and seed. If you are finding cases where the model runs correctly, it might be helpful to report the code and your system info.

xukai92 commented 5 years ago

This bug should be fixed by the recent merged PR in AdvancedHMC.jl. There was an interface bug after AdvancedHMC.jl was upgraded.

xukai92 commented 5 years ago

Fixed by https://github.com/TuringLang/AdvancedHMC.jl/pull/85