The allowance of a distribution call to lp through a function seems like a bug. However, it brings up the case that there should be an allowed lp increment for log-abs-det-jacobian adjustments in transformed parameters. I’m thinking a special function like increment_target()
such as
parameters {
real<lower=0> y_inv;
}
transformed parameters {
real<lower=0> y;
y = 1 / y_inv; // change variables
increment_target(-2 * log(y_inv)); // Jacobian adjustment;
}
model {
y ~ gamma(2,4);
}
It seems unnecessary in this situation but there are more complicated situations where I’m doing an adjustment in a function in transformed parameters and then I have to figure out a way to pass it to a dummy variable. This is difficult especially when one has matrices as outputs. So now I have an array of 2 matrices where matrix is my output and the other is mostly zeros except for the jacobian adjustment. Such as
// what's done today
matrix[] f (int a){
matrix[a, a] out[2];
...
out[1] = my_output;
out[2, 1, 1] = log(abs(jacobian(y));
return out;
}
// proposal
matrix f (int a){
matrix[a, a] out;
...
out = my_output;
jac_adj = log(abs(jacobian(y));
increment_target(jac_adj);
return out;
}