bpp-core3  3.0.0
FullHmmTransitionMatrix.cpp
Go to the documentation of this file.
1 //
2 // File: FullHmmTransitionMatrix.cpp
3 // Authors:
4 // Laurent Guéguen
5 // Created: samedi 21 septembre 2013, à 14h 43
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++Development Team, (November 16, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for phylogenetic data analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 
42 #include "../../Text/TextTools.h"
43 #include "../Matrix/MatrixTools.h"
44 #include "../VectorTools.h"
46 
47 using namespace bpp;
48 using namespace std;
49 
52  AbstractParametrizable(prefix),
53  vSimplex_()
54 {
55  size_t size = (size_t)getNumberOfStates();
56 
57  for (size_t i = 0; i < size; i++)
58  {
59  vSimplex_.push_back(Simplex(size, 1, false, prefix + TextTools::toString(i + 1) + "."));
61  }
62 }
63 
67  vSimplex_(hptm.vSimplex_)
68 {}
69 
71 {
73  AbstractParametrizable::operator=(hptm);
74 
75  return *this;
76 }
77 
79 {
80  if (mat.getNumberOfRows() != vSimplex_.size())
81  throw BadSizeException("FullHmmTransitionMatrix::setTransitionProbabilities: Wrong number of rows in given Matrix", mat.getNumberOfRows(), vSimplex_.size());
82 
83  ParameterList pl;
84 
85  for (size_t i = 0; i < mat.getNumberOfRows(); i++)
86  {
87  vSimplex_[i].setFrequencies(mat.row(i));
88  ParameterList pls = vSimplex_[i].getParameters();
89  for (size_t j = 0; j < pls.size(); j++)
90  {
91  Parameter* p = pls[j].clone();
92  p->setName(TextTools::toString(i + 1) + "." + p->getName());
93  pl.addParameter(p);
94  }
95  }
96 
98 }
99 
100 
102 {
103  if (!upToDate_)
104  {
105  for (size_t i = 0; i < vSimplex_.size(); i++)
106  {
107  for (size_t j = 0; j < vSimplex_[i].dimension(); j++)
108  {
109  pij_(i, j) = vSimplex_[i].prob(j);
110  }
111  }
112  upToDate_ = true;
113  }
114 
115  return pij_;
116 }
117 
118 const std::vector<double>& FullHmmTransitionMatrix::getEquilibriumFrequencies() const
119 {
120  size_t salph = getNumberOfStates();
121 
122  if (!upToDate_)
123  {
124  pij_ = getPij();
125 
127 
128  for (size_t i = 0; i < salph; i++)
129  {
130  eqFreq_[i] = tmpmat_(0, i);
131  }
132 
133  upToDate_ = true;
134  }
135 
136  return eqFreq_;
137 }
138 
140 {
141  size_t salph = getNumberOfStates();
142 
143  for (size_t i = 0; i < salph; i++)
144  {
145  vSimplex_[i].matchParametersValues(parameters);
146  }
147 
148  upToDate_ = false;
149 }
Partial implementation of HmmTransitionMatrix.
AbstractHmmTransitionMatrix & operator=(const AbstractHmmTransitionMatrix &hptm)
A partial implementation of the Parametrizable interface.
const ParameterList & getParameters() const
Get all parameters available.
virtual void addParameters_(const ParameterList &parameters)
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
Wrong size exception class.
Definition: Exceptions.h:190
Describe the transition probabilities between hidden states of a Hidden Markov Model.
const std::vector< double > & getEquilibriumFrequencies() const
FullHmmTransitionMatrix(const HmmStateAlphabet *alph, const std::string &prefix="")
void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
FullHmmTransitionMatrix & operator=(const FullHmmTransitionMatrix &hptm)
void setTransitionProbabilities(const Matrix< double > &mat)
Set the matrix of the transition probabilities.
const Matrix< double > & getPij() const
Get all transition probabilities as a matrix.
Hidden states alphabet.
static void pow(const Matrix &A, size_t p, Matrix &O)
Compute the power of a given matrix.
Definition: MatrixTools.h:509
The matrix template interface.
Definition: Matrix.h:61
virtual std::vector< Scalar > row(size_t i) const =0
virtual size_t getNumberOfRows() const =0
The parameter list object.
Definition: ParameterList.h:65
size_t size() const
Definition: ParameterList.h:92
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
ParameterList * clone() const
Create a copy of this object and send a pointer to it.
Definition: ParameterList.h:84
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual void setName(const std::string &name)
Set the name of this parameter.
Definition: Parameter.h:185
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:211
A Simplex object, used to define sets of probabilities that sum 1.
Definition: Simplex.h:105
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153