# Getting generated\_quantities from optimization result in Turing.jl

**URL:** <https://discourse.julialang.org/t/getting-generated-quantities-from-optimization-result-in-turing-jl/87025>\
**Category:** General Usage\
**Tags:** turing\
**Created:** [September 10, 2022, 12:57am UTC](https://discourse.julialang.org/t/getting-generated-quantities-from-optimization-result-in-turing-jl/87025 "2022-09-10T00:57:45Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [September 10, 2022, 12:57am UTC](https://discourse.julialang.org/t/getting-generated-quantities-from-optimization-result-in-turing-jl/87025/1 "2022-09-10T00:57:45Z")

</div>

Suppose I optimize my Turing model to get some kind of reasonable parameter values… Is there a way to get the generated\_quantities?

```julia
mapest = optimize(mymodel,MAP())
gen_quant = ???

```

---

<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:** [September 10, 2022, 10:36am UTC](https://discourse.julialang.org/t/getting-generated-quantities-from-optimization-result-in-turing-jl/87025/2 "2022-09-10T10:36:19Z")

</div>

I tried `generated_quantities(model, opt_parms)`, but it did not work. I don’t know if it is possible, but I will leave a MWE for others to use. Computing the generated quantity outside of the model is one workaround: `z = 1 - opt_parms.values[:p]`

**MWE**

```julia
using Turing, Random, Optim

# Set the true probability of heads in a coin.
p_true = 0.5

# Iterate from having seen 0 observations to 100 observations.
Ns = 0:100

# Draw data from a Bernoulli distribution, i.e. draw heads or tails.
Random.seed!(12)
data = rand(Bernoulli(p_true), last(Ns))

# Declare our Turing model.
@model function coinflip(y)
    # Our prior belief about the probability of heads in a coin.
    p ~ Beta(1, 1)
    z = 1 - p

    # The number of observations.
    N = length(y)
    for n in 1:N
        # Heads or tails of a coin are drawn from a Bernoulli distribution.
        y[n] ~ Bernoulli(p)
    end
    return (;z)
end

model = coinflip(data)
opt_parms = optimize(model, MAP())
generated_quantities(model, opt_parms)

```

---

<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:** [September 10, 2022, 2:03pm UTC](https://discourse.julialang.org/t/getting-generated-quantities-from-optimization-result-in-turing-jl/87025/3 "2022-09-10T14:03:41Z")

</div>

> [@Christopher\_Fisher](#):
>
> Computing the generated quantity outside of the model is one workaround: `z = 1 - opt_parms.values[:p]`

This is no good for general models as now the code can diverge in the two places leading to bugs. Thanks for the MWE!

---

<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:** [September 10, 2022, 11:45pm UTC](https://discourse.julialang.org/t/getting-generated-quantities-from-optimization-result-in-turing-jl/87025/4 "2022-09-10T23:45:41Z")

</div>

No problem.

It might be worth making a feature request on GitHub.
