00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00042 #ifndef FASTLIB_DISCRETE_HMM_H
00043 #define FASTLIB_DISCRETE_HMM_H
00044
00045 #include "fastlib/fastlib.h"
00046
00055 class DiscreteHMM {
00057 private:
00059 Matrix transmission_;
00060
00062 Matrix emission_;
00063
00064 OT_DEF(DiscreteHMM) {
00065 OT_MY_OBJECT(transmission_);
00066 OT_MY_OBJECT(emission_);
00067 }
00068
00069 public:
00071 const Matrix& transmission() const { return transmission_; }
00072 const Matrix& emission() const { return emission_; }
00073
00075 void setModel(const Matrix& transmission, const Matrix& emission);
00076
00078 void Init(const Matrix& transmission, const Matrix& emission);
00079
00081 void InitFromFile(const char* profile);
00082
00084 void InitFromData(const ArrayList<Vector>& list_data_seq, int numstate);
00085
00087 void LoadProfile(const char* profile);
00088
00090 void SaveProfile(const char* profile) const;
00091
00093 void GenerateSequence(int length, Vector* data_seq, Vector* state_seq) const;
00094
00099 void EstimateModel(const Vector& data_seq, const Vector& state_seq);
00100 void EstimateModel(int numstate, int numsymbol, const Vector& data_seq, const Vector& state_seq);
00101
00107 void DecodeOverwrite(const Vector& data_seq, Matrix* state_prob_mat, Matrix* forward_prob_mat, Matrix* backward_prob_mat, Vector* scale_vec) const;
00108
00110 void DecodeInit(const Vector& data_seq, Matrix* state_prob_mat, Matrix* forward_prob_mat, Matrix* backward_prob_mat, Vector* scale_vec) const;
00111
00113 double ComputeLogLikelihood(const Vector& data_seq) const;
00114
00116 void ComputeLogLikelihood(const ArrayList<Vector>& list_data_seq, ArrayList<double>* list_likelihood) const;
00117
00119 void ComputeViterbiStateSequence(const Vector& data_seq, Vector* state_seq) const;
00120
00125 void TrainBaumWelch(const ArrayList<Vector>& list_data_seq, int max_iteration, double tolerance);
00126
00131 void TrainViterbi(const ArrayList<Vector>& list_data_seq, int max_iteration, double tolerance);
00132
00133
00135
00144 static void GenerateInit(int L, const Matrix& trans, const Matrix& emis, Vector* seq, Vector * states);
00145
00147 static void EstimateInit(const Vector& seq, const Vector& states, Matrix* trans, Matrix* emis);
00148 static void EstimateInit(int numSymbols, int numStates, const Vector& seq, const Vector& states, Matrix* trans, Matrix* emis);
00149
00161 static void ForwardProcedure(const Vector& seq, const Matrix& trans, const Matrix& emis, Vector *scales, Matrix* fs);
00162 static void BackwardProcedure(const Vector& seq, const Matrix& trans, const Matrix& emis, const Vector& scales, Matrix* bs);
00163 static double Decode(const Vector& seq, const Matrix& trans, const Matrix& emis, Matrix* pstates, Matrix* fs, Matrix* bs, Vector* scales);
00164
00173 static double ViterbiInit(const Vector& seq, const Matrix& trans, const Matrix& emis, Vector* states);
00174 static double ViterbiInit(int L, const Vector& seq, const Matrix& trans, const Matrix& emis, Vector* states);
00175
00177 static void Train(const ArrayList<Vector>& seqs, Matrix* guessTR, Matrix* guessEM, int max_iter, double tol);
00178
00180 static void TrainViterbi(const ArrayList<Vector>& seqs, Matrix* guessTR, Matrix* guessEM, int max_iter, double tol);
00181
00182 };
00183 #endif