This is because you use a strictly typed array x
in your model definition, which is not necessary btw. Automatic differentiation fails in this case, this is Turing unrelated.
If you just want to sample from the prior (which is what you do here), you can simply to the following:
@model function test(x)
for i in eachindex(x)
x[i] ~ Normal(0, i)
end
return x
end
N = 5
samples = test(Array{Union{Float64,Missing},1}(missing, N))()
If you really need to define an array within a model definition, you need to use a special syntax atm. This will ensure that the array is correctly typed during automatic differentiation and that the models stays type-stable at all times.
Here is an example:
@model function test2(x, ::Type{T}=Vector{Float64}) where {T}
s = T(undef, length(x))
for i in eachindex(x)
s[i] ~ truncated(Normal(), 0, Inf)
x[i] ~ Normal(0, s[i])
end
return x
end
This syntax is further described in the docs: https://turing.ml/dev/docs/using-turing/performancetips
That said, the above examples both do not need the definition of an array at all. The second example can simply be written as follows:
@model function test2(x)
s ~ filldist(truncated(Normal()), length(x))
for i in eachindex(x)
x[i] ~ Normal(0, s[i])
end
return x
end
or using broadcasting,
@model function test2(x)
s ~ filldist(truncated(Normal()), length(x))
x .~ Normal.(0, s)
end