# Sample dependent variables in Turing

**URL:** https://discourse.julialang.org/t/sample-dependent-variables-in-turing/114993
**Category:** Probabilistic Programming
**Tags:** question, turing
**Created:** [May 31, 2024, 4:55am UTC](https://discourse.julialang.org/t/sample-dependent-variables-in-turing/114993 "2024-05-31T04:55:05Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![swsw](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@swsw](https://discourse.julialang.org/u/swsw)
#### Post date: [May 31, 2024, 4:55am UTC](https://discourse.julialang.org/t/sample-dependent-variables-in-turing/114993/1 "2024-05-31T04:55:05Z")

</div>

I’d like to sample two variables X1 ~ Uniform(0,1) and X2 ~ Uniform(0, X1)

> using Turing  
> @model function model()  
> X1 ~ Uniform(1e-5, 1)  
> X2 ~ Uniform(0, X1)  
> end  
> my\_model = model(); sample(my\_model, NUTS(), 10000)

![image](https://global.discourse-cdn.com/julialang/original/3X/c/f/cf080ef5d63555745fca7fbd8d49697b619de0e8.png)

![image](https://global.discourse-cdn.com/julialang/original/3X/9/f/9fa4af88700b9201c54f5366fa2f9d9f012fc136.png)

However, while the distribution of X1 looks correct (that is it follows Uniform(0,1)), the distribution of X2 is very strange and each time it changes a lot. Analytically the mean of X2 should be 0.25 but this result is never reached. Can anyone help me figure out where I did wrong?

---

<div class="post-metadata">

### Author: ![mhauru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhauru/32/208880_2.png) [@mhauru](https://discourse.julialang.org/u/mhauru)
#### Post date: [May 31, 2024, 9:29am UTC](https://discourse.julialang.org/t/sample-dependent-variables-in-turing/114993/2 "2024-05-31T09:29:29Z")

</div>

Could this be a matter of not having enough samples? I increased the chain length 100-fold to `1_000_000` and am seeing stats like

```julia
Summary Statistics
  parameters mean std mcse ess_bulk ess_tail rhat ess_per_sec
      Symbol Float64 Float64 Float64 Float64 Float64 Float64 Float64

          X1 0.4999 0.2887 0.0003 796644.8328 663422.4909 1.0000 19973.0440
          X2 0.2497 0.2201 0.0002 765366.0740 647466.4402 1.0000 19188.8400

```

---

<div class="post-metadata">

### Author: ![swsw](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@swsw](https://discourse.julialang.org/u/swsw)
#### Post date: [May 31, 2024, 9:46am UTC](https://discourse.julialang.org/t/sample-dependent-variables-in-turing/114993/3 "2024-05-31T09:46:28Z")

</div>

Thanks so much! That’s interesting. But I still have this problem.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/b/7b60200c00dacc5bd8dff1eec5b38af80ca17e12.png)

Actually such a problem doesn’t exist if X2 ~ Normal(X1, 1) or Exponential(X1). Seems only happen if X2 ~ Uniform(0, X1).

what version of turing are you using? Mine are v0.29.3 and v0.30, with both having the same issue. Did you see the same problem when sample size is 10^4 (actually I think that’s big enough if I am not mistaken).

Thanks so much!

---

<div class="post-metadata">

### Author: ![mhauru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhauru/32/208880_2.png) [@mhauru](https://discourse.julialang.org/u/mhauru)
#### Post date: [May 31, 2024, 1:03pm UTC](https://discourse.julialang.org/t/sample-dependent-variables-in-turing/114993/4 "2024-05-31T13:03:08Z")

</div>

I thought I was getting wildly varying means at 10k samples but I’m not, my bad. Indeed, 10k seems plenty.

I’m using Turing v0.32.2 and Distributions v0.25.109.

---

<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 3, 2024, 8:48am UTC](https://discourse.julialang.org/t/sample-dependent-variables-in-turing/114993/5 "2024-06-03T08:48:41Z")

</div>

Ah yes, this was a bug that was recently fixed. In short, the inference was actually performed correctly, but there was a bug in the code transforming the variables to the format we present it to the end-user. If you go to Turing 0.32 everything should work just fine again:)

Can confirm this works on my end with Turing.jl@0.32.3 and 10k samples (reproducible).

> Did you see the same problem when sample size is 10^4 (actually I think that’s big enough if I am not mistaken).

Recommend looking at effective sample size (ESS) in the chain when considering “how many samples do I have”. Even if you run 10k iterations, the sampler could get stuck, but that would be indicated in the ESS.

When I run your code (again, on v0.32.3), with 10k iterations I consistently get \>5k ESS, which is pleeeeenty for evaluating whether we’re getting the mean correctly:) And in my case, I consistently get 0.5 and 0.25 as expected 👍

---

<div class="post-metadata">

### Author: ![swsw](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@swsw](https://discourse.julialang.org/u/swsw)
#### Post date: [June 5, 2024, 1:29am UTC](https://discourse.julialang.org/t/sample-dependent-variables-in-turing/114993/6 "2024-06-05T01:29:57Z")

</div>

thank you so much! the newest version got it correct. Also thanks for ESS!

---

<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 5, 2024, 12:50pm UTC](https://discourse.julialang.org/t/sample-dependent-variables-in-turing/114993/7 "2024-06-05T12:50:07Z")

</div>

Glad to hear:)

Would you mind marking the answer as the “solution”? That way others who arrive at the same issue can easily see the answer:)

---

<div class="post-metadata">

### Author: ![swsw](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@swsw](https://discourse.julialang.org/u/swsw)
#### Post date: [June 7, 2024, 8:31am UTC](https://discourse.julialang.org/t/sample-dependent-variables-in-turing/114993/8 "2024-06-07T08:31:35Z")

</div>

Thank you for the reminder. Marked!
