NCBI C++ ToolKit
sls_alp_sim.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: sls_alp_sim.cpp 85261 2019-01-29 14:28:02Z rackerst $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's offical duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================*/
25 
26 /*****************************************************************************
27 
28 File name: sls_alp_sim.cpp
29 
30 Author: Sergey Sheetlin
31 
32 Contents: Calculation of Gumbel parameters
33 
34 ******************************************************************************/
35 
36 
37 #include <ncbi_pch.hpp>
38 
39 #include "sls_alp_sim.hpp"
40 
42 USING_SCOPE(blast);
44 
45 
46 alp_sim::alp_sim(//constructor
47 alp_data *alp_data_)
48 {
49 
50 
52 
53 
56 
57  d_C_tmp=NULL;
59 
60 
61  d_alp_data=alp_data_;
62  if(!d_alp_data)
63  {
64  throw error("Unexpected error",4);
65  };
66  bool ee_error_flag=false;
67  error ee_error("",0);
68  ofstream frand;
69 
70  try
71  {
72  try
73  {
74 
75 
78  d_n_alp_obj=0;
79 
81 
82 
83  double memory_before1=d_alp_data->d_memory_size_in_MB;
84  double time_before1;
85  alp_data::get_current_time(time_before1);
86 
87 
88  d_alp_data->d_time_before1=time_before1;
89 
91  {
93  };
94 
95 
96  //trial simulation for estimation time and memory
97  Int4 M_min;
98  Int4 nalp;
99 
100  double time_after_tmp;
101 
104 
107 
110 
113 
114 
115 
116  Int4 maximum_number_of_realizations_for_preliminary_simulation=1000;
117 
118  bool loop_break_flag;
119  Int4 rand_i=-1;
120 
121  bool lambda_accuracy_flag=true;
122  Int4 sim_number=1;
123  do{
124 
125  Int4 nalp_lambda;
126  bool C_calculation=false;
127 
128  Int4 number_tmp;
129 
130  bool check_time_flag=true;
131 
133  {
134  check_time_flag=false;
135  rand_i++;
137  }
138  else
139  {
140  number_tmp=alp_data::Tmin(maximum_number_of_realizations_for_preliminary_simulation-1,d_n_alp_obj+sim_number*d_alp_data->d_minimum_realizations_number-1);
141  };
142 
143 
145  0,
146  number_tmp,
147  M_min,
148  nalp,
149  nalp_lambda,
150  C_calculation,
151  check_time_flag);
152 
153  if(!d_alp_data->d_rand_flag)
154  {
156  };
157 
158 
159 
160  sim_number*=2;
161 
162  if(d_lambda_tmp->d_elem[nalp]>=0)
163  {
164  if(d_lambda_tmp_errors->d_elem[nalp]/d_lambda_tmp->d_elem[nalp]<alp_data_->d_eps_lambda)
165  {
166  lambda_accuracy_flag=false;
167  };
168  };
169 
170  alp_data::get_current_time(time_after_tmp);
171 
172 
173  if(number_tmp>=maximum_number_of_realizations_for_preliminary_simulation-1)
174  {
175  if(!d_alp_data->d_rand_flag)
176  {
177  break;
178  };
179  };
180 
182  {
183  loop_break_flag=(rand_i>=(Int4)(d_alp_data->d_rand_all->d_first_stage_preliminary_realizations_numbers_ALP.size()-1));
184  }
185  else
186  {
187  loop_break_flag=!(maximum_number_of_realizations_for_preliminary_simulation>d_n_alp_obj-1&&
188  lambda_accuracy_flag&&((time_after_tmp-time_before1)<=0||((time_after_tmp-time_before1)<0.01*d_alp_data->d_max_time&&d_alp_data->d_memory_size_in_MB<d_alp_data->d_max_mem)));
189  };
190 
191  }
192  while(!loop_break_flag);
193 
194 
195 
196 
197  double memory_after1=d_alp_data->d_memory_size_in_MB;
198  double time_after1;
199  alp_data::get_current_time(time_after1);
200 
201  if(memory_after1<=memory_before1)
202  {
203  delete d_alp_obj;d_alp_obj=NULL;
204  throw error("Unexpected error\n",4);
205  };
206 
207  Int4 limit_by_memory=(Int4)alp_data::Tmin((double)kMax_I4-1,alp_data::round(d_alp_data->d_max_mem/2.0/(memory_after1-memory_before1)*d_n_alp_obj));
208 
210  {
211  //cout<<"\nWarning: memory limit is reached.\nTo get the requested accuracy please\nincrease maximum allowed amount of memory if possible\n\n";
212  };
213 
214 
215  Int4 limit_by_time;
216  if(time_after1<=time_before1)
217  {
218  limit_by_time=kMax_I4;
219  }
220  else
221  {
222  limit_by_time=(Int4)alp_data::Tmin((double)kMax_I4-1,alp_data::round(d_alp_data->d_max_time/3.0/4.0/(time_after1-time_before1)*d_n_alp_obj));
223  };
224 
225 
226  Int4 realizations_number2=alp_data::Tmin((Int4)alp_data::round(limit_by_time)-1,limit_by_memory-1);
227 
228  realizations_number2=alp_data::Tmin(maximum_number_of_realizations_for_preliminary_simulation-(Int4)1,realizations_number2);
229 
230  realizations_number2=alp_data::Tmax(d_n_alp_obj-(Int4)1,realizations_number2);
231 
232 
233 
236 
237  delete d_C_tmp;d_C_tmp=NULL;
239 
240 
241  //trial simulation for estimation M_min and nalp
244 
247 
250 
253 
254 
255 
256  Int4 nalp_lambda;
257  bool C_calculation=false;
258 
259 
260 
261 
262  double lambda;
263  double eps_K=d_alp_data->d_eps_K;
264  double K_C;
265  double K_C_error;
266  Int4 level;
267  Int4 diff_opt;
268 
269 
270  rand_i=-1;
271  loop_break_flag=false;
272 
273  double time_before_ALP;
274  double time_during_ALP;
275  Int4 number_of_realizations_with_ALP=alp_data::Tmin(realizations_number2,d_n_alp_obj-(Int4)1+d_alp_data->d_minimum_realizations_number);
276  Int4 number_of_realizations_with_ALP_pred;
277 
278  alp_data::get_current_time(time_before_ALP);
279  do{
280 
281  bool check_time_flag=true;
282 
284  {
285  check_time_flag=false;
286  rand_i++;
287  number_of_realizations_with_ALP=d_alp_data->d_rand_all->d_preliminary_realizations_numbers_ALP[rand_i];
288  };
289 
291  0,
292  number_of_realizations_with_ALP,
293  M_min,
294  nalp,
295  nalp_lambda,
296  C_calculation,
297  check_time_flag);
298 
299  if(!d_alp_data->d_rand_flag)
300  {
301  d_alp_data->d_rand_all->d_preliminary_realizations_numbers_ALP.push_back(number_of_realizations_with_ALP);
302  };
303 
304 
305 
306 
307  lambda=d_lambda_tmp->d_elem[nalp];
308 
309  double tmp_lambda=2.0;
310 
311  if(d_lambda_tmp->d_elem[nalp]>0)
312  {
314  };
315 
316 
317 
318 
319 
320  number_of_realizations_with_ALP_pred=number_of_realizations_with_ALP;
321 
322 
323 
324  alp_data::get_current_time(time_during_ALP);
325 
327  {
328  loop_break_flag=(rand_i>=(Int4)(d_alp_data->d_rand_all->d_preliminary_realizations_numbers_ALP.size()-1));
329  if(loop_break_flag)
330  {
331  break;
332  };
333  };
334 
335 
336  if(time_during_ALP-time_before1>=d_alp_data->d_max_time*0.25||number_of_realizations_with_ALP>=realizations_number2||tmp_lambda<=1.0)
337  {
338  if(!d_alp_data->d_rand_flag)
339  {
340  break;
341  };
342  };
343 
344  if(time_during_ALP<=time_before_ALP)
345  {
346  number_of_realizations_with_ALP=alp_data::Tmin(realizations_number2,number_of_realizations_with_ALP+d_alp_data->d_minimum_realizations_number);
347  }
348  else
349  {
350 
351  Int4 max_number_of_realizations=(Int4)floor(number_of_realizations_with_ALP*(d_alp_data->d_max_time*0.35-(time_before_ALP-time_before1))/(time_during_ALP-time_before_ALP));
352  number_of_realizations_with_ALP=alp_data::Tmin(realizations_number2,(Int4)floor(0.5*number_of_realizations_with_ALP+0.5*max_number_of_realizations));
353  if(number_of_realizations_with_ALP>=max_number_of_realizations)
354  {
355  number_of_realizations_with_ALP=d_alp_data->Tmin(realizations_number2,number_of_realizations_with_ALP+d_alp_data->d_minimum_realizations_number);
356  };
357 
358  if((double)(number_of_realizations_with_ALP-number_of_realizations_with_ALP_pred)/(double)number_of_realizations_with_ALP_pred<0.005)
359  {
360  number_of_realizations_with_ALP=number_of_realizations_with_ALP_pred;
361  if(!d_alp_data->d_rand_flag)
362  {
363  break;
364  };
365 
366  };
367  };
368 
369 
370 
371  }
372  while(!loop_break_flag);
373 
374  realizations_number2=number_of_realizations_with_ALP;
375  Int4 realizations_number2_lambda=number_of_realizations_with_ALP;
376 
377  rand_i=-1;
378  loop_break_flag=false;
379 
380  double time_before_kill;
381  double time_during_kill;
382  Int4 number_of_realizations_with_killing=d_alp_data->Tmin(realizations_number2,d_alp_data->d_minimum_realizations_number-(Int4)1);
383  Int4 number_of_realizations_with_killing_pred;
384 
385  alp_data::get_current_time(time_before_kill);
386  do{
387 
388  bool check_time_flag=false;
389 
391  {
392  check_time_flag=false;
393  rand_i++;
394  number_of_realizations_with_killing=d_alp_data->d_rand_all->d_preliminary_realizations_numbers_killing[rand_i];
395  };
396 
397 
398  kill(
399  check_time_flag,
400  0,
401  number_of_realizations_with_killing,
402  M_min,
403  lambda,
404  eps_K,
405  K_C,
406  K_C_error,
407  level,
408  diff_opt);
409 
410  if(!d_alp_data->d_rand_flag)
411  {
412  d_alp_data->d_rand_all->d_preliminary_realizations_numbers_killing.push_back(number_of_realizations_with_killing);
413  };
414 
415 
416 
417  number_of_realizations_with_killing_pred=number_of_realizations_with_killing;
418 
419 
420  alp_data::get_current_time(time_during_kill);
421 
422  double tmp_K=2.0;
423 
424  if(K_C>0)
425  {
426  tmp_K=((K_C_error/K_C)/d_alp_data->d_eps_K);
427  };
428 
430  {
431  loop_break_flag=(rand_i>=(Int4)(d_alp_data->d_rand_all->d_preliminary_realizations_numbers_killing.size()-1));
432  if(loop_break_flag)
433  {
434  break;
435  };
436  };
437 
438 
439  if(time_during_kill-time_before1>=d_alp_data->d_max_time||number_of_realizations_with_killing>=realizations_number2||tmp_K<=1.0)
440  {
441  if(!d_alp_data->d_rand_flag)
442  {
443  break;
444  };
445  };
446 
447 
448 
449  if(time_during_kill<=time_before_kill)
450  {
451  number_of_realizations_with_killing=d_alp_data->Tmin(realizations_number2,number_of_realizations_with_killing+d_alp_data->d_minimum_realizations_number);
452  }
453  else
454  {
455 
456  Int4 max_number_of_realizations=(Int4)floor(number_of_realizations_with_killing*(d_alp_data->d_max_time-(time_before_kill-time_before1))/(time_during_kill-time_before_kill));
457  number_of_realizations_with_killing=d_alp_data->Tmin(realizations_number2,(Int4)floor(0.5*number_of_realizations_with_killing+0.5*max_number_of_realizations));
458  if(number_of_realizations_with_killing>=max_number_of_realizations)
459  {
460  number_of_realizations_with_killing=d_alp_data->Tmin(realizations_number2,number_of_realizations_with_killing+d_alp_data->d_minimum_realizations_number);
461  };
462 
463  if((double)(number_of_realizations_with_killing-number_of_realizations_with_killing_pred)/(double)number_of_realizations_with_killing_pred<0.005)
464  {
465  number_of_realizations_with_killing=number_of_realizations_with_killing_pred;
466  if(!d_alp_data->d_rand_flag)
467  {
468  break;
469  };
470  };
471  };
472 
473 
474  }
475  while(!loop_break_flag);
476 
477 #ifdef DEBUG_GUMBEL_PARAMS
478  double memory_after2=d_alp_data->d_memory_size_in_MB;
479 #endif
480 
481  Int4 k;
482  for(k=0;k<=number_of_realizations_with_killing;k++)
483  {
485  };
486 
487 
488  realizations_number2=number_of_realizations_with_killing;
489  Int4 realizations_number2_K=number_of_realizations_with_killing;
490 
491 
492 
493  d_alp_data->d_realizations_number2=realizations_number2_lambda;
494 
495 
496  if(K_C<=0)
497  {
498  throw error("The program cannot estimate the parameters; please repeat the calculation",2);
499  };
500 
501  double tmp=((K_C_error/K_C)/d_alp_data->d_eps_K);
502  tmp=alp_data::Tmin(ceil((realizations_number2_K+1)*tmp*tmp),(double)kMax_I4);
503  Int4 realizations_number_killing=(Int4)tmp;
505  tmp=alp_data::Tmin(ceil((realizations_number2_lambda+1)*tmp*tmp),(double)kMax_I4);
506  Int4 realizations_number_lambda=(Int4)tmp;
507 
508  double time_after2;
509  alp_data::get_current_time(time_after2);
510  time_after2=alp_data::Tmax(time_after2,time_after_tmp);
511 
514 
515  delete d_C_tmp;d_C_tmp=NULL;
517 
518 
519 
520 
521  //main simulation
522 
523  Int4 j;
524  j=1;
525  Int4 kill_j=0;
526 
527 
528 
529 
530  bool kill_flag=(realizations_number_killing>realizations_number2_K+1+j);
531  bool lambda_flag=(realizations_number_lambda>realizations_number2_lambda+1+j);
532  Int4 nalp_for_simulation=nalp;
533 
535  {
536  lambda_flag=(d_alp_data->d_rand_all->d_total_realizations_number_with_ALP>realizations_number2_K+j-1);
537  kill_flag=(d_alp_data->d_rand_all->d_total_realizations_number_with_killing>realizations_number2_K+j-1);
538  };
539 
540 
541 
542 
543  if(kill_flag||lambda_flag)
544  {
545 
546 
547 
548  Int4 step_for_time=1;
549 
550  Int4 number_of_unsuccesful_objects=0;
551 
552 
553 
554  while(d_n_alp_obj<kMax_I4-realizations_number2_lambda-j)
555  {
556  kill_flag=(realizations_number_killing>realizations_number2_K+j);
557  lambda_flag=(realizations_number_lambda>realizations_number2_lambda+j);
558 
560  {
561  lambda_flag=(d_alp_data->d_rand_all->d_total_realizations_number_with_ALP>realizations_number2_K+j-1);
562  kill_flag=(d_alp_data->d_rand_all->d_total_realizations_number_with_killing>realizations_number2_K+j-1);
563  };
564 
565  if(!(kill_flag||lambda_flag))
566  {
567  if(!d_alp_data->d_rand_flag)
568  {
569  //cout<<"\nThe parameters were calculated with the required accuracy\n\n";
570  break;
571  }
572  else
573  {
574  break;
575  };
576  };
577 
578 
579 
580 
581 
582  if(!kill_flag)
583  {
584  nalp_for_simulation=alp_data::Tmin(nalp_lambda,nalp);
585  };
586 
587  bool sucess_flag=false;
588 
589 
590 
591  if(realizations_number2_K+j<=realizations_number2_lambda)
592  {
593 
594  }
595  else
596  {
597  d_alp_obj->set_elem(realizations_number2_K+j,NULL);
598  d_n_alp_obj++;
599  };
600 
601  alp *&obj=d_alp_obj->d_elem[realizations_number2_K+j];
602 
603 
604 
605  try
606  {
607  double eps_tmp;
608 
609  while(!sucess_flag)
610  {
611 
612 
613  bool check_time_flag=true;
614 
616  {
617  check_time_flag=false;
618  };
619 
621  check_time_flag,
622  M_min,
623  nalp_for_simulation,
624  kill_flag,
625  level,
626  diff_opt,
627  obj,
628  sucess_flag,
629  eps_tmp);
630 
631 
632 
633 
634 
635  if(!sucess_flag)
636  {
637  number_of_unsuccesful_objects++;
638 
639  if(number_of_unsuccesful_objects>5+j*eps_tmp)
640  {
641  if(realizations_number2_K+j>realizations_number2_lambda)
642  {
643  d_n_alp_obj--;
644  };
645  throw error("The program cannot calculate the parameters\n",3);
646  };
647  };
648  };
649 
650  }
652  {
654  {
655  throw error("Unexpected error in ramdomization\n",4);
656  };
657 
658  //cout<<"\nWarning: time limit is reached.\nTo get the requested accuracy please\nincrease the allowed calculation time if possible\n\n";
659  if(realizations_number2_K+j>realizations_number2_lambda)
660  {
661  delete obj;obj=NULL;
662  d_n_alp_obj--;
663  };
664  break;
665  };
666 
667 
668 
669 
670 
671  if(realizations_number2_K+j>realizations_number2_lambda)
672  {
673 
674  if(kill_flag)
675  {
676  kill_j=j;
677  };
678 
679  };
680 
681 
682  d_alp_obj->d_elem[realizations_number2_K+j]->partially_release_memory();
683 
684  j++;
685 
686 
687 
689  {
690  if(!d_alp_data->d_rand_flag)
691  {
692  //cout<<"\nWarning: memory limit is reached.\nTo get the requested accuracy please\nincrease the allowed amount of memory if possible\n\n";
693  break;
694  };
695  };
696 
697 
698  if(j%step_for_time==0)
699  {
700  double time_after3;
701  alp_data::get_current_time(time_after3);
702 
703  if((time_after3-time_before1)>d_alp_data->d_max_time)
704  {
705  if(!d_alp_data->d_rand_flag)
706  {
707  //cout<<"\nWarning: time limit is reached.\nTo get the requested accuracy please\nincrease the allowed calculation time if possible\n\n";
708  break;
709  };
710  };
711  };
712  };
713  }
714  else
715  {
716  //cout<<"\nThe parameters were calculated with required accuracy\n\n";
717  };
718 
719 
720  Int4 final_realizations_number_killing=kill_j+realizations_number2_K+1;
721  Int4 final_realizations_number_lambda=alp_data::Tmax(realizations_number2_lambda+1,j+realizations_number2_K);
722  d_n_alp_obj=final_realizations_number_lambda;
723 
724 
725 #ifdef DEBUG_GUMBEL_PARAMS
726  double memory_after3=d_alp_data->d_memory_size_in_MB;
727 #endif
728 
729  double time_after100;
730  alp_data::get_current_time(time_after100);
731 
732 #ifdef DEBUG_GUMBEL_PARAMS
733  cout<<"\nActual calculation time is "<<time_after100-time_before1<<" seconds\n";
734  cout<<"Actual memory usage is "<<d_alp_data->Tmax(memory_after2,memory_after3)<<" MBs\n\n";
735 #endif
736  if(!d_alp_data->d_rand_flag)
737  {
738  d_alp_data->d_rand_all->d_total_realizations_number_with_ALP=final_realizations_number_lambda-1;
739  d_alp_data->d_rand_all->d_total_realizations_number_with_killing=final_realizations_number_killing-1;
740  };
741 
742 
743 
744  bool inside_simulation_flag;
745 
746  this->m_CalcTime=time_after100-time_before1;
747 
749  time_after100-time_before1,
750  nalp,
751  nalp_for_simulation,
752  level,
753  M_min,
754  inside_simulation_flag,
755  final_realizations_number_lambda,
756  final_realizations_number_killing);
757 
758 
759  }
760  catch (error er)
761  {
762  ee_error_flag=true;
763  ee_error=er;
764  };
765  }
766  catch (...)
767  {
768  ee_error_flag=true;
769  ee_error=error("Internal error in the program\n",4);
770  };
771 
772  //memory release
773  if(frand.is_open())
774  {
775  frand.close();
776  };
777 
778  if(ee_error_flag)
779  {
782 
783  delete d_C_tmp;d_C_tmp=NULL;
785 
786  this->~alp_sim();
787  throw error(ee_error.st,ee_error.error_code);
788  };
789 
790 };
791 
792 
794 Int4 final_realizations_number_lambda_,
795 Int4 final_realizations_number_killing_)
796 {
797  randomize_realizations_ind(0,final_realizations_number_killing_-1);
798  randomize_realizations_ind(final_realizations_number_killing_,final_realizations_number_lambda_-1);
799 
800 };
801 
803 Int4 ind1_,
804 Int4 ind2_)
805 {
806  bool ee_error_flag=false;
807  error ee_error("",0);
808  alp**array_ind=NULL;
809  Int4 *perm=NULL;
810 
811  try
812  {
813  try
814  {
815 
816  if(ind1_>=ind2_)
817  {
818  return;
819  };
820 
821  if(ind2_>d_n_alp_obj-1)
822  {
823  throw error("Unexpected error",4);
824  };
825 
826 
827  Int4 total_number=ind2_-ind1_+1;
828 
829  array_ind=new alp*[total_number];
830  alp_data::assert_mem(array_ind);
831 
832 
833 
834 
835 
836  perm=new Int4 [total_number];
837  alp_data::assert_mem(perm);
838 
839  generate_random_permulation(perm,total_number);
840 
841  Int4 i;
842  for(i=0;i<total_number;i++)
843  {
844  array_ind[i]=d_alp_obj->d_elem[ind1_+perm[i]];
845  };
846 
847  for(i=0;i<total_number;i++)
848  {
849  d_alp_obj->d_elem[ind1_+i]=array_ind[i];
850  };
851 
852 
853  }
854  catch (error er)
855  {
856  ee_error_flag=true;
857  ee_error=er;
858  };
859  }
860  catch (...)
861  {
862  ee_error_flag=true;
863  ee_error=error("Internal error in the program\n",4);
864  };
865 
866  //memory release
867  delete[]array_ind;array_ind=NULL;
868  delete[]perm;perm=NULL;
869 
870 
871  if(ee_error_flag)
872  {
873  throw error(ee_error.st,ee_error.error_code);
874  };
875 
876 
877 };
878 
880 Int4 *perm_,
881 Int4 dim_)
882 {
883  Int4 i;
884  for(i=0;i<dim_;i++)
885  {
886  perm_[i]=i;
887  };
888 
889  for(i=0;i<dim_-1;i++)
890  {
891  Int4 ind_swap=i+alp_data::random_long(d_alp_data->ran2(),dim_-i);
892  Int4 tmp=perm_[ind_swap];
893  perm_[ind_swap]=perm_[i];
894  perm_[i]=tmp;
895  };
896 };
897 
898 
900 double time_,
901 Int4 nalp,
902 Int4 nalp_for_lambda_simulation,
903 Int4 level,
904 Int4 M_min_,
905 bool &inside_simulation_flag,
906 Int4 final_realizations_number_lambda_,
907 Int4 final_realizations_number_killing_)
908 {
909  bool ee_error_flag=false;
910  error ee_error("",0);
911 
912  try
913  {
914  try
915  {
916 
917  double lambda;
918  double lambda_error;
919  double test_difference;
920  double test_difference_error;
921  double C;
922  double C_error;
923  double C2;
924  double C2_error;
925  double C4;
926  double C4_error;
927  double K_C;
928  double K_C_error;
929  double a_I;
930  double a_I_error;
931  double a_J;
932  double a_J_error;
933  double sigma;
934  double sigma_error;
935  double alpha_I;
936  double alpha_I_error;
937  double alpha_J;
938  double alpha_J_error;
939  double K;
940  double K_error;
941 
942  bool flag=false;
943  Int4 number_of_trials=0;
944  Int4 number_of_trials_threshold=4;
945 
946  do{
947 
949  final_realizations_number_lambda_,
950  final_realizations_number_killing_,
951  nalp,
952  nalp_for_lambda_simulation,
953  level,
954  inside_simulation_flag,
955  lambda,
956  lambda_error,
957  test_difference,
958  test_difference_error,
959  C,
960  C_error,
961  C2,
962  C2_error,
963  C4,
964  C4_error,
965  K_C,
966  K_C_error,
967  a_I,
968  a_I_error,
969  a_J,
970  a_J_error,
971  sigma,
972  sigma_error,
973  alpha_I,
974  alpha_I_error,
975  alpha_J,
976  alpha_J_error,
977  K,
978  K_error,
979  flag);
980 
981 
982  number_of_trials++;
983 
984  if(!flag)
985  {
987  final_realizations_number_lambda_,
988  final_realizations_number_killing_);
989  //cout<<"Randomization attempt\t"<<number_of_trials<<endl;
990  };
991  }
992  while(!flag&&number_of_trials<=number_of_trials_threshold);
993 
994  if(!flag)
995  {
996  throw error("Error - please run the program once again\n",2);
997  };
998 
999 
1000  }
1001  catch (error er)
1002  {
1003  ee_error_flag=true;
1004  ee_error=er;
1005  };
1006  }
1007  catch (...)
1008  {
1009  ee_error_flag=true;
1010  ee_error=error("Internal error in the program\n",4);
1011  };
1012 
1013  //memory release
1014 
1015  if(ee_error_flag)
1016  {
1017  throw error(ee_error.st,ee_error.error_code);
1018  };
1019 };
1020 
1021 
1022 
1024 double val_,
1025 Int4 digits_)
1026 {
1027  Int4 i;
1028  for(i=0;i<digits_;i++)
1029  {
1030  val_*=10;
1031  };
1032  val_=alp_data::round(val_);
1033  for(i=0;i<digits_;i++)
1034  {
1035  val_/=10.0;
1036  };
1037 
1038  return val_;
1039 };
1040 
1042 double val_,
1043 double val_error_)
1044 {
1045  if(val_==0)
1046  {
1047  return DBL_MAX;
1048  };
1049 
1050  return fabs(round_doulbe(val_error_/val_*100.0,1));
1051 
1052 };
1053 
1054 
1056 Int4 number_of_realizations_)
1057 {
1058  Int4 max_number_of_subsimulations=20;
1059  Int4 min_number_of_subsimulations=3;
1060 
1061  Int4 min_number_of_realizations_for_subsimulation=2;
1062 
1063  if(number_of_realizations_<min_number_of_realizations_for_subsimulation*min_number_of_subsimulations)
1064  {
1065  throw error("Please repeat the simulation or increase calculation time\n",1);
1066  };
1067 
1068  Int4 res_subsimulations=(Int4)ceil(sqrt((double)number_of_realizations_));
1069  res_subsimulations=alp_data::Tmin(res_subsimulations,max_number_of_subsimulations);
1070  res_subsimulations=alp_data::Tmax(res_subsimulations,min_number_of_subsimulations);
1071 
1072  return res_subsimulations;
1073 };
1074 
1075 
1076 
1078 Int4 final_realizations_number_lambda_,
1079 Int4 final_realizations_number_killing_,
1080 Int4 nalp,
1081 Int4 nalp_for_lambda_simulation,
1082 Int4 level,
1083 bool &inside_simulation_flag,
1084 double &lambda,
1085 double &lambda_error,
1086 double &test_difference,
1087 double &test_difference_error,
1088 double &C,
1089 double &C_error,
1090 double &C2,
1091 double &C2_error,
1092 double &C4,
1093 double &C4_error,
1094 double &K_C,
1095 double &K_C_error,
1096 double &a_I,
1097 double &a_I_error,
1098 double &a_J,
1099 double &a_J_error,
1100 double &sigma,
1101 double &sigma_error,
1102 double &alpha_I,
1103 double &alpha_I_error,
1104 double &alpha_J,
1105 double &alpha_J_error,
1106 double &K,
1107 double &K_error,
1108 bool &flag_)
1109 {
1110 
1111  bool ee_error_flag=false;
1112  error ee_error("",0);
1113 
1114  Int4 *d_mult_realizations=NULL;
1115  Int4 *d_mult_K_realizations=NULL;
1116 
1117  double *lambda_mult=NULL;
1118  double *lambda_mult_error=NULL;
1119 
1120  double *C_mult=NULL;
1121  double *C_mult_error=NULL;
1122 
1123  double *a_I_mult=NULL;
1124  double *a_I_mult_error=NULL;
1125 
1126  double *a_J_mult=NULL;
1127  double *a_J_mult_error=NULL;
1128 
1129  double *sigma_mult=NULL;
1130  double *sigma_mult_error=NULL;
1131 
1132  double *alpha_I_mult=NULL;
1133  double *alpha_I_mult_error=NULL;
1134 
1135  double *alpha_J_mult=NULL;
1136  double *alpha_J_mult_error=NULL;
1137 
1138  double *K_C_mult=NULL;
1139  double *K_C_mult_error=NULL;
1140 
1141  double *K_mult=NULL;
1142  double *K_mult_error=NULL;
1143 
1144  void **alp_distr=NULL;
1145  void **alp_distr_errors=NULL;
1146 
1147  void ***alp_mult_distr=NULL;
1148  void ***alp_mult_distr_errors=NULL;
1149 
1150 
1151  try
1152  {
1153  try
1154  {
1155 
1156  flag_=false;
1157 
1158 
1159  if(final_realizations_number_killing_>final_realizations_number_lambda_)
1160  {
1161  throw error("Unexpected error\n",4);
1162  };
1163 
1164  Int4 mult_number_lambda=get_number_of_subsimulations(d_n_alp_obj);
1165  Int4 mult_number_K=get_number_of_subsimulations(final_realizations_number_killing_);
1166 
1167  d_mult_number=alp_data::Tmin(mult_number_lambda,mult_number_K);
1168 
1169  double mult_number_double_lambda=mult_number_lambda;
1170  double mult_number_double_K=mult_number_lambda;
1171 
1172 
1173 
1174  Int4 j;
1175 
1176 
1177  d_mult_realizations=new Int4[d_mult_number+1];
1178  alp_data::assert_mem(d_mult_realizations);
1179 
1180 
1181 
1182  d_mult_K_realizations=new Int4[d_mult_number+1];
1183  alp_data::assert_mem(d_mult_K_realizations);
1184 
1185  lambda_mult=new double[d_mult_number+1];
1186  alp_data::assert_mem(lambda_mult);
1187  lambda_mult_error=new double[d_mult_number+1];
1188  alp_data::assert_mem(lambda_mult_error);
1189 
1190  C_mult=new double[d_mult_number+1];
1191  alp_data::assert_mem(C_mult);
1192  C_mult_error=new double[d_mult_number+1];
1193  alp_data::assert_mem(C_mult_error);
1194 
1195  a_I_mult=new double[d_mult_number+1];
1196  alp_data::assert_mem(a_I_mult);
1197  a_I_mult_error=new double[d_mult_number+1];
1198  alp_data::assert_mem(a_I_mult_error);
1199 
1200  a_J_mult=new double[d_mult_number+1];
1201  alp_data::assert_mem(a_J_mult);
1202  a_J_mult_error=new double[d_mult_number+1];
1203  alp_data::assert_mem(a_J_mult_error);
1204 
1205  sigma_mult=new double[d_mult_number+1];
1206  alp_data::assert_mem(sigma_mult);
1207  sigma_mult_error=new double[d_mult_number+1];
1208  alp_data::assert_mem(sigma_mult_error);
1209 
1210  alpha_I_mult=new double[d_mult_number+1];
1211  alp_data::assert_mem(alpha_I_mult);
1212  alpha_I_mult_error=new double[d_mult_number+1];
1213  alp_data::assert_mem(alpha_I_mult_error);
1214 
1215  alpha_J_mult=new double[d_mult_number+1];
1216  alp_data::assert_mem(alpha_J_mult);
1217  alpha_J_mult_error=new double[d_mult_number+1];
1218  alp_data::assert_mem(alpha_J_mult_error);
1219 
1220  K_C_mult=new double[d_mult_number+1];
1221  alp_data::assert_mem(K_C_mult);
1222  K_C_mult_error=new double[d_mult_number+1];
1223  alp_data::assert_mem(K_C_mult_error);
1224 
1225  K_mult=new double[d_mult_number+1];
1226  alp_data::assert_mem(K_mult);
1227  K_mult_error=new double[d_mult_number+1];
1228  alp_data::assert_mem(K_mult_error);
1229 
1230 
1231  double lambda_mult2=0;
1232  double C_mult2=0;
1233  double K_C_mult2=0;
1234  double a_I_mult2=0;
1235  double a_J_mult2=0;
1236  double sigma_mult2=0;
1237  double alpha_I_mult2=0;
1238  double alpha_J_mult2=0;
1239  double K_mult2=0;
1240 
1241  double lambda_mult2_error=0;
1242  double C_mult2_error=0;
1243  double K_C_mult2_error=0;
1244  double a_I_mult2_error=0;
1245  double a_J_mult2_error=0;
1246  double sigma_mult2_error=0;
1247  double alpha_I_mult2_error=0;
1248  double alpha_J_mult2_error=0;
1249  double K_mult2_error=0;
1250 
1251 
1252 
1253 
1254 
1255  for(j=0;j<=nalp_for_lambda_simulation;j++)
1256  {
1258  0,
1259  d_n_alp_obj-1,
1260  alp_distr,
1261  alp_distr_errors,
1262  j);
1263  };
1264 
1265 
1266  alp_mult_distr=new void **[d_mult_number+1];
1267  alp_data::assert_mem(alp_mult_distr);
1268 
1269  for(j=0;j<=d_mult_number;j++)
1270  {
1271  alp_mult_distr[j]=NULL;
1272  };
1273 
1274  alp_mult_distr_errors=new void **[d_mult_number+1];
1275  alp_data::assert_mem(alp_mult_distr_errors);
1276  for(j=0;j<=d_mult_number;j++)
1277  {
1278  alp_mult_distr_errors[j]=NULL;
1279  };
1280 
1281  alp_mult_distr[0]=alp_distr;
1282  alp_mult_distr_errors[0]=alp_distr_errors;
1283 
1284  Int4 real_number=(Int4)floor((double)final_realizations_number_lambda_/(double)d_mult_number);
1285 
1286  d_mult_realizations[0]=final_realizations_number_lambda_;
1287 
1288  Int4 k;
1289  for(k=1;k<=d_mult_number;k++)
1290  {
1291  d_mult_realizations[k]=real_number;
1292  };
1293 
1294 
1295  Int4 nr_tmp=0;
1296  for(k=1;k<=d_mult_number;k++)
1297  {
1298  nr_tmp+=d_mult_realizations[k];
1299  Int4 j;
1300  for(j=0;j<=nalp_for_lambda_simulation;j++)
1301  {
1303  nr_tmp-d_mult_realizations[k],
1304  nr_tmp-1,
1305  alp_mult_distr[k],
1306  alp_mult_distr_errors[k],
1307  j);
1308  };
1309  };
1310 
1311  nr_tmp=0;
1312  for(k=1;k<=d_mult_number;k++)
1313  {
1314  nr_tmp+=d_mult_realizations[k];
1315  Int4 nalp_tmp;
1316 
1317  double test_difference;
1318  double test_difference_error;
1319 
1320 
1321 
1323  false,
1324  nalp_for_lambda_simulation,
1325  nalp_tmp,
1326  inside_simulation_flag,
1327  nr_tmp-d_mult_realizations[k],
1328  nr_tmp-1,
1329  alp_mult_distr[k],
1330  alp_mult_distr_errors[k],
1331  lambda_mult[k],
1332  lambda_mult_error[k],
1333  test_difference,
1334  test_difference_error);
1335 
1336 
1337  if(!inside_simulation_flag)
1338  {
1339  goto label1;
1340  };
1341 
1342 
1343  lambda_mult2+=lambda_mult[k];
1344  lambda_mult2_error+=lambda_mult[k]*lambda_mult[k];
1345  };
1346 
1347 
1348 
1349  Int4 nalp_tmp;
1350 
1352  false,
1353  nalp_for_lambda_simulation,
1354  nalp_tmp,
1355  inside_simulation_flag,
1356  0,
1357  final_realizations_number_lambda_-1,
1358  alp_distr,
1359  alp_distr_errors,
1360  lambda,
1361  lambda_error,
1362  test_difference,
1363  test_difference_error);
1364 
1365  if(!inside_simulation_flag)
1366  {
1367  throw error("Error - please run the program once again\n",2);
1368  };
1369 
1370  lambda_mult[0]=lambda;
1371  lambda_mult_error[0]=lambda_error;
1372 
1373 
1374 
1375  nr_tmp=0;
1376  for(k=1;k<=d_mult_number;k++)
1377  {
1378 
1379  nr_tmp+=d_mult_realizations[k];
1380  calculate_C(
1381  0,
1382  nalp_for_lambda_simulation,
1383  nr_tmp-d_mult_realizations[k],
1384  nr_tmp-1,
1385  alp_mult_distr[k],
1386  alp_mult_distr_errors[k],
1387  lambda_mult[k],
1388  lambda_mult_error[k],
1389  C_mult[k],
1390  C_mult_error[k]);
1391 
1392  C_mult2+=C_mult[k];
1393  C_mult2_error+=C_mult[k]*C_mult[k];
1394 
1395 
1396  };
1397 
1398 
1399  calculate_C(
1400  0,
1401  nalp_for_lambda_simulation,
1402  0,
1403  final_realizations_number_lambda_-1,
1404  alp_distr,
1405  alp_distr_errors,
1406  lambda,
1407  lambda_error,
1408  C,
1409  C_error);
1410 
1411 
1412  C_mult[0]=C;
1413  C_mult_error[0]=C_error;
1414 
1415 
1416 
1417  nr_tmp=0;
1418  for(k=1;k<=d_mult_number;k++)
1419  {
1420 
1421  nr_tmp+=d_mult_realizations[k];
1422  calculate_FSC(
1423  nalp_for_lambda_simulation,
1424  nr_tmp-d_mult_realizations[k],
1425  nr_tmp-1,
1426  alp_mult_distr[k],
1427  alp_mult_distr_errors[k],
1428  lambda_mult[k],
1429  lambda_mult_error[k],
1430  a_I_mult[k],
1431  a_I_mult_error[k],
1432  a_J_mult[k],
1433  a_J_mult_error[k],
1434  sigma_mult[k],
1435  sigma_mult_error[k],
1436  alpha_I_mult[k],
1437  alpha_I_mult_error[k],
1438  alpha_J_mult[k],
1439  alpha_J_mult_error[k]);
1440 
1441  a_I_mult2+=a_I_mult[k];
1442  a_I_mult2_error+=a_I_mult[k]*a_I_mult[k];
1443 
1444  a_J_mult2+=a_J_mult[k];
1445  a_J_mult2_error+=a_J_mult[k]*a_J_mult[k];
1446 
1447  sigma_mult2+=sigma_mult[k];
1448  sigma_mult2_error+=sigma_mult[k]*sigma_mult[k];
1449 
1450  alpha_I_mult2+=alpha_I_mult[k];
1451  alpha_I_mult2_error+=alpha_I_mult[k]*alpha_I_mult[k];
1452 
1453  alpha_J_mult2+=alpha_J_mult[k];
1454  alpha_J_mult2_error+=alpha_J_mult[k]*alpha_J_mult[k];
1455 
1456 
1457  };
1458 
1459 
1460  calculate_FSC(
1461  nalp_for_lambda_simulation,
1462  0,
1463  final_realizations_number_lambda_-1,
1464  alp_distr,
1465  alp_distr_errors,
1466  lambda,
1467  lambda_error,
1468  a_I,
1469  a_I_error,
1470  a_J,
1471  a_J_error,
1472  sigma,
1473  sigma_error,
1474  alpha_I,
1475  alpha_I_error,
1476  alpha_J,
1477  alpha_J_error);
1478 
1479 
1480  a_I_mult[0]=a_I;
1481  a_I_mult_error[0]=a_I_error;
1482  a_J_mult[0]=a_J;
1483  a_J_mult_error[0]=a_J_error;
1484  sigma_mult[0]=sigma;
1485  sigma_mult_error[0]=sigma_error;
1486  alpha_I_mult[0]=alpha_I;
1487  alpha_I_mult_error[0]=alpha_I_error;
1488  alpha_J_mult[0]=alpha_J;
1489  alpha_J_mult_error[0]=alpha_J_error;
1490 
1491 
1492  real_number=(Int4)floor((double)final_realizations_number_killing_/(double)d_mult_number);
1493 
1494 
1495  d_mult_K_realizations[0]=final_realizations_number_killing_;
1496 
1497 
1498  for(k=1;k<=d_mult_number;k++)
1499  {
1500  d_mult_K_realizations[k]=real_number;
1501  };
1502 
1503 
1504  nr_tmp=0;
1505  for(k=1;k<=d_mult_number;k++)
1506  {
1507  nr_tmp+=d_mult_K_realizations[k];
1508 
1509  Int4 recommended_level;
1510  Int4 diff_opt;
1511 
1512 
1513 
1515  nr_tmp-d_mult_K_realizations[k],
1516  nr_tmp-1,
1517  lambda_mult[k],
1519  level,
1520  recommended_level,
1521  diff_opt,
1522  K_C_mult[k],
1523  K_C_mult_error[k]);
1524 
1525 
1526  K_mult[k]=C_mult[k]*K_C_mult[k];
1527  K_mult_error[k]=alp_reg::error_of_the_product(
1528  C_mult[k],
1529  C_mult_error[k],
1530  K_C_mult[k],
1531  K_C_mult_error[k]);
1532 
1533  K_C_mult2+=K_C_mult[k];
1534  K_C_mult2_error+=K_C_mult[k]*K_C_mult[k];
1535 
1536  K_mult2+=K_mult[k];
1537  K_mult2_error+=K_mult[k]*K_mult[k];
1538 
1539  };
1540 
1541 
1542  Int4 recommended_level;
1543  Int4 diff_opt;
1544 
1545 
1546 
1548  0,
1549  final_realizations_number_killing_-1,
1550  lambda,
1552  level,
1553  recommended_level,
1554  diff_opt,
1555  K_C,
1556  K_C_error);
1557 
1558 
1559 
1560 
1561 
1562  K=C*K_C;
1564  C,
1565  C_error,
1566  K_C,
1567  K_C_error);
1568 
1569  K_C_mult[0]=K_C;
1570  K_C_mult_error[0]=K_C_error;
1571 
1572  K_mult[0]=K;
1573  K_mult_error[0]=K_error;
1574 
1575 
1576 
1577  lambda_mult2/=d_mult_number;
1578  C_mult2/=d_mult_number;
1579  K_C_mult2/=d_mult_number;
1580  a_I_mult2/=d_mult_number;
1581  a_J_mult2/=d_mult_number;
1582  sigma_mult2/=d_mult_number;
1583  alpha_I_mult2/=d_mult_number;
1584  alpha_J_mult2/=d_mult_number;
1585  K_mult2/=d_mult_number;
1586 
1587  lambda_mult2_error/=d_mult_number;
1588  C_mult2_error/=d_mult_number;
1589  K_C_mult2_error/=d_mult_number;
1590  a_I_mult2_error/=d_mult_number;
1591  a_J_mult2_error/=d_mult_number;
1592  sigma_mult2_error/=d_mult_number;
1593  alpha_I_mult2_error/=d_mult_number;
1594  alpha_J_mult2_error/=d_mult_number;
1595  K_mult2_error/=d_mult_number;
1596 
1597 
1598  mult_number_double_lambda=(double)final_realizations_number_lambda_/(double)real_number;
1599  mult_number_double_K=(double)final_realizations_number_killing_/(double)real_number;
1600 
1601 
1602  lambda_mult2_error=alp_reg::sqrt_for_errors(lambda_mult2_error-lambda_mult2*lambda_mult2)/sqrt((double)mult_number_double_lambda);
1603  C_mult2_error=alp_reg::sqrt_for_errors(C_mult2_error-C_mult2*C_mult2)/sqrt((double)mult_number_double_lambda);
1604  K_C_mult2_error=alp_reg::sqrt_for_errors(K_C_mult2_error-K_C_mult2*K_C_mult2)/sqrt((double)mult_number_double_K);
1605  a_I_mult2_error=alp_reg::sqrt_for_errors(a_I_mult2_error-a_I_mult2*a_I_mult2)/sqrt((double)mult_number_double_lambda);
1606  a_J_mult2_error=alp_reg::sqrt_for_errors(a_J_mult2_error-a_J_mult2*a_J_mult2)/sqrt((double)mult_number_double_lambda);
1607  sigma_mult2_error=alp_reg::sqrt_for_errors(sigma_mult2_error-sigma_mult2*sigma_mult2)/sqrt((double)mult_number_double_lambda);
1608  alpha_I_mult2_error=alp_reg::sqrt_for_errors(alpha_I_mult2_error-alpha_I_mult2*alpha_I_mult2)/sqrt((double)mult_number_double_lambda);
1609  alpha_J_mult2_error=alp_reg::sqrt_for_errors(alpha_J_mult2_error-alpha_J_mult2*alpha_J_mult2)/sqrt((double)mult_number_double_lambda);
1610  K_mult2_error=alp_reg::sqrt_for_errors(K_mult2_error-K_mult2*K_mult2)/sqrt((double)alp_data::Tmin(mult_number_double_lambda,mult_number_double_K));
1611 
1612 
1614  lambda,
1615  lambda_error,
1616  lambda_mult2,
1617  lambda_mult2_error);
1618 
1620  C,
1621  C_error,
1622  C_mult2,
1623  C_mult2_error);
1624 
1626  K_C,
1627  K_C_error,
1628  K_C_mult2,
1629  K_C_mult2_error);
1630 
1632  a_I,
1633  a_I_error,
1634  a_I_mult2,
1635  a_I_mult2_error);
1636 
1638  a_J,
1639  a_J_error,
1640  a_J_mult2,
1641  a_J_mult2_error);
1642 
1643 
1645  sigma,
1646  sigma_error,
1647  sigma_mult2,
1648  sigma_mult2_error);
1649 
1651  alpha_I,
1652  alpha_I_error,
1653  alpha_I_mult2,
1654  alpha_I_mult2_error);
1655 
1657  alpha_J,
1658  alpha_J_error,
1659  alpha_J_mult2,
1660  alpha_J_mult2_error);
1661 
1663  K,
1664  K_error,
1665  K_mult2,
1666  K_mult2_error);
1667 
1668  flag_=true;
1669 
1670 
1671  this->m_AI=a_I;
1672  this->m_AIError=a_I_error;
1673  this->m_AJ=a_J;
1674  this->m_AJError=a_J_error;
1675  this->m_Sigma=sigma;
1676  this->m_SigmaError=sigma_error;
1677  this->m_C=C;
1678  this->m_CError=C_error;
1679  this->m_K=K;
1680  this->m_KError=K_error;
1681  this->m_Lambda=lambda;
1682  this->m_LambdaError=lambda_error;
1683 
1684  this->m_AlphaI=alpha_I;
1685  this->m_AlphaIError=alpha_I_error;
1686  this->m_AlphaJ=alpha_J;
1687  this->m_AlphaJError=alpha_J_error;
1688 
1689  this->m_AISbs.resize(d_mult_number);
1690  this->m_AJSbs.resize(d_mult_number);
1691  this->m_SigmaSbs.resize(d_mult_number);
1692  this->m_CSbs.resize(d_mult_number);
1693  this->m_KSbs.resize(d_mult_number);
1694  this->m_LambdaSbs.resize(d_mult_number);
1695 
1696  this->m_AlphaISbs.resize(d_mult_number);
1697  this->m_AlphaJSbs.resize(d_mult_number);
1698 
1699 
1700  for(k=1;k<=d_mult_number;k++)
1701  {
1702  this->m_AISbs[k-1]=a_I_mult[k];
1703  this->m_AJSbs[k-1]=a_J_mult[k];
1704  this->m_SigmaSbs[k-1]=sigma_mult[k];
1705  this->m_CSbs[k-1]=C_mult[k];
1706  this->m_KSbs[k-1]=K_mult[k];
1707  this->m_LambdaSbs[k-1]=lambda_mult[k];
1708 
1709  this->m_AlphaISbs[k-1]=alpha_I_mult[k];
1710  this->m_AlphaJSbs[k-1]=alpha_J_mult[k];
1711  };
1712 
1713 
1714 
1715  label1:;
1716 
1717 
1718 
1719  }
1720  catch (error er)
1721  {
1722  ee_error_flag=true;
1723  ee_error=er;
1724  };
1725  }
1726  catch (...)
1727  {
1728  ee_error_flag=true;
1729  ee_error=error("Internal error in the program\n",4);
1730  };
1731 
1732  //memory release
1733  if(alp_distr)
1734  {
1735  Int4 j;
1736  for(j=1;j<=nalp_for_lambda_simulation;j++)
1737  {
1738  delete (array_positive<double>*)alp_distr[j];alp_distr[j]=NULL;
1739  };
1740 
1741  delete[]alp_distr;alp_distr=NULL;
1742  };
1743 
1744 
1745 
1746  if(alp_distr_errors)
1747  {
1748  Int4 j;
1749  for(j=1;j<=nalp_for_lambda_simulation;j++)
1750  {
1751  delete (array_positive<double>*)alp_distr_errors[j];alp_distr_errors[j]=NULL;
1752  };
1753 
1754  delete[]alp_distr_errors;alp_distr_errors=NULL;
1755  };
1756 
1757 
1758  if(alp_mult_distr)
1759  {
1760  Int4 k,j;
1761  for(k=1;k<=d_mult_number;k++)
1762  {
1763  if(alp_mult_distr[k])
1764  {
1765  for(j=1;j<=nalp_for_lambda_simulation;j++)
1766  {
1767  delete (array_positive<double>*)alp_mult_distr[k][j];alp_mult_distr[k][j]=NULL;
1768  };
1769 
1770  delete[]alp_mult_distr[k];alp_mult_distr[k]=NULL;
1771  };
1772  };
1773 
1774  delete[]alp_mult_distr;alp_mult_distr=NULL;
1775  };
1776 
1777 
1778  if(alp_mult_distr_errors)
1779  {
1780  Int4 k,j;
1781  for(k=1;k<=d_mult_number;k++)
1782  {
1783  if(alp_mult_distr_errors[k])
1784  {
1785  for(j=1;j<=nalp_for_lambda_simulation;j++)
1786  {
1787  delete (array_positive<double>*)alp_mult_distr_errors[k][j];alp_mult_distr_errors[k][j]=NULL;
1788  };
1789 
1790  delete[]alp_mult_distr_errors[k];alp_mult_distr_errors[k]=NULL;
1791  };
1792  };
1793 
1794  delete[]alp_mult_distr_errors;alp_mult_distr_errors=NULL;
1795  };
1796 
1797 
1798  delete[]d_mult_realizations;d_mult_realizations=NULL;
1799  delete[]d_mult_K_realizations;d_mult_K_realizations=NULL;
1800 
1801  delete[]lambda_mult;lambda_mult=NULL;
1802  delete[]lambda_mult_error;lambda_mult_error=NULL;
1803 
1804  delete[]C_mult;C_mult=NULL;
1805  delete[]C_mult_error;C_mult_error=NULL;
1806 
1807  delete[]a_I_mult;a_I_mult=NULL;
1808  delete[]a_I_mult_error;a_I_mult_error=NULL;
1809  delete[]a_J_mult;a_J_mult=NULL;
1810  delete[]a_J_mult_error;a_J_mult_error=NULL;
1811  delete[]sigma_mult;sigma_mult=NULL;
1812  delete[]sigma_mult_error;sigma_mult_error=NULL;
1813  delete[]alpha_I_mult;alpha_I_mult=NULL;
1814  delete[]alpha_I_mult_error;alpha_I_mult_error=NULL;
1815  delete[]alpha_J_mult;alpha_J_mult=NULL;
1816  delete[]alpha_J_mult_error;alpha_J_mult_error=NULL;
1817 
1818  delete[]K_C_mult;K_C_mult=NULL;
1819  delete[]K_C_mult_error;K_C_mult_error=NULL;
1820 
1821  delete[]K_mult;K_mult=NULL;
1822  delete[]K_mult_error;K_mult_error=NULL;
1823 
1824  if(ee_error_flag)
1825  {
1826  throw error(ee_error.st,ee_error.error_code);
1827  };
1828 
1829 };
1830 
1832 double C,
1833 double &C_error,
1834 double C_mult2,
1835 double C_mult2_error)
1836 {
1837  if(C!=0&&C_mult2!=0)
1838  {
1839  C_error=fabs(C*C_mult2_error/C_mult2);
1840  }
1841  else
1842  {
1843  C_error=C_mult2_error;
1844  };
1845 };
1846 
1847 alp_sim::~alp_sim()//destructor
1848 {
1849  Int4 i;
1850  for(i=0;i<d_n_alp_obj;i++)
1851  {
1852  delete d_alp_obj->d_elem[i];d_alp_obj->d_elem[i]=NULL;
1853  };
1854 
1855 
1856  if(d_alp_data)
1857  {
1859  };
1860 
1861  delete d_alp_obj;d_alp_obj=NULL;
1862  if(d_alp_data)
1863  {
1865  };
1866 
1867 
1868 };
1869 
1871 bool check_time_,
1872 Int4 ind1_,
1873 Int4 ind2_,
1874 Int4 M_min_,
1875 double lambda_,
1876 double eps_K_,
1877 double &K_C_,
1878 double &K_C_error_,
1879 Int4 &level_,
1880 Int4 &diff_opt_)
1881 {
1882 
1883  bool flag=false;
1884  Int4 current_level=(Int4)floor(M_min_*0.5);
1885  Int4 recommended_level;
1886  Int4 number_of_unsucesful_objects=0;
1887 
1888 
1889  Int4 i;
1890  for(i=ind1_;i<=ind2_;i++)
1891  {
1892 
1893  alp* &alp_obj_tmp=d_alp_obj->d_elem[i];
1894  if(i-ind1_+1>alp_obj_tmp->d_alp_data->d_minimum_realizations_number)
1895  {
1896  alp_obj_tmp->d_check_time_flag=check_time_;
1897  alp_obj_tmp->d_time_error_flag=check_time_;
1898  };
1899  };
1900 
1901  do{
1902  Int4 i;
1903  for(i=ind1_;i<=ind2_;i++)
1904  {
1905  alp* &alp_obj_tmp=d_alp_obj->d_elem[i];
1906  bool flag=false;
1907  while(!flag)
1908  {
1909  alp_obj_tmp->d_sentinels_flag=false;
1910  alp_obj_tmp->kill_upto_level(M_min_,current_level);
1911  if(!alp_obj_tmp->d_success)
1912  {
1913  number_of_unsucesful_objects++;
1914  if(number_of_unsucesful_objects>5+(ind2_-ind1_+1)*
1916  )
1917  {
1918  throw error("The program cannot estimate the parameters.\nPlease try to increase the allowed amount of memory or change parameters of the scoring system to ensure the logarithmic regime of the alignment score\n",1);
1919  };
1920  delete alp_obj_tmp;alp_obj_tmp=NULL;
1921  alp_obj_tmp=new alp(d_alp_data);
1922  alp_data::assert_mem(alp_obj_tmp);
1923 
1924  if(i-ind1_+1>alp_obj_tmp->d_alp_data->d_minimum_realizations_number)
1925  {
1926  alp_obj_tmp->d_check_time_flag=check_time_;
1927  alp_obj_tmp->d_time_error_flag=check_time_;
1928  };
1929 
1930  bool flag=false;
1931  while(!flag)
1932  {
1933  alp_obj_tmp->simulate_alp_upto_the_given_level(M_min_);
1934  if(!alp_obj_tmp->d_success)
1935  {
1936  number_of_unsucesful_objects++;
1937  if(number_of_unsucesful_objects>5+(ind2_-ind1_+1)*
1939  )
1940  {
1941  throw error("The program cannot estimate the parameters.\nPlease try to increase the allowed amount of memory or change parameters of the scoring system to ensure the logarithmic regime of the alignment score\n",1);
1942  };
1943  };
1944  flag=alp_obj_tmp->d_success;
1945  };
1946 
1947  };
1948  flag=alp_obj_tmp->d_success;
1949  };
1950  };
1951 
1952 
1954  ind1_,
1955  ind2_,
1956  lambda_,
1957  eps_K_,
1958  current_level,
1959  recommended_level,
1960  diff_opt_,
1961  K_C_,
1962  K_C_error_);
1963 
1964  current_level=recommended_level;
1965 
1966  }
1967  while(!flag);
1968 
1969  level_=current_level;
1970 
1971 };
1972 
1974 bool check_time_,
1975 Int4 M_min_,
1976 Int4 nalp_,
1977 bool killing_flag_,
1978 Int4 level_,
1979 Int4 diff_opt_,
1980 alp *&obj_,
1981 bool &sucess_flag_,
1982 double &d_eps_)
1983 {
1984  if(!obj_)
1985  {
1986  obj_=new alp(d_alp_data);
1987  alp_data::assert_mem(obj_);
1989  };
1991  obj_->d_check_time_flag=check_time_;
1992 
1994 
1995 
1996 
1997  alp*&obj=obj_;
1998 
1999  obj->d_diff_opt=diff_opt_;
2000 
2002 
2003  sucess_flag_=true;
2004 
2005  while(obj->d_nalp<nalp_)
2006  {
2007  obj->simulate_next_alp();
2008  if(!obj->d_success)
2009  {
2010  sucess_flag_=false;
2011  delete obj_;obj_=NULL;
2012  d_eps_=d_alp_data->d_eps_lambda;
2014  return;
2015  };
2016  };
2017 
2018 
2019  if(killing_flag_)
2020  {
2021  obj->kill_upto_level(M_min_,level_);
2022  if(!obj->d_success)
2023  {
2024  sucess_flag_=false;
2025  delete obj_;obj_=NULL;
2026  d_eps_=d_alp_data->d_eps_K;
2028  return;
2029  };
2030  };
2031 
2032 };
2033 
2034 
2036 Int4 ind1_,
2037 Int4 ind2_,
2038 Int4 &M_min_,
2039 Int4 &nalp_,
2040 Int4 &nalp_lambda_,
2041 bool C_calculation_,
2042 bool check_time_flag_)
2043 {
2044  bool ee_error_flag=false;
2045  error ee_error("",0);
2046 
2047  Int4 &alp_number=nalp_;
2048  alp_number=0;
2049 
2050  void **alp_distr=NULL;
2051  void **alp_distr_errors=NULL;
2052 
2053 
2054  try
2055  {
2056  try
2057  {
2058 
2059 
2060  if(d_n_alp_obj<ind1_||d_n_alp_obj-1>ind2_)
2061  {
2062  throw error("Unexpected error\n",4);
2063  };
2064 
2065  Int4 nalp_lambda_equilibration=-1;
2066 
2067  //create the objects
2068  Int4 i;
2069  for(i=d_n_alp_obj;i<=ind2_;i++)
2070  {
2072  d_n_alp_obj=i+1;
2073 
2074 
2075  alp *&alp_obj_tmp=d_alp_obj->d_elem[i];
2076 
2077  alp_obj_tmp=new alp(d_alp_data);
2078  alp_data::assert_mem(alp_obj_tmp);
2079 
2081 
2082 
2083  alp_obj_tmp->d_check_time_flag=check_time_flag_;
2084  alp_obj_tmp->d_time_error_flag=check_time_flag_;
2085 
2086  };
2087 
2088 
2089  bool M_min_flag=false;
2090  bool nalp_flag=false;
2091 
2092 
2093  bool criterion_flag=false;
2094  Int4 number_of_fails=0;
2095  Int4 number_of_fails_threshold=5;
2096 
2097  do{
2098 
2099  Int4 number_of_unsuccessful_objects=0;
2100  Int4 i;
2101  for(i=ind1_;i<=ind2_;i++)
2102  {
2103  alp* &alp_obj_tmp=d_alp_obj->d_elem[i];
2104 
2105  alp_obj_tmp->d_check_time_flag=check_time_flag_;
2106  alp_obj_tmp->d_time_error_flag=check_time_flag_;
2107 
2108 
2109  if(alp_obj_tmp->d_nalp<alp_number+1)
2110  {
2111 
2112  alp_obj_tmp->simulate_alp_upto_the_given_number(alp_number+1);
2113 
2114  if(!alp_obj_tmp->d_success)
2115  {
2116  number_of_unsuccessful_objects++;
2117  delete alp_obj_tmp;
2118  alp_obj_tmp=NULL;
2119 
2120  if(number_of_unsuccessful_objects>5+(ind2_-d_n_alp_obj+1)*
2121  d_alp_obj->d_alp_data->d_eps_lambda*(alp_number+1)
2122  )
2123  {
2124 
2125  throw error("The parameters cannot be correctly calculated for the given accuracy, calculation time and memory usage\n",1);
2126  };
2127 
2128 
2129  bool success2=false;
2130  while(!success2)
2131  {
2132  alp_obj_tmp=new alp(d_alp_data);
2133  alp_data::assert_mem(alp_obj_tmp);
2134 
2135 
2136  Int4 j;
2137  for(j=0;j<=alp_number;j++)
2138  {
2139  alp_obj_tmp->simulate_alp_upto_the_given_number(j+1);
2140  };
2141 
2142  success2=alp_obj_tmp->d_success;
2143 
2144 
2145 
2146  if(!success2)
2147  {
2148  delete alp_obj_tmp;
2149  alp_obj_tmp=NULL;
2150  number_of_unsuccessful_objects++;
2151  if(number_of_unsuccessful_objects>5+(ind2_-d_n_alp_obj+1)*
2152  d_alp_obj->d_alp_data->d_eps_lambda*(alp_number+1)
2153  )
2154  {
2155 
2156  throw error("The parameters cannot be correctly calculated for the given accuracy, calculation time and memory usage\n",1);
2157  };
2158  };
2159 
2160  };
2161  };
2162 
2163  };
2164 
2165  };
2166 
2167  alp_number++;
2168 
2169 
2170 
2171  bool inside_simulation_flag=false;
2172 
2173  criterion_flag=the_criterion(
2174  alp_number,
2175  nalp_lambda_,
2176  0,
2177  ind2_,
2178  alp_distr,
2179  alp_distr_errors,
2180  M_min_,
2181  M_min_flag,
2182  nalp_flag,
2183  inside_simulation_flag,
2184  C_calculation_);
2185 
2186 
2187  if(nalp_lambda_equilibration==-1&&nalp_flag)
2188  {
2189  nalp_lambda_equilibration=alp_number;
2190  };
2191 
2192 
2193 
2194  if(!inside_simulation_flag)
2195  {
2196  number_of_fails++;
2197 
2198  Int4 i;
2199  if(alp_distr)
2200  {
2201  for(i=1;i<=alp_number;i++)
2202  {
2203  delete (array_positive<double>*)alp_distr[i];alp_distr[i]=NULL;
2204  };
2205 
2206  delete[]alp_distr;alp_distr=NULL;
2207  };
2208 
2209  if(alp_distr_errors)
2210  {
2211  for(i=1;i<=alp_number;i++)
2212  {
2213  delete (array_positive<double>*)alp_distr_errors[i];alp_distr_errors[i]=NULL;
2214  };
2215 
2216  delete[]alp_distr_errors;alp_distr_errors=NULL;
2217  };
2218 
2219 
2220  M_min_flag=false;
2221  nalp_flag=false;
2222 
2223 
2224  alp_distr=NULL;
2225  alp_distr_errors=NULL;
2226 
2227  alp_number=0;
2228 
2229  criterion_flag=false;
2230 
2231 
2232 
2233  for(i=ind1_;i<=ind2_;i++)
2234  {
2235  alp* &alp_obj_tmp=d_alp_obj->d_elem[i];
2236  delete alp_obj_tmp;alp_obj_tmp=NULL;
2237 
2238  };
2239 
2240  if(number_of_fails>number_of_fails_threshold)
2241  {
2242  throw error("The program is not able to calculate the parameters accurately.\nPlease try to increase the allowed calculation time and memory limit\n",1);
2243  };
2244 
2245  for(i=ind1_;i<=ind2_;i++)
2246  {
2247  alp *alp_obj_tmp;
2248  alp_obj_tmp=new alp(d_alp_data);
2249  alp_data::assert_mem(alp_obj_tmp);
2250  d_alp_obj->set_elem(i,alp_obj_tmp);
2251 
2252 
2253  alp_obj_tmp->d_check_time_flag=check_time_flag_;
2254  alp_obj_tmp->d_time_error_flag=check_time_flag_;
2255 
2256  };
2257 
2258  };
2259 
2260  }
2261  while(!criterion_flag);
2262 
2263 
2264  nalp_lambda_=alp_data::Tmax(nalp_lambda_equilibration,nalp_lambda_);
2265  nalp_lambda_=alp_data::Tmin(nalp_lambda_,nalp_);
2266 
2267  nalp_lambda_=nalp_;
2268 
2269  }
2270  catch (error er)
2271  {
2272  ee_error_flag=true;
2273  ee_error=er;
2274  };
2275  }
2276  catch (...)
2277  {
2278  ee_error_flag=true;
2279  ee_error=error("Internal error in the program\n",4);
2280  };
2281 
2282  //memory release
2283 
2284  if(alp_distr)
2285  {
2286  Int4 i;
2287  for(i=1;i<=nalp_;i++)
2288  {
2289  delete (array_positive<double>*)alp_distr[i];alp_distr[i]=NULL;
2290  };
2291 
2292  delete[]alp_distr;alp_distr=NULL;
2293  };
2294 
2295  if(alp_distr_errors)
2296  {
2297  Int4 i;
2298  for(i=1;i<=nalp_;i++)
2299  {
2300  delete (array_positive<double>*)alp_distr_errors[i];alp_distr_errors[i]=NULL;
2301  };
2302 
2303  delete[]alp_distr_errors;alp_distr_errors=NULL;
2304  };
2305 
2306  if(ee_error_flag)
2307  {
2308  throw error(ee_error.st,ee_error.error_code);
2309  };
2310 
2311 };
2312 
2313 bool alp_sim::the_criterion(//criteria of stopping of the simulating ALP
2314 //if the function returns true then calculates optimal M_min and ALP number
2315 //sets the flags M_min_flag_ and nalp_flag_ checking the corresponding condition
2316 Int4 upto_nalp_,
2317 Int4 &nalp_for_lambda_simulation_,
2318 Int4 ind1_,
2319 Int4 ind2_,
2320 void **&alp_distr,
2321 void **&alp_distr_errors,
2322 Int4 &M_min_,
2323 bool &M_min_flag_,
2324 bool &nalp_flag_,
2325 bool &inside_simulation_flag_,
2326 bool C_calculation_)
2327 {
2328 
2329  nalp_flag_=false;
2330  M_min_flag_=false;
2331 
2332  if(ind1_>ind2_)
2333  {
2334  throw error("Unexpected error\n",4);
2335  };
2336 
2337 
2338 
2339  double lambda;
2340  double lambda_error;
2341 
2342  double test_difference;
2343  double test_difference_error;
2344 
2345  Int4 nalp=upto_nalp_;
2346 
2347  if(nalp<1)
2348  {
2349  throw error("Unexpected error\n",4);
2350  };
2351 
2352 
2354  ind1_,
2355  ind2_,
2356  alp_distr,
2357  alp_distr_errors,
2358  nalp);
2359 
2360 
2362  true,
2363  upto_nalp_,
2364  nalp_for_lambda_simulation_,
2365  inside_simulation_flag_,
2366  ind1_,
2367  ind2_,
2368  alp_distr,
2369  alp_distr_errors,
2370  lambda,
2371  lambda_error,
2372  test_difference,
2373  test_difference_error);
2374 
2375  if(!inside_simulation_flag_)
2376  {
2377  return false;
2378  };
2379 
2380 
2381  d_lambda_tmp->set_elem(upto_nalp_,lambda);
2382  d_lambda_tmp_errors->set_elem(upto_nalp_,lambda_error);
2383 
2384 
2385  if(C_calculation_)
2386  {
2387  double C;
2388  double C_error;
2389 
2390 
2391  calculate_C(
2392  0,
2393  upto_nalp_,
2394  ind1_,
2395  ind2_,
2396  alp_distr,
2397  alp_distr_errors,
2398  lambda,
2399  lambda_error,
2400  C,
2401  C_error);
2402 
2403 
2404  d_C_tmp->set_elem(upto_nalp_,C);
2405  d_C_tmp_errors->set_elem(upto_nalp_,C_error);
2406 
2407  };
2408 
2409 
2410 
2411 
2412  if(nalp>=1)
2413  {
2414 
2415  if(test_difference<=test_difference_error)
2416  {
2417  nalp_flag_=true;
2418  M_min_flag_=check_K_criterion(
2419  nalp,
2420  ind1_,
2421  ind2_,
2422  lambda,
2424  M_min_);
2425 
2426  return M_min_flag_;
2427  };
2428  };
2429 
2430 
2431  return false;
2432 };
2433 
2435 Int4 &i_,
2436 double *&exp_array_)
2437 {
2438  if(exp_array_[i_]==-1)
2439  {
2440  throw error("The program is not able to calculate the parameters; rescaling penalties and scoring matrix might help\n",3);
2441  };
2442 
2443  return exp_array_[i_];
2444 };
2445 
2447 Int4 nalp_,
2448 Int4 ind1_,
2449 Int4 ind2_,
2450 void **alp_distr,
2451 void **alp_distr_errors,
2452 double lambda_,
2453 double lambda_error_,
2454 double &a_I_,
2455 double &a_I_error_,
2456 double &a_J_,
2457 double &a_J_error_,
2458 double &sigma_,
2459 double &sigma_error_,
2460 double &alpha_I_,
2461 double &alpha_I_error_,
2462 double &alpha_J_,
2463 double &alpha_J_error_)
2464 {
2465  bool ee_error_flag=false;
2466  error ee_error("",0);
2467 
2468  double *exp_array=NULL;
2469 
2470  double *delta_E=NULL;
2471  double *delta_E_error=NULL;
2472 
2473  double *delta_E_E=NULL;
2474  double *delta_E_E_error=NULL;
2475 
2476 
2477  double *delta_I=NULL;
2478  double *delta_I_error=NULL;
2479 
2480  double *delta_J=NULL;
2481  double *delta_J_error=NULL;
2482 
2483  double *delta_I_I=NULL;
2484  double *delta_I_I_error=NULL;
2485 
2486  double *delta_I_J=NULL;
2487  double *delta_I_J_error=NULL;
2488 
2489  double *delta_J_J=NULL;
2490  double *delta_J_J_error=NULL;
2491 
2492  double *cov_J_J=NULL;
2493  double *cov_J_J_error=NULL;
2494 
2495  double *cov_I_J=NULL;
2496  double *cov_I_J_error=NULL;
2497 
2498  double *cov_I_I=NULL;
2499  double *cov_I_I_error=NULL;
2500 
2501  double *cov_E_E=NULL;
2502  double *cov_E_E_error=NULL;
2503 
2504  const double dbl_max_log = log(DBL_MAX);
2505 
2506  try
2507  {
2508  try
2509  {
2510 
2511 
2512  if(nalp_<1)
2513  {
2514  throw error("Unexpected error\n",4);
2515  };
2516 
2517  array_positive<double>* tmp=((array_positive<double>*)alp_distr[nalp_]);
2518  Int4 dim=tmp->d_dim;
2519 
2520  exp_array=new double[dim+1];
2521  alp_data::assert_mem(exp_array);
2522 
2523 
2524  Int4 i;
2525  for(i=0;i<=dim;i++)
2526  {
2527  double tmp=(double)i*lambda_;
2528  if(tmp<dbl_max_log)
2529  {
2530  exp_array[i]=exp(tmp);
2531  }
2532  else
2533  {
2534  exp_array[i]=-1;
2535  };
2536  };
2537 
2538 
2539 
2540 
2541  delta_E=new double[nalp_];
2542  alp_data::assert_mem(delta_E);
2543  delta_E_error=new double[nalp_];
2544  alp_data::assert_mem(delta_E_error);
2545 
2546  delta_E_E=new double[nalp_];
2547  alp_data::assert_mem(delta_E_E);
2548  delta_E_E_error=new double[nalp_];
2549  alp_data::assert_mem(delta_E_E_error);
2550 
2551  cov_E_E=new double[nalp_];
2552  alp_data::assert_mem(cov_E_E);
2553  cov_E_E_error=new double[nalp_];
2554  alp_data::assert_mem(cov_E_E_error);
2555 
2556 
2557  delta_I=new double[nalp_];
2558  alp_data::assert_mem(delta_I);
2559  delta_I_error=new double[nalp_];
2560  alp_data::assert_mem(delta_I_error);
2561 
2562  delta_J=new double[nalp_];
2563  alp_data::assert_mem(delta_J);
2564  delta_J_error=new double[nalp_];
2565  alp_data::assert_mem(delta_J_error);
2566 
2567  delta_I_I=new double[nalp_];
2568  alp_data::assert_mem(delta_I_I);
2569  delta_I_I_error=new double[nalp_];
2570  alp_data::assert_mem(delta_I_I_error);
2571 
2572  delta_I_J=new double[nalp_];
2573  alp_data::assert_mem(delta_I_J);
2574  delta_I_J_error=new double[nalp_];
2575  alp_data::assert_mem(delta_I_J_error);
2576 
2577  delta_J_J=new double[nalp_];
2578  alp_data::assert_mem(delta_J_J);
2579  delta_J_J_error=new double[nalp_];
2580  alp_data::assert_mem(delta_J_J_error);
2581 
2582  cov_J_J=new double[nalp_];
2583  alp_data::assert_mem(cov_J_J);
2584  cov_J_J_error=new double[nalp_];
2585  alp_data::assert_mem(cov_J_J_error);
2586 
2587  cov_I_J=new double[nalp_];
2588  alp_data::assert_mem(cov_I_J);
2589  cov_I_J_error=new double[nalp_];
2590  alp_data::assert_mem(cov_I_J_error);
2591 
2592  cov_I_I=new double[nalp_];
2593  alp_data::assert_mem(cov_I_I);
2594  cov_I_I_error=new double[nalp_];
2595  alp_data::assert_mem(cov_I_I_error);
2596 
2597  Int4 j;
2598  for(j=0;j<nalp_;j++)
2599  {
2600  delta_E[j]=0.0;
2601  delta_E_error[j]=0.0;
2602 
2603  delta_E_E[j]=0.0;
2604  delta_E_E_error[j]=0.0;
2605 
2606  delta_I[j]=0.0;
2607  delta_I_error[j]=0.0;
2608  delta_J[j]=0.0;
2609  delta_J_error[j]=0.0;
2610 
2611  delta_I_I[j]=0.0;
2612 
2613  delta_I_I_error[j]=0.0;
2614  delta_I_J[j]=0.0;
2615  delta_I_J_error[j]=0.0;
2616  delta_J_J[j]=0.0;
2617  delta_J_J_error[j]=0.0;
2618  };
2619 
2620 
2621  for(i=ind1_;i<=ind2_;i++)
2622  {
2623  alp* alp_obj_tmp=d_alp_obj->d_elem[i];
2624 
2625  Int4 j;
2626  for(j=1;j<=nalp_;j++)
2627  {
2628  Int4 j_1=j-1;
2629 
2630  Int4 &E_j_1=alp_obj_tmp->d_alp->d_elem[j_1];
2631  Int4 &E_j=alp_obj_tmp->d_alp->d_elem[j];
2632  double &weight_j=alp_obj_tmp->d_alp_weights->d_elem[j];
2633 
2634  Int4 &I_j_1=alp_obj_tmp->d_H_I->d_elem[j_1];
2635  Int4 &I_j=alp_obj_tmp->d_H_I->d_elem[j];
2636 
2637  Int4 &J_j_1=alp_obj_tmp->d_H_J->d_elem[j_1];
2638  Int4 &J_j=alp_obj_tmp->d_H_J->d_elem[j];
2639 
2640  double delta_I_tmp=(I_j-I_j_1)*lambda_exp(E_j,exp_array)*weight_j;
2641  double delta_J_tmp=(J_j-J_j_1)*lambda_exp(E_j,exp_array)*weight_j;
2642  double delta_E_tmp=(E_j-E_j_1)*lambda_exp(E_j,exp_array)*weight_j;
2643  double delta_E_E_tmp=(E_j-E_j_1)*(E_j-E_j_1)*lambda_exp(E_j,exp_array)*weight_j;
2644 
2645 
2646  double delta_I_I_tmp=delta_I_tmp*(I_j-I_j_1);
2647  double delta_J_J_tmp=delta_J_tmp*(J_j-J_j_1);
2648  double delta_I_J_tmp=delta_I_tmp*(J_j-J_j_1);
2649 
2650 
2651 
2652 
2653  delta_E[j_1]+=delta_E_tmp;
2654  delta_E_error[j_1]+=delta_E_tmp*delta_E_tmp;
2655 
2656  delta_E_E[j_1]+=delta_E_E_tmp;
2657  delta_E_E_error[j_1]+=delta_E_E_tmp*delta_E_E_tmp;
2658 
2659  delta_I[j_1]+=delta_I_tmp;
2660  delta_I_error[j_1]+=delta_I_tmp*delta_I_tmp;
2661  delta_J[j_1]+=delta_J_tmp;
2662  delta_J_error[j_1]+=delta_J_tmp*delta_J_tmp;
2663 
2664  delta_I_I[j_1]+=delta_I_I_tmp;
2665  delta_I_I_error[j_1]+=delta_I_I_tmp*delta_I_I_tmp;
2666 
2667  delta_I_J[j_1]+=delta_I_J_tmp;
2668  delta_I_J_error[j_1]+=delta_I_J_tmp*delta_I_J_tmp;
2669 
2670  delta_J_J[j_1]+=delta_J_J_tmp;
2671  delta_J_J_error[j_1]+=delta_J_J_tmp*delta_J_J_tmp;
2672 
2673  };
2674  };
2675 
2676 
2677  double ind_diff=(double)(ind2_-ind1_+1);
2678  for(j=0;j<nalp_;j++)
2679  {
2680  delta_E[j]/=ind_diff;
2681  delta_E_error[j]/=ind_diff;
2682  delta_E_error[j]-=delta_E[j]*delta_E[j];
2683  delta_E_error[j]/=ind_diff;
2684  delta_E_error[j]=alp_reg::sqrt_for_errors(delta_E_error[j]);
2685 
2686  delta_E_E[j]/=ind_diff;
2687  delta_E_E_error[j]/=ind_diff;
2688  delta_E_E_error[j]-=delta_E_E[j]*delta_E_E[j];
2689  delta_E_E_error[j]/=ind_diff;
2690 
2691 
2692  delta_I[j]/=ind_diff;
2693  delta_I_error[j]/=ind_diff;
2694  delta_I_error[j]-=delta_I[j]*delta_I[j];
2695  delta_I_error[j]/=ind_diff;
2696  delta_I_error[j]=alp_reg::sqrt_for_errors(delta_I_error[j]);
2697 
2698  delta_J[j]/=ind_diff;
2699  delta_J_error[j]/=ind_diff;
2700  delta_J_error[j]-=delta_J[j]*delta_J[j];
2701  delta_J_error[j]/=ind_diff;
2702  delta_J_error[j]=alp_reg::sqrt_for_errors(delta_J_error[j]);
2703 
2704  delta_I_J[j]/=ind_diff;
2705  delta_I_J_error[j]/=ind_diff;
2706  delta_I_J_error[j]-=delta_I_J[j]*delta_I_J[j];
2707  delta_I_J_error[j]/=ind_diff;
2708 
2709 
2710  delta_I_I[j]/=ind_diff;
2711  delta_I_I_error[j]/=ind_diff;
2712  delta_I_I_error[j]-=delta_I_I[j]*delta_I_I[j];
2713  delta_I_I_error[j]/=ind_diff;
2714 
2715 
2716  delta_J_J[j]/=ind_diff;
2717  delta_J_J_error[j]/=ind_diff;
2718  delta_J_J_error[j]-=delta_J_J[j]*delta_J_J[j];
2719  delta_J_J_error[j]/=ind_diff;
2720 
2721 
2722  cov_I_J[j]=delta_I_J[j]-delta_I[j]*delta_J[j];
2723  cov_I_I[j]=delta_I_I[j]-delta_I[j]*delta_I[j];
2724  cov_J_J[j]=delta_J_J[j]-delta_J[j]*delta_J[j];
2725 
2726  cov_E_E[j]=delta_E_E[j]-delta_E[j]*delta_E[j];
2727 
2728  cov_I_J_error[j]=alp_reg::error_of_the_product(delta_I[j],delta_I_error[j],delta_J[j],delta_J_error[j]);
2729  cov_I_J_error[j]=sqrt(delta_I_J_error[j]+cov_I_J_error[j]*cov_I_J_error[j]);
2730 
2731  cov_I_I_error[j]=alp_reg::error_of_the_product(delta_I[j],delta_I_error[j],delta_I[j],delta_I_error[j]);
2732  cov_I_I_error[j]=sqrt(delta_I_I_error[j]+cov_I_I_error[j]*cov_I_I_error[j]);
2733 
2734  cov_J_J_error[j]=alp_reg::error_of_the_product(delta_J[j],delta_J_error[j],delta_J[j],delta_J_error[j]);
2735  cov_J_J_error[j]=sqrt(delta_J_J_error[j]+cov_J_J_error[j]*cov_J_J_error[j]);
2736 
2737  cov_E_E_error[j]=alp_reg::error_of_the_product(delta_E[j],delta_E_error[j],delta_E[j],delta_E_error[j]);
2738  cov_E_E_error[j]=sqrt(delta_E_E_error[j]+cov_E_E_error[j]*cov_E_E_error[j]);
2739 
2740  };
2741 
2742 
2743  //regression
2744 
2745  double beta1=0;
2746  double beta1_error=0;
2747 
2748  Int4 number_of_elements=nalp_;
2749 
2750  bool cut_left_tail=true;
2751  bool cut_right_tail=false;
2752 
2753  double y=2;
2754 
2755  Int4 k1_opt;
2756  Int4 k2_opt;
2757 
2758 
2759  double delta_I_aver;
2760  double delta_I_aver_error;
2761 
2762 
2763 
2764  bool res_was_calculated;
2765 
2767  0,
2768  number_of_elements,
2769  delta_I,
2770  delta_I_error,
2771  cut_left_tail,
2772  cut_right_tail,
2773  y,
2774  delta_I_aver,
2775  beta1,
2776  delta_I_aver_error,
2777  beta1_error,
2778  k1_opt,
2779  k2_opt,
2780  res_was_calculated);
2781 
2782  if(!res_was_calculated)
2783  {
2784  throw error("The program cannot estimate the parameters; please repeat the calculation2\n",2);
2785  };
2786 
2787 
2788 
2789  double delta_J_aver;
2790  double delta_J_aver_error;
2791 
2792 
2793 
2795  0,
2796  number_of_elements,
2797  delta_J,
2798  delta_J_error,
2799  cut_left_tail,
2800  cut_right_tail,
2801  y,
2802  delta_J_aver,
2803  beta1,
2804  delta_J_aver_error,
2805  beta1_error,
2806  k1_opt,
2807  k2_opt,
2808  res_was_calculated);
2809 
2810  if(!res_was_calculated)
2811  {
2812  throw error("The program cannot estimate the parameters; please repeat the calculation3\n",2);
2813  };
2814 
2815 
2816  double delta_E_aver;
2817  double delta_E_aver_error;
2818 
2820  0,
2821  number_of_elements,
2822  delta_E,
2823  delta_E_error,
2824  cut_left_tail,
2825  cut_right_tail,
2826  y,
2827  delta_E_aver,
2828  beta1,
2829  delta_E_aver_error,
2830  beta1_error,
2831  k1_opt,
2832  k2_opt,
2833  res_was_calculated);
2834 
2835  if(!res_was_calculated)
2836  {
2837  throw error("The program cannot estimate the parameters; please repeat the calculation4\n",2);
2838  };
2839 
2840  double cov_I_I_aver;
2841  double cov_I_I_aver_error;
2842 
2843  double cov_I_J_aver;
2844  double cov_I_J_aver_error;
2845 
2846  double cov_J_J_aver;
2847  double cov_J_J_aver_error;
2848 
2849  double cov_E_E_aver;
2850  double cov_E_E_aver_error;
2851 
2852 
2854  0,
2855  number_of_elements,
2856  cov_I_J,
2857  cov_I_J_error,
2858  cut_left_tail,
2859  cut_right_tail,
2860  y,
2861  cov_I_J_aver,
2862  beta1,
2863  cov_I_J_aver_error,
2864  beta1_error,
2865  k1_opt,
2866  k2_opt,
2867  res_was_calculated);
2868 
2869  if(!res_was_calculated)
2870  {
2871  throw error("The program cannot estimate the parameters; please repeat the calculation5\n",2);
2872  };
2873 
2875  0,
2876  number_of_elements,
2877  cov_I_I,
2878  cov_I_I_error,
2879  cut_left_tail,
2880  cut_right_tail,
2881  y,
2882  cov_I_I_aver,
2883  beta1,
2884  cov_I_I_aver_error,
2885  beta1_error,
2886  k1_opt,
2887  k2_opt,
2888  res_was_calculated);
2889 
2890  if(!res_was_calculated)
2891  {
2892  throw error("The program cannot estimate the parameters; please repeat the calculation6\n",2);
2893  };
2894 
2896  0,
2897  number_of_elements,
2898  cov_J_J,
2899  cov_J_J_error,
2900  cut_left_tail,
2901  cut_right_tail,
2902  y,
2903  cov_J_J_aver,
2904  beta1,
2905  cov_J_J_aver_error,
2906  beta1_error,
2907  k1_opt,
2908  k2_opt,
2909  res_was_calculated);
2910 
2911  if(!res_was_calculated)
2912  {
2913  throw error("The program cannot estimate the parameters; please repeat the calculation7\n",2);
2914  };
2915 
2917  0,
2918  number_of_elements,
2919  cov_E_E,
2920  cov_E_E_error,
2921  cut_left_tail,
2922  cut_right_tail,
2923  y,
2924  cov_E_E_aver,
2925  beta1,
2926  cov_E_E_aver_error,
2927  beta1_error,
2928  k1_opt,
2929  k2_opt,
2930  res_was_calculated);
2931 
2932  if(!res_was_calculated)
2933  {
2934  throw error("The program cannot estimate the parameters; please repeat the calculation7_E\n",2);
2935  };
2936 
2937 
2938 
2939  if(delta_E_aver<=0)
2940  {
2941  throw error("FSC parameters estimations failed\n",2);
2942  };
2943 
2944 
2945 
2946  a_I_=delta_I_aver/delta_E_aver;
2947  a_I_error_=alp_reg::error_of_the_ratio(delta_I_aver,delta_I_aver_error,delta_E_aver,delta_E_aver_error);
2948  a_J_=delta_J_aver/delta_E_aver;
2949  a_J_error_=alp_reg::error_of_the_ratio(delta_J_aver,delta_J_aver_error,delta_E_aver,delta_E_aver_error);
2950 
2952  delta_I_aver,
2953  delta_I_aver_error,
2954  delta_J_aver,
2955  delta_J_aver_error,
2956  delta_E_aver,
2957  delta_E_aver_error,
2958  cov_E_E_aver,
2959  cov_E_E_aver_error,
2960  cov_I_J_aver,
2961  cov_I_J_aver_error,
2962  sigma_,
2963  sigma_error_);
2964 
2965 
2967  delta_I_aver,
2968  delta_I_aver_error,
2969  delta_I_aver,
2970  delta_I_aver_error,
2971  delta_E_aver,
2972  delta_E_aver_error,
2973  cov_E_E_aver,
2974  cov_E_E_aver_error,
2975  cov_I_I_aver,
2976  cov_I_I_aver_error,
2977  alpha_I_,
2978  alpha_I_error_);
2979 
2980 
2981 
2983  delta_J_aver,
2984  delta_J_aver_error,
2985  delta_J_aver,
2986  delta_J_aver_error,
2987  delta_E_aver,
2988  delta_E_aver_error,
2989  cov_E_E_aver,
2990  cov_E_E_aver_error,
2991  cov_J_J_aver,
2992  cov_J_J_aver_error,
2993  alpha_J_,
2994  alpha_J_error_);
2995 
2996 
2997  }
2998  catch (error er)
2999  {
3000  ee_error_flag=true;
3001  ee_error=er;
3002  };
3003  }
3004  catch (...)
3005  {
3006  ee_error_flag=true;
3007  ee_error=error("Internal error in the program\n",4);
3008  };
3009 
3010  //memory release
3011  delete[]exp_array;exp_array=NULL;
3012 
3013  delete[]delta_E;delta_E=NULL;
3014  delete[]delta_E_error;delta_E_error=NULL;
3015  delete[]delta_E_E;delta_E_E=NULL;
3016  delete[]delta_E_E_error;delta_E_E_error=NULL;
3017  delete[]delta_I;delta_I=NULL;
3018  delete[]delta_I_error;delta_I_error=NULL;
3019  delete[]delta_J;delta_J=NULL;
3020  delete[]delta_J_error;delta_J_error=NULL;
3021 
3022  delete[]delta_I_J;delta_I_J=NULL;
3023  delete[]delta_I_J_error;delta_I_J_error=NULL;
3024  delete[]delta_J_J;delta_J_J=NULL;
3025  delete[]delta_J_J_error;delta_J_J_error=NULL;
3026  delete[]delta_I_I;delta_I_I=NULL;
3027  delete[]delta_I_I_error;delta_I_I_error=NULL;
3028 
3029  delete[]cov_I_J;cov_I_J=NULL;
3030  delete[]cov_I_J_error;cov_I_J_error=NULL;
3031  delete[]cov_J_J;cov_J_J=NULL;
3032  delete[]cov_J_J_error;cov_J_J_error=NULL;
3033  delete[]cov_I_I;cov_I_I=NULL;
3034  delete[]cov_I_I_error;cov_I_I_error=NULL;
3035  delete[]cov_E_E;cov_E_E=NULL;
3036  delete[]cov_E_E_error;cov_E_E_error=NULL;
3037 
3038  if(ee_error_flag)
3039  {
3040  throw error(ee_error.st,ee_error.error_code);
3041  };
3042 
3043 };
3044 
3046 double delta_I_aver_,
3047 double delta_I_aver_error_,
3048 double delta_J_aver_,
3049 double delta_J_aver_error_,
3050 double delta_E_aver_,
3051 double delta_E_aver_error_,
3052 double cov_E_E_aver_,
3053 double cov_E_E_aver_error_,
3054 double cov_I_J_aver_,
3055 double cov_I_J_aver_error_,
3056 double &alpha_,
3057 double &alpha_error_)
3058 {
3059  double nom1_1=delta_I_aver_*delta_J_aver_;
3060  double nom2_2=delta_E_aver_*delta_E_aver_;
3061 
3062  double den=nom2_2*delta_E_aver_;
3063 
3064  double nom1=nom1_1*cov_E_E_aver_;
3065  double nom2=nom2_2*cov_I_J_aver_;
3066 
3067  alpha_=(nom1+nom2)/den;
3068 
3069 
3070  double nom1_alpha_error=alp_reg::error_of_the_product(delta_I_aver_,delta_I_aver_error_,delta_J_aver_,delta_J_aver_error_);
3071  nom1_alpha_error=alp_reg::error_of_the_product(nom1_1,nom1_alpha_error,cov_E_E_aver_,cov_E_E_aver_error_);
3072 
3073 
3074  double nom2_alpha_error_2=alp_reg::error_of_the_product(delta_E_aver_,delta_E_aver_error_,delta_E_aver_,delta_E_aver_error_);
3075  double nom2_alpha_error=alp_reg::error_of_the_product(nom2_2,nom2_alpha_error_2,cov_I_J_aver_,cov_I_J_aver_error_);
3076 
3077 
3078  double den_alpha_error=alp_reg::error_of_the_product(nom2_2,nom2_alpha_error_2,delta_E_aver_,delta_E_aver_error_);
3079 
3080  double nom_alpha_error=alp_reg::error_of_the_sum(nom1,nom1_alpha_error,nom2,nom2_alpha_error);
3081 
3082  alpha_error_=alp_reg::error_of_the_ratio(nom1+nom2,nom_alpha_error,den,den_alpha_error);
3083 
3084 };
3085 
3086 
3087 
3089 Int4 starting_point,
3090 Int4 nalp_,
3091 Int4 ind1_,
3092 Int4 ind2_,
3093 void **alp_distr,
3094 void **alp_distr_errors,
3095 double lambda_,
3096 double lambda_error_,
3097 double &C_,
3098 double &C_error_)
3099 {
3100  bool ee_error_flag=false;
3101  error ee_error("",0);
3102 
3103  double *P=NULL;
3104  double *P_errors=NULL;
3105  double *values_P_ratio=NULL;
3106  double *errors_P_ratio=NULL;
3107 
3108  double *E=NULL;
3109  double *E_errors=NULL;
3110 
3111  double *E_T_beta=NULL;
3112  double *E_T_beta_errors=NULL;
3113 
3114 
3115  try
3116  {
3117  try
3118  {
3119 
3120  Int4 total_number_of_ALP=nalp_;
3121 
3122  if(total_number_of_ALP<1)
3123  {
3124  throw error("Unexpected error\n",4);
3125  };
3126 
3127 
3128  //1)P(beta=infinity)
3129  Int4 j;
3130 
3131 
3132  P=new double[total_number_of_ALP+1];
3134  P_errors=new double[total_number_of_ALP+1];
3135  alp_data::assert_mem(P_errors);
3136 
3137  P[0]=1.0;
3138  P_errors[0]=0.0;
3139 
3140 
3141  for(j=1;j<=total_number_of_ALP;j++)
3142  {
3144  array_positive<double>* tmp_errors=((array_positive<double>*)alp_distr_errors[j]);
3145 
3146  P[j]=0;
3147  P_errors[j]=0;
3148  Int4 i;
3149  for(i=0;i<=tmp->d_dim;i++)
3150  {
3151  P[j]+=tmp->d_elem[i];
3152  P_errors[j]+=tmp_errors->d_elem[i];
3153  };
3154 
3155  P_errors[j]=alp_reg::sqrt_for_errors(P_errors[j]);
3156  };
3157 
3158 
3159 
3160  values_P_ratio=new double[total_number_of_ALP];
3161  alp_data::assert_mem(values_P_ratio);
3162  errors_P_ratio=new double[total_number_of_ALP];
3163  alp_data::assert_mem(errors_P_ratio);
3164 
3165 
3166 
3167  for(j=0;j<total_number_of_ALP;j++)
3168  {
3169  values_P_ratio[j]=P[j+1]/P[j];
3170  errors_P_ratio[j]=alp_reg::error_of_the_ratio(P[j+1],P_errors[j+1],P[j],P_errors[j]);
3171  };
3172 
3173 
3174 
3175  double beta1=0;
3176  double beta1_error=0;
3177 
3178  Int4 number_of_elements=total_number_of_ALP;
3179 
3180  bool cut_left_tail=true;
3181  bool cut_right_tail=false;
3182 
3183  double y=2;
3184 
3185  Int4 k1_opt;
3186  Int4 k2_opt;
3187 
3188 
3189  double P_beta_inf;
3190  double P_beta_inf_error=0;
3191 
3192  bool res_was_calculated;
3193 
3195  0,
3196  number_of_elements-starting_point,
3197  values_P_ratio+starting_point,
3198  errors_P_ratio+starting_point,
3199  cut_left_tail,
3200  cut_right_tail,
3201  y,
3202  P_beta_inf,
3203  beta1,
3204  P_beta_inf_error,
3205  beta1_error,
3206  k1_opt,
3207  k2_opt,
3208  res_was_calculated);
3209 
3210 
3211 
3212 
3213  if(!res_was_calculated)
3214  {
3215  throw error("The program cannot estimate the parameters; please repeat the calculation\n",2);
3216  };
3217 
3218  P_beta_inf=1-P_beta_inf;
3219 
3220 
3221  //2)E(exp(lambda*T_beta)) and E(T_beta*exp(lambda*T_beta))
3222  E=new double[total_number_of_ALP+1];
3224  E_errors=new double[total_number_of_ALP+1];
3225  alp_data::assert_mem(E_errors);
3226 
3227  E_T_beta=new double[total_number_of_ALP+1];
3228  alp_data::assert_mem(E_T_beta);
3229  E_T_beta_errors=new double[total_number_of_ALP+1];
3230  alp_data::assert_mem(E_T_beta);
3231 
3232 
3233  E[0]=1;
3234  E_T_beta[0]=0;
3235 
3236  E_errors[0]=0;
3237  E_T_beta_errors[0]=0;
3238 
3239 
3240 
3241 
3242  for(j=1;j<=total_number_of_ALP;j++)
3243  {
3245  array_positive<double>* tmp_errors=((array_positive<double>*)alp_distr_errors[j]);
3246 
3247  E[j]=0;
3248  E_T_beta[j]=0;
3249 
3250  E_errors[j]=0;
3251  E_T_beta_errors[j]=0;
3252 
3253  Int4 i;
3254  for(i=0;i<=tmp->d_dim;i++)
3255  {
3256  double tmp_double=exp(lambda_*(double)i);
3257  E[j]+=tmp_double*tmp->d_elem[i];
3258  E_errors[j]+=tmp_double*tmp_double*tmp_errors->d_elem[i];
3259 
3260  tmp_double=(double)i*exp(lambda_*(double)i);
3261  E_T_beta[j]+=tmp_double*tmp->d_elem[i];
3262  E_T_beta_errors[j]+=tmp_double*tmp_double*tmp_errors->d_elem[i];
3263  };
3264 
3265  E_errors[j]=alp_reg::sqrt_for_errors(E_errors[j]);
3266  E_T_beta_errors[j]=alp_reg::sqrt_for_errors(E_T_beta_errors[j]);
3267 
3268  };
3269 
3270 
3271  double E_aver;
3272  double E_aver_error;
3273 
3274  double E_T_beta_diff_aver;
3275  double E_T_beta_diff_aver_error;
3276 
3277 
3278  if(total_number_of_ALP==1)
3279  {
3280  E_aver=E[1];
3281  E_aver_error=E_errors[1];
3282 
3283  E_T_beta_diff_aver=E_T_beta[1]-E_T_beta[0];
3284  E_T_beta_diff_aver_error=E_T_beta_errors[1];
3285 
3286  }
3287  else
3288  {
3289  Int4 number_of_elements=total_number_of_ALP;
3290 
3291  bool cut_left_tail=true;
3292  bool cut_right_tail=false;
3293 
3294 
3295  double beta0;
3296  double beta1=0;
3297  double beta0_error;
3298  double beta1_error=0;
3299 
3300  bool res_was_calculated;
3301 
3303  0,
3304  number_of_elements-starting_point,
3305  E+1+starting_point,
3306  E_errors+1+starting_point,
3307  cut_left_tail,
3308  cut_right_tail,
3309  y,
3310  E_aver,
3311  beta1,
3312  E_aver_error,
3313  beta1_error,
3314  k1_opt,
3315  k2_opt,
3316  res_was_calculated);
3317 
3318 
3319  if(!res_was_calculated)
3320  {
3321  throw error("The program cannot estimate the parameters; please repeat the calculation\n",2);
3322  };
3323 
3324 
3325  number_of_elements=total_number_of_ALP;
3326 
3327 
3329  0,
3330  number_of_elements-starting_point,
3331  E_T_beta+1+starting_point,
3332  E_T_beta_errors+1+starting_point,
3333  cut_left_tail,
3334  cut_right_tail,
3335  y,
3336  beta0,
3337  beta1,
3338  beta0_error,
3339  beta1_error,
3340  k1_opt,
3341  k2_opt,
3342  res_was_calculated);
3343 
3344 
3345 
3346  if(!res_was_calculated)
3347  {
3348  throw error("The program cannot estimate the parameters; please repeat the calculation\n",2);
3349  };
3350 
3351 
3352  E_T_beta_diff_aver=beta1;
3353  E_T_beta_diff_aver_error=beta1_error;
3354 
3355 
3356 
3357  };
3358 
3359  double exp_lambda_error=exp(-lambda_)*lambda_error_;
3360  double exp_lambda=(1-exp(-lambda_));
3361 
3362 
3363  double den_error=alp_reg::error_of_the_product(E_T_beta_diff_aver,E_T_beta_diff_aver_error,exp_lambda,exp_lambda_error);
3364  double den=(1-exp(-lambda_))*E_T_beta_diff_aver;
3365 
3366  double E_aver_sqr_error=alp_reg::error_of_the_product(E_aver,E_aver_error,E_aver,E_aver_error);
3367  double E_aver_sqr=E_aver*E_aver;
3368 
3369  double nom_error=alp_reg::error_of_the_product(P_beta_inf,P_beta_inf_error,E_aver_sqr,E_aver_sqr_error);
3370  double nom=P_beta_inf*E_aver_sqr;
3371 
3372  C_error_=alp_reg::error_of_the_ratio(nom,nom_error,den,den_error);
3373  C_=nom/den;
3374 
3375 
3376 
3377 
3378  }
3379  catch (error er)
3380  {
3381  ee_error_flag=true;
3382  ee_error=er;
3383  };
3384  }
3385  catch (...)
3386  {
3387  ee_error_flag=true;
3388  ee_error=error("Internal error in the program\n",4);
3389  };
3390 
3391  //memory release
3392 
3393  delete[]values_P_ratio;values_P_ratio=NULL;
3394  delete[]errors_P_ratio;errors_P_ratio=NULL;
3395 
3396 
3397  delete[]P;P=NULL;
3398  delete[]P_errors;P_errors=NULL;
3399 
3400  delete[]E;E=NULL;
3401  delete[]E_T_beta;E_T_beta=NULL;
3402  delete[]E_errors;E_errors=NULL;
3403  delete[]E_T_beta_errors;E_T_beta_errors=NULL;
3404 
3405  if(ee_error_flag)
3406  {
3407  throw error(ee_error.st,ee_error.error_code);
3408  };
3409 
3410 };
3411 
3413 Int4 ind1_,
3414 Int4 ind2_,
3415 void **&alp_distr,
3416 void **&alp_distr_errors,
3417 Int4 nalp)
3418 {
3419  if(nalp<=0)
3420  {
3421  if(nalp<0)
3422  {
3423  throw error("Unexpected error\n",4);
3424  };
3425 
3426  alp_distr=NULL;
3427  alp_distr_errors=NULL;
3428 
3429  return;
3430  };
3431 
3432  void **alp_distr_tmp=NULL;
3433  void **alp_distr_errors_tmp=NULL;
3434 
3435  Int4 allocation_dim=nalp;
3436  Int4 allocation_dim_tmp=nalp+1;
3437 
3438  bool ee_error_flag=false;
3439  error ee_error("",0);
3440 
3441  try
3442  {
3443  try
3444  {
3445 
3446 
3447  Int4 i;
3448  alp_distr_tmp=new void*[nalp+1];
3449  alp_data::assert_mem(alp_distr_tmp);
3450 
3451  alp_distr_errors_tmp=new void*[nalp+1];
3452  alp_data::assert_mem(alp_distr_errors_tmp);
3453 
3454  for(i=0;i<=nalp;i++)
3455  {
3456  alp_distr_tmp[i]=NULL;
3457  alp_distr_errors_tmp[i]=NULL;
3458  };
3459 
3460 
3461  for(i=1;i<=nalp-1;i++)
3462  {
3463  alp_distr_tmp[i]=alp_distr[i];
3464  alp_distr_errors_tmp[i]=alp_distr_errors[i];
3465  };
3466 
3467  delete[]alp_distr;alp_distr=NULL;
3468  delete[]alp_distr_errors;alp_distr_errors=NULL;
3469 
3470  alp_distr=alp_distr_tmp;alp_distr_tmp=NULL;
3471  alp_distr_errors=alp_distr_errors_tmp;alp_distr_errors_tmp=NULL;
3472 
3473  allocation_dim=nalp+1;
3474 
3475 
3476 
3477  alp_distr[nalp]=new array_positive<double>(d_alp_data);
3478  alp_data::assert_mem(alp_distr[nalp]);
3479 
3480  alp_distr_errors[nalp]=new array_positive<double>(d_alp_data);
3481  alp_data::assert_mem(alp_distr_errors[nalp]);
3482 
3483 
3484 
3485  for(i=ind1_;i<=ind2_;i++)
3486  {
3487  alp* &alp_obj_tmp=d_alp_obj->d_elem[i];
3488  Int4 k=nalp;
3489  Int4 &alp_tmp=alp_obj_tmp->d_alp->d_elem[k];
3490  double &weight_tmp=alp_obj_tmp->d_alp_weights->d_elem[k];
3491 
3492 
3493  ((array_positive<double>*)alp_distr[k])->increase_elem_by_x(alp_tmp,weight_tmp);
3494  ((array_positive<double>*)alp_distr_errors[k])->increase_elem_by_x(alp_tmp,weight_tmp*weight_tmp);
3495  };
3496 
3497  double ind_diff=(double)(ind2_-ind1_+1);
3498  Int4 k=nalp;
3500  array_positive<double>* tmp_errors=((array_positive<double>*)alp_distr_errors[k]);
3501  Int4 j;
3502  for(j=0;j<=tmp->d_dim;j++)
3503  {
3504  tmp->d_elem[j]/=ind_diff;
3505  tmp_errors->d_elem[j]/=ind_diff;
3506  tmp_errors->d_elem[j]-=tmp->d_elem[j]*tmp->d_elem[j];
3507  tmp_errors->d_elem[j]/=ind_diff;
3508  };
3509  }
3510  catch (error er)
3511  {
3512  ee_error_flag=true;
3513  ee_error=er;
3514  };
3515  }
3516  catch (...)
3517  {
3518  ee_error_flag=true;
3519  ee_error=error("Internal error in the program\n",4);
3520  };
3521 
3522  //memory release
3523 
3524  if(ee_error_flag)
3525  {
3526  Int4 i;
3527  if(alp_distr_tmp)
3528  {
3529  for(i=0;i<=allocation_dim_tmp;i++)
3530  {
3531  delete (array_positive<double>*)alp_distr_tmp[i];alp_distr_tmp[i]=NULL;
3532  };
3533  delete []alp_distr_tmp;alp_distr_tmp=NULL;
3534  };
3535 
3536  if(alp_distr_errors_tmp)
3537  {
3538  for(i=0;i<=allocation_dim_tmp;i++)
3539  {
3540  delete (array_positive<double>*)alp_distr_errors_tmp[i];alp_distr_errors_tmp[i]=NULL;
3541  };
3542  delete []alp_distr_errors_tmp;alp_distr_errors_tmp=NULL;
3543  };
3544 
3545  if(alp_distr)
3546  {
3547  for(i=0;i<=allocation_dim;i++)
3548  {
3549  delete (array_positive<double>*)alp_distr[i];alp_distr[i]=NULL;
3550  };
3551  delete []alp_distr;alp_distr=NULL;
3552  };
3553 
3554  if(alp_distr_errors)
3555  {
3556  for(i=0;i<=allocation_dim;i++)
3557  {
3558  delete (array_positive<double>*)alp_distr_errors[i];alp_distr_errors[i]=NULL;
3559  };
3560  delete []alp_distr_errors;alp_distr_errors=NULL;
3561  };
3562 
3563 
3564  throw error(ee_error.st,ee_error.error_code);
3565  };
3566 
3567 };
3568 
3570 Int4 nalp_,
3571 Int4 ind1_,
3572 Int4 ind2_,
3573 double lambda_,
3574 double eps_K_,
3575 Int4 &M_min_)
3576 {
3577  if(nalp_<=0)
3578  {
3579  throw error("Unexpected error\n",4);
3580  };
3581  bool ee_error_flag=false;
3582  error ee_error("",0);
3584 
3585  try
3586  {
3587  try
3588  {
3589 
3591  alp_data::assert_mem(diff);
3592 
3593  double sum_of_weights=0;
3594  double M_aver=0;
3595 
3596  Int4 i;
3597  for(i=ind1_;i<=ind2_;i++)
3598  {
3599  alp* &alp_obj_tmp=d_alp_obj->d_elem[i];
3600  Int4 &alp_tmp=alp_obj_tmp->d_alp->d_elem[nalp_];
3601  double &weight_tmp=alp_obj_tmp->d_alp_weights->d_elem[nalp_];
3602  sum_of_weights+=weight_tmp;
3603  M_aver+=alp_tmp*weight_tmp;
3604 
3605  array<Int4> *cells_counts=alp_obj_tmp->d_cells_counts;
3606 
3607  Int4 k;
3608  for(k=cells_counts->d_ind0;k<=alp_data::Tmin(alp_tmp,cells_counts->d_dim_plus_d_ind0);k++)
3609  {
3610  diff->increase_elem_by_x(alp_tmp-k,cells_counts->d_elem[k-cells_counts->d_ind0]*weight_tmp);
3611  };
3612  };
3613 
3614 
3615 
3616  double den=0;
3617  for(i=0;i<=diff->d_dim;i++)
3618  {
3619  den+=exp(-lambda_*(double)i)*diff->d_elem[i];
3620  };
3621 
3622 
3623  if(den<=0||sum_of_weights<=0)
3624  {
3625  throw error("The program is not able to estimate the parameters\n",2);
3626  };
3627 
3628 
3629  M_aver/=sum_of_weights;
3630 
3631 
3632  double delta_val=den*eps_K_*(1-exp(-lambda_));
3633 
3634  Int4 diff_opt=1;;
3635  for(i=diff->d_dim;i>=0;i--)
3636  {
3637  if(exp(-lambda_*(double)i)*diff->d_elem[i]>delta_val)
3638  {
3639  diff_opt=i+1;
3640  break;
3641  };
3642  };
3643 
3644 
3645 
3646 
3647  M_min_=(Int4)alp_data::round(M_aver);
3648 
3649 
3650  delete diff;diff=NULL;
3651  if(M_aver<diff_opt)
3652  {
3653  return false;
3654  };
3655 
3656  return true;
3657  }
3658  catch (error er)
3659  {
3660  ee_error_flag=true;
3661  ee_error=er;
3662  };
3663  }
3664  catch (...)
3665  {
3666  ee_error_flag=true;
3667  ee_error=error("Internal error in the program\n",4);
3668  };
3669 
3670  //memory release
3671 
3672  delete diff;diff=NULL;
3673 
3674  if(ee_error_flag)
3675  {
3676  throw error(ee_error.st,ee_error.error_code);
3677  };
3678  return false;
3679 
3680 };
3681 
3683 Int4 ind1_,
3684 Int4 ind2_,
3685 double lambda_,
3686 double eps_K_,
3687 Int4 current_level_,
3688 Int4 &recommended_level_,
3689 Int4 &diff_opt_,
3690 double &K_C_,
3691 double &K_C_error_)
3692 {
3693  if(ind1_>ind2_)
3694  {
3695  throw error("Unexpected error\n",4);
3696  };
3697 
3699  array_positive<double>* diff_error=NULL;
3700 
3701  bool ee_error_flag=false;
3702  error ee_error("",0);
3703 
3704  try
3705  {
3706  try
3707  {
3708 
3710  alp_data::assert_mem(diff);
3711 
3712  diff_error=new array_positive<double>(d_alp_data);
3713  alp_data::assert_mem(diff_error);
3714 
3715 
3716  double sum_of_weights=0;
3717  double sum_of_weights_error=0;
3718 
3719  double M_aver=0;
3720 
3721  Int4 i;
3722  for(i=ind1_;i<=ind2_;i++)
3723  {
3724  alp* &alp_obj_tmp=d_alp_obj->d_elem[i];
3725  Int4 &alp_tmp=alp_obj_tmp->d_M;
3726  double &weight_tmp=alp_obj_tmp->d_alp_weights->d_elem[alp_obj_tmp->d_nalp_killing];
3727  sum_of_weights+=weight_tmp;
3728  sum_of_weights_error+=weight_tmp*weight_tmp;
3729  M_aver+=alp_tmp*weight_tmp;
3730 
3731 
3732  array<Int4> *cells_counts=alp_obj_tmp->d_cells_counts;
3733 
3734  Int4 k;
3735  for(k=cells_counts->d_ind0;k<=alp_data::Tmin(alp_tmp,cells_counts->d_dim_plus_d_ind0);k++)
3736  {
3737  double tmp=cells_counts->d_elem[k-cells_counts->d_ind0]*weight_tmp;
3738  diff->increase_elem_by_x(alp_tmp-k,tmp);
3739  diff_error->increase_elem_by_x(alp_tmp-k,tmp*tmp);
3740  };
3741  };
3742 
3743 
3744 
3745  double tmp2=(double)(ind2_-ind1_+1);
3746 
3747  sum_of_weights/=tmp2;
3748  sum_of_weights_error/=tmp2;
3749  sum_of_weights_error-=sum_of_weights*sum_of_weights;
3750  sum_of_weights_error/=tmp2;
3751  sum_of_weights_error=alp_reg::sqrt_for_errors(sum_of_weights_error);
3752 
3753 
3754 
3755  for(i=0;i<=diff->d_dim;i++)
3756  {
3757  diff->d_elem[i]/=tmp2;
3758  diff_error->d_elem[i]/=tmp2;
3759  diff_error->d_elem[i]-=diff->d_elem[i]*diff->d_elem[i];
3760  diff_error->d_elem[i]/=tmp2;
3761  };
3762 
3763 
3764 
3765  double den=0;
3766  double den_error=0;
3767  for(i=0;i<=diff->d_dim;i++)
3768  {
3769  double tmp=exp(-lambda_*(double)i);
3770  den+=tmp*diff->d_elem[i];
3771  den_error+=tmp*tmp*diff_error->d_elem[i];
3772 
3773  };
3774 
3775 
3776 
3777  den_error=alp_reg::sqrt_for_errors(den_error);
3778 
3779 
3780  if(den<=0||sum_of_weights<=0)
3781  {
3782  throw error("The program is not able to estimate the parameters\n",2);
3783  };
3784 
3785  K_C_=sum_of_weights/den;
3786  K_C_error_=alp_reg::error_of_the_ratio(sum_of_weights,sum_of_weights_error,den,den_error);
3787 
3788 
3789  M_aver/=tmp2;
3790  M_aver/=sum_of_weights;
3791 
3792 
3793  double delta_val=den*eps_K_*(1-exp(-lambda_));
3794 
3795  Int4 diff_opt=1;;
3796  for(i=diff->d_dim;i>=0;i--)
3797  {
3798  if(exp(-lambda_*(double)i)*diff->d_elem[i]>delta_val)
3799  {
3800  diff_opt=i+1;
3801  break;
3802  };
3803  };
3804 
3805  delete diff;diff=NULL;
3806  delete diff_error;diff_error=NULL;
3807 
3808  if(M_aver-diff_opt<current_level_)
3809  {
3810  recommended_level_=(Int4)floor(M_aver-diff_opt*1.1);
3811  diff_opt_=(Int4)ceil(M_aver-recommended_level_);
3812  return false;
3813  };
3814  recommended_level_=current_level_;
3815  diff_opt_=(Int4)ceil(M_aver-recommended_level_);
3816  return true;
3817  }
3818  catch (error er)
3819  {
3820  ee_error_flag=true;
3821  ee_error=er;
3822  };
3823  }
3824  catch (...)
3825  {
3826  ee_error_flag=true;
3827  ee_error=error("Internal error in the program\n",4);
3828  };
3829 
3830  //memory release
3831 
3832  if(ee_error_flag)
3833  {
3834  delete diff;diff=NULL;
3835  delete diff_error;diff_error=NULL;
3836 
3837  throw error(ee_error.st,ee_error.error_code);
3838  };
3839  return false;
3840 
3841 };
3842 
3843 
3844 
3846 bool check_the_criteria_,
3847 Int4 nalp_,
3848 Int4 &nalp_thr_,
3849 bool &inside_simulation_flag_,
3850 Int4 ind1_,
3851 Int4 ind2_,
3852 void **alp_distr,
3853 void **alp_distr_errors,
3854 double &lambda_,
3855 double &lambda_error_,
3856 double &test_difference_,
3857 double &test_difference_error_)
3858 {
3859  Int4 nalp=nalp_;
3860 
3861  if(nalp<=0)
3862  {
3863  throw error("Unexpected error\n",4);
3864  };
3865 
3866 
3867 
3868  struct_for_lambda_calculation tmp_struct;
3869  tmp_struct.d_alp_distr=alp_distr;
3870  tmp_struct.d_alp_distr_errors=alp_distr_errors;
3871  tmp_struct.d_nalp=nalp;
3872  tmp_struct.d_calculate_alp_number=false;
3873 
3874 
3876  void* func_pointer=&tmp_struct;
3877  double a=0;
3878  double b=d_alp_data->d_is->d_lambda*2;
3879  Int4 n_partition=30;
3880  double eps=1e-10;
3881  std::vector<double> res;
3882 
3883 
3884 
3886  func,
3887  func_pointer,
3888  a,//[a,b] is the interval for search of equation solution
3889  b,
3890  n_partition,
3891  eps,
3892  res);
3893 
3894 
3895 
3896 
3897  inside_simulation_flag_=true;
3898  if(res.size()==0)
3899  {
3900  inside_simulation_flag_=false;
3901  return;
3902  };
3903 
3904 
3905 
3906  lambda_=get_root(res,d_alp_data->d_is->d_lambda);
3907 
3908 
3909  tmp_struct.d_calculate_alp_number=true;
3910  double f1=func(lambda_,func_pointer);
3911  nalp_thr_=tmp_struct.d_alp_number;
3912  tmp_struct.d_calculate_alp_number=false;
3913 
3914  double slope_error=tmp_struct.d_f_error;
3915 
3916 
3917  double sum1=tmp_struct.d_last_sum;
3918  double sum1_error=tmp_struct.d_last_sum_error;
3919 
3920  double delta_lambda=lambda_/100.0;
3921  double f2=func(lambda_+delta_lambda,func_pointer);
3922 
3923 
3924 
3925  if(delta_lambda==0||f1==f2)
3926  {
3927  lambda_error_=0.0;
3928  }
3929  else
3930  {
3931  double derivative=(f2-f1)/delta_lambda;
3932  lambda_error_=fabs(slope_error/derivative);
3933  };
3934 
3935  if(!check_the_criteria_)
3936  {
3937  return;
3938  };
3939 
3940  if(nalp>1)
3941  {
3942  func(d_lambda_tmp->d_elem[nalp-1],func_pointer);
3943  }
3944  else
3945  {
3946  func(d_alp_data->d_is->d_ungap_lambda,func_pointer);
3947  };
3948 
3949 
3950 
3951  double sum2=tmp_struct.d_last_sum;
3952  double sum2_error=tmp_struct.d_last_sum_error;
3953 
3954  double max_sum=alp_data::Tmax(fabs(sum1),fabs(sum2));
3955 
3956  if(max_sum!=0)
3957  {
3958  test_difference_=fabs((sum1-sum2)/max_sum);
3959  test_difference_error_=0.5*(sum1_error+sum2_error)/max_sum;
3960  }
3961  else
3962  {
3963  test_difference_=-1;
3964  test_difference_error_=0;
3965  };
3966 
3967 
3968 
3969 
3970 
3971 
3972 
3973 };
3974 
3976 const std::vector<double> &res_tmp_,
3977 double point_)
3978 {
3979  if(res_tmp_.size()==0)
3980  {
3981  throw error("Error in alp_sim::get_root - the equation does not have roots\n",2);
3982  return 0;
3983  };
3984 
3985  Int4 i;
3986  Int4 p=0;
3987  double d1=fabs(point_-res_tmp_[0]);
3988  for(i=1;i<(Int4)res_tmp_.size();i++)
3989  {
3990  double d2=fabs(point_-res_tmp_[i]);
3991  if(d2<d1)
3992  {
3993  p=i;
3994  d1=d2;
3995  };
3996  };
3997 
3998  return res_tmp_[p];
3999 };
4000 
4001 
4002 
4004 double lambda_,
4005 void * data_)
4006 {
4007 
4008  double *expect=NULL;
4009  double *expect_errors=NULL;
4010 
4011  bool ee_error_flag=false;
4012  error ee_error("",0);
4013 
4014  try
4015  {
4016  try
4017  {
4018 
4020  void **alp_distr=tmp_struct->d_alp_distr;
4021  void **alp_distr_errors=tmp_struct->d_alp_distr_errors;
4022  Int4 nalp=tmp_struct->d_nalp;
4023 
4024  expect=new double[nalp];
4026  expect_errors=new double[nalp];
4027  alp_data::assert_mem(expect_errors);
4028 
4029  if(nalp<1)
4030  {
4031  throw error("Unexpected error\n",4);
4032  };
4033 
4034 
4035 
4036  Int4 k;
4037  for(k=1;k<=nalp;k++)
4038  {
4040  array_positive<double>* tmp_errors=((array_positive<double>*)alp_distr_errors[k]);
4041 
4042  double val=0;
4043  double val_error=0;
4044 
4045  Int4 j;
4046  for(j=0;j<=tmp->d_dim;j++)
4047  {
4048  if(tmp->d_elem[j] <= 0)
4049  {
4050  continue;
4051  };
4052  double exp_tmp=exp(lambda_*j);
4053  val+=exp_tmp*tmp->d_elem[j];
4054  val_error+=exp_tmp*exp_tmp*tmp_errors->d_elem[j];
4055  };
4056  val_error=alp_reg::sqrt_for_errors(val_error);
4057  expect[k-1]=val;
4058  expect_errors[k-1]=val_error;
4059 
4060 
4061  };
4062 
4063  tmp_struct->d_last_sum=expect[nalp-1];
4064  tmp_struct->d_last_sum_error=expect_errors[nalp-1];
4065 
4066  if(tmp_struct->d_calculate_alp_number)
4067  {
4068  double tmp=0.0;
4069  Int4 k;
4070  for(k=0;k<nalp;k++)
4071  {
4072  if(expect_errors[k]!=0)
4073  {
4074  tmp+=1.0/(expect_errors[k]*expect_errors[k]);
4075  };
4076 
4077  };
4078 
4079  Int4 tmp_alp=nalp;
4080  double tmp1=0.0;
4081  for(k=nalp-1;k>=0;k--)
4082  {
4083  if(expect_errors[k]!=0)
4084  {
4085  tmp1+=1.0/(expect_errors[k]*expect_errors[k]);
4086  };
4087  if(tmp1>0.2*tmp)
4088  {
4089  tmp_alp=k+1;
4090  break;
4091  };
4092  };
4093 
4094  tmp_struct->d_alp_number=tmp_alp;
4095  };
4096 
4097  if(nalp==1)
4098  {
4099  double tmp=expect[0]-1.0;
4100  tmp_struct->d_f_error=expect_errors[0];
4101  delete[]expect;expect=NULL;
4102  delete[]expect_errors;expect_errors=NULL;
4103  return tmp;
4104  };
4105 
4106 
4107  Int4 min_length=0;
4108  Int4 number_of_elements=nalp;
4109  bool cut_left_tail=true;
4110  bool cut_right_tail=false;
4111  double y=2;
4112  double beta0;
4113  double beta1;
4114  double beta0_error;
4115  double beta1_error;
4116  Int4 k1_opt;
4117  Int4 k2_opt;
4118 
4119  bool res_was_calculated;
4120 
4122  min_length,
4123  number_of_elements,
4124  expect,
4125  expect_errors,
4126  cut_left_tail,
4127  cut_right_tail,
4128  y,
4129  beta0,
4130  beta1,
4131  beta0_error,
4132  beta1_error,
4133  k1_opt,
4134  k2_opt,
4135  res_was_calculated);
4136 
4137  if(!res_was_calculated)
4138  {
4139  throw error("The program cannot estimate the parameters; please repeat the calculation\n",2);
4140  };
4141 
4142 
4143  delete[]expect;expect=NULL;
4144  delete[]expect_errors;expect_errors=NULL;
4145 
4146  tmp_struct->d_f_error=beta1_error;
4147  return beta1;
4148 
4149  }
4150  catch (error er)
4151  {
4152  ee_error_flag=true;
4153  ee_error=er;
4154  };
4155  }
4156  catch (...)
4157  {
4158  ee_error_flag=true;
4159  ee_error=error("Internal error in the program\n",4);
4160  };
4161 
4162  //memory release
4163 
4164  if(ee_error_flag)
4165  {
4166  delete[]expect;expect=NULL;
4167  delete[]expect_errors;expect_errors=NULL;
4168 
4169  throw error(ee_error.st,ee_error.error_code);
4170  };
4171 
4172  return 0.0;
4173 
4174 };
4175 
double d_eps_K
importance_sampling * d_is
struct_for_randomization * d_rand_all
static double round(const double &x_)
double d_eps_lambda
static T Tmax(T i_, T j_)
double d_max_mem
static void get_current_time(double &seconds_)
static T Tmin(T i_, T j_)
Int4 d_minimum_realizations_number
double d_time_before1
static Int4 random_long(double value_, Int4 dim_)
bool d_rand_flag
double ran2()
Uint4 d_random_factor
static void assert_mem(void *pointer_)
double d_memory_size_in_MB
double d_max_time
bool d_sentinels_flag
Int4 d_realizations_number2
static double sqrt_for_errors(double x_)
static double error_of_the_product(double v1_, double v1_error_, double v2_, double v2_error_)
static double error_of_the_sum(double v1_, double v1_error_, double v2_, double v2_error_)
static void robust_regression_sum_with_cut_LSM(Int4 min_length_, Int4 number_of_elements_, double *values_, double *errors_, bool cut_left_tail_, bool cut_right_tail_, double y_, double &beta0_, double &beta1_, double &beta0_error_, double &beta1_error_, Int4 &k1_opt_, Int4 &k2_opt_, bool &res_was_calculated_)
static void robust_regression_sum_with_cut_LSM_beta1_is_defined(Int4 min_length_, Int4 number_of_elements_, double *values_, double *errors_, bool cut_left_tail_, bool cut_right_tail_, double y_, double &beta0_, double beta1_, double &beta0_error_, double beta1_error_, Int4 &k1_opt_, Int4 &k2_opt_, bool &res_was_calculated_)
static void find_tetta_general(function_type *func_, void *func_pointer_, double a_, double b_, Int4 n_partition_, double eps_, std::vector< double > &res_)
static double error_of_the_ratio(double v1_, double v1_error_, double v2_, double v2_error_)
static double round_doulbe(double val_, Int4 digits_)
double m_KError
static double function_for_lambda_calculation(double lambda_, void *data_)
double m_AI
void output_main_parameters2m_new(double time_, Int4 nalp, Int4 nalp_for_lambda_simulation, Int4 level, Int4 M_min_, bool &inside_simulation_flag, Int4 final_realizations_number_lambda_, Int4 final_realizations_number_killing_)
array_positive< double > * d_lambda_tmp_errors
double m_AlphaI
void get_minimal_simulation(Int4 ind1_, Int4 ind2_, Int4 &M_min_, Int4 &nalp_, Int4 &nalp_lambda_, bool C_calculation_, bool check_time_flag_)
bool check_K_criterion_during_killing(Int4 ind1_, Int4 ind2_, double lambda_, double eps_K_, Int4 current_level_, Int4 &recommended_level_, Int4 &diff_opt_, double &K_C_, double &K_C_error_)
array_positive< double > * d_C_tmp
vector< double > m_KSbs
bool the_criterion(Int4 upto_nalp_, Int4 &nalp_for_lambda_simulation_, Int4 ind1_, Int4 ind2_, void **&alp_distr, void **&alp_distr_errors, Int4 &M_min_, bool &M_min_flag_, bool &nalp_flag_, bool &inside_simulation_flag_, bool C_calculation_)
static double lambda_exp(Int4 &i_, double *&exp_array_)
double m_AJ
vector< double > m_AISbs
static void error_in_calculate_main_parameters2m(double C, double &C_error, double C_mult2, double C_mult2_error)
double m_Lambda
void randomize_realizations_ind(Int4 ind1_, Int4 ind2_)
static double relative_error_in_percents(double val_, double val_error_)
vector< double > m_CSbs
double m_SigmaError
double m_CalcTime
void kill(bool check_time_, Int4 ind1_, Int4 ind2_, Int4 M_min_, double lambda_, double eps_K_, double &K_C_, double &K_C_error_, Int4 &level_, Int4 &diff_opt_)
vector< double > m_AlphaJSbs
static double get_root(const std::vector< double > &res_tmp_, double point_)
array_positive< alp * > * d_alp_obj
Int4 d_mult_number
void calculate_C(Int4 starting_point, Int4 nalp_, Int4 ind1_, Int4 ind2_, void **alp_distr, void **alp_distr_errors, double lambda_, double lambda_error_, double &C_, double &C_error_)
void calculate_main_parameters2m(Int4 final_realizations_number_lambda_, Int4 final_realizations_number_killing_, Int4 nalp, Int4 nalp_for_lambda_simulation, Int4 level, bool &inside_simulation_flag, double &lambda, double &lambda_error, double &test_difference, double &test_difference_error, double &C, double &C_error, double &C2, double &C2_error, double &C4, double &C4_error, double &K_C, double &K_C_error, double &a_I, double &a_I_error, double &a_J, double &a_J_error, double &sigma, double &sigma_error, double &alpha_I, double &alpha_I_error, double &alpha_J, double &alpha_J_error, double &K, double &K_error, bool &flag_)
void calculate_lambda(bool check_the_criteria_, Int4 nalp_, Int4 &nalp_thr_, bool &inside_simulation_flag_, Int4 ind1_, Int4 ind2_, void **alp_distr, void **alp_distr_errors, double &lambda_, double &lambda_error_, double &test_difference_, double &test_difference_error_)
void get_and_allocate_alp_distribution(Int4 ind1_, Int4 ind2_, void **&alp_distr, void **&alp_distr_errors, Int4 nalp)
double m_AIError
void calculate_FSC(Int4 nalp_, Int4 ind1_, Int4 ind2_, void **alp_distr, void **alp_distr_errors, double lambda_, double lambda_error_, double &a_I_, double &a_I_error_, double &a_J_, double &a_J_error_, double &sigma_, double &sigma_error_, double &alpha_I_, double &alpha_I_error_, double &alpha_J_, double &alpha_J_error_)
alp_data * d_alp_data
void generate_random_permulation(Int4 *perm_, Int4 dim_)
double m_AJError
double m_LambdaError
vector< double > m_SigmaSbs
Int4 d_n_alp_obj
double m_CError
void randomize_realizations(Int4 final_realizations_number_lambda_, Int4 final_realizations_number_killing_)
double m_Sigma
bool check_K_criterion(Int4 nalp_, Int4 ind1_, Int4 ind2_, double lambda_, double eps_K_, Int4 &M_min_)
vector< double > m_AlphaISbs
double m_C
void alpha_calculation(double delta_I_aver_, double delta_I_aver_error_, double delta_J_aver_, double delta_J_aver_error_, double delta_E_aver_, double delta_E_aver_error_, double cov_E_E_aver_, double cov_E_E_aver_error_, double cov_I_J_aver_, double cov_I_J_aver_error_, double &alpha_, double &alpha_error_)
double m_AlphaJError
alp_sim(alp_data *alp_data_)
Definition: sls_alp_sim.cpp:46
vector< double > m_AJSbs
vector< double > m_LambdaSbs
double m_K
static Int4 get_number_of_subsimulations(Int4 number_of_realizations_)
double m_AlphaIError
array_positive< double > * d_C_tmp_errors
array_positive< double > * d_lambda_tmp
double m_AlphaJ
void get_single_realization(bool check_time_, Int4 M_min_, Int4 nalp_, bool killing_flag_, Int4 level_, Int4 diff_opt_, alp *&obj_, bool &sucess_flag_, double &d_eps_)
Definition: sls_alp.hpp:92
void simulate_alp_upto_the_given_level(Int4 M_min_)
Definition: sls_alp.cpp:1980
void partially_release_memory()
Definition: sls_alp.cpp:357
array_positive< Int4 > * d_H_J
Definition: sls_alp.hpp:316
array_positive< Int4 > * d_alp
Definition: sls_alp.hpp:313
void kill_upto_level(Int4 M_min_, Int4 M_level_)
Definition: sls_alp.cpp:1744
Int4 d_nalp
Definition: sls_alp.hpp:311
bool d_success
Definition: sls_alp.hpp:305
bool d_time_error_flag
Definition: sls_alp.hpp:335
bool d_sentinels_flag
Definition: sls_alp.hpp:332
Int4 d_nalp_killing
Definition: sls_alp.hpp:312
array_positive< double > * d_alp_weights
Definition: sls_alp.hpp:319
bool d_check_time_flag
Definition: sls_alp.hpp:334
bool d_single_realiztion_calculation_flag
Definition: sls_alp.hpp:338
Int4 d_diff_opt
Definition: sls_alp.hpp:331
void simulate_next_alp()
Definition: sls_alp.cpp:1903
array< Int4 > * d_cells_counts
Definition: sls_alp.hpp:321
alp_data * d_alp_data
Definition: sls_alp.hpp:222
array_positive< Int4 > * d_H_I
Definition: sls_alp.hpp:315
Int4 d_M
Definition: sls_alp.hpp:309
void simulate_alp_upto_the_given_number(Int4 nalp_)
Definition: sls_alp.cpp:1966
alp_data * d_alp_data
void increase_elem_by_x(Int4 ind_, T x_)
void set_elem(Int4 ind_, T elem_)
Int4 d_dim_plus_d_ind0
Int4 d_ind0
T * d_elem
#define C(s)
Definition: common.h:231
#define NULL
Definition: ncbistd.hpp:225
int32_t Int4
4-byte (32-bit) signed integer
Definition: ncbitype.h:102
#define kMax_I4
Definition: ncbi_limits.h:218
int i
#define P(a, b, c, d, k, s, t)
#define fabs(v)
Definition: ncbi_dispd.c:46
unsigned int a
Definition: ncbi_localip.c:102
const double E
double lambda(size_t dimMatrix_, const Int4 *const *scoreMatrix_, const double *q_)
static char tmp[2048]
Definition: utf8.c:42
#define K
const double mb_bytes
double function_type(double x_, void *func_number_)
USING_SCOPE(blast)
USING_NCBI_SCOPE
Definition: sls_alp_sim.cpp:41
std::string st
Int4 error_code
vector< Int4 > d_preliminary_realizations_numbers_ALP
Int4 d_total_realizations_number_with_killing
vector< Int4 > d_preliminary_realizations_numbers_killing
vector< Int4 > d_first_stage_preliminary_realizations_numbers_ALP
static const char * expect
Definition: tables.c:54
Modified on Mon Mar 04 05:13:29 2024 by modify_doxy.py rev. 669887