# Turing.jl: Warnings when running generated\_quantities

**URL:** <https://discourse.julialang.org/t/turing-jl-warnings-when-running-generated-quantities/64698>\
**Category:** Probabilistic Programming\
**Tags:** turing\
**Created:** [July 15, 2021, 3:16pm UTC](https://discourse.julialang.org/t/turing-jl-warnings-when-running-generated-quantities/64698 "2021-07-15T15:16:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hakan\_Kjellerstrand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hakan_kjellerstrand/32/8447_2.png) [@Hakan\_Kjellerstrand](https://discourse.julialang.org/u/Hakan_Kjellerstrand)\
**Post date:** [July 15, 2021, 3:16pm UTC](https://discourse.julialang.org/t/turing-jl-warnings-when-running-generated-quantities/64698/1 "2021-07-15T15:16:49Z")

</div>

When calling `generated_quantities` there is always a huge number of warnings with the following message

```julia
Warning: the following keys were not found in `vi`, and thus `kernel!` was not applied to these: ["lp"]
└ @ DynamicPPL ~/.julia/packages/DynamicPPL/h8FWT/src/varinfo.jl:1314

```

Is there some way I can get around this warning messages, alternative to get the same functionality without need to run `generated_quantities`.

Since Turing.jl now supports the great `Dirac` function, I don’t have that much need to use `generated_quantities`, but sometimes it’s nice to be able to work with non parameters or collection of parameters.

Here’s an example where I use `generated_quantities` to show the probability distributions of the combination of `female`, `recovery`, and `drug` (using my `show_var_dist_pct` defined below). The summary of the chains is shown first but is then “drowned” in the huge number of warnings from `generated_quantities`.

```julia
using Turing

# Show distribution of a variable in a MCMCChain
# Sort the dictionary in order of decreasing occurrence (percentage)
# Examples:
# - show_var_dist_pct(chains, :n) show all entries
# - show_var_dist_pct(chains, :n, 10) show first 10 entries (e.g. for large tables)
function show_var_dist_pct(chains::Chains, var, num=0)

    if var in chains.name_map.parameters
        println("Distributions of variable $var (num:$num)")
        len = length(vcat(chains[var]...)) # handle multiple chains
        c = 0
        for kv in sort(collect(make_hash(chains[var])),by=x->x[2],rev=true)
            c += 1
            if (num == 0) || (num > 0 && c <= num)
                @printf "%-3.5f => % 7d (%2.6f)\n" kv[1] kv[2] kv[2]/len
            end
        end
    else
        # println("Variable $var is not in chains")
    end
end

#
# Simple model of Simpson's "paradox"
#
@model function simpson(problem)
    female ~ flip(0.5)
    drug ~ female ? flip(10/40.0) : flip(30/40.0)

    recovery ~ if drug == true && female==true flip(0.2)
                elseif drug == true && female==false flip(0.6)
                elseif drug == false && female==true flip(0.3)
                elseif drug == false && female==false flip(0.7)
                else flip(0.0)
                end

    # 6. prob(recovery|(\+ drug,\+female)): 0.7
    true ~ Dirac(drug == false && female == false)

    return female, recovery, drug
end

model = simpson(problem)
chains = sample(model, MH(), 10_000)
display(chains)

# This the distributions of the different combinations of
# female, recovery, group
genq = generated_quantities(model, chains)
show_var_dist_pct(genq)

```

Here’s an example of the probability distribution for this problem:

```julia
Distributions of variable (num:0)
(false, true, false)	=>	29007 (0.725175)
(false, false, false)	=>	10970 (0.27425)
(false, false, true)	=>	17 (0.000425)
(true, true, false)	=>	4 (0.0001)
(true, true, true)	=>	2 (5.0e-5)

```

(The full model is here: [http://hakank.org/julia/turing/simpson.jl](http://hakank.org/julia/turing/simpson.jl) )

---

<div class="post-metadata">

**Author:** ![sethaxen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sethaxen/32/35604_2.png) [@sethaxen](https://discourse.julialang.org/u/sethaxen)\
**Post date:** [July 15, 2021, 3:50pm UTC](https://discourse.julialang.org/t/turing-jl-warnings-when-running-generated-quantities/64698/2 "2021-07-15T15:50:23Z")

</div>

`generated_quantities` and `pointwise_loglikelihoods` don’t like the internal parameters (i.e. statistics) in `Chains`. Just extracting the actual sampled parameters should resolve this:

```julia
chains_params = Turing.MCMCChains.get_sections(chain, :parameters)
generated_quantities(model, chains_params)

```

---

<div class="post-metadata">

**Author:** ![Hakan\_Kjellerstrand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hakan_kjellerstrand/32/8447_2.png) [@Hakan\_Kjellerstrand](https://discourse.julialang.org/u/Hakan_Kjellerstrand)\
**Post date:** [July 15, 2021, 3:57pm UTC](https://discourse.julialang.org/t/turing-jl-warnings-when-running-generated-quantities/64698/3 "2021-07-15T15:57:52Z")

</div>

@sethaxen Thanks, Seth. This works really great!
