I have a model where my observed variable is the output of a deterministic function. However it is unclear to me how I can condition the model on my observed samples. Ideally I would like to use something like:
y = numpyro.deterministic('y', x, obs=y_obs)
Similar to using the obs parameter in numpyro.sample.
Is this something that would be possible to implement?
Hi @peterroelants, this may be possible in some cases via handlers.collapse, but that will only work in special cases of conjugate distributions and a narrow class of deterministic transformations. Can you paste an example with a little more detail, showing (1) the distributions from which x's inputs are drawn; (2) the entire transformation from those inputs to your deterministic x; and (3) the distribution in your conditioning numpyro.sample statement?
See also
Hi @fritzo , thank you for providing some more context.
I have been trying to see if/how PyMC (which I assume works fairly similar) tackles this problem. I came across this PyMC forum post and related Stack Exchange question. If I understand correctly the main problem is coming up with a likelihood function for any deterministic function (Note that PyMC also doesn't provided an observed deterministic function).
My specific use case was shifting a HalfNormal distribution by adding a constant. However, it occurred to me that I can avoid this problem by subtracting the same constant from my observations.
@peterroelants You can use TransformedDistribution as in the last link of @fritzo's comment.
sample("obs", TransformedDistribution(HalfNormal(scale), AffineTransform(loc, 1)), obs=data)
Or you can use TruncatedDistribution
sample("obs", TruncatedDistribution(Normal(loc, scale), low=loc), obs=data)
@peterroelants I think this issue has been resolved. In general, if you have a bijective deterministic transform, you can always use TransformedDistribution API to do the job. Please feel free to reopen this again if needed.
Yes, I have solved my use case.
Thank you for your help!