# 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:** 1\
**Showing post:** 7

<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)

---

_[View the full topic](https://discourse.julialang.org/t/traces-of-transformed-variables-within-turing-jl-models/53013)._
