17 state_val =
new(std::nothrow) std::vector<stoch_value>;
20 position =
new(std::nothrow) std::vector<size_t>(seq_size,0);
22 std::cerr <<
"Cannot allocate stochTable- OUT OF MEMORY" << std::endl;
64 double sum(-INFINITY);
66 size_t state_start(-1);
70 for(
size_t i=0;i<
position->size()-1;++i){
75 for (
size_t j = (i==0) ? 0 : (*position)[i-1]+1; j <= (*position)[i] ; ++j ){
82 if ((*
state_val)[j].state_id != current_state){
85 for(
size_t k = state_start; k < j ;++k){
86 (*state_val)[k].prob = exp((*
state_val)[k].prob - sum);
90 current_state = (*state_val)[j].state_id;
91 sum = (*state_val)[j].prob;
101 for(
size_t k = state_start; k <= (*position)[i]; ++k){
102 (*state_val)[k].prob = exp((*
state_val)[k].prob - sum);
106 state_start = (*position)[i] + 1;
114 for (
size_t j=state_start; j <
state_val->size();j++){
119 for (
size_t j=state_start; j <
state_val->size();j++){
120 (*state_val)[j].prob = exp((*
state_val)[j].prob - sum);
128 for (
size_t i =1; i <
position->size(); ++i){
129 for (
size_t j = (*
position)[i-1]+1; j <= (*position)[i] ; ++j){
142 size_t start_val = (pos == 0) ? 0 : (*
position)[pos-1]+1;
143 for (
size_t i = start_val ; i <= (*position)[pos] ; ++i){
160 std::stringstream str;
164 for(
size_t i=0;i<
position->size();++i){
165 for (
size_t j = (i==0) ? 0 : (*position)[i-1]+1 ; j <= (*position)[i] ; ++j ){
166 str << (*state_val)[j].state_id <<
":"<< (*state_val)[j].state_prev <<
" : " << (*state_val)[j].prob <<
"\t";
179 double random((
double)rand()/((
double)(RAND_MAX)+(
double)(1)));
180 double cumulative_prob(0.0);
188 cumulative_prob += (*state_val)[i].prob;
189 if (random <= cumulative_prob){
190 state_prev = (*state_val)[i].state_prev;
192 offset = (*state_val)[i].prev_cell;
200 random = (double)rand()/((double)(RAND_MAX)+(double)(1));
204 for (
size_t cells = (i == 0) ? 0 + offset : (*position)[i-1]+offset+1; cells <= (*position)[i] ; ++cells){
209 cumulative_prob+=(*state_val)[cells].prob;
211 if (random <= cumulative_prob){
212 state_prev = (*state_val)[cells].state_prev;
214 offset = (*state_val)[cells].prev_cell;
354 table =
new (std::nothrow) std::vector<std::vector<std::vector<stoch_val> > > ;
363 (*table)[pos][state].push_back(
stoch_val(state_to,val));
378 double sum(-INFINITY);
382 for(
size_t position=0; position <
seq_length;++position){
387 for(
size_t value=0; value < (*table)[position][
state].size(); value++){
391 for(
size_t value=0; value < (*table)[position][
state].size();value++){
392 (*table)[position][
state][value].prob = exp((*
table)[position][
state][value].prob-sum);
399 for(
size_t i=0;i<
ending.size();i++){
403 for(
size_t i=0;i<
ending.size();i++){
414 double random((
double)rand()/((
double)(RAND_MAX)+(
double)(1)));
415 double cumulative_prob(0.0);
420 for(
size_t i = 0; i<
ending.size(); ++i){
421 cumulative_prob += (
ending)[i].prob;
422 if (random <= cumulative_prob){
423 state_prev =
ending[i].previous_state;
430 for(
size_t position =
table[state_prev].size()-1; position !=
SIZE_MAX ; --position){
431 random = (double)rand()/((double)(RAND_MAX)+(double)(1));
434 for (
size_t st = 0; st < (*table)[position][state_prev].size(); st++){
436 cumulative_prob+=(*table)[position][state_prev][st].prob;
438 if (random <= cumulative_prob){
439 state_prev = (*table)[position][state_prev][st].previous_state;
450 std::stringstream str;
451 for(
size_t position = 0; position <
seq_length-1; position++){
454 for (
size_t val = 0; val < (*table)[position][
state].size();val++){
455 str <<
"\t" <<
state <<
":" << (*table)[position][
state][val].previous_state <<
" : " << (*table)[position][
state][val].prob ;