# Issue querying probabilities using chain

**URL:** <https://discourse.julialang.org/t/issue-querying-probabilities-using-chain/50383>\
**Category:** Probabilistic Programming\
**Tags:** turing\
**Created:** [November 18, 2020, 3:53pm UTC](https://discourse.julialang.org/t/issue-querying-probabilities-using-chain/50383 "2020-11-18T15:53:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![michielver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michielver/32/19554_2.png) [@michielver](https://discourse.julialang.org/u/michielver)\
**Post date:** [November 18, 2020, 3:53pm UTC](https://discourse.julialang.org/t/issue-querying-probabilities-using-chain/50383/1 "2020-11-18T15:53:06Z")

</div>

Hi,

This is my first post, so I will try to make it as clear as possible, otherwise point out anything that is unclear.

I am running into an issue with querying probabilities using chains. I tried to reduce it to as small of a problem as possible which has led me to believe it might be a bug/not the intended behavior. Either that, or I am missing something else.

In the code I pasted below, the first 3 results (result1, result2, and result3) all print different values whenever they are rerun as individual cells, even though I would expect them to stay the same since the chain doesn’t change.  
Afterwards, trying the joint probabilities of x and y, they do stay consistent when rerunning the cell (as one might expect).

So the question:

- Am I doing something wrong here or might this be a bug?

The code below can also be found as a Pluto.jl notebook on github: [https://github.com/mgmverburg/Turing\_examples/blob/master/potential\_query\_bug.jl](https://github.com/mgmverburg/Turing_examples/blob/master/potential_query_bug.jl)

```julia
using Turing, Distributions

@model function gdemo(x, y)
	if x === missing || x === nothing
        # Initialize `x` if missing
        x = Vector{Float64}(undef, 2)
	end
	n = length(x)
    s ~ InverseGamma(2, 3)
    m ~ Normal(0, sqrt(s))
    x ~ filldist(Normal(m, sqrt(s)), n)
	for i in 1:length(y)
    	y[i] ~ Normal(x[i], sqrt(s))
	end
end

model_gdemo = gdemo([1.0, 0.0], [1.5, 0.0])
c2 = sample(model_gdemo, NUTS(0.65), 100)
result1 = prob"y = [1.5] | chain=c2, model = model_gdemo, x = [1.0]"
println(mean(result1))
result2 = prob"y = [1.5] | chain=c2, model = model_gdemo, x = [0.0]"
println(mean(result2))
result3 = prob"y = [1.5] | chain=c2, model = model_gdemo, x = nothing"
println(mean(result3))
result4 = prob"y = [1.5], x = [1.0] | chain=c2, model = model_gdemo"
println(mean(result4))
result5 = prob"y = [1.5], x = [0.0] | chain=c2, model = model_gdemo"
println(mean(result5))

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![wulpuqu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wulpuqu/32/17640_2.png) [@wulpuqu](https://discourse.julialang.org/u/wulpuqu)\
**Post date:** [November 18, 2020, 4:55pm UTC](https://discourse.julialang.org/t/issue-querying-probabilities-using-chain/50383/2 "2020-11-18T16:55:02Z")

</div>

From your model, I find the following factorization for your joint probability  
`p(y,x,m,s)=p(x|m,s)p(y|m,s)p(m,s)`  
So since `y` does not depend on `x` then for any value of `x`, `p(y|x)=p(y)`

---

<div class="post-metadata">

**Author:** ![michielver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michielver/32/19554_2.png) [@michielver](https://discourse.julialang.org/u/michielver)\
**Post date:** [November 18, 2020, 6:50pm UTC](https://discourse.julialang.org/t/issue-querying-probabilities-using-chain/50383/3 "2020-11-18T18:50:09Z")

</div>

Thank you very much! You are right, my ‘minimum example’ here is wrong. When I went back to my bigger model to check the behavior, the main issue was also different. Indeed (as in my updated question is now stated) the value keeps changing for when I compute conditional probability queries, which are also not giving me the correct values. I update my example and now it correctly shows the ‘unexpected’ behavior. What I previously showed was also unexpected behavior, but caused by my incorrect model xD

TL;DR: Updated my example with the correct ‘incorrect’ behavior

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [November 18, 2020, 8:06pm UTC](https://discourse.julialang.org/t/issue-querying-probabilities-using-chain/50383/4 "2020-11-18T20:06:27Z")

</div>

Yeah, that seems to be a bug. You can see that Turing “accidentally” resamples `x` even though it is passed in the list of values to condition on.

```julia
@model function gdemo(x, y)
    s ~ InverseGamma(2, 3)
    m ~ Normal(0, sqrt(s))
    x ~ filldist(Normal(m, sqrt(s)), length(y))
    @show x
    for i in 1:length(y)
        @show x[i]
        y[i] ~ Normal(x[i], sqrt(s))
    end
end

result1 = prob"y = [1.5] | chain=c2, model = model_gdemo, x = [1.0]"

```

Btw. you don’t need to load `Distributions` as this is reexported by Turing. Also the lines in which you initialize `x` if it is missing is not necessary.

cc: @mohamed82008

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [November 19, 2020, 3:20am UTC](https://discourse.julialang.org/t/issue-querying-probabilities-using-chain/50383/5 "2020-11-19T03:20:22Z")

</div>

Could you file an issue for this [at DynamicPPL](https://github.com/TuringLang/DynamicPPL.jl)?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [November 19, 2020, 3:41am UTC](https://discourse.julialang.org/t/issue-querying-probabilities-using-chain/50383/6 "2020-11-19T03:41:13Z")

</div>

Please open an issue to track this. (oops didn’t see your post Cameron!)

---

<div class="post-metadata">

**Author:** ![michielver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michielver/32/19554_2.png) [@michielver](https://discourse.julialang.org/u/michielver)\
**Post date:** [November 19, 2020, 8:18am UTC](https://discourse.julialang.org/t/issue-querying-probabilities-using-chain/50383/7 "2020-11-19T08:18:23Z")

</div>

Thanks, I submitted an issue at dynamic PPL! [https://github.com/TuringLang/DynamicPPL.jl/issues/190](https://github.com/TuringLang/DynamicPPL.jl/issues/190)
