Hi everyone,
I’m new to Turing/probabilistic programming and would like to know when you should use TArrays vs. normal arrays in the model definition.
The package guidelines mention that “stack allocated variables” are fine to use, but mutable “heap allocated variables” are not. I am assuming that they mean that normal arrays cause problems and this is why in the HMM example (copy pasted below for reference) the s
variable is a TArray (instantiated with tzeros). However, I am confused about why m
and T
are not TArrays? Aren’t they also distributions? What is the difference between s
and m
or T
?
# Turing model definition.
@model BayesHmm(y, K) = begin
# Get observation length.
N = length(y)
# State sequence.
s = tzeros(Int, N)
# Emission matrix.
m = Vector(undef, K) # <= why not a TArray?
# Transition matrix.
T = Vector{Vector}(undef, K) # <= why not a TArray?
# Assign distributions to each element
# of the transition matrix and the
# emission matrix.
for i = 1:K
T[i] ~ Dirichlet(ones(K)/K)
m[i] ~ Normal(i, 0.5)
end
# Observe each point of the input.
s[1] ~ Categorical(K)
y[1] ~ Normal(m[s[1]], 0.1)
for i = 2:N
s[i] ~ Categorical(vec(T[s[i-1]]))
y[i] ~ Normal(m[s[i]], 0.1)
end
end;