# Two equivalent conditioning syntaxes giving different likelihood values?

**URL:** <https://discourse.julialang.org/t/two-equivalent-conditioning-syntaxes-giving-different-likelihood-values/100320>\
**Category:** Probabilistic Programming\
**Tags:** bug, turing\
**Created:** [June 14, 2023, 9:03am UTC](https://discourse.julialang.org/t/two-equivalent-conditioning-syntaxes-giving-different-likelihood-values/100320 "2023-06-14T09:03:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BatyLeo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/batyleo/32/35017_2.png) [@BatyLeo](https://discourse.julialang.org/u/BatyLeo)\
**Post date:** [June 14, 2023, 9:03am UTC](https://discourse.julialang.org/t/two-equivalent-conditioning-syntaxes-giving-different-likelihood-values/100320/1 "2023-06-14T09:03:16Z")

</div>

Hello,  
I’m new to Turing.j,l and probabilistic programming in general.  
While playing around with small example codes, I faced a behaviour I don’t understand about the conditioning syntax:

If I understand well, these two model definitions are equivalent:

```julia
@model function model1(x)
    μ ~ Uniform(0, 2)
    x ~ LogNormal(μ, 1)
end;

@model function model2()
    μ ~ Uniform(0, 2)
    x ~ LogNormal(μ, 1)
end;

model2(x) = model2() | (; x)

```

However, when evaluating the negative log likelihood (using syntax from source code [here](https://github.com/TuringLang/Turing.jl/blob/861ae37ec67e57cda83ff5a85c063a624718b167/src/modes/OptimInterface.jl#L136) and [there](https://github.com/TuringLang/Turing.jl/blob/861ae37ec67e57cda83ff5a85c063a624718b167/src/modes/ModeEstimation.jl#L102)) for these two models I obtain different results:

```julia
v = 1.0
w = [1.0]

ctx = Turing.OptimizationContext(DynamicPPL.LikelihoodContext())
Turing.OptimLogDensity(model1(v), ctx)(w) # = 1.4189385332046727
Turing.OptimLogDensity(model2(v), ctx)(w) # = 2.112085713764618

```

When computing it by hand it seems that the first model is correct:

```julia
nll(μ) = -logpdf(LogNormal(μ, 1), v)
nll(w[1]) # = 1.4189385332046727

```

Am I missing something ?

---

<div class="post-metadata">

**Author:** ![torfjelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torfjelde/32/206542_2.png) [@torfjelde](https://discourse.julialang.org/u/torfjelde)\
**Post date:** [June 19, 2023, 11:18am UTC](https://discourse.julialang.org/t/two-equivalent-conditioning-syntaxes-giving-different-likelihood-values/100320/2 "2023-06-19T11:18:24Z")

</div>

This is a bug with `OptimizationContext` 😬 I’ll get on it asap!

---

<div class="post-metadata">

**Author:** ![torfjelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torfjelde/32/206542_2.png) [@torfjelde](https://discourse.julialang.org/u/torfjelde)\
**Post date:** [June 19, 2023, 11:42pm UTC](https://discourse.julialang.org/t/two-equivalent-conditioning-syntaxes-giving-different-likelihood-values/100320/3 "2023-06-19T23:42:06Z")

</div>

Should be resolved once this goes through: [Bugfix for `condition` + optimization by torfjelde · Pull Request #2016 · TuringLang/Turing.jl · GitHub](https://github.com/TuringLang/Turing.jl/pull/2016)

---

<div class="post-metadata">

**Author:** ![BatyLeo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/batyleo/32/35017_2.png) [@BatyLeo](https://discourse.julialang.org/u/BatyLeo)\
**Post date:** [June 20, 2023, 7:31am UTC](https://discourse.julialang.org/t/two-equivalent-conditioning-syntaxes-giving-different-likelihood-values/100320/4 "2023-06-20T07:31:55Z")

</div>

Thank you !

---

<div class="post-metadata">

**Author:** ![torfjelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torfjelde/32/206542_2.png) [@torfjelde](https://discourse.julialang.org/u/torfjelde)\
**Post date:** [June 21, 2023, 1:45pm UTC](https://discourse.julialang.org/t/two-equivalent-conditioning-syntaxes-giving-different-likelihood-values/100320/5 "2023-06-21T13:45:51Z")

</div>

This should now be fixed on 0.16.2:))
