How does Stan handle IF conditions where condition is stochastic in model

Let’s say you have a model like

data {
    int<lower=1, upper=K> min_idx;
parameters {
    simplex[K] alpha

model {
    alpha ~ Dirichlet(...);
    if (sort_indices_asc(alpha)[1] !=  min_idx) {
        target += -1e50

Is it bad practice to have a IF condition like so because it makes the target discontinuous and might hamper inference using HMC? How does HMC in Stan handle conditionals like this?

I’m not expert, but my sense is that yes, this induces a complex discontinuity in the gradient that would cause HMC to diverge and fail. If you are working with a model where you think you need something like that conditional, maybe post an explanation of the data and post the model so we can advise on the possibility of alternative strategies.


To generate a Dirichlet distribution with entry at min_idx has the minimum value, one way I think may work is to first generate a Dirichlet with order K-1, and use the marginal Beta distribution to generate the min_idx entry constrained above by the minimum of the order K-1 Dirichlet sample, then create a new simplex by inserting the beta sample.


Yes, that’s the work around I ended up doing! Thanks!