build n-th order markov model