How do I enforce a positive constraint on a deterministic parameter?

I’m expressing a model as

\text{treatment} = \text{baseline} + \text{delta}

where \text{baseline} \sim \text{TruncatedNormal(5, 10, low=0)} and \text{delta} \sim N(0, 10)

and treatment = numpyro.deterministic(“treatment”, baseline + delta)

I know for a fact that both baseline and treatment are positive. I have enforced constraint on baseline by using a TruncatedNormal, but how do I make sure that the posterior samples of treatment only contains positive samples?

i don’t think there’s any particularly easy way of doing so.

you can either revisit your priors on baseline and delta and ensure their sum is always positive by definition of the priors or you can put a soft constraint on treatment with a factor statement, e.g.

penalty = penalty_strength * (jnp.fabs(treatment) - treatment)
numpyro.factor("soft_constraint", -penalty)

but note this not a hard constraint so treatment may still be (slightly) negative depending on the strength of the penalty


Hi @martinjankowiak. Thank you, this helped. Could you please tell me where this penalty is getting added?

factor is added directly to the log density of the model.