# Specifying model with threshold / ifelse likelihood in Turing / Soss / Tilde

**URL:** <https://discourse.julialang.org/t/specifying-model-with-threshold-ifelse-likelihood-in-turing-soss-tilde/83473>\
**Category:** Probabilistic Programming\
**Tags:** turing, bayesian-inference, soss\
**Created:** [June 28, 2022, 5:05pm UTC](https://discourse.julialang.org/t/specifying-model-with-threshold-ifelse-likelihood-in-turing-soss-tilde/83473 "2022-06-28T17:05:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakob](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jakob](https://discourse.julialang.org/u/Jakob)\
**Post date:** [June 28, 2022, 5:05pm UTC](https://discourse.julialang.org/t/specifying-model-with-threshold-ifelse-likelihood-in-turing-soss-tilde/83473/1 "2022-06-28T17:05:31Z")

</div>

Hi everyone,

I have a model which is a simplified version of the one presented in [Mulder & Hoff (2021)](http://arxiv.org/abs/2101.05135) and looks something like this:

\begin{align} \mathbf{z}\_{i} &\sim \textrm{MvNormal}(\boldsymbol{\theta}\_{i},\mathbf{I}) \\[0.8ex] \boldsymbol{\theta}\_{ir} &= \mathbf{x}\_{ir}^\top \boldsymbol{\beta} \\[0.8ex] c\_{i} \mid \mathbf{z}\_{i} &\sim \textrm{truncate} \Big( \textrm{Normal}(0, \sigma\_c), -\infty,\max(\textbf{z}\_{i}) \Big) \\[0.8ex] y\_{ir} &= \begin{cases} 1 &\textrm{if } z\_{ir} \> c\_{i} \\ 0 & \textrm{otherwise} \end{cases} \end{align}

In the paper, they discuss a Gibbs sampler implementation but I would like to specify this in a Julia PPL. Here’s how far I got in Turing (probably very non-optimized):

```julia
@model function MRREM(X, y)
	N, A, K = size(X)
	
	β ~ filldist(Normal(0, 1), K)
	σc ~ Exponential()
	
	θ = [@views X[i,:,:] * β for i in 1:N]

	z ~ arraydist([MvNormal(t, I) for t in θ])
	zmax = maximum(z, dims=1) |> vec
	c ~ arraydist([Truncated(Normal(0, σc), -Inf, zm) for zm in zmax])
	# y ~ ???
end

let 
	N, A, k = 100, 20, 3
	X = rand(N, A, k)
	MRREM(X, missing) |> rand
end

```

But I don’t really know how to specify the ifelse for the y\_{ir} in a PPL or how to transform this into something that you can sample from / evaluate the logdensity of. Does someone know what to do here?

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [June 28, 2022, 5:27pm UTC](https://discourse.julialang.org/t/specifying-model-with-threshold-ifelse-likelihood-in-turing-soss-tilde/83473/2 "2022-06-28T17:27:57Z")

</div>

Hi @Jakob ,

It’s pretty typical for PPLs to have difficulty “observing” deterministic functions of a distribution. I think the standard way to do this would be to integrate out `c`, so each `y[i]` follows a Bernoulli distribution as a function of `σc` and `zmax[i]`.

You could write this in the model itself, or (in Soss or Tilde) build a new measure to express this, then use that measure in the model. The advantage of this is that you can test the measure independently and do any performance doing in isolation.

---

<div class="post-metadata">

**Author:** ![Jakob](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jakob](https://discourse.julialang.org/u/Jakob)\
**Post date:** [June 29, 2022, 7:32am UTC](https://discourse.julialang.org/t/specifying-model-with-threshold-ifelse-likelihood-in-turing-soss-tilde/83473/3 "2022-06-29T07:32:14Z")

</div>

Thanks, @cscherrer. That sounded like more math than I’m comfortable with but luckily I found that further down in the paper they give a specification of the probability of the y\_{ir}'s as:

Pr(y\_{ir}=1 |\mathbf{z\_i},\sigma\_c)=\frac{\Phi\big(\frac{z\_{ir}}{\sigma\_c}\big)}{\Phi \Big(\frac{\max(\textbf{z}\_{i})}{\sigma\_c}\Big)}

So I guess I can just work in terms of that.

Btw., I remember seeing something about truncated measures in MeasureTheory but couldn’t find it anymore. Is there something I overlooked or is there anything planned?

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [June 29, 2022, 8:15pm UTC](https://discourse.julialang.org/t/specifying-model-with-threshold-ifelse-likelihood-in-turing-soss-tilde/83473/4 "2022-06-29T20:15:32Z")

</div>

Great! Yes, without working through it explicitly that’s how I’d expect the result to look.

> [@Jakob](#):
>
> I remember seeing something about truncated measures in MeasureTheory but couldn’t find it anymore. Is there something I overlooked or is there anything planned?

There’s a start on it here:

> <https://github.com/JuliaMath/MeasureTheory.jl/pull/194>

It’s not hard in principle, just a matter of working through some corner cases.

---

<div class="post-metadata">

**Author:** ![Jakob](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Jakob](https://discourse.julialang.org/u/Jakob)\
**Post date:** [June 29, 2022, 8:47pm UTC](https://discourse.julialang.org/t/specifying-model-with-threshold-ifelse-likelihood-in-turing-soss-tilde/83473/5 "2022-06-29T20:47:32Z")

</div>

Ah great, I’m looking forward to giving the model a go in Soss / Tilde when that lands.

---

<div class="post-metadata">

**Author:** ![EvoArt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evoart/32/25357_2.png) [@EvoArt](https://discourse.julialang.org/u/EvoArt)\
**Post date:** [July 17, 2022, 8:31pm UTC](https://discourse.julialang.org/t/specifying-model-with-threshold-ifelse-likelihood-in-turing-soss-tilde/83473/6 "2022-07-17T20:31:20Z")

</div>

Please post the code, if you get something working.
