data {
real<lower=0> y;
}
// ...
model {
log(y) ~ normal(mu, sigma);
}target += - log(fabs(y));data {
real<lower=0> y;
}
// ...
model {
log(y) ~ normal(mu, sigma);
}and latertarget += - log(fabs(y));
How does Stan know this is part of a of adjustment, and do not add the 2nd derivative of log(y),
log(y) ~ normal(mu, sigma)y ~ lognormal(y | mu, sigma)real<lower = 0.0> sigma;@BGoodrichSince y is data here and isn't being sampled over, wouldn't it make sense to change this example?In that model,log(y) ~ normal(mu, sigma)andy ~ lognormal(y | mu, sigma)*Are*, from a sampling perspective, equivalent. The log probabilities are a bit off, but whatever. I was looking at this transformed parameter stuff a couple days ago and that was bugging me.
Also, and I don't know if this is what Andre was asking, but if this were a parameter and we wanted to enforce a lower bound constraint:real<lower = 0.0> sigma;Then if we wrote that in the model, Stan would already apply that transformation, and so manually adding an adjustment would be like a 2nd adjustment (dunno if that's what he's asking). In this case, the <lower=0.0> specification is in the data block so it's only used to verify the input data and there's no transform, but again, in this example it doesn't really matter :D.
Stan knows that y is a constant array, so the derivatives are constants, so adding them to log posterior kernel would have no effect on the sampling, so Stan doesn't bother. You would be better off constructing log_y in the transformed data block and feeding log_y to the normal log-likelihood function.
For explicit Jacobian adjustments, it's the log of the absolute
value of the determinant of the Jacobian matrix.
You need the adjustment whenever you have a parameter theta
and you have a function f and want to define your distribution
on f(theta) and then pull it back so it does the right thing
on theta. Whereas if you have a variable theta and define the
density on theta, then f(theta) will be distributed correctly
without a Jacobian adjustment.
- Bob