# Truncate a distribution using draws of a random variable as bound

**URL:** <https://discourse.julialang.org/t/truncate-a-distribution-using-draws-of-a-random-variable-as-bound/117683>\
**Category:** Statistics\
**Tags:** turing, distributions\
**Created:** [July 31, 2024, 3:04pm UTC](https://discourse.julialang.org/t/truncate-a-distribution-using-draws-of-a-random-variable-as-bound/117683 "2024-07-31T15:04:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobias-Thomas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias-thomas/32/20416_2.png) [@Tobias-Thomas](https://discourse.julialang.org/u/Tobias-Thomas)\
**Post date:** [July 31, 2024, 3:04pm UTC](https://discourse.julialang.org/t/truncate-a-distribution-using-draws-of-a-random-variable-as-bound/117683/1 "2024-07-31T15:04:27Z")

</div>

Hi,

Is it possible to use the draw of one random variable as the bound for another one? In my example, I have a simple linear relationship with x-values in the interval [0,1] and I want to bound the resulting y-value to be positive.  
I have tried to use the first variable as the bound for the second one, yet that does not work (see the MWE below).

I already saw this [discussion](https://github.com/JuliaStats/Distributions.jl/issues/745), which is sadly only about the parameters of the distribution and not the values to truncate on.

Is this even possible, if yes how? Thanks a lot for any kind of help!

MWE:

```julia
a = 0:.05:1
b = 2 .- 1.5 * a + randn(21)
@model function truncate_mwe(x, y)
	intercept ~ truncated(Normal(0, 1), 0, Inf)
	slope ~ truncated(Normal(0, 1), -intercept, Inf)

	for i in eachindex(y)
		y[i] ~ Normal(intercept + slope * x[i], 1)
	end
end
chain_mwe = sample(truncate_mwe(a,b), NUTS(), 1000)
# for me this chain always has some samples,
# for which intercept + slope < 0

```

---

<div class="post-metadata">

**Author:** ![Tobias-Thomas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias-thomas/32/20416_2.png) [@Tobias-Thomas](https://discourse.julialang.org/u/Tobias-Thomas)\
**Post date:** [September 2, 2024, 7:52am UTC](https://discourse.julialang.org/t/truncate-a-distribution-using-draws-of-a-random-variable-as-bound/117683/2 "2024-09-02T07:52:27Z")

</div>

For anyone who might stumble upon this in the future, I found help in a GitHub issue (which also links many other relevant sources).  
The TLDR is that it works now, you just have to set the `upper` and `lower` kwargs (and update your packages).

> <https://github.com/JuliaStats/Distributions.jl/issues/1889>
>
> Hi,
> I am currently building a \[Turing.jl\](https://github.com/TuringLang/Turing.…jl) model and want to use the draw of one random variable as the bound for another one, using the \`truncated\` function.
> While the usage does not throw an error, it definitely does not seem to work (see MWE below).
> I already saw this other \[issue\](https://github.com/JuliaStats/Distributions.jl/issues/745), yet it was only about using random variables as parameters for the distribution. The case for using the draw of a random variable as a bound still seems to be not working.
> Am I doing something wrong, or is there another way I could achieve using one random variable as the bound for another one?
> 
> \`\`\`julia
> a = 0:.05:1
> b = 2 .- 1.5 \* a + randn(21)
> @model function truncate\_mwe(x, y)
> intercept ~ truncated(Normal(0, 1), 0, Inf)
> slope ~ truncated(Normal(0, 1), -intercept, Inf)
> 
> for i in eachindex(y)
> y\[i\] ~ Normal(intercept + slope \* x\[i\], 1)
> end
> end
> chain\_mwe = sample(truncate\_mwe(a,b), NUTS(), 1000)
> \`\`\`
> For me, this chain always has some samples, for which intercept + slope \< 0, which should not be the case as the maximum value for x in this case is 1.
> 
> Kind regards and thanks in advance for any help!
