Choosing Different Distributions based on if - else condition in WinBugs/JAGS
Asked Answered
O

1

11

I am trying to write a Winbugs/Jags model for modeling multi grain topic models (exactly this paper -> http://www.ryanmcd.com/papers/mg_lda.pdf)

Here I would like to choose a different distribution based on a particular value. For Eg: I would like to do something like

`if ( X[i] > 0.5 )
{
Z[i] ~ dcat(theta-gl[D[i], 1:K-gl])
W[i] ~ dcat(phi-gl[z[i], 1:V])
}
else 
{
Z[i] ~ dcat(theta-loc[D[i], 1:K-loc])
W[i] ~ dcat(phi-loc[z[i], 1:V])
}
`

Is this possible to be done in Winbugs/JAGS?

Overmaster answered 14/3, 2013 at 16:4 Comment(1)
i do not too sure I fully understand your code. if statements are possible in WinBUGS....mrc-bsu.cam.ac.uk/bugs/faqs/contents.shtml#q15Brandibrandice
A
10

Winbugs/JAGS is not a procedural language, so you cannot use the construct like that. Use step function. Quote from the manual:

step(e) ...... 1 if e >= 0; 0 otherwise

So you need a trick to change the condition:

X[i] > 0.5   <=>     
X[i] - 0.5 > 0  <=> 
!(X[i] - 0.5 <= 0) <=>
!(-(X[i] - 0.5) >= 0) <=>
!(step(-(X[i] - 0.5)) == 1) <=>
step(-(X[i] - 0.5)) == 0

and then use this for indexing trick:

# then branch
Z_branch[i, 1] ~ dcat(theta-gl[D[i], 1:K-gl])
W_branch[i, 1] ~ dcat(phi-gl[z[i], 1:V])

# else branch
Z_branch[i, 2] ~ dcat(theta-loc[D[i], 1:K-loc])
W_branch[i, 2] ~ dcat(phi-loc[z[i], 1:V])

# decision here
if_branch[i] <- 1 + step(-(X[i] - 0.5)) # 1 for "then" branch, 2 for "else" branch
Z[i] ~ Z_branch[i, if_branch[i]]
W[i] ~ W_branch[i, if_branch[i]]
Artificiality answered 11/12, 2013 at 21:13 Comment(2)
What is <=> here? Are you just indicating that they're equivalent?Derte
@Derte exactly :-) This is just a mathematic symbol to indicate equivalence, not a symbol of actual computer language :)Artificiality

© 2022 - 2024 — McMap. All rights reserved.