Loops for tuning HMM