On 9/19/12 9:29 PM, terrance savitsky wrote:
> Hi Marcus, ... You also note that the truncation ranges on the prior should match the restricted values for
> "y" set via your trick for sampler efficiency. I'm confused by why this set-up works to produce a correct model.
> While the range for each element of "y" is restricted to the correct range by using your algorithm, "y" is still
> declared as unrestricted. So how does Stan know to perform the transformation to unrestricted - with the accompanying
> Jacobian adjustment - for HMC sampling?
The parameters that HMC/NUTS sees are the unconstrained
versions of variables declared in the parameters block.
So declaring a 5-array of [0,1]-bounded parameters,
as in your example
> parameters {
> real<lower=0,upper=1> base_y[5];
> }
uses logit() to unconstrain to (-infinity,infinity).
What happens in the code is that the parameters are always
kept in the unconstrained space by the sampler and then
inverse transformed to their constrained variants,
here with inv_logit(). This means the log absolute
Jacobian for the n-th parameter is log(inv_logit'(y[n])),
and that is what gets added to the log probability
accumulator lp__ implicitly before the user code
executes over the constrained parameter which it
sees as base_y[n].
After that, setting the base_y[n] from the array into
a vector doesn't require a transform.
All of this clunkiness is because I haven't yet
implemented lower- and upper-bound constraints for
vector parameters. But they're on the to-do list,
as I can see they're going to be more important for
vectorized operations than I'd originally anticipated.
> Is it the truncated prior that communicates the need to transform?
No. Although the probability functions do check their
domains and throw exceptions if inputs are out of support.
That's why declarations with constraints are important.
> If so, then
> it seems like it would be correct to simply declare "y" as unrestricted (but initialize with values in the correct
> truncated range) and just employ a restricted distribution. The issue would then be one of sampling efficiency, though
> the result would still be correct, right?
No, see above.
- Bob