# How to obtain log likelihoods from turing model

**URL:** https://discourse.julialang.org/t/how-to-obtain-log-likelihoods-from-turing-model/88700
**Category:** Modelling & Simulations
**Tags:** question, turing
**Created:** [October 13, 2022, 11:28pm UTC](https://discourse.julialang.org/t/how-to-obtain-log-likelihoods-from-turing-model/88700 "2022-10-13T23:28:01Z")
**Posts on this page:** 5
**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 13, 2022, 11:28pm UTC](https://discourse.julialang.org/t/how-to-obtain-log-likelihoods-from-turing-model/88700/1 "2022-10-13T23:28:01Z")

</div>

What are some good ways of obtaining the likelihood of observing some data given the parameters ( P(y|\theta), where I feed in y and \theta )? I couldn’t quite find it in the documentation.

edit: would be especially helpful if I can get likelihoods when sampling from the posterior using one of the samplers, such as NUTS.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 14, 2022, 12:10am UTC](https://discourse.julialang.org/t/how-to-obtain-log-likelihoods-from-turing-model/88700/2 "2022-10-14T00:10:55Z")

</div>

call `loglikelihood`

---

<div class="post-metadata">

### Author: ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)
#### Post date: [October 14, 2022, 4:54pm UTC](https://discourse.julialang.org/t/how-to-obtain-log-likelihoods-from-turing-model/88700/3 "2022-10-14T16:54:05Z")

</div>

To elaborate, here’s a simple example showing how to actually do it:

```julia
using Turing

@model function demo(x)
    μ ~ Normal(0, 1)
    σ ~ Exponential(1)
    x .~ Normal(μ, σ)
end

m = demo(randn(10))
loglikelihood(m, (μ=2, σ=3))

```

The `MCMCChain` from sampling contains two (identical?) fields, `:lp` and `:log_density`, with the value of the log-posterior at each sample point, but those values will include the priors as well as the likelihood. Not sure if there’s a built-in way to get that from a posterior chain, but you can calculate the log-likelihood manually inside the model, return it, and get it after sampling via `generated_quantities`:

```julia
@model function demo1(x)
    μ ~ Normal(0, 1)
    σ ~ Exponential(1)
    loglik = loglikelihood(Normal(μ, σ), x)
    Turing.@addlogprob!(loglik)
    return loglik
end
m1 = demo1(randn(10))
c1 = sample(m1, NUTS(), 100)
ll1 = generated_quantities(m1, c1)

```

---

<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 14, 2022, 6:59pm UTC](https://discourse.julialang.org/t/how-to-obtain-log-likelihoods-from-turing-model/88700/4 "2022-10-14T18:59:55Z")

</div>

Thanks so much for the detailed example!

---

<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 14, 2022, 7:00pm UTC](https://discourse.julialang.org/t/how-to-obtain-log-likelihoods-from-turing-model/88700/5 "2022-10-14T19:00:07Z")

</div>

thanks!
