# When to use Tarrays

**URL:** <https://discourse.julialang.org/t/when-to-use-tarrays/46758>\
**Category:** Probabilistic Programming\
**Tags:** question, turing\
**Created:** [September 17, 2020, 7:57am UTC](https://discourse.julialang.org/t/when-to-use-tarrays/46758 "2020-09-17T07:57:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [September 17, 2020, 7:57am UTC](https://discourse.julialang.org/t/when-to-use-tarrays/46758/1 "2020-09-17T07:57:23Z")

</div>

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`?

```julia
# 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;

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 17, 2020, 10:07am UTC](https://discourse.julialang.org/t/when-to-use-tarrays/46758/2 "2020-09-17T10:07:03Z")

</div>

Variables which are sampled from particle samplers like `PG` or `SMC` need to be `TArray`s. 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.

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [September 17, 2020, 1:44pm UTC](https://discourse.julialang.org/t/when-to-use-tarrays/46758/3 "2020-09-17T13:44:48Z")

</div>

@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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Elmo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elmo/32/17979_2.png) [@Elmo](https://discourse.julialang.org/u/Elmo)\
**Post date:** [September 17, 2020, 7:10pm UTC](https://discourse.julialang.org/t/when-to-use-tarrays/46758/4 "2020-09-17T19:10:31Z")

</div>

Aahh okay, thank you both very much!
