# The exact meaning of "iteration"

I asked my Stan code to run for one chain and 4 iterations but it runs for ever.

Is that correct that for each round of the block “model”, you start with specific set of parameters, calculate L leapfrogs of HMC and endup with a new set of parameters (peoposal) with associated posterior probability? Then you need to decide whether to accept or reject the proposal. If this is true, then each round of model is equal to one iteration.

Is “transition” different than “iteration” ?

This is my Stan code:

``````functions{
vector PK3cmt(real t, vector states, vector theta){
real ka = theta[1];
real CL = theta[2];
real Vc = theta[3];
real Q = theta[4];
real Vp = theta[5];
real gut = states[1];
real cent = states[2];
real peri = states[3];
vector[3] dif;
dif[1] = - ka * gut;
dif[2] = ka*gut - ((CL / Vc) + (Q / Vc))*cent + (Q * peri) / Vp;
dif[3] = (Q*cent)/ Vc - (Q*peri)/Vp;
return dif;
}
}
data{
int N;  // N = 1320 total number of events
int nObs; // nObs = 1020 the number of measurements
int ID; // ID = 20 the number of patients
int id[N];
int iObs[nObs];
vector<lower = 0>[N] times;
int evid[N];
int cmt[N];
vector<lower = 0>[N] amt;
vector<lower=0>[nObs] cObs;
vector<lower=0>[ID] weight;
}
transformed data {
int nTheta = 5;
int nCmt = 3;
}
parameters{
vector<lower=0>[nTheta] theta_bar; //{ka, CL, Vc, Q, Vp},  population parameters
vector<lower=0>[nTheta] sigma_bar;
real<lower = 0> sigma;
matrix[ID, 5] myz;                // standard normal noise, inter-individuals and inter-parameters variation
}
model{
print ("START");
print (theta_bar);
matrix[ID, nTheta] theta;
for(j in 1:5) theta[, j] = theta_bar[j] + sigma_bar[j] * myz[, j];    // individual parameters

theta[, 2] = theta[, 2] .* exp(0.75 * log(weight/70));    // adjusting CL for the weight of the patient
theta[, 3] = theta[, 3] .* weight/70;                     // adjusting Vc for the weight
theta[, 4] = theta[, 4] .* exp(0.75 * log(weight/70));    // adjusting Q for the weight
theta[, 5] = theta[, 5] .* weight/70;                     // adjusting Vp for the weight

vector[N] traj;
int i = 1;
for(k in 1:ID){
vector[nCmt] U = to_vector([0.0, 0, 0]);
while (id[i] == k){
if (evid[i] == 1) U[cmt[i]] += amt[i];
else {
U = ode_rk45(PK3cmt, U, times[i-1], {times[i]}, to_vector(theta[k]))[1];
traj[i] = U[2] / theta[k, 3];
}
i +=1;
}
}
vector[nObs] concentrationHat = traj[iObs];
// priors:
theta_bar[1] ~ lognormal(log(2.5), 1);
theta_bar[2] ~ lognormal(log(10), 0.25);
theta_bar[3] ~ lognormal(log(35), 0.25);
theta_bar[4] ~ lognormal(log(15), 0.5);
theta_bar[5] ~ lognormal(log(105), 0.5);
sigma ~ normal(0, 0.5);
to_vector(myz) ~ normal(0, 1);
sigma_bar ~ normal(0, 0.5);
//likelihood:
cObs ~ lognormal(log(concentrationHat), sigma);
print("END");
}

H3cmtpop.m = stan_model(file = "H3cmtpop.stan")

init <- function() {
list(theta_bar = c(exp(rnorm(1, log(2), 0.2)),
exp(rnorm(1, log(10), 0.2)),
exp(rnorm(1, log(35), 0.2)),
exp(rnorm(1, log(15), 0.2)),
exp(rnorm(1, log(105), 0.2))),
sigma_bar = abs(rnorm(5, 0, 0.5)),
sigma = abs(rnorm(1, 0, 0.5)),
myz = matrix(rnorm(100), 20, 5))}

test.f = sampling(H3cmtpop.m, data=H3cmtpop.dat, iter = 4, cores = 1, chains = 1, control=list(adapt_delta=0.85),
init = init)
``````

and here is a small part of the output:

``````SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).
Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1:
Chain 1: Gradient evaluation took 0.346604 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 3466.04 seconds.
Chain 1:
Chain 1:
Chain 1: WARNING: No variance estimation is
Chain 1:          performed for num_warmup < 20
Chain 1:
Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1: START
[inf,inf,0,1.64549e-269,7.52746e+273]

Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1: START
[inf,inf,0,3.0224e-269,9.08433e+273]

Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1: START
[inf,inf,8.78137e-131,4.96486e-67,1.36957e+70]

Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1: START
[inf,inf,4.69078e-32,2.26126e-16,9.92922e+18]

Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1: START
[4.62259e+97,6.59466e+77,2.28684e-07,0.000940978,1.85184e+06]

Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1: START
[3.7977e+24,1.50756e+20,0.279156,1.23789,1276.8]

Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1: START
[2.09615e+06,577615,10.144,7.44705,192.614]

Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1: START
[57.2685,147.589,23.6528,11.3866,123.333]
END

Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1: START
[4.25857,18.5561,29.6483,13.1213,111.732]
END

Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1: START
[2.21735,10.8815,31.4194,13.3843,108.905]
END

Chain 1: Iteration: 1 / 4 [ 25%]  (Warmup)
Chain 1: START
[1.77561,9.21684,31.8655,13.5266,107.015]
END

Chain 1: START
[2.20406,11.0011,31.3619,13.4129,108.341]
END

Chain 1: START
[2.78571,14.2682,31.4578,13.4422,109.885]
END

Chain 1: START
[3.48316,17.1775,32.5715,13.7864,111.2]
END

Chain 1: START
[4.32323,19.3935,34.4035,14.3511,112.501]
END

Chain 1: START
[5.34924,21.2182,36.6685,15.0365,113.855]
END
``````

Iteration and transition can be used interchangeably.

I think for the purposes of your question, this is correct. However, it is incorrect in detail. In particular, you simulate the hamiltonian trajectory until a stopping criterion is satisfied (so L isn’t fixed), and then draw a value for the next iteration via weighted sampling along the entire simulated trajectory.