# Printing loglikelihood values

**URL:** <https://discourse.julialang.org/t/printing-loglikelihood-values/99384>\
**Category:** New to Julia\
**Tags:** turing, chain\
**Created:** [May 25, 2023, 2:38pm UTC](https://discourse.julialang.org/t/printing-loglikelihood-values/99384 "2023-05-25T14:38:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [May 25, 2023, 2:38pm UTC](https://discourse.julialang.org/t/printing-loglikelihood-values/99384/1 "2023-05-25T14:38:59Z")

</div>

Can we print likelihood values while sampling by NUTS() in turing.jl? Is there any way ? So, we don’t sit blind during run and have some info.

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [May 25, 2023, 4:11pm UTC](https://discourse.julialang.org/t/printing-loglikelihood-values/99384/2 "2023-05-25T16:11:45Z")

</div>

Do something like the following:

```julia
using Distributions, Turing, StatsPlots
function main(n)
# sample is from exponential, prior is lognormal
y = rand(Exponential(3.0), n)
# the model: prior and likelihood
@model function ExpModel(y)
    θ ~ LogNormal(1.,1.) # the prior
    y ~ Exponential(θ) # the likelihood
    @show loglikelihood(Exponential(θ), y)
end    
# get the chain
chain = sample(ExpModel(y), NUTS(200, 0.65), 1000)
end

## run it
n = 300
chain = main(n)

## examine and plot last bit of chain chain
display(chain)
plot(chain[end-500:end])

```

I just explained this example in class earlier today, so I had it in mind! The line with @show is what displays the loglikelihood. Doing that might slow things down, I haven’t checked.

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [May 26, 2023, 7:19am UTC](https://discourse.julialang.org/t/printing-loglikelihood-values/99384/3 "2023-05-26T07:19:00Z")

</div>

Thank you for your reply and example 🙂

It shows loglikelihood but its more like just printing everything. I was looking for something which be more like updating loglikelihood lets say after every x = 100 . So, for your example it should only print 3 times for n=300

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [May 26, 2023, 8:23am UTC](https://discourse.julialang.org/t/printing-loglikelihood-values/99384/4 "2023-05-26T08:23:26Z")

</div>

I suppose that would require modifying `sample` from Turing. It might be a nice issue to raise, I can see it being useful for long running samples that might be straying into problems.
