# Posterior samples as vector of named tuples

**URL:** <https://discourse.julialang.org/t/posterior-samples-as-vector-of-named-tuples/121430>\
**Category:** Probabilistic Programming\
**Tags:** turing\
**Created:** [October 17, 2024, 7:37pm UTC](https://discourse.julialang.org/t/posterior-samples-as-vector-of-named-tuples/121430 "2024-10-17T19:37:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [October 17, 2024, 7:37pm UTC](https://discourse.julialang.org/t/posterior-samples-as-vector-of-named-tuples/121430/1 "2024-10-17T19:37:53Z")

</div>

Let’s say I have a simple Turing model

```julia
@model function model()
    C ~ LKJ(3, 1.0)
    x ~ MvNormal(zeros(3), C)
end

```

and obtained some posterior samples via MCMC

```julia
chain = sample(model(), NUTS(), 100)

```

or variational inference

```julia
q = vi(model(), ADVI(10, 1_000))
samples = rand(q, 100)

```

Is there an (easy) way to transform such samples into a vector of named tuples, i.e., `[(C = Matrix, x = Vector), (C = Matrix, x = Vector), ...]` corresponding to the sampled random variables of the Turing model?  
Could not find it in the documentation (hope to have just missed it).

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [October 21, 2024, 3:56pm UTC](https://discourse.julialang.org/t/posterior-samples-as-vector-of-named-tuples/121430/2 "2024-10-21T15:56:34Z")

</div>

What are you trying to accomplish that requires the vector of named tuples?

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [October 21, 2024, 5:01pm UTC](https://discourse.julialang.org/t/posterior-samples-as-vector-of-named-tuples/121430/3 "2024-10-21T17:01:48Z")

</div>

Basically, I wanted to condition the model on some of its parameters using the posterior mean or a couple of samples as values.  
Have found a way to do it though, e.g., `DynamicPPL.value_iterator_from_chain ` provides samples in a suitable format.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [October 21, 2024, 7:00pm UTC](https://discourse.julialang.org/t/posterior-samples-as-vector-of-named-tuples/121430/4 "2024-10-21T19:00:20Z")

</div>

Here is something that might be along those lines: [API · DynamicPPL](https://turinglang.org/DynamicPPL.jl/stable/api/#Condition-and-decondition)
