hidden markov model forward, backward algorithm implementation