bpp-core3  3.0.0
FullHmmTransitionMatrix.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "../../Text/TextTools.h"
6 #include "../Matrix/MatrixTools.h"
7 #include "../VectorTools.h"
9 
10 using namespace bpp;
11 using namespace std;
12 
13 FullHmmTransitionMatrix::FullHmmTransitionMatrix(std::shared_ptr<const HmmStateAlphabet> alph, const string& prefix) :
15  AbstractParametrizable(prefix),
16  vSimplex_()
17 {
18  size_t size = static_cast<size_t>(getNumberOfStates());
19 
20  for (size_t i = 0; i < size; ++i)
21  {
22  vSimplex_.push_back(Simplex(size, 1, false, prefix + TextTools::toString(i + 1) + "."));
24  }
25 }
26 
30  vSimplex_(hptm.vSimplex_)
31 {}
32 
34 {
36  AbstractParametrizable::operator=(hptm);
37 
38  return *this;
39 }
40 
42 {
43  if (mat.getNumberOfRows() != vSimplex_.size())
44  throw BadSizeException("FullHmmTransitionMatrix::setTransitionProbabilities: Wrong number of rows in given Matrix", mat.getNumberOfRows(), vSimplex_.size());
45 
46  ParameterList pl;
47 
48  for (size_t i = 0; i < mat.getNumberOfRows(); ++i)
49  {
50  vSimplex_[i].setFrequencies(mat.row(i));
51  ParameterList pls = vSimplex_[i].getParameters();
52  for (size_t j = 0; j < pls.size(); ++j)
53  {
54  Parameter* p = pls[j].clone();
55  p->setName(TextTools::toString(i + 1) + "." + p->getName());
56  pl.addParameter(p);
57  }
58  }
59 
61 }
62 
63 
65 {
66  if (!upToDate_)
67  {
68  for (size_t i = 0; i < vSimplex_.size(); ++i)
69  {
70  for (size_t j = 0; j < vSimplex_[i].dimension(); ++j)
71  {
72  pij_(i, j) = vSimplex_[i].prob(j);
73  }
74  }
75  upToDate_ = true;
76  }
77 
78  return pij_;
79 }
80 
81 const std::vector<double>& FullHmmTransitionMatrix::getEquilibriumFrequencies() const
82 {
83  size_t salph = getNumberOfStates();
84 
85  if (!upToDate_)
86  {
87  pij_ = getPij();
88 
90 
91  for (size_t i = 0; i < salph; ++i)
92  {
93  eqFreq_[i] = tmpmat_(0, i);
94  }
95 
96  upToDate_ = true;
97  }
98 
99  return eqFreq_;
100 }
101 
103 {
104  size_t salph = getNumberOfStates();
105 
106  for (size_t i = 0; i < salph; ++i)
107  {
108  vSimplex_[i].matchParametersValues(parameters);
109  }
110 
111  upToDate_ = false;
112 }
The matrix template interface.
Definition: Matrix.h:22
FullHmmTransitionMatrix & operator=(const FullHmmTransitionMatrix &hptm)
void fireParameterChanged(const ParameterList &parameters)
From AbstractParametrizable interface.
A partial implementation of the Parametrizable interface.
const Matrix< double > & getPij() const
Get all transition probabilities as a matrix.
FullHmmTransitionMatrix(std::shared_ptr< const HmmStateAlphabet > alph, const std::string &prefix="")
size_t size() const
Definition: ParameterList.h:56
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
Describe the transition probabilities between hidden states of a Hidden Markov Model.
A Simplex object, used to define sets of probabilities that sum 1.
Definition: Simplex.h:66
The parameter list object.
Definition: ParameterList.h:27
Wrong size exception class.
Definition: Exceptions.h:151
Partial implementation of HmmTransitionMatrix.
bool matchParametersValues(const ParameterList &parameters) override
Update the parameters from parameters.
ParameterList * clone() const
Create a copy of this object and send a pointer to it.
Definition: ParameterList.h:48
static void pow(const Matrix &A, size_t p, Matrix &O)
Compute the power of a given matrix.
Definition: MatrixTools.h:472
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:174
virtual void setName(const std::string &name)
Set the name of this parameter.
Definition: Parameter.h:148
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
virtual std::vector< Scalar > row(size_t i) const =0
void setTransitionProbabilities(const Matrix< double > &mat)
Set the matrix of the transition probabilities.
virtual size_t getNumberOfRows() const =0
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:115
AbstractHmmTransitionMatrix & operator=(const AbstractHmmTransitionMatrix &hptm)
const std::vector< double > & getEquilibriumFrequencies() const
const ParameterList & getParameters() const override
Get all parameters available.
virtual void addParameters_(const ParameterList &parameters)