Creating a piece-wise pdf based on NaN values

I would like to build a piecewise pdf which takes in a boolean (1 means the value is missing) and outputs the probability of a value being NaN. I know stan doesn’t have any NaNs so I’m wondering how to create this pdf:

p(s|m) \left\{ \begin{array}{ll} p(s=NaN | m=0) = 0 \\ p(s=NaN | m=1) = 1 \\ p(s=x \in \mathbb{R} ^{+} | m=0) = lognorm(x)\\ p(s=x | m=1) = 0 \end{array} \right.

In pseudo code, something like this…

functions {
    real<lower=0> lognorm_missing(m, mu, sd){
    if (m==0) {
    if (x=NaN) return 0; 
    else return lognorm(mu, sd); 
    else {
    if (x=NaN) return 1;
    else return 0;

I’m pretty sure Stan works with NaNs in data.

You can check for them with the is_nan function (documented here:

The issue would be if the NaN were included in the likelihood evaluation. You can filter them out with is_nan and avoid this.