u = center(observed and prediction locations)
S = box size (u)
max_iter = 30
# initialization
j = 0
check = FALSE
diagnosis = logical(max_iter) # store checking results for each iteration
# initial guess
rho = 0.5*S # recommended initial value
c = minimum c given rho
m = minimum m given c, and rho
B = c*S
while (!check & j<=max_iter){
fit = runHSGP(B,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
update c using rho_hat
update rho using m and c
} else {
# if the check failed, update our knowledge about rho
rho = rho_hat
update c and m using rho_hat
}
} 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
update c using rho_hat
update rho using m and c
} else if (!diagnosis[j]){
# if the check failed, update our knowledge about rho
rho = rho_hat
update c and m using rho_hat
}
}
B = c*S
}






