When to use Tarrays

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;
1 Like

Variables which are sampled from particle samplers like PG or SMC need to be TArrays. Others can be normal arrays which are going to be faster. I think the above example maybe using a Gibbs sampler of PG and HMC.

2 Likes

@mohamed82008 is right here. The reason m and T don’t need to be TArrays is because they aren’t actually being sampled with a particle sampler:

g = Gibbs(HMC(0.001, 7, :m, :T), PG(20, :s))

s is the only parameter that needs to have a TArray because it’s the only thing that is touched by a particle sampler.

1 Like

Aahh okay, thank you both very much!