Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/klib/blob/master/khmm.h # klib [Ported from github.com/attractivechaos/klib] added block arena Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `klib / khmm.h` 107 lines ยท 2929 bytes Raw Blame History 1 #ifndef AC_SCHMM_H_ 2 #define AC_SCHMM_H_ 3 4 /* 5 * Last Modified: 2008-03-10 6 * Version: 0.1.0-8 7 * 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 14 */ 15 16 #include 17 18 #define HMM_VERSION "0.1.0-7" 19 20 #define HMM_FORWARD 0x02 21 #define HMM_BACKWARD 0x04 22 #define HMM_VITERBI 0x40 23 #define HMM_POSTDEC 0x80 24 25 #ifndef FLOAT 26 #define FLOAT double 27 #endif 28 #define HMM_TINY 1e-25 29 #define HMM_INF 1e300 30 31 typedef struct 32 { 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 37 } hmm_par_t; 38 39 typedef struct 40 { 41 int L; 42 unsigned status; 43 char *seq; 44 FLOAT **f, **b, *s; 45 int *v; // Viterbi path 46 int *p; // posterior decoding 47 } hmm_data_t; 48 49 typedef struct 50 { 51 int m, n; 52 FLOAT Q0, **A, **E, *A0; 53 } hmm_exp_t; 54 55 typedef struct 56 { 57 int l, *obs; 58 FLOAT *thr; 59 } hmm_gof_t; 60 61 #ifdef __cplusplus 62 extern "C" { 63 #endif 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); 93 #ifdef __cplusplus 94 } 95 #endif 96 97 static inline void **calloc2(int n_row, int n_col, int size) 98 { 99 char **p; 100 int k; 101 p = (char**)malloc(sizeof(char*) * n_row); 102 for (k = 0; k != n_row; ++k) 103 p[k] = (char*)calloc(n_col, size); 104 return (void**)p; 105 } 106 107 #endif