# How to obtain maximized ELBO value from Turing to use as approximation to model evidence?

**URL:** <https://discourse.julialang.org/t/how-to-obtain-maximized-elbo-value-from-turing-to-use-as-approximation-to-model-evidence/88787>\
**Category:** Modelling & Simulations\
**Tags:** question, turing\
**Created:** [October 15, 2022, 10:28pm UTC](https://discourse.julialang.org/t/how-to-obtain-maximized-elbo-value-from-turing-to-use-as-approximation-to-model-evidence/88787 "2022-10-15T22:28:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![neuro\_enthusiast](https://avatars.discourse-cdn.com/v4/letter/n/43a26b/32.png) [@neuro\_enthusiast](https://discourse.julialang.org/u/neuro_enthusiast)\
**Post date:** [October 15, 2022, 10:28pm UTC](https://discourse.julialang.org/t/how-to-obtain-maximized-elbo-value-from-turing-to-use-as-approximation-to-model-evidence/88787/1 "2022-10-15T22:28:59Z")

</div>

Hi all! I’m running variational inference in Turing. However, I can’t seem to find a way to extract the maximized ELBO from the Turing ADVI. If anyone can point me to relevant information that would be wonderful!

---

<div class="post-metadata">

**Author:** ![neuro\_enthusiast](https://avatars.discourse-cdn.com/v4/letter/n/43a26b/32.png) [@neuro\_enthusiast](https://discourse.julialang.org/u/neuro_enthusiast)\
**Post date:** [October 22, 2022, 2:58pm UTC](https://discourse.julialang.org/t/how-to-obtain-maximized-elbo-value-from-turing-to-use-as-approximation-to-model-evidence/88787/2 "2022-10-22T14:58:54Z")

</div>

If there isn’t an easy way to extract this value, is there a way straightforward way to calculate ELBO with given parameter values and observed data?

---

<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:** [October 22, 2022, 5:37pm UTC](https://discourse.julialang.org/t/how-to-obtain-maximized-elbo-value-from-turing-to-use-as-approximation-to-model-evidence/88787/3 "2022-10-22T17:37:35Z")

</div>

I looked at some info in the tutorials etc and didn’t see an obvious way. I’m on my phone so I can’t look now, but have you tried looking at the code for ADVI and see where it calculates the ELBO? my guess is it should be quite straightforward to use but it’s not in the docs

---

<div class="post-metadata">

**Author:** ![neuro\_enthusiast](https://avatars.discourse-cdn.com/v4/letter/n/43a26b/32.png) [@neuro\_enthusiast](https://discourse.julialang.org/u/neuro_enthusiast)\
**Post date:** [October 22, 2022, 9:31pm UTC](https://discourse.julialang.org/t/how-to-obtain-maximized-elbo-value-from-turing-to-use-as-approximation-to-model-evidence/88787/4 "2022-10-22T21:31:34Z")

</div>

Thanks for the tip! I looked into it and turns out Turing interfaces with AdvancedVI.jl, which has a built in function for extracting the ELBO, detailed in its [doc](https://github.com/TuringLang/AdvancedVI.jl).

The doc example only passes in a custom log joint probability function to get the maximized ELBO, but it seems like passing in a Turing model works too, fortunately.

```julia
using Turing, AdvancedVI, Turing: Variational
d = 2; n = 100;
observations = randn((d, n));
advi = ADVI(10, 10000)
## turing model
@model function model(y, d)
    μ ~ MvNormal(ones(d))
    y ~ MvNormal(μ, ones(d))
end;

q = vi(model(observations, d), advi)
#I'm not sure yet what the last number does in elbo()
AdvancedVI.elbo(advi, q, model(observations, d), 10000) 

```

The contributors seem to be interested in redesigning it at some point, so the solution above might not be applicable to future readers.

---

<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:** [October 23, 2022, 12:41am UTC](https://discourse.julialang.org/t/how-to-obtain-maximized-elbo-value-from-turing-to-use-as-approximation-to-model-evidence/88787/5 "2022-10-23T00:41:38Z")

</div>

> [@neuro\_enthusiast](#):
>
> ```julia
> = vi(model(observations, d), advi)
> #I'm not sure yet what the last number does in elbo()
> 
> ```

The ELBO is calculated by drawing a sample and calculating a sum, I’d guess this number might be setting the size of the sample

---

<div class="post-metadata">

**Author:** ![neuro\_enthusiast](https://avatars.discourse-cdn.com/v4/letter/n/43a26b/32.png) [@neuro\_enthusiast](https://discourse.julialang.org/u/neuro_enthusiast)\
**Post date:** [October 23, 2022, 11:02am UTC](https://discourse.julialang.org/t/how-to-obtain-maximized-elbo-value-from-turing-to-use-as-approximation-to-model-evidence/88787/6 "2022-10-23T11:02:33Z")

</div>

I see, that makes sense. Thanks!
