# Traces of transformed variables within Turing.jl models

**URL:** <https://discourse.julialang.org/t/traces-of-transformed-variables-within-turing-jl-models/53013>\
**Category:** Probabilistic Programming\
**Tags:** turing\
**Created:** [January 7, 2021, 10:30pm UTC](https://discourse.julialang.org/t/traces-of-transformed-variables-within-turing-jl-models/53013 "2021-01-07T22:30:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BradGroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bradgroff/32/21156_2.png) [@BradGroff](https://discourse.julialang.org/u/BradGroff)\
**Post date:** [January 7, 2021, 10:30pm UTC](https://discourse.julialang.org/t/traces-of-transformed-variables-within-turing-jl-models/53013/1 "2021-01-07T22:30:07Z")

</div>

Hi all,

Is it possible to make Turing return the transformations of random variables in chains? For instance:

```julia
@model function silly_example(y)
    x ~ Beta(1,1)
    z = x > 0.5 # This guy!
    y ~ Normal(0, x)
end

```

Is it possible to get traces of `z` in the chains when sampling?

Thanks!  
Brad

---

<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:** [January 8, 2021, 12:48am UTC](https://discourse.julialang.org/t/traces-of-transformed-variables-within-turing-jl-models/53013/2 "2021-01-08T00:48:41Z")

</div>

One stupid way if you are on `Distributions.jl >= 0.24.7` is to make `z` a draw from a `Dirac` distribution:

```julia
@model function silly_example(y)
    x ~ Beta(1,1)
    z ~ Dirac(x > 0.5)
    y ~ Normal(0, x)
end

```

This is a little limited to univariate quantites, unfortunately.

Alternatively, you can use the “generated quantities” method:

```julia
    x ~ Beta(1,1)
    z = x > 0.5
    y ~ Normal(0, x)

    return z
end

model2 = silly_example_2(1.0)
chain2 = sample(model2, NUTS(), 1000)
gen = generated_quantities(model2, chain2)

```

Note the addition of the `return` statement here. `generated_quantities` goes back to extract whatever is in the `return` statement for each parameterization in the chain.

Honestly it’s been a long discussion on how to include these in chains more easily, and I don’t think we’ve settled on a good solution yet.

---

<div class="post-metadata">

**Author:** ![BradGroff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bradgroff/32/21156_2.png) [@BradGroff](https://discourse.julialang.org/u/BradGroff)\
**Post date:** [January 8, 2021, 4:15pm UTC](https://discourse.julialang.org/t/traces-of-transformed-variables-within-turing-jl-models/53013/3 "2021-01-08T16:15:53Z")

</div>

Oh great, generated quantities was what I needed! Thanks!

---

<div class="post-metadata">

**Author:** ![drbenvincent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drbenvincent/32/3046_2.png) [@drbenvincent](https://discourse.julialang.org/u/drbenvincent)\
**Post date:** [January 15, 2021, 5:04pm UTC](https://discourse.julialang.org/t/traces-of-transformed-variables-within-turing-jl-models/53013/4 "2021-01-15T17:04:34Z")

</div>

I had the same question on twitter [https://twitter.com/inferencelab/status/1350067286539964417](https://twitter.com/inferencelab/status/1350067286539964417) before I knew about this discourse. FYI there’s not much on `generated_quantities` in the docs, so as a beginner to Julia and Turing, I felt a bit lost.

---

<div class="post-metadata">

**Author:** ![Andreas\_Koher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreas_koher/32/11566_2.png) [@Andreas\_Koher](https://discourse.julialang.org/u/Andreas_Koher)\
**Post date:** [November 17, 2021, 2:22pm UTC](https://discourse.julialang.org/t/traces-of-transformed-variables-within-turing-jl-models/53013/5 "2021-11-17T14:22:50Z")

</div>

I am somewhat late to the discussion but, is there also a way to conveniently use `generated_quantities` or something similar with variational inference?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 17, 2021, 3:44pm UTC](https://discourse.julialang.org/t/traces-of-transformed-variables-within-turing-jl-models/53013/6 "2021-11-17T15:44:27Z")

</div>

If I understand correctly, generated\_quantities simply takes your chain, and then sets the parameters equal to the values found in the chain and executes the other statements to compute the quantities of interest. It shouldn’t matter whether the chain was made by variational inference or any other sampling method.

---

<div class="post-metadata">

**Author:** ![Andreas\_Koher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreas_koher/32/11566_2.png) [@Andreas\_Koher](https://discourse.julialang.org/u/Andreas_Koher)\
**Post date:** [November 19, 2021, 4:20pm UTC](https://discourse.julialang.org/t/traces-of-transformed-variables-within-turing-jl-models/53013/7 "2021-11-19T16:20:55Z")

</div>

Nice and thanks for the quick response @dlakelan! For future reference, here is the example from the [docs](https://docs.juliahub.com/DynamicPPL/2PWyN/0.10.16/autodocs/#DynamicPPL.generated_quantities-Tuple%7BModel,%20AbstractMCMC.AbstractChains%7D) using variational inference:

```julia
using Turing
using Optim
using StatsPlots
using Turing: Variational

@model function demo(xs)
    s ~ InverseGamma(2, 3)
    m_shifted ~ Normal(10, √s)
    m = m_shifted - 10

    for i in eachindex(xs) 
        xs[i] ~ Normal(m, √s)
    end

    return (m, )
end

q = vi(model, ADVI(10, 2000))
samples = rand(q, 1000)

#NOTE: constructing the chain becomes slightly more involved with sampled vectors and matrices
_, sym2range = bijector(model, Val(true))
val = vcat([samples[range...,:] for (sym, range) in pairs(sym2range)]...)'
parameter_names = [keys(sym2range)...]
chain = Chains(val, parameter_names)

gq = generated_quantities(model, chain_vi)
m = vec(getindex.(gq, 1))
density!(m, lab="generated quantity (VI)")
vline!([0], lab="true value")

```

![example](https://global.discourse-cdn.com/julialang/original/3X/c/b/cb847242258a2b96e5f7f114591f973a7a9130f0.png)

---

<div class="post-metadata">

**Author:** ![NowanIlfideme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nowanilfideme/32/24348_2.png) [@NowanIlfideme](https://discourse.julialang.org/u/NowanIlfideme)\
**Post date:** [December 8, 2021, 7:33am UTC](https://discourse.julialang.org/t/traces-of-transformed-variables-within-turing-jl-models/53013/8 "2021-12-08T07:33:58Z")

</div>

PyMC3 solves this with pm.Deterministic, could there be an easy way to provide this special case? Hopefully with fewer symbols to type, though… 😋
