## How to do: X~N(0,1) w.p. P and X=0 w.p. 1-P

Help
Owen
2011-05-11
2012-09-01

• Owen
2011-05-11

Hello all,

I'm struggling to construct a certain distribution within JAGS:

with probability P, X ~ N( mu, tau )

with probability 1-P, X = 0

X can constructed be with a normal random variable and a bernoulli random
variable feeding a logical node.

However, I need the result to be a stochastic node.

I suppose this distribution is a degenerate case of a two component normal
mixture, with one component having zero variance. But I hesitate to push any
software into such a corner case.

I wonder if there is a better approach?

Thanks for any suggestions.

Kind regards,

Owen

keywords: mixture, switching

• Martyn Plummer
2011-05-11

Well it's a degenerate case. Since the normal distribution is continuous, the
even X=0 has probability 0. So you know whether you are in the first case or
the second case depending on whether X==0.

Can create an indicator variable R st

```R = 1 if X!=0
R = 0 if X==0
```

Then set X to missing (NA) if R==0. Your model is

```R ~ dcat(P)
X ~ dnorm(mu, tau)
```

and that's it.

• Owen
2011-05-11

Martyn,

Thank you for your suggestion. I think I may not have presented the problem
clearly.

If we can solve the problem I first asked (Problem 1), then together with
dsum(), I can solve Problem 2 described later.

Restating Problem 1:

Suppose P = 0.4.

I would like to construct a stochastic node with a mass a of 0.4 at x=0 and
the density N(0,1) (scaled by 0.6) for other X values.

Building on your model, I could have

```R ~ dcat(P)
X ~ dnorm(mu,tau)
Y <- equals(R,1)*X + equals(R,0)*0
```

So if R is one, then Y is normal, else Y is zero. And Y has the required
distribution, but it is not a stochastic node.

Problem 2:

Let me back up and describe the real problem:

```mu[1] <- 75
mu[2] <- 65
tau[1] <- 1/25
tau[2] <- 1
R ~ dcat(P)
index <- 1+R  # index of the Normal distribution chosen by R
X ~ dnorm( mu[index], tau[index] )
```

I have not yet tried this in JAGS, but OpenBUGS disallows the last line of the
model, and I assume it is also a problem in JAGS. Although X looks like a
mixture distribution, it is slightly more because the mode variable R is
exposed, so I can incorporate it into the model in other ways. For example, in
place of R~dcat(P), R could be the state of a Markov chain.

Another formulation is to always generate both Normals and then select between
them:

```Z[1] ~ dnorm( mu[1], tau[1] )
Z[2] ~ dnorm( mu[2], tau[2] )
R ~ dcat(P)
X <- equals(R,1)*Z[1] + equals(R,0)*Z[2]
```

Again X is not a stochastic node, but it could be made into a stochastic node
using dsum().

But only if the quantity "equals(R,1)*Z" could be constructed as a stochastic
node (and that is Problem 1).

Thanks and Regards,

Owen

• Owen
2011-05-12

SOLVED:

I found a solution for Problem 2 (which is what I really need), but not a
solution for problem one.

The OpenBUGS example _Eyes _ gives a proper formulation of how to randomly
switch the input parameters of a distribution.

Thanks again Martyn for your time.