ToPS
PhasedFactorableModelEvaluationAlgorithm.cpp
00001 /*
00002  *       PhasedFactorableModelEvaluationAlgorithm.cpp
00003  *
00004  *       Copyright 2011 Andre Yoshiaki Kashiwabara <akashiwabara@usp.br>
00005  *                      �gor Bon�dio <ibonadio@ime.usp.br>
00006  *                      Vitor Onuchic <vitoronuchic@gmail.com>
00007  *                      Alan Mitchell Durham <aland@usp.br>
00008  *
00009  *       This program is free software; you can redistribute it and/or modify
00010  *       it under the terms of the GNU  General Public License as published by
00011  *       the Free Software Foundation; either version 3 of the License, or
00012  *       (at your option) any later version.
00013  *
00014  *       This program is distributed in the hope that it will be useful,
00015  *       but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *       GNU General Public License for more details.
00018  *
00019  *       You should have received a copy of the GNU General Public License
00020  *       along with this program; if not, write to the Free Software
00021  *       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00022  *       MA 02110-1301, USA.
00023  */
00024 
00025 #include "PhasedFactorableModelEvaluationAlgorithm.hpp"
00026 #include "ThreePeriodicMarkovChain.hpp"
00027 
00028 namespace tops {
00029 
00030   double  PhasedFactorableModelEvaluationAlgorithm::compute(int begin, int end, int phase) const
00031   {
00032     int nphase = _alpha.size();
00033     int p = mod(begin, nphase);
00034     int k = 0;
00035     while((phase >= 0) &&  (mod(p + k, nphase) !=  phase))
00036       k++;
00037     p = k;
00038 #if 0
00039     std::cerr << p << " " << begin <<  " " << end << " " << phase << " " << _alpha[p][begin] << " " << _alpha[p][end+1] << " " << _precision[p][end+1] - _precision[p][begin] <<  std::endl;
00040 #endif
00041     if((end  + 1) >= _precision[p].size())
00042       end = _precision[p].size() - 2;
00043     double t = _precision[p][end+1] - _precision[p][begin];
00044     if(t > 0)
00045       return -HUGE;
00046 
00047     return _alpha[p][end+1] - _alpha[p][begin];
00048   }
00049 
00050   double PhasedFactorableModelEvaluationAlgorithm::compute(int begin, int end) const
00051   {
00052     int nphase = _alpha.size();
00053     int p = mod(begin, nphase);
00054     int k = 0;
00055     while((_phase > 0) &&  (mod(p + k, nphase) !=  _phase))
00056       k++;
00057     p = k;
00058     double t = _precision[p][end+1] - _precision[p][begin];
00059     if(t > 0)
00060       return -HUGE;
00061     return _alpha[p][end+1] - _alpha[p][begin];
00062   }
00063 
00064   void PhasedFactorableModelEvaluationAlgorithm::initialize(const Sequence & s, const ProbabilisticModel *m)
00065   {
00066     _alpha.resize(1);
00067     for(int i = 0; i < (int)s.size(); i++)
00068       _alpha[0][i] += m->evaluate(s, i,i);
00069     _phase = 0;
00070   }
00071 
00072 
00073   void PhasedFactorableModelEvaluationAlgorithm::initialize(const Sequence & s, const ProbabilisticModel *m, int phase)
00074   {
00075     _phase = phase;
00076     int nphases = m->numberOfPhases();
00077     _alpha.resize(nphases);
00078     _precision.resize(nphases);
00079     Sequence history;
00080     for(int k = 0; k < nphases; k++)
00081       {
00082         _alpha[k].resize(s.size() +1);
00083         _precision[k].resize(s.size()+1);
00084       }
00085     for(int k = 0; k < nphases; k++)
00086       {
00087         _alpha[k][0] = 0;
00088         _precision[k][0] = 0;
00089         history.resize(0);
00090         for(int i = 0; i < (int)s.size(); i++)
00091           {
00092             double prob = m->log_probability_of (history, s, i, k);
00093             if(close(prob, -HUGE, 1))
00094               {
00095                 _precision[k][i+1] = _precision[k][i]+ 1;
00096                 history.resize(0);
00097                 _alpha[k][i+1] = m->log_probability_of(history,s,i,k);
00098 
00099               }
00100             else
00101               {
00102                 _precision[k][i+1] = _precision[k][i];
00103                 _alpha[k][i+1] = _alpha[k][i] + prob;
00104                 history.push_back(s[i]);
00105               }
00106           }
00107       }
00108 #if 0
00109     for(int k = 0; k < nphases; k++){
00110       std::cerr << k << std::endl;
00111       for(int i = 0; i < s.size(); i++)
00112         std::cerr << " "  << i << " " << m->alphabet()->getSymbol(s[i])->name() << " " << _alpha[k][i] << " " << _precision[k][i] << std::endl;
00113     }
00114 #endif
00115   }
00116 
00117 }
00118