5 * Last Modified: 2008-03-10
8 * 2008-03-10, 0.1.0-8: make icc report two more "VECTORIZED"
9 * 2008-03-10, 0.1.0-7: accelerate for some CPU
10 * 2008-02-07, 0.1.0-6: simulate sequences
11 * 2008-01-15, 0.1.0-5: goodness of fit
12 * 2007-11-20, 0.1.0-4: add function declaration of hmm_post_decode()
13 * 2007-11-09: fix a memory leak
18 #define HMM_VERSION "0.1.0-7"
20 #define HMM_FORWARD 0x02
21 #define HMM_BACKWARD 0x04
22 #define HMM_VITERBI 0x40
23 #define HMM_POSTDEC 0x80
28 #define HMM_TINY 1e-25
33 int m
, n
; // number of symbols, number of states
34 FLOAT
**a
, **e
; // transition matrix and emitting probilities
35 FLOAT
**ae
; // auxiliary array for acceleration, should be calculated by hmm_pre_backward()
36 FLOAT
*a0
; // trasition matrix from the start state
45 int *v
; // Viterbi path
46 int *p
; // posterior decoding
52 FLOAT Q0
, **A
, **E
, *A0
;
64 /* initialize and destroy hmm_par_t */
65 hmm_par_t
*hmm_new_par(int m
, int n
);
66 void hmm_delete_par(hmm_par_t
*hp
);
67 /* initialize and destroy hmm_data_t */
68 hmm_data_t
*hmm_new_data(int L
, const char *seq
, const hmm_par_t
*hp
);
69 void hmm_delete_data(hmm_data_t
*hd
);
70 /* initialize and destroy hmm_exp_t */
71 hmm_exp_t
*hmm_new_exp(const hmm_par_t
*hp
);
72 void hmm_delete_exp(hmm_exp_t
*he
);
73 /* Viterbi, forward and backward algorithms */
74 FLOAT
hmm_Viterbi(const hmm_par_t
*hp
, hmm_data_t
*hd
);
75 void hmm_pre_backward(hmm_par_t
*hp
);
76 void hmm_forward(const hmm_par_t
*hp
, hmm_data_t
*hd
);
77 void hmm_backward(const hmm_par_t
*hp
, hmm_data_t
*hd
);
78 /* log-likelihood of the observations (natural based) */
79 FLOAT
hmm_lk(const hmm_data_t
*hd
);
80 /* posterior probability at the position on the sequence */
81 FLOAT
hmm_post_state(const hmm_par_t
*hp
, const hmm_data_t
*hd
, int u
, FLOAT
*prob
);
82 /* posterior decoding */
83 void hmm_post_decode(const hmm_par_t
*hp
, hmm_data_t
*hd
);
84 /* expected counts of transitions and emissions */
85 hmm_exp_t
*hmm_expect(const hmm_par_t
*hp
, const hmm_data_t
*hd
);
86 /* add he0 counts to he1 counts*/
87 void hmm_add_expect(const hmm_exp_t
*he0
, hmm_exp_t
*he1
);
88 /* the Q function that should be maximized in EM */
89 FLOAT
hmm_Q(const hmm_par_t
*hp
, const hmm_exp_t
*he
);
90 FLOAT
hmm_Q0(const hmm_par_t
*hp
, hmm_exp_t
*he
);
91 /* simulate sequences */
92 char *hmm_simulate(const hmm_par_t
*hp
, int L
);
97 static inline void **calloc2(int n_row
, int n_col
, int size
)
101 p
= (char**)malloc(sizeof(char*) * n_row
);
102 for (k
= 0; k
!= n_row
; ++k
)
103 p
[k
] = (char*)calloc(n_col
, size
);