u = center(observed and prediction locations)
S = box size (u)
max_iter = 30
# initialization
j = 0
check = FALSE
rho = 0.5*S # the practical paper recommends setting the initial guess of rho to be 0.5 to 1 times S
c = c(rho/S) # minimum c given rho and S
m = m(c,rho/S) # minimum m given c, and rho/S
L = c*S
diagnosis = logical(max_iter) # store checking results for each iteration
while (!check & j<=max_iter){
fit = runHSGP(L,m) # stan run
j = j + 1
rho_hat = mean(fit$rho) # obtain fitted value for rho
# check the fitted is larger than the minimum rho that can be well approximated
diagnosis[j] = (rho_hat + 0.01 >= rho)
if (j==1) {
if (diagnosis[j]){
# if the diagnosis check is passed, do one more run just to make sure
m = m + 2
c = c(rho_hat/S)
rho = rho(m,c,S)
} else {
# if the check failed, update our knowledge about rho
rho = rho_hat
c = c(rho/S)
m = m(c,rho/S)
}
} else {
if (diagnosis[j] & diagnosis[j-2]){
# if the check passed for the last two runs, we finish tuning
check = TRUE
} else if (diagnosis[j] & !diagnosis[j-2]){
# if the check failed last time but passed this time, do one more run
m = m + 2
c = c(rho_hat/S)
rho = rho(m,c,S)
} else if (!diagnosis[j]){
# if the check failed, update our knowledge about rho
rho = rho_hat
c = c(rho/S)
m = m(c,rho/S)
}
}
L = c*S
}








