# Turing.jl - update variables within the model

**URL:** <https://discourse.julialang.org/t/turing-jl-update-variables-within-the-model/35168>\
**Category:** Probabilistic Programming\
**Tags:** turing\
**Created:** [February 26, 2020, 1:55pm UTC](https://discourse.julialang.org/t/turing-jl-update-variables-within-the-model/35168 "2020-02-26T13:55:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tpoisot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tpoisot/32/19812_2.png) [@tpoisot](https://discourse.julialang.org/u/tpoisot)\
**Post date:** [February 26, 2020, 1:55pm UTC](https://discourse.julialang.org/t/turing-jl-update-variables-within-the-model/35168/1 "2020-02-26T13:55:59Z")

</div>

I’m using Turing to fit a model, where every data point is a pair (S,L), as we describe in the following paper: [Revisiting the links-species scaling relationship in food webs | bioRxiv](https://www.biorxiv.org/content/10.1101/2020.02.13.947531v1)

In practice, we do not use L to fit the model, but L-(S-1), and I have not been able to use this directly from within Turing - what I had to do was create a variable (equal to L.-(S.-1)) and then call the model on this.

The model is as follows:

```julia
@model flexible_links(s,f) = begin
    N = length(s)
    # Transformed data
    F = (s.*s) .- (s .- 1.0)
    # Parameters
    ϕ ~ Normal(3.0, 0.5)
    μ ~ Beta(3.0, 7.0)
    for i in 1:N
        f[i] ~ BetaBinomial(F[i], μ*exp(ϕ), (1-μ)*exp(ϕ))
    end
    return μ, ϕ
end

```

What I would like to write instead would look something like this:

```julia
@model flexible_links(s,l) = begin
    N = length(s)
    # Transformed data
    f = [l[i]-(s[i]-1) for i in 1:N]
    F = (s.*s) .- (s .- 1.0)
    # Parameters
    ϕ ~ Normal(3.0, 0.5)
    μ ~ Beta(3.0, 7.0)
    for i in 1:N
        f[i] ~ BetaBinomial(F[i], μ*exp(ϕ), (1-μ)*exp(ϕ))
    end
    return μ, ϕ
end

```

But it does not work, because f is of the wrong type - I’d appreciate any points as to where I should be looking to solve the problem.

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [February 26, 2020, 2:11pm UTC](https://discourse.julialang.org/t/turing-jl-update-variables-within-the-model/35168/2 "2020-02-26T14:11:03Z")

</div>

Turing assumes that arguments to the model are observations and that all other model parameters should be treated as parameters. If f should be considered an observation you need to pass it to the model definition. Your model will also train faster if you construct f outside.

As an alternative you could use the @logpdf macro. But I would refrain from using it if not necessary.

---

<div class="post-metadata">

**Author:** ![tpoisot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tpoisot/32/19812_2.png) [@tpoisot](https://discourse.julialang.org/u/tpoisot)\
**Post date:** [February 26, 2020, 2:21pm UTC](https://discourse.julialang.org/t/turing-jl-update-variables-within-the-model/35168/3 "2020-02-26T14:21:00Z")

</div>

Gotcha - so there is no way in Turing to emulate what stan does with transformed variables, as in [ms\_straight\_lines/betabin\_connectance.stan at master · PoisotLab/ms\_straight\_lines · GitHub](https://github.com/PoisotLab/ms_straight_lines/blob/master/stan/betabin_connectance.stan#L6) ?

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [February 26, 2020, 2:41pm UTC](https://discourse.julialang.org/t/turing-jl-update-variables-within-the-model/35168/4 "2020-02-26T14:41:58Z")

</div>

At the moment we don’t have a way to do this, mainly because we assume that observations are passed as arguments. Transformations itself should not be an issue if we would be able to track that `f` depends on `l` and `s`. At the moment we don’t have a handle on this but it could be added to the compiler.

Could you open an issue if you feel that this would be an useful feature?

– EDIT –

Thanks for trying to implement your model in Turing! 🙂
