I have a model for which parameters outside a region D ⊂ ℝⁿ are invalid. D is not characterized in closed form, and no amount of change of variable acrobatics can transform it to ℝⁿ (actually, I can find some A ⊃ D where A is not much larger than D, but that merely mitigates the problem). But I can detect if the parameters are outside D, the question is what to assign to the likelihood there.
Fortunately, there is no mass at the edges of D, and the likelihood goes to -∞ at the edges quickly. I looked at the Stan sources and found something like
if (boost::math::isnan(h))
h = std::numeric_limits<double>::infinity();
in multiple places, eg here for NUTS. So is (stylized code)
if (parameter is invalid)
target += NaN
the right thing to do? Or should I use -Inf? The advantage of NaN is that for some invalid calculations, that’s what I get out of the box.
Some experimentation with toy models suggests that it works as long as I pick the starting point inside D. I guess it would help if I control the initial stepsize (before adaptation) but I am not sure how to do that (using rstan).