📄 ghmmwrapper.i
字号:
/** @name background_distributions A container for background distributions to be used in the reestimation. Model has an ID (== index) into the arrays.*/struct background_distributions { int n; /* Number of distributions */ int* order; /* Order of the respective distribution */ double **b; /* The probabilities */ };typedef struct background_distributions background_distributions;/** @name state The basic structure, keeps all parameters that belong to a state. */struct state { /** Initial probability */ double pi; /** Output probability */ double *b; int order; /** IDs of the following states */ int *out_id; /** IDs of the previous states */ int *in_id; /** transition probs to successor states. */ double *out_a; /** transition probs from predecessor states. */ double *in_a; /** Transition probability to a successor double *out_a; */ /** Transition probablity to a precursor double *in_a;*/ /** Number of successor states */ int out_states; /** Number of precursor states */ int in_states; /** if fix == 1 --> b stays fix during the training */ int fix; int label; };typedef struct state state;/** @name model The complete HMM. Contains all parameters, that define a HMM.*/struct model { /** Number of states */ int N; /** Number of outputs */ int M; /** Vector of the states */ state *s; /** The a priori probability for the model. A value of -1 indicates that no prior is defined. Note: this is not to be confused with priors on emission distributions*/ double prior; /* contains a arbitrary name for the model */ char* name; /** Contains bit flags for varios model extensions such as kSilentStates, kTiedEmissions (see ghmm.h for a complete list) */ int model_type; /** Flag variables for each state indicating whether it is emitting or not. Note: silent != NULL iff (model_type & kSilentStates) == 1 */ int* silent; /*AS*/ /** Flag variables for each state indicating whether the states emissions are tied to another state. Groups of tied states are represented by their tie group leader (the lowest numbered member of the group). tied_to[s] == kUntied : s is not a tied state tied_to[s] == s : s is a tie group leader tied_to[t] == s : t is tied to state s Note: tied_to != NULL iff (model_type & kTiedEmissions) == 1 */ int* tied_to; /** Flag variables for each state giving the order of the emissions Classic HMMS have emission order 0, that is the emission probability is conditioned only on the state emitting the symbol. For higher order emissions, the emission are conditioned on the state s as well as the previous emission_order[s] observed symbols. The emissions are stored in the state's usual double* b. The order is set state.order. Note: state.order != NULL iff (model_type & kHigherOrderEmissions) == 1 */ /** Note: background_id != NULL iff (model_type & kHasBackgroundDistributions) == 1 */ int *background_id; background_distributions* bp; /** (WR) added these variables for topological ordering of silent states Condition: topo_order != NULL iff (model_type & kSilentStates) == 1 */ int* topo_order; int topo_order_length;};typedef struct model model;/** Frees the memory of a model. @return 0 for succes; -1 for error @param mo: pointer to a model */extern int model_free(model **mo);/** Reads in ASCII data to initialize an array of models. Memory allocation for the models is done here. @return array of pointers to the models @param filename: the ASCII input file @param mo_number: filled with number of models read */extern model** model_read(char *filename, int *mo_number);/** Writes a model in matrix format. @param file: output file @param mo: model*/void model_print(FILE *file, model *mo); /** Produces simple left-right models given sequences. The function "model_generate_from_sequence" is called for each model that should be made. The sequences are read in from the ASCII file and thrown away again when leaving the function. @return vector of models @param s: scanner @param new_models: number of models to produce */extern model **model_from_sequence_ascii(scanner_t *s, long *mo_number);/** Produces simple left-right models given sequences. The sequences are not read in from file, but exists already as a structur. @return vector of models @param s: scanner @param new_models: number of models to produce */extern model **model_from_sequence(sequence_t *sq, long *mo_number);/** Copies a given model. Allocates the necessary memory. @return copy of the model @param mo: model to copy */extern model* model_copy(const model *mo);/** Tests if all standardization requirements of model are fulfilled. (That is, if the sum of the probabilities is 1). @return 0 for succes; -1 for error @param mo: model to test */extern int model_check(const model* mo);/** Tests if number of states and number of outputs in the models match. @return 0 for succes; -1 for error @param mo: vector of models @param model_number: numbr of models */extern int model_check_compatibility(model **mo, int model_number);/** Produces a model, which generates the given sequence with probability 1. The model is a strict left-right model with one state for each element in the sequence and the output in state i is the i-th value in the sequence with probability 1. The model also has a final state, a state with no output. @return pointer to the produced model @param seq: sequence @param seq_len: length of the sequence @param anz_symb: number of symbols in the sequence*/extern model* model_generate_from_sequence(const int *seq, int seq_len, int anz_symb);/** Produces sequences to a given model. All memory that is needed for the sequences is allocated inside the function. It is possible to define the length of the sequences global (global_len > 0) or it can be set inside the function, when a final state in the model is reach (a state with no output). If the model has no final state, the sequences will have length MAX_SEQ_LEN. @return pointer to an array of sequences @param mo: model @param seed: initial parameter for the random value generator (an integer). If seed == 0, then the random value generator is not initialized. @param global_len: length of sequences (=0: automatically via final states) @param seq_number: number of sequences*/extern sequence_t *model_generate_sequences(model* mo, int seed, int global_len, long seq_number, int Tmax);/** Calculates the sum log( P( O | lambda ) ). Sequences, that can not be generated from the given model, are neglected. @return log(P) @param mo model @param sq sequences */extern double model_likelihood(model *mo, sequence_t *sq);/** Computes probabilistic distance of two models @return the distance @param m0 model used for generating random output @param m model to compare with @param maxT maximum output length (for HMMs with absorbing states multiple sequences with a toal langth of at least maxT will be generated) @param symmetric flag, whether to symmetrize distance (not implemented yet) @param verbose flag, whether to monitor distance in 40 steps. Prints to stdout (yuk!)*/double model_prob_distance(model *m0, model *m, int maxT, int symmetric, int verbose);/******** Reestimate Baum-Welch (reestimate.c) *******/extern int reestimate_baum_welch(model *mo, sequence_t *sq);extern int reestimate_baum_welch_nstep(model *mo, sequence_t *sq, int max_step, double likelihood_delta);extern void reestimate_update_tie_groups(model *mo);/******* Viterbi (viterbi.c)*******//** Viterbi algorithm. Calculates the Viterbi path (the optimal path trough the model) and the Viterbi probability to a given model and a given sequence. @return Viterbi path @param mo: model @param o: sequence @param len: length of the sequence @param log_p: probability of the sequence in the Viterbi path */// XXX use swig OUTPUT typemapextern int *viterbi(model *mo, int *o, int len, double *log_p);/** Calculates the logarithmic probability to a given path through the states (does not have to be the Viterbi path), given sequence and a model. @param mo: model @param o: sequence @param len: length of the sequence @param state_seq: path through the states @return log P */extern double viterbi_logp(model *mo, int *o, int len, int *state_seq);/******* Forward , backward (foba.c) ******//** Forward-Algorithm. Calculates alpha[t][i], scaling factors scale[t] and log( P(O|lambda) ) for a given double sequence and a given model. @param smo model @param O sequence @param length: length of sequence @param alpha: alpha[t][i] @param scale: scale factors @param log\_p: log likelihood log( P(O|lambda) ) @return 0 for success, -1 for error */extern int foba_forward(model *mo, const int *O, int length, double **alpha, double *scale, double *log_p);/** Backward-Algorithm. Calculates beta[t][i] given an integer sequence and a model. Scale factors given as parameter (come from foba\_forward). @param mo model @param O sequence @param length length of sequence @param beta beta[t][i] @param scale scale factors @return 0 for success, -1 for error */extern int foba_backward(model *mo, const int *O, int length, double **beta, const double *scale);/** Calculation of log( P(O|lambda) ). Done by calling foba\_forward. Use this function if only the log likelihood and not alpha[t][i] is needed. @param mo model @param O sequence @param len length of sequence @param log\_p log likelihood log( P(O|lambda) ) @return 0 for success, -1 for error */extern int foba_logp(model *mo, const int *O, int len, double *log_p);/** Set transition from state 'i' to state 'j' to value 'prob'. NOTE: No internal checks - model might get broken if used carelessly. @param mo model @param i state index @param j state index @param prob probabilitys */extern void model_set_transition(model *mo, int i, int j, double prob);/* Labeled HMMs */extern int foba_label_forward(model *mo, const int *O, const int *label, int len, double **alpha, double *scale, double *log_p);extern int foba_label_logp(model *mo, const int *O, const int *label, int len, double *log_p);%inline%{ /* allocation of an empty model struct */ model *new_model() { return (struct model *)(struct model *) calloc(1, sizeof(struct model)); } /* allocation of an array of state structs*/ state *arraystate(int size) { return (state *) malloc(size*sizeof(state)); } /* extract pointer to a state */ state *get_stateptr(state *ary, int index) { return ary + index; } void call_model_print(char *filename, model *mo) { FILE *fp=fopen(filename, "a"); if (fp == NULL) { fprintf(stderr, "call_smodel_print(0): cannot open file %s\n", filename); } else { model_print(fp, mo); fclose(fp); } } void call_model_free(model *mo ) {model_free(&mo);} model *get_model_ptr(model **mo, int index) { return mo[index]; } model **cast_model_ptr(model *mo){ model** result = &mo; return result; } %}/*============================================================================================= =============================== labeled models (model.c) ============================================== */extern sequence_t *model_label_generate_sequences(model* mo, int seed, int global_len, long seq_number, int Tmax);/*============================================================================================= =============================== sdmodel.c ============================================== *//** @name state The basic structure, keeps all parameters that belong to a sdstate. */struct sdstate { /** Initial probability */ double pi; /** Output probability */ double *b; /** ID of the following state */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -