# Using Dict() to directly define variables

**URL:** https://discourse.julialang.org/t/using-dict-to-directly-define-variables/42972
**Category:** Probabilistic Programming
**Created:** [July 13, 2020, 6:27am UTC](https://discourse.julialang.org/t/using-dict-to-directly-define-variables/42972 "2020-07-13T06:27:25Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Emmanuel-R8](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emmanuel-r8/32/11839_2.png) [@Emmanuel-R8](https://discourse.julialang.org/u/Emmanuel-R8)
#### Post date: [July 13, 2020, 6:27am UTC](https://discourse.julialang.org/t/using-dict-to-directly-define-variables/42972/1 "2020-07-13T06:27:25Z")

</div>

Inspired by this article "[Time Series Structure Discovery via Probabilistic Program Synthesis](https://arxiv.org/abs/1611.07051), I wanted to use a dictionary to store a tree of random variables and operations. Below `model1` works; `model2` fails with `"VarName: Mis-formed variable name (m[:top]).var1!"`.

```julia
using Turing

mutable struct ModelNode
    isleaf::Bool
    op::Symbol
    left::ModelNode
    right::ModelNode
    var1
    var2
    var3
end

@model modeltree1() = begin

    m = Dict{Symbol, ModelNode}()

    σ ~ InverseGamma(2,3)
    μ ~ Normal(0,sqrt(σ))
    x ~ Normal(μ, sqrt(σ))
    
    m[:top] = ModelNode(isleaf = true)
    m[:top].var1 = σ
    m[:top].var2 = μ
    m[:top].var3 = x

    return m[:top].var3
end

@model modeltree2() = begin

    m = Dict{Symbol, ModelNode}()

    m[:top] = ModelNode(isleaf = true)
    # σ
    m[:top].var1 ~ InverseGamma(2,3)
    # μ
    m[:top].var2 ~ Normal(0,sqrt(m[:top].var1))
    # variable
    m[:top].var3 ~ Normal(m[:top].var2, sqrt(m[:top].var1))

    return m[:top].var3
end

```

Any idea why this would be the case?

More generally, if anybody is familiar with this paper, do you think that `Turing` is actually the right library to use (versus `Soss` or `Gen`)?

Thank.
