bpp-phyl3  3.0.0
AbstractDiscreteRatesAcrossSitesTreeLikelihood.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 bpp;
10 
11 // From the STL:
12 #include <iostream>
13 
14 using namespace std;
15 
16 /******************************************************************************/
17 
19  std::shared_ptr<DiscreteDistributionInterface> rDist,
20  bool verbose) :
21  rateDistribution_(rDist)
22 {
24 }
25 
26 /******************************************************************************/
27 
29 {
30  if (!initialized_)
31  throw Exception("AbstractDiscreteRatesAcrossSitesTreeLikelihood::getRateDistributionParameters(). Object is not initialized.");
32  return rateDistribution_->getParameters().getCommonParametersWith(getParameters());
33 }
34 
35 /******************************************************************************/
36 
38 {
39  if (!initialized_)
40  throw Exception("AbstractDiscreteRatesAcrossSitesTreeLikelihood::getDerivableParameters(). Object is not initialized.");
42 }
43 
44 /******************************************************************************/
45 
47 {
48  if (!initialized_)
49  throw Exception("AbstractDiscreteRatesAcrossSitesTreeLikelihood::getNonDerivableParameters(). Object is not initialized.");
52  return tmp;
53 }
54 
55 /******************************************************************************/
56 
58 {
59  size_t nbSites = getNumberOfSites();
60  size_t nbClasses = getNumberOfClasses();
61  VVdouble l(nbSites);
62  for (size_t i = 0; i < nbSites; i++)
63  {
64  l[i].resize(nbClasses);
65  for (size_t j = 0; j < nbClasses; j++)
66  {
67  l[i][j] = getLikelihoodForASiteForARateClass(i, j);
68  }
69  }
70  return l;
71 }
72 
73 /******************************************************************************/
74 
76 {
77  size_t nbClasses = getNumberOfClasses();
78  double l = 0;
79  for (size_t i = 0; i < nbClasses; i++)
80  {
81  l += getLikelihoodForASiteForARateClassForAState(site, i, state) * rateDistribution_->getProbability(i);
82  }
83  return l;
84 }
85 
86 /******************************************************************************/
87 
89 {
90  size_t nbClasses = getNumberOfClasses();
91  double l = 0;
92  for (size_t i = 0; i < nbClasses; i++)
93  {
94  l += getLikelihoodForASiteForARateClassForAState(site, i, state) * rateDistribution_->getProbability(i);
95  }
96  // if(l <= 0.) cerr << "WARNING!!! Negative likelihood." << endl;
97  return log(l);
98 }
99 
100 /******************************************************************************/
101 
103 {
104  size_t nbSites = getNumberOfSites();
105  size_t nbClasses = getNumberOfClasses();
106  VVdouble l(nbSites);
107  for (size_t i = 0; i < nbSites; i++)
108  {
109  l[i] = Vdouble(nbClasses);
110  for (size_t j = 0; j < nbClasses; j++)
111  {
113  }
114  }
115  return l;
116 }
117 
118 /******************************************************************************/
119 
121 {
122  size_t nbSites = getNumberOfSites();
123  size_t nbClasses = getNumberOfClasses();
124  size_t nbStates = getNumberOfStates();
125  VVVdouble l(nbSites);
126  for (size_t i = 0; i < nbSites; i++)
127  {
128  l[i].resize(nbClasses);
129  for (size_t j = 0; j < nbClasses; j++)
130  {
131  l[i][j].resize(nbStates);
132  for (size_t x = 0; x < nbStates; x++)
133  {
134  l[i][j][x] = getLikelihoodForASiteForARateClassForAState(i, j, static_cast<int>(x));
135  }
136  }
137  }
138  return l;
139 }
140 
141 /******************************************************************************/
142 
144 {
145  size_t nbSites = getNumberOfSites();
146  size_t nbClasses = getNumberOfClasses();
147  size_t nbStates = getNumberOfStates();
148  VVVdouble l(nbSites);
149  for (size_t i = 0; i < nbSites; i++)
150  {
151  l[i].resize(nbClasses);
152  for (size_t j = 0; j < nbClasses; j++)
153  {
154  l[i][j].resize(nbStates);
155  for (size_t x = 0; x < nbStates; x++)
156  {
157  l[i][j][x] = getLogLikelihoodForASiteForARateClassForAState(i, j, static_cast<int>(x));
158  }
159  }
160  }
161  return l;
162 }
163 
164 /*******************************************************************************/
165 
167 {
168  size_t nbSites = getNumberOfSites();
169  size_t nbClasses = getNumberOfClasses();
172  for (size_t i = 0; i < nbSites; i++)
173  {
174  for (size_t j = 0; j < nbClasses; j++)
175  {
176  pb[i][j] = pb[i][j] * rateDistribution_->getProbability(j) / l[i];
177  }
178  }
179  return pb;
180 }
181 
182 /******************************************************************************/
183 
185 {
186  size_t nbSites = getNumberOfSites();
187  size_t nbClasses = getNumberOfClasses();
190  Vdouble rates(nbSites, 0.);
191  for (size_t i = 0; i < nbSites; i++)
192  {
193  for (size_t j = 0; j < nbClasses; j++)
194  {
195  rates[i] += (lr[i][j] / l[i]) * rateDistribution_->getProbability(j) * rateDistribution_->getCategory(j);
196  }
197  }
198  return rates;
199 }
200 
201 /******************************************************************************/
202 
204 {
205  size_t nbSites = getNumberOfSites();
207  vector<size_t> classes(nbSites);
208  for (size_t i = 0; i < nbSites; i++)
209  {
210  classes[i] = VectorTools::whichMax<double>(l[i]);
211  }
212  return classes;
213 }
214 
215 /******************************************************************************/
216 
218 {
219  size_t nbSites = getNumberOfSites();
221  Vdouble rates(nbSites);
222  for (size_t i = 0; i < nbSites; i++)
223  {
224  rates[i] = rateDistribution_->getCategory(VectorTools::whichMax<double>(l[i]));
225  }
226  return rates;
227 }
228 
229 /******************************************************************************/
230 
232  VVVdouble& likelihoodArray)
233 {
234  size_t nbSites = likelihoodArray.size();
235  size_t nbClasses = likelihoodArray[0].size();
236  size_t nbStates = likelihoodArray[0][0].size();
237  for (size_t i = 0; i < nbSites; i++)
238  {
239  for (size_t c = 0; c < nbClasses; c++)
240  {
241  for (size_t s = 0; s < nbStates; s++)
242  {
243  likelihoodArray[i][c][s] = 1.;
244  }
245  }
246  }
247 }
248 
249 /******************************************************************************/
250 
252  const VVVdouble& likelihoodArray)
253 {
254  size_t nbSites = likelihoodArray.size();
255  size_t nbClasses = likelihoodArray[0].size();
256  size_t nbStates = likelihoodArray[0][0].size();
257  for (size_t i = 0; i < nbSites; i++)
258  {
259  cout << "Site " << i << ":" << endl;
260  for (size_t c = 0; c < nbClasses; c++)
261  {
262  cout << "Rate class " << c;
263  for (size_t s = 0; s < nbStates; s++)
264  {
265  cout << "\t" << likelihoodArray[i][c][s];
266  }
267  cout << endl;
268  }
269  cout << endl;
270  }
271 }
272 
273 /******************************************************************************/
274 
276 {
277  VVVdouble p3 = getTransitionProbabilitiesPerRateClass(nodeId, siteIndex);
278  VVdouble p2;
279  Vdouble probs = rateDistribution_->getProbabilities();
280  p2.resize(getNumberOfStates());
281  for (size_t i = 0; i < p2.size(); i++)
282  {
283  p2[i].resize(getNumberOfStates());
284  for (size_t j = 0; j < p2.size(); j++)
285  {
286  for (size_t k = 0; k < getNumberOfClasses(); k++)
287  {
288  p2[i][j] += p3[k][i][j] * probs[k];
289  }
290  }
291  }
292  return p2;
293 }
294 
295 /******************************************************************************/
Vdouble getPosteriorRatePerSite() const
Get the posterior rate, i.e. averaged over all classes and weighted with posterior probabilities,...
ParameterList getRateDistributionParameters() const
Get the parameters associated to the rate distribution.
Vdouble getRateWithMaxPostProbPerSite() const
Get the posterior rate (the one with maximum posterior probability) for each site.
VVdouble getLogLikelihoodPerSitePerRateClass() const
Get the logarithm of the likelihood for each site and each rate class.
VVdouble getTransitionProbabilities(int nodeId, size_t siteIndex) const
Retrieves all Pij(t) for a particular branch, defined by the upper node and site.
double getLikelihoodForASiteForAState(size_t site, int state) const
Get the likelihood for a site and for a state.
AbstractDiscreteRatesAcrossSitesTreeLikelihood(std::shared_ptr< DiscreteDistributionInterface > rDist, bool verbose=true)
ParameterList getNonDerivableParameters() const
All non derivable parameters.
VVdouble getPosteriorProbabilitiesPerRate() const
Get the posterior probability for each site of belonging to a particular rate class.
VVVdouble getLogLikelihoodPerSitePerRateClassPerState() const
Get the logarithm of the likelihood for each site and each rate class and each state.
double getLogLikelihoodForASiteForAState(size_t site, int state) const
Get the logarithm of the likelihood for a site and for a state.
static void displayLikelihoodArray(const VVVdouble &likelihoodArray)
Print the likelihood array to terminal (debugging tool).
static void resetLikelihoodArray(VVVdouble &likelihoodArray)
Set all conditional likelihoods to 1.
VVdouble getLikelihoodPerSitePerRateClass() const
Get the likelihood for each site and each rate class.
std::vector< size_t > getRateClassWithMaxPostProbPerSite() const
Get the posterior rate class (the one with maximum posterior probability) for each site.
VVVdouble getLikelihoodPerSitePerRateClassPerState() const
Get the likelihood for each site and each rate class and each state.
const ParameterList & getParameters() const override
void enableDerivatives(bool yn)
Tell if derivatives must be computed.
Vdouble getLikelihoodPerSite() const
Get the likelihood for each site.
size_t getNumberOfSites() const
Get the number of sites in the dataset.
virtual double getLogLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const =0
Get the logarithm of the likelihood for a site knowing its rate class.
virtual double getLikelihoodForASiteForARateClass(size_t site, size_t rateClass) const =0
Get the likelihood for a site knowing its rate class.
virtual VVVdouble getTransitionProbabilitiesPerRateClass(int nodeId, size_t siteIndex) const =0
Retrieves all Pij(t) for a particular branch, defined by the upper node.
virtual double getLogLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const =0
Get the logarithm of the likelihood for a site knowing its rate class and its ancestral state.
virtual double getLikelihoodForASiteForARateClassForAState(size_t site, size_t rateClass, int state) const =0
Get the likelihood for a site knowing its rate class and its ancestral state.
virtual void addParameters(const ParameterList &params)
virtual size_t getNumberOfStates() const =0
virtual ParameterList getSubstitutionModelParameters() const =0
Get the parameters associated to substitution model(s).
virtual ParameterList getBranchLengthsParameters() const =0
Get the branch lengths parameters.
Defines the basic types of data flow nodes.
double log(const ExtendedFloat &ef)
std::vector< double > Vdouble
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble