forward_g function

General forward algorithm with time-varying transition probability matrix