Hi,
This is a common type issue with autodifferentiation (that’s why you didn’t encounter it with ohter samplers).
Your vector A is pre-allocated with a Matrix{Float64} as element type, but later in the for loop you update it with [1.0 - γ γ; γ 1.0 - γ] which is of type Matrix{ForwardDiff.Dual}.
A quick fix is to avoid specifying the element type when you initialize arrays.
If you need type specification (e.g. for multiple dispatch), you can use a function like :
check_type(x) = x
check_type(x::T) where T <:ForwardDiff.Dual = x.value
Hi -
This is the corrected version. There is probably plenty of room for optimisation. Would appreciate any suggestions.
@model function msm(y, kbar)
N = length(y)
A = Vector{Matrix}(undef, kbar)
S = Vector(undef, kbar)
b ~ Uniform(1.0, 50.)
m0 ~ Uniform(1.0, 1.9999)
γ1 ~ Uniform(0.001, 0.999)
σ0 ~ Uniform(0.0001, 5.)
M = [m0, 2.0 - m0]
for i = 1:kbar
γ = (1. - (1. - γ1)^(b^(i - 1))) / 2.
A[i] = [1.0 - γ γ; γ 1.0 - γ]
end
S ~ product_distribution([Categorical([0.5, 0.5]) for i = 1:kbar])
σ = σ0 * sqrt(prod([M[u] for u in S]))
y[1] ~ Normal(0.0, σ)
for j = 2:N
S ~ product_distribution([Categorical(A[i][S[i],:]) for i = 1:kbar])
σ = σ0 * sqrt(prod([M[u] for u in S]))
y[j] ~ Normal(0.0, σ)
end
end