Hi,
The following minor modification of your code runs fine on the current #master branch of Turing using Julia 1.1.0.
using Turing, FillArrays, LinearAlgebra
# Sigmoid function (numerically stable)
sigmoid(x::T) where {T<:Real} = x >= zero(x) ? inv(exp(-x) + one(x)) : exp(x) / (exp(x) + one(x))
@model BayesLogisticRegression(y, X) = begin
N,D = size(X)
Sigma ~ InverseWishart(D+2, Matrix{Float64}(I, D+1, D+1))
Mu ~ MvNormal(Fill(0.0, D+1), 1.0 )
Coefficients ~ MvNormal(Mu, Sigma)
Logit = X[:, 1]*Coefficients[2] .+ Coefficients[1]
Probability = sigmoid.(Logit)
for i = 1:N
y[i] ~ Bernoulli(Probability[i])
end
end
# initialize model with some dummy data
Model = BayesLogisticRegression(rand(0:1, 100), randn(100, 10));
# NUTS sampling for 10 iterations
Turing.setadbackend(:reverse_diff)
sample(Model, NUTS(0.65), 10);
However, if the initial values for Sigma
are such that the matrix is not positive-definite & symmetric the sampling will break in the first iteration. Which means you have to be lucky or better set some reasonable initial values for Sigma
.
Note: Unfortunately, we have to provid tailored gradients in Turing using our own DistributionsAD package for some distributions (including MvNormal) as not all distributions from the Distributions package allow for proper AD.
@cpfiffer is it possible to set initial values for Sigma
using the new interface? If so, how would you do it?
– EDIT –
I seemed to have been lucky and cannot reproduce it anymore. I think you will have to set appropriate initial values.
@Kai_Xu Do you think we might have to change the initialisation procedure for such cases?
Stacktrace:
[1] checkpositivedefinite(::Int64) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/factorization.jl:11
[2] #cholesky!#97(::Bool, ::Function, ::Array{Float64,2}, ::Val{false}) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/cholesky.jl:182
[3] #cholesky#101 at ./none:0 [inlined]
[4] cholesky at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/cholesky.jl:275 [inlined] (repeats 2 times)
[5] logpdf_with_trans(::InverseWishart{Float64,PDMats.PDMat{Float64,Array{Float64,2}}}, ::Array{Float64,2}, ::Bool) at /Users/martin/.julia/packages/Bijectors/hFDmJ/src/Bijectors.jl:365
[6] assume(::Turing.SampleFromUniform, ::InverseWishart{Float64,PDMats.PDMat{Float64,Array{Float64,2}}}, ::Turing.Core.RandomVariables.VarName{:Sigma}, ::Turing.Core.RandomVariables.VarInfo{Turing.Core.RandomVariables.Metadata{Dict{Turing.Core.RandomVariables.VarName,Int64},Array{Distribution,1},Array{Turing.Core.RandomVariables.VarName,1},Array{Real,1},Array{Set{Turing.Selector},1}},Real}) at /Users/martin/.julia/dev/Turing/src/inference/Inference.jl:519