bpp-phyl3  3.0.0
AbstractPhyloLikelihood.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_PHYL_LIKELIHOOD_PHYLOLIKELIHOODS_ABSTRACTPHYLOLIKELIHOOD_H
6 #define BPP_PHYL_LIKELIHOOD_PHYLOLIKELIHOODS_ABSTRACTPHYLOLIKELIHOOD_H
7 
8 
9 #include "../DataFlow/DataFlowNumeric.h"
10 #include "../DataFlow/LikelihoodCalculation.h"
11 #include "../DataFlow/Parametrizable.h"
12 #include "PhyloLikelihood.h"
13 
14 namespace bpp
15 {
16 class LikelihoodCalculation;
17 
19  public virtual PhyloLikelihoodInterface
20 {
21 public:
22  // Cache generated nodes representing derivatives, to avoid recreating them every time.
23  // Using the mutable keyword because the table must be changed even in const methods.
25  {
26  std::size_t operator()(const std::pair<std::string, std::string>& p) const
27  {
28  std::hash<std::string> strHash{};
29  return strHash (p.first) ^ (strHash (p.second) << 1);
30  }
31  };
32 
33 protected:
35 
40 
44  mutable std::unordered_map<std::string, ValueRef<DataLik>> firstOrderDerivativeNodes_;
45 
46  mutable std::unordered_map<std::pair<std::string, std::string>, ValueRef<DataLik>,
49 
50 public:
53  minusLogLik_(0)
54  {}
55 
57  context_(apl.context_),
59  {
61  }
62 
64  {
65  context_ = apl.context_;
68  return *this;
69  }
70 
72 
73  const Context& context() const override { return context_; }
74 
75  Context& context() override { return context_; }
76 
81  virtual bool isInitialized() const override
82  {
83  return false;
84  }
85 
86 public:
90  void shareParameters(const ParameterList& variableNodes)
91  {
92  this->getParameters_().shareParameters(variableNodes);
93  }
94 
95  void setParameters(const ParameterList& parameters) override
96  {
97  setParametersValues(parameters);
98  }
99 
101  {
102  return getLikelihoodCalculation()->getLikelihoodNode();
103  }
104 
105 
106  // bpp::Function
107 
113  virtual void enableFirstOrderDerivatives(bool yn) override {}
114  virtual void enableSecondOrderDerivatives(bool yn) override {}
115  bool enableFirstOrderDerivatives() const override { return true; }
116  bool enableSecondOrderDerivatives() const override { return true; }
117 
118  /*
119  * @brief return the value, ie -loglikelihood
120  *
121  * !!! check on computeLikelihoods_ is not done here.
122  *
123  */
124  double getValue() const override
125  {
126  minusLogLik_ = -getLikelihoodNode()->targetValue();
127  return convert(minusLogLik_);
128  }
129 
130  // bpp::DerivableFirstOrder
131  double getFirstOrderDerivative (const std::string& variable) const override
132  {
133  using namespace std; // for isinf
134  using namespace numeric; // for isinf
135  return convert(-firstOrderDerivativeNode (variable)->targetValue ());
136  }
137 
138  // Get nodes of derivatives directly
139  ValueRef<DataLik> firstOrderDerivativeNode (const std::string& variable) const
140  {
141  const auto it = firstOrderDerivativeNodes_.find (variable);
142  if (it != firstOrderDerivativeNodes_.end ())
143  {
144  return it->second;
145  }
146  else
147  {
148  auto node = getLikelihoodNode()->deriveAsValue (context_, accessVariableNode (variable));
149  firstOrderDerivativeNodes_.emplace (variable, node);
150  return node;
151  }
152  }
153 
154  // bpp::DerivableSecondOrder
155  double getSecondOrderDerivative (const std::string& variable) const override
156  {
157  return getSecondOrderDerivative (variable, variable);
158  }
159 
160  double getSecondOrderDerivative (const std::string& variable1,
161  const std::string& variable2) const override
162  {
163  using namespace std; // for isinf
164  using namespace numeric; // for isinf
165  return convert(-secondOrderDerivativeNode (variable1, variable2)->targetValue ());
166  }
167 
168  ValueRef<DataLik> secondOrderDerivativeNode (const std::string& variable1,
169  const std::string& variable2) const
170  {
171  const auto key = std::make_pair (variable1, variable2);
172  const auto it = secondOrderDerivativeNodes_.find (key);
173  if (it != secondOrderDerivativeNodes_.end ())
174  {
175  return it->second;
176  }
177  else
178  {
179  // Reuse firstOrderDerivative() to generate the first derivative with caching
180  auto node =
181  firstOrderDerivativeNode (variable1)->deriveAsValue (context_, accessVariableNode (variable2));
182  secondOrderDerivativeNodes_.emplace (key, node);
183  return node;
184  }
185  }
186 
187 protected:
188  static Node_DF& accessVariableNode (const Parameter& param)
189  {
190  return *dynamic_cast<const ConfiguredParameter&>(param).dependency(0);
191  }
192 
193  Node_DF& accessVariableNode (const std::string& name) const
194  {
195  return accessVariableNode (parameter(name));
196  }
197 };
198 } // end of namespace bpp.
199 #endif // BPP_PHYL_LIKELIHOOD_PHYLOLIKELIHOODS_ABSTRACTPHYLOLIKELIHOOD_H
virtual bool isInitialized() const override
Sets the computeLikelihoods_ to true.
double getFirstOrderDerivative(const std::string &variable) const override
std::unordered_map< std::pair< std::string, std::string >, ValueRef< DataLik >, StringPairHash > secondOrderDerivativeNodes_
bool enableSecondOrderDerivatives() const override
virtual void enableSecondOrderDerivatives(bool yn) override
double getSecondOrderDerivative(const std::string &variable1, const std::string &variable2) const override
bool enableFirstOrderDerivatives() const override
const Context & context() const override
ValueRef< DataLik > firstOrderDerivativeNode(const std::string &variable) const
ValueRef< DataLik > secondOrderDerivativeNode(const std::string &variable1, const std::string &variable2) const
virtual void enableFirstOrderDerivatives(bool yn) override
Tell if derivatives must be computed: for Function inheritance.
ValueRef< DataLik > getLikelihoodNode() const override
void shareParameters(const ParameterList &variableNodes)
Share Parameters, that are DF_parameters.
AbstractPhyloLikelihood(const AbstractPhyloLikelihood &apl)
Node_DF & accessVariableNode(const std::string &name) const
AbstractPhyloLikelihood & operator=(const AbstractPhyloLikelihood &apl)
void setParameters(const ParameterList &parameters) override
double getSecondOrderDerivative(const std::string &variable) const override
std::unordered_map< std::string, ValueRef< DataLik > > firstOrderDerivativeNodes_
For Dataflow computing.
static Node_DF & accessVariableNode(const Parameter &param)
Data flow node representing a parameter.
Definition: Parameter.h:27
Context for dataflow node construction.
Definition: DataFlow.h:527
Base dataflow Node class.
Definition: DataFlow.h:152
virtual void shareParameters(const ParameterList &params)
virtual const ParameterList & getParameters() const=0
virtual const Parameter & parameter(const std::string &name) const=0
virtual ParameterList & getParameters_()=0
virtual void setParametersValues(const ParameterList &parameters)=0
The PhyloLikelihood interface, for phylogenetic likelihood.
virtual std::shared_ptr< LikelihoodCalculation > getLikelihoodCalculation() const =0
Defines the basic types of data flow nodes.
std::shared_ptr< Value< T > > ValueRef
Shared pointer alias for Value<T>.
Definition: DataFlow.h:84
double convert(const bpp::ExtendedFloat &ef)
std::size_t operator()(const std::pair< std::string, std::string > &p) const