Implementing ICAR/Soft Constraints in NumPyro

I’m looking to add an ICAR prior to a model that I’m building. I’ve had success with the following example in STAN:

Looking through some of the other posts here I think I could implement icar_normal_lpdf as a custom distribution, but what is tripping me up is the following:

// soft sum-to-zero constraint on phi
// more efficient than mean(phi) ~ normal(0, 0.001)
sum(phi) ~ normal(0, 0.001 * N);

Any advice on how to approach this in numpyro?

@jkgiesler I think sum(phi) ~ normal(0, 0.001 * N) just adds an observation node to the model

def model():
    phi = ...
    numpyro.sample("sum(phi)", dist.Normal(0, 0.001 * N), obs=phi.sum())

@fehiepsi Excellent, that worked. I didn’t realize that was possible with observation nodes. Thank you!