struct HMM { MatDoub a, b; VecInt obs; Int fbdone; Int mstat, nobs, ksym; Int lrnrm; MatDoub alpha, beta, pstate; VecInt arnrm, brnrm; Doub BIG, BIGI, lhood; HMM(MatDoub_I &aa, MatDoub_I &bb, VecInt_I &obs); void forwardbackward(); void baumwelch(); Doub loglikelihood() {return log(lhood)+lrnrm*log(BIGI);} }; HMM::HMM(MatDoub_I &aa, MatDoub_I &bb, VecInt_I &obss) : a(aa), b(bb), obs(obss), fbdone(0), mstat(a.nrows()), nobs(obs.size()), ksym(b.ncols()), alpha(nobs,mstat), beta(nobs,mstat), pstate(nobs,mstat), arnrm(nobs), brnrm(nobs), BIG(1.e20), BIGI(1./BIG) { Int i,j,k; Doub sum; if (a.ncols() != mstat) throw("transition matrix not square"); if (b.nrows() != mstat) throw("symbol prob matrix wrong size"); for (i=0; i= ksym) throw("bad data in obs"); } for (i=0; i 0.01) throw("transition matrix not normalized"); for (j=0; j 0.01) throw("symbol prob matrix not normalized"); for (k=0; k=0; t--) { bsum = 0.; for (i=0; i