bpp-phyl3  3.0.0
TransitionMatrix.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 <Bpp/Exceptions.h>
9 
10 
11 using namespace std;
12 using namespace bpp;
13 
14 // TransitionMatrix node
15 
16 ConfiguredTransitionMatrix::ConfiguredTransitionMatrix (Context& context, NodeRefVec&& deps, std::unique_ptr<HmmTransitionMatrix>&& hmm)
17  : Value<const HmmTransitionMatrix*>(std::move (deps), hmm.get ()), AbstractParametrizable(hmm->getNamespace()) // , context_(context)
18  , hmm_(std::move(hmm))
19 {
20  for (const auto& dep:dependencies())
21  {
22  shareParameter_(std::dynamic_pointer_cast<ConfiguredParameter>(dep));
23  }
24 }
25 
27 
28 std::string ConfiguredTransitionMatrix::description () const { return "TransitionMatrix(HMM)"; }
29 
31 {
32  return "nbState=" + std::to_string (hmm_->getNumberOfStates ());
33 }
34 
35 // TransitionMatrix node additional arguments = (type of BranchTransitionMatrix).
36 // Everything else is determined by the node dependencies.
38 {
39  const auto* derived = dynamic_cast<const Self*>(&other);
40  if (derived == nullptr)
41  {
42  return false;
43  }
44  else
45  {
46  const auto& thisHMM = *hmm_;
47  const auto& otherHMM = *derived->hmm_;
48  return typeid (thisHMM) == typeid (otherHMM);
49  }
50 }
51 
53 {
54  const auto& bppTransitionMatrix = *hmm_;
55  return typeid (bppTransitionMatrix).hash_code ();
56 }
57 
59 {
60  auto m = ConfiguredParametrizable::createConfigured<Target, Self>(c, std::move (deps), std::unique_ptr<Target>(dynamic_cast<Target*>(hmm_->clone ())));
61  m->config = this->config; // Duplicate derivation config
62  return m;
63 }
64 
65 // EquilibriumFrequenciesFromTransitionMatrix
66 
68  NodeRefVec&& deps, const Dimension<T>& dim)
69  : Value<T>(std::move (deps)), targetDimension_ (dim) {}
70 
72 {
73  using namespace numeric;
74  return debug (this->accessValueConst ()) + " targetDim=" + to_string (targetDimension_);
75 }
76 
77 // EquilibriumFrequenciesFromTransitionMatrix additional arguments = ().
79 {
80  return dynamic_cast<const Self*>(&other) != nullptr;
81 }
82 
84 {
85  // d(equFreqs)/dn = sum_i d(equFreqs)/dx_i * dx_i/dn (x_i = model parameters)
86  auto hmmDep = this->dependency (0);
87  auto& hmm = static_cast<Dep&>(*hmmDep);
88  auto buildFWithNewTransitionMatrix = [this, &c](NodeRef&& newTransitionMatrix) {
89  return ConfiguredParametrizable::createVector<Dep, Self>(c, {std::move (newTransitionMatrix)}, targetDimension_);
90  };
91 
92  NodeRefVec derivativeSumDeps = ConfiguredParametrizable::generateDerivativeSumDepsForComputations<Dep, T>(
93  c, hmm, node, targetDimension_, buildFWithNewTransitionMatrix);
94 
95  return CWiseAdd<T, ReductionOf<T>>::create (c, std::move (derivativeSumDeps), targetDimension_);
96 }
97 
99 {
100  return ConfiguredParametrizable::createVector<Dep, Self>(c, std::move (deps), targetDimension_);
101 }
102 
104 {
105  const auto phmm = dynamic_cast<const HmmTransitionMatrix*>(accessValueConstCast<const HmmTransitionMatrix*>(*this->dependency (0)));
106 
107  const Vdouble& freqs = phmm->getEquilibriumFrequencies();
108 
109  auto& r = this->accessValueMutable ();
110  r = Eigen::Map<const T>(freqs.data (), static_cast<Eigen::Index>(freqs.size ()));
111 }
112 
113 // TransitionMatrixFromTransitionMatrix
114 
116  const Dimension<Eigen::MatrixXd>& dim)
117  : Value<Eigen::MatrixXd>(std::move (deps)), targetDimension_ (dim) {}
118 
120 {
121  using namespace numeric;
122  return debug (this->accessValueConst ()) + " targetDim=" + to_string (targetDimension_);
123 }
124 
125 // TransitionMatrixFromTransitionMatrix additional arguments = ().
127 {
128  return dynamic_cast<const Self*>(&other) != nullptr;
129 }
130 
132 {
133  // dtm/dn = sum_i dtm/dx_i * dx_i/dn + dtm/dbrlen * dbrlen/dn (x_i = model parameters).
134  auto hmmDep = this->dependency (0);
135 
136  // TransitionMatrix part
137  auto& hmm = static_cast<Dep&>(*hmmDep);
138  auto buildFWithNewTransitionMatrix = [this, &c](NodeRef&& newTransitionMatrix) {
139  return ConfiguredParametrizable::createMatrix<Dep, Self>(c, {std::move (newTransitionMatrix)}, targetDimension_);
140  };
141 
142  NodeRefVec derivativeSumDeps = ConfiguredParametrizable::generateDerivativeSumDepsForComputations<Dep, T>(
143  c, hmm, node, targetDimension_, buildFWithNewTransitionMatrix);
144 
145  return CWiseAdd<T, ReductionOf<T>>::create (c, std::move (derivativeSumDeps), targetDimension_);
146 }
147 
149 {
150  return ConfiguredParametrizable::createMatrix<Dep, Self>(c, std::move (deps), targetDimension_);
151 }
152 
154 {
155  const auto phmm = dynamic_cast<const HmmTransitionMatrix*>(accessValueConstCast<const HmmTransitionMatrix*>(*this->dependency (0)));
156 
157  auto& r = this->accessValueMutable ();
158  copyBppToEigen (phmm->getPij(), r);
159 }
virtual void shareParameter_(const std::shared_ptr< Parameter > &parameter)
Data flow node representing a TransitionMatrix configured with parameter values.
NumericalDerivativeConfiguration config
Configuration for numerical derivation of computation nodes using this TransitionMatrix.
std::string description() const final
Node pretty name (default = type name).
bool compareAdditionalArguments(const Node_DF &other) const
Compare node-specific configuration to another.
std::string debugInfo() const final
Node debug info (default = ""): user defined detailed info for DF graph debug.
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
std::unique_ptr< HmmTransitionMatrix > hmm_
std::size_t hashAdditionalArguments() const
Return the hash of node-specific configuration.
Context for dataflow node construction.
Definition: DataFlow.h:527
std::string debugInfo() const final
Node debug info (default = ""): user defined detailed info for DF graph debug.
void compute() final
Computation implementation.
bool compareAdditionalArguments(const Node_DF &other) const
Compare node-specific configuration to another.
EquilibriumFrequenciesFromTransitionMatrix(NodeRefVec &&deps, const Dimension< T > &dim)
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
virtual const std::vector< double > & getEquilibriumFrequencies() const=0
Base dataflow Node class.
Definition: DataFlow.h:152
const NodeRef & dependency(std::size_t i) const noexcept
Definition: DataFlow.h:185
const NodeRefVec & dependencies() const noexcept
Definition: DataFlow.h:183
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
void compute() final
Computation implementation.
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
TransitionMatrixFromTransitionMatrix(NodeRefVec &&deps, const Dimension< T > &dim)
Build a new TransitionMatrixFromTransitionMatrix node with the given output dimensions.
bool compareAdditionalArguments(const Node_DF &other) const
Compare node-specific configuration to another.
std::string debugInfo() const final
Node debug info (default = ""): user defined detailed info for DF graph debug.
Abstract Node storing a value of type T.
Definition: DataFlow.h:352
const Eigen::VectorXd & accessValueConst() const noexcept
Raw value access (const).
Definition: DataFlow.h:385
Eigen::VectorXd & accessValueMutable() noexcept
Definition: DataFlow.h:416
std::string debug(const T &t, typename std::enable_if< std::is_arithmetic< T >::value >::type *=0)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
std::string to_string(const NoDimension &)
std::vector< NodeRef > NodeRefVec
Alias for a dependency vector (of NodeRef).
Definition: DataFlow.h:81
template void copyBppToEigen(const std::vector< ExtendedFloat > &bppVector, Eigen::RowVectorXd &eigenVector)
std::shared_ptr< Node_DF > NodeRef
Definition: DataFlow.h:78
Store a dimension for type T.