bpp-phyl3  3.0.0
MixtureProcessPhyloLikelihood.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 
8 
9 using namespace std;
10 using namespace bpp;
11 using namespace numeric;
12 
13 /******************************************************************************/
14 
15 MixtureProcessPhyloLikelihood::MixtureProcessPhyloLikelihood(
16  std::shared_ptr<const AlignmentDataInterface> data,
17  std::shared_ptr<MixtureSequenceEvolution> processSeqEvol,
18  std::shared_ptr<CollectionNodes> collNodes,
19  size_t nSeqEvol,
20  size_t nData) :
21  AbstractPhyloLikelihood(collNodes->context()),
22  AbstractAlignedPhyloLikelihood(collNodes->context(), data->getNumberOfSites()),
24  collNodes->context(),
25  data->getNumberOfSites(),
26  (processSeqEvol->getSubstitutionProcessNumbers().size() != 0)
27  ? processSeqEvol->substitutionProcess(processSeqEvol->getSubstitutionProcessNumbers()[0]).getNumberOfStates()
28  : 0,
29  nData),
30  AbstractSequencePhyloLikelihood(collNodes->context(), processSeqEvol, nData),
32  MultiProcessSequencePhyloLikelihood(data, processSeqEvol, collNodes, nSeqEvol, nData),
33  mSeqEvol_(processSeqEvol),
34  likCal_(make_shared<AlignedLikelihoodCalculation>(collNodes->context()))
35 {
36  if (vLikCal_.size() == 0)
37  throw Exception("MixtureProcessPhyloLikelihood::MixtureProcessPhyloLikelihood : empty singleprocesslikelihoods set.");
38 
39  auto& simplex = mSeqEvol_->simplex();
40 
41  // parameters of the simplex
42  const auto& param = simplex.getParameters();
43  ParameterList paramList;
44 
45  for (size_t i = 0; i < param.size(); i++)
46  {
47  paramList.shareParameter(ConfiguredParameter::create(context(), param[i]));
48  }
49 
50  shareParameters_(paramList);
51 
52  // make Simplex DF & Frequencies from it
53  simplex_ = ConfiguredParametrizable::createConfigured<Simplex, ConfiguredSimplex>(context(), simplex, paramList, "");
54 
55  // for derivates
56  auto deltaNode = NumericMutable<double>::create(context(), 0.001);
58 
59  simplex_->config.delta = deltaNode;
60  simplex_->config.type = config;
61 
62  auto fsf = ConfiguredParametrizable::createRowVector<ConfiguredSimplex, FrequenciesFromSimplex, Eigen::RowVectorXd>(context(), {simplex_}, RowVectorDimension (Eigen::Index(simplex.dimension())));
63 
64  // get RowVectorXd for each single Calculation
65  std::vector<std::shared_ptr<Node_DF>> vSL;
66 
67  for (auto& lik : vLikCal_)
68  {
69  vSL.push_back(lik->getSiteLikelihoods(true));
70  }
71 
72  // put probabilities of the simplex
73  vSL.push_back(fsf);
74 
75  auto single0 = vLikCal_[0];
76  auto nbSite = single0->getNumberOfDistinctSites();
77 
78  auto sL = CWiseMean<RowLik, ReductionOf<RowLik>, Eigen::RowVectorXd>::create(context(), std::move(vSL), RowVectorDimension ((int)nbSite));
79 
80  likCal_->setSiteLikelihoods(sL, true);
81 
82  // likelihoods per site
83  likCal_->setSiteLikelihoods(single0->expandVector(sL), false);
84 
85  auto su = SumOfLogarithms<RowLik>::create (context(), {sL, single0->getRootWeights()}, RowVectorDimension ((int)nbSite));
86 
87  likCal_->setLikelihoodNode(su);
88 }
89 
90 
91 /******************************************************************************/
92 
94 {
95  size_t nbProcess = getNumberOfSubstitutionProcess();
96 
98  auto l = getLikelihoodPerSite();
99 
100  const auto& freq = simplex_->targetValue()->getFrequencies();
101 
102  for (size_t i = 0; i < nbSites_; ++i)
103  {
104  for (size_t j = 0; j < nbProcess; ++j)
105  {
106  pb[i][j] = pb[i][j] * freq[j] / convert(l[i]);
107  }
108  }
109  return pb;
110 }
VDataLik getLikelihoodPerSite() const
Get the likelihood for each site.
virtual void shareParameters_(const ParameterList &parameters)
const Context & context() const override
static std::shared_ptr< Self > create(Context &c, NodeRefVec &&deps, const Parameter &param)
Build a new ConfiguredParameter node.
Definition: Parameter.h:36
std::shared_ptr< AlignedLikelihoodCalculation > likCal_
std::shared_ptr< ConfiguredSimplex > simplex_
std::shared_ptr< MixtureSequenceEvolution > mSeqEvol_
to avoid the dynamic casts
VVdouble getPosteriorProbabilitiesPerSitePerProcess() const override
return the posterior probabilities of subprocess on each site.
Partial implementation of the Likelihood interface for multiple processes.
std::vector< std::shared_ptr< LikelihoodCalculationSingleProcess > > vLikCal_
size_t getNumberOfSubstitutionProcess() const
Return the number of process used for computation.
static std::shared_ptr< Self > create(Context &, Args &&... args)
Build a new NumericMutable node with T(args...) value.
virtual void shareParameter(const std::shared_ptr< Parameter > &param)
static ValueRef< DataLik > create(Context &c, NodeRefVec &&deps, const Dimension< F > &mDim)
Build a new SumOfLogarithms node with the given input matrix dimensions.
Defines the basic types of data flow nodes.
double convert(const bpp::ExtendedFloat &ef)
std::vector< Vdouble > VVdouble