Log posterior does not fit the double check. Why?

For the following simple STAN code

data {
int V;
vector[V] y;
}
parameters {
real<lower=-pi(),upper=pi()> mu;
real<lower=0,upper=1> sigma;
}
model {
print(“before everything: log-posterior=”,target());
mu~uniform(-pi(),pi());
print(“after mu: log-posterior=”,target());
sigma~uniform(0,1);
print(“after sigma: log-posterior=”,target());
for(v in 1:V){
y[v] ~ normal(mu, sigma);
}
print(“after: log-posterior=”,target());
print(“mu=”,mu,“; sigma=”,sigma);
}

Then I run the following R code:

stan.model=stan_model(file=“Loglik_test_uniform_prior.stan”,
model_name=“Loglik_test_uniform_prior”,auto_write=F)
y=(0:400)/401
stan.fit=sampling(object=stan.model,
data=list(V=length(y),y=y),
init=list(list(mu=pi/2,sigma=0.6)),
iter=20, warmup=10, chains = 1,control=list(adapt_delta=0.99),
verbose=T, algorithm=“NUTS”)

The printed result said:

Chain 1: Iteration: 16 / 20 [ 80%] (Sampling)
Chain 1: before everything: log-posterior=-1.15506
after mu: log-posterior=-1.15506
after sigma: log-posterior=-1.15506
after: log-posterior=295.526
mu=0.477933; sigma=0.288601

Chain 1: before everything: log-posterior=-1.15568
after mu: log-posterior=-1.15568
after sigma: log-posterior=-1.15568
after: log-posterior=295.35
mu=0.476289; sigma=0.288221

Chain 1: Iteration: 18 / 20 [ 90%] (Sampling)
Chain 1: before everything: log-posterior=-1.15254
after mu: log-posterior=-1.15254
after sigma: log-posterior=-1.15254
after: log-posterior=296.185
mu=0.510924; sigma=0.291499

Chain 1: Iteration: 20 / 20 [100%] (Sampling)
Chain 1: before everything: log-posterior=-1.15848
after mu: log-posterior=-1.15848
after sigma: log-posterior=-1.15848
after: log-posterior=296.298
mu=0.488381; sigma=0.287457

I noticed that the log-posterior at the beginning of every step changes. Why doesn’t it start with 0?

Moreover, I double checked the log-posterior in R:

max.iter=length(stan.fit@sim$samples[[1]]$mu)
log.posterior=rep(NA,max.iter)
for(tt in 1:max.iter){
print(tt)
log.posterior[tt]=sum(dnorm(y,mean=stan.fit@sim$samples[[1]]$mu[tt],
sd=stan.fit@sim$samples[[1]]$sigma[tt],log=T))
}
print(log.posterior-stan.fit@sim$samples[[1]]$lp__)

[1] -367.4451 -367.4451 -367.4451 -367.4351 -367.4339 -367.4409 -367.4188 -367.3953
[9] -367.3509 -367.3335 -367.3570 -367.3520 -367.3366 -367.3384 -367.3393 -367.3402
[17] -367.3418 -367.3411 -367.3359 -367.3368

This difference is not constant among iterations. Why?

Thank you.

I assume the difference has to do with the constraints in the parameters in this model. Stan samples on an unconstrained space, so it adds adjustments to the log density to account for the transforms, see here: https://mc-stan.org/docs/2_19/reference-manual/change-of-variables-section.html .

Maybe try this experiment with an unconstrained model and see if it behaves as expected.

@bbbales2 Thanks Ben.
Yes, as you said, after removing the constraints in parameter definition, the beginning log-posterior with “before everything” becomes 0, and the difference becomes constant.