Dear Stan veterans,
I would like to understand Jacobians, when they are required and when I should worry about them even though Stan may not complain. Here’s a super simple example of a prior defined via built-in distribution and then again, defined via ICDF.
I read the manual and the user guide and I am still a little unsure that I absolutely understand when Jacobians are not required. One thing I want to confirm with you that this here is not that case
library(rstan)
library(magrittr)
stanmodelcode1 <- "
data {
int<lower=0> N;
int<lower=0> y[N];
}
parameters {
real<lower=0.00001> Theta;
}
model {
Theta ~ rayleigh(0.1);
for (n in 1:N)
y[n] ~ exponential(Theta);
}
generated quantities{
real y_pred;
y_pred = exponential_rng(Theta);
}
"
stanmodelcode2 <- "
functions{
real rayleigh_icdf(real u, real s){
return s*sqrt(-2*log1m(u));
}
}
data {
int<lower=0> N;
int<lower=0> y[N];
}
parameters {
real<lower=0, upper=1> u;
}
transformed parameters{
real QTheta = rayleigh_icdf(u, 0.1);
}
model {
u ~ uniform(0,1);
for (n in 1:N)
y[n] ~ exponential(QTheta);
}
generated quantities{
real y_pred;
y_pred = exponential_rng(QTheta);
}
"
claims.obs <- c(100, 950, 450)
N <- length(claims.obs)
dat <- list(N = N, y = claims.obs);
# fit the model with regual prior
stanDso1 <- stan_model(model_code = stanmodelcode1)
fit1 <- sampling(stanDso1, data = dat, iter = 10000, warmup=2000)
fit1
draws1 <- posterior::as_draws_array(fit1) %>%
posterior::subset_draws(variable="Theta")
# fit the model with ICDF prior
stanDso2 <- stan_model(model_code = stanmodelcode2)
fit2 <- sampling(stanDso2, data = dat, iter = 10000, warmup=2000)
fit2
draws2 <- posterior::as_draws_array(fit2) %>%
posterior::subset_draws(variable="QTheta")
posterior::bind_draws(draws1, draws2) %>%
# bayesplot::mcmc_dens()
posterior::as_draws_df() %>%
tidyr::pivot_longer(-c(.iteration, .chain, .draw)) %>%
ggplot()+
geom_density(aes(x=value, color=name, fill=name), alpha=0.2)+
theme_minimal()
UPDATE: ggplot shows the distributions not to be very different, so I take my words back.
Posterior distributions seem to be different, so my rule of thumb “if it can be listed in transformed parameters
it requires the adjustment” seems to be acceptable?