16 ConfiguredModel::ConfiguredModel(
19 shared_ptr<BranchModelInterface>&& model) :
45 const auto* derived =
dynamic_cast<const Self*
>(&other);
46 if (derived ==
nullptr)
52 const auto& thisModel = *
model_;
53 const auto& otherModel = *derived->model_;
54 return typeid (thisModel) ==
typeid (otherModel);
60 const auto& bppModel = *
model_;
61 return typeid (bppModel).hash_code ();
66 auto m = ConfiguredParametrizable::createConfigured<Target, Self>(c, std::move (deps), std::unique_ptr<Target>{
dynamic_cast<Target*
>(
model_->clone ())});
75 :
Value<
Eigen::RowVectorXd>(std::move (deps)), targetDimension_ (dim)
80 using namespace numeric;
87 return dynamic_cast<const Self*
>(&other) !=
nullptr;
94 auto& model =
static_cast<Dep&
>(*modelDep);
96 auto buildFWithNewModel = [
this, &c, &subNode](
NodeRef&& newModel) {
97 return ConfiguredParametrizable::createRowVector<Dep, Self>(c, {std::move (newModel), subNode},
targetDimension_);
100 NodeRefVec derivativeSumDeps = ConfiguredParametrizable::generateDerivativeSumDepsForComputations<Dep, T>(
107 return ConfiguredParametrizable::createRowVector<Dep, Self>(c, std::move (deps),
targetDimension_);
116 auto nMod = accessValueConstCast<size_t>(*this->
dependency(1));
117 freqsFromModel = &mixmodel->nModel(nMod).getFrequencies();
125 throw Exception(
"EquilibriumFrequenciesFromModel::compute only possible for Transition Models.");
129 r = Eigen::Map<const T>(freqsFromModel->data (),
static_cast<Eigen::Index
>(freqsFromModel->size ()));
141 using namespace numeric;
142 const auto nDeriv = accessValueConstCast<size_t>(*this->
dependency (2));
150 return dynamic_cast<const Self*
>(&other) !=
nullptr;
161 const auto nDeriv = accessValueConstCast<size_t>(*derivNode);
164 auto& model =
static_cast<Dep&
>(*modelDep);
165 auto buildFWithNewModel = [
this, &c, &brlenDep, &derivNode, &subNode](
NodeRef&& newModel) {
166 return ConfiguredParametrizable::createMatrix<Dep, Self>(c, {std::move (newModel), brlenDep, derivNode, subNode},
targetDimension_);
168 NodeRefVec derivativeSumDeps = ConfiguredParametrizable::generateDerivativeSumDepsForComputations<Dep, T>(
171 auto dbrlen_dn = brlenDep->derive (c, node);
177 ConfiguredParametrizable::createMatrix<Dep, TransitionMatrixFromModel>(c, {modelDep, brlenDep, nDerivp, subNode},
targetDimension_);
178 derivativeSumDeps.emplace_back (
CWiseMul<
T, std::tuple<double, T>>::create (
186 return ConfiguredParametrizable::createMatrix<Dep, Self>(c, std::move (deps),
targetDimension_);
192 const auto nDeriv = accessValueConstCast<size_t>(*this->
dependency (2));
196 const auto* model1 = accessValueConstCast<const BranchModelInterface*>(*this->
dependency (0));
201 std::cerr <<
"=== TransitionMatrixFromModel::compute === " <<
this << std::endl;
202 std::cerr <<
"brlen= " << brlen << std::endl;
203 std::cerr <<
"nDeriv=" << nDeriv << std::endl;
204 std::cerr <<
"model= " << model1 <<
" : " << model1->
getName() << std::endl;
205 model1->getParameters().printParameters(std::cerr);
206 std::cerr <<
"=== end TransitionMatrixFromModel::compute === " <<
this << std::endl << std::endl;
211 auto nMod = accessValueConstCast<size_t>(*this->
dependency (3));
233 throw Exception(
"TransitionMatrixFromModel::compute only possible for Transition Models.");
261 using namespace numeric;
262 const auto nDeriv = accessValueConstCast<size_t>(*this->
dependency (2));
270 return dynamic_cast<const Self*
>(&other) !=
nullptr;
277 checkNthDependencyIs<ConfiguredModel>(
typeid (
Self), deps, 0);
278 checkNthDependencyIs<ConfiguredParameter>(
typeid (
Self), deps, 1);
281 return cachedAs<TransitionFunctionFromModel>(c, std::make_shared<TransitionFunctionFromModel>(std::move (deps), dim));
288 throw Exception(
"TransitionFunctionFromModel::derive : Jacobian not implemented. Ask developers.");
293 const auto nDeriv = accessValueConstCast<size_t>(*this->
dependency (2));
296 auto& model =
static_cast<Dep&
>(*modelDep);
297 auto buildFWithNewModel = [
this, &c, &brlenDep, &derivNode](
NodeRef&& newModel) {
301 NodeRefVec derivativeSumDeps = ConfiguredParametrizable::generateDerivativeSumDepsForComputations<Dep, T>(c, model, node,
targetDimension_, buildFWithNewModel);
304 auto dbrlen_dn = brlenDep->derive (c, node);
309 derivativeSumDeps.emplace_back (
CWiseMul<
T, std::tuple<double, T>>::
create (
327 const auto* model = accessValueConstCast<const BranchModelInterface*>(*this->
dependency(0));
328 const auto nDeriv = accessValueConstCast<size_t>(*this->
dependency (2));
335 r = [model, brlen, nDeriv, dimin, dimout](
const VectorLik& values)
340 return numeric::convert(model->Lik_t(numeric::convert<Eigen::Dynamic, 1>(values, dimin), brlen), dimout);
342 return numeric::convert(model->dLik_dt(numeric::convert<Eigen::Dynamic, 1>(values, dimin), brlen), dimout);
344 return numeric::convert(model->d2Lik_dt2(numeric::convert<Eigen::Dynamic, 1>(values, dimin), brlen), dimout);
358 :
Value<
Eigen::RowVectorXd>(std::move (deps)), nbClass_ (dim) {}
363 using namespace numeric;
370 return dynamic_cast<const Self*
>(&other) !=
nullptr;
378 auto buildPWithNewModel = [
this, &c](
NodeRef&& newModel) {
379 return ConfiguredParametrizable::createRowVector<Dep, Self>(c, {std::move (newModel)},
nbClass_);
382 NodeRefVec derivativeSumDeps = ConfiguredParametrizable::generateDerivativeSumDepsForComputations<Dep, T >(
383 c, model, node,
nbClass_, buildPWithNewModel);
389 return ConfiguredParametrizable::createRowVector<Dep, Self>(c, std::move (deps),
nbClass_);
397 r = Eigen::Map<const T>(probasFromModel.data(),
static_cast<Eigen::Index
>(probasFromModel.size ()));
404 checkNthDependencyIs<ConfiguredModel>(
typeid (
Self), deps, 0);
406 auto& deps0 = *deps[0];
407 const auto mixmodel = dynamic_pointer_cast<const MixedTransitionModelInterface>(model.targetValue());
411 size_t nbCat = mixmodel->getNumberOfModels();
412 return cachedAs<ProbabilitiesFromMixedModel>(c, make_shared<ProbabilitiesFromMixedModel>(std::move(deps),
RowVectorDimension(Eigen::Index(nbCat))));
421 :
Value<double>(std::move (deps)), nCat_ (nCat) {}
426 using namespace numeric;
433 const auto* derived =
dynamic_cast<const Self*
>(&other);
434 return derived !=
nullptr &&
nCat_ == derived->nCat_;
441 checkNthDependencyIs<ConfiguredModel>(
typeid (
Self), deps, 0);
443 const auto& deps0 = *deps[0];
444 const auto mixmodel = dynamic_pointer_cast<const MixedTransitionModelInterface>(model.targetValue());
448 return cachedAs<ProbabilityFromMixedModel>(c, std::make_shared<ProbabilityFromMixedModel>(std::move (deps), nCat));
455 auto& model =
static_cast<Dep&
>(*modelDep);
456 auto buildPWithNewModel = [
this, &c](
NodeRef&& newModel) {
457 return this->
create (c, {std::move (newModel)},
nCat_);
460 NodeRefVec derivativeSumDeps = ConfiguredParametrizable::generateDerivativeSumDepsForComputations<Dep, T >(
461 c, model, node, 1, buildPWithNewModel);
virtual void shareParameter_(const std::shared_ptr< Parameter > ¶meter)
Interface for all Branch models.
virtual std::string getName() const =0
Get the name of the model.
Context for dataflow node construction.
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
Dimension< T > targetDimension_
EquilibriumFrequenciesFromModel(NodeRefVec &&deps, const Dimension< T > &dim)
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.
void compute() final
Computation implementation.
Interface for Transition models, defined as a mixture of "simple" transition models.
virtual const std::vector< double > & getProbabilities() const =0
Base dataflow Node class.
const NodeRef & dependency(std::size_t i) const noexcept
std::size_t nbDependencies() const noexcept
Number of dependencies (ie nodes we depend on)
const NodeRefVec & dependencies() const noexcept
static std::shared_ptr< Self > create(Context &c, Args &&... args)
Build a new NumericConstant node with T(args...) value.
ProbabilitiesFromMixedModel(NodeRefVec &&deps, const Dimension< T > &dim)
std::string debugInfo() const final
Node debug info (default = ""): user defined detailed info for DF graph debug.
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps)
void compute() final
Computation implementation.
bool compareAdditionalArguments(const Node_DF &other) const final
Compare node-specific configuration to another.
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
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.
void compute() final
Computation implementation.
bool compareAdditionalArguments(const Node_DF &other) const final
Compare node-specific configuration to another.
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps, size_t nCat)
ProbabilityFromMixedModel(NodeRefVec &&deps, size_t nCat)
TransitionFunctionFromModel(NodeRefVec &&deps, const Dimension< T > &dim)
Build a new TransitionMatrixFromModel node with the given output dimensions.
Dimension< T > targetDimension_
void compute() final
Computation implementation.
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.
static std::shared_ptr< Self > create(Context &c, 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).
bpp::TransitionFunction T
NodeRef recreate(Context &c, NodeRefVec &&deps) final
Recreate the node with different dependencies.
TransitionMatrixFromModel(NodeRefVec &&deps, const Dimension< T > &dim)
Build a new TransitionMatrixFromModel node with the given output dimensions.
Dimension< T > targetDimension_
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.
void compute() final
Computation implementation.
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.
Interface for all transition models.
virtual const Vdouble & getFrequencies() const =0
Abstract Node storing a value of type T.
const Eigen::RowVectorXd & accessValueConst() const noexcept
Raw value access (const).
Eigen::RowVectorXd & accessValueMutable() noexcept
std::string toString(T t)
std::string debug(const T &t, typename std::enable_if< std::is_arithmetic< T >::value >::type *=0)
R convert(const F &from, const Dimension< R > &)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
NumericConstant< char > NodeX('X')
void checkDependenciesNotNull(const std::type_info &contextNodeType, const NodeRefVec &deps)
Checks that all dependencies are not null, throws if not.
std::string to_string(const NoDimension &)
std::vector< NodeRef > NodeRefVec
Alias for a dependency vector (of NodeRef).
template void copyBppToEigen(const std::vector< ExtendedFloat > &bppVector, Eigen::RowVectorXd &eigenVector)
void checkDependencyVectorSize(const std::type_info &contextNodeType, const NodeRefVec &deps, std::size_t expectedSize)
void failureDependencyTypeMismatch(const std::type_info &contextNodeType, std::size_t depIndex, const std::type_info &expectedType, const std::type_info &givenNodeType)
std::shared_ptr< Node_DF > NodeRef
std::function< VectorLik(const VectorLik &)> TransitionFunction
Store a dimension for type T.