In this thread https://discourse.mc-stan.org/t/what-is-the-difference-between-specifying-x-n-mu-simga2-and-target-normal-lpdf-mu-sigma2 @bbbales2 and I were both surprised that using the same inits and same seed weren’t enough to give the same results when changing from sampling notation to target += notation. I expected differences in lp__
obviously, but not in parameter values.
For example, why aren’t the inferences for x
identical in the example below?
# Fit two simple models, only differing on whether
# sampling notation or target+= is used
library("cmdstanr")
file1 <- write_stan_tempfile(
"
parameters {
real x;
}
model {
x ~ normal(1, 1);
}
"
)
file2 <- write_stan_tempfile(
"
parameters {
real x;
}
model {
target += normal_lpdf(x | 1, 1);
}
"
)
mod1 <- cmdstan_model(file1)
mod2 <- cmdstan_model(file2)
# initialize to 0 and use same seed
fit1 <- mod1$sample(init = 0, seed = 123, save_warmup = TRUE)
fit2 <- mod2$sample(init = 0, seed = 123, save_warmup = TRUE)
I expect different values for lp__
but I also get slightly different results for x
, certainly within monte carlo error, but it still surprised me because the initial values and seed are the same:
> fit1$print("x", "mean", "sd")
variable mean sd
x 1.01 0.99
> fit2$print("x", "mean", "sd")
variable mean sd
x 1.00 1.01
Presumably this is the right behavior but I’m struggling to understand why. Can anyone explain this?