forward backward algorithm to compute posterior probabilities