bpp-phyl  2.2.0
AbstractDiscreteRatesAcrossSitesTreeLikelihood.cpp
Go to the documentation of this file.
1 //
2 // File: AbstractDiscreteRatesAcrossSitesTreeLikelihood.cpp
3 // Created by: Julien Dutheil
4 // Created on: Wue Jun 15 09:42 2005
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for phylogenetic data analysis.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
41 
42 #include <Bpp/Numeric/VectorTools.h>
43 
44 using namespace bpp;
45 
46 // From the STL:
47 #include <iostream>
48 
49 using namespace std;
50 
51 /******************************************************************************/
52 
54  DiscreteDistribution* rDist,
55  bool verbose)
56 throw (Exception) :
57  rateDistribution_(rDist)
58 {
60 }
61 
62 /******************************************************************************/
63 
65 {
66  if (!initialized_)
67  throw Exception("AbstractDiscreteRatesAcrossSitesTreeLikelihood::getRateDistributionParameters(). Object is not initialized.");
68  return rateDistribution_->getParameters().getCommonParametersWith(getParameters());
69 }
70 
71 /******************************************************************************/
72 
74 {
75  if (!initialized_)
76  throw Exception("AbstractDiscreteRatesAcrossSitesTreeLikelihood::getDerivableParameters(). Object is not initialized.");
77  return getBranchLengthsParameters();
78 }
79 
80 /******************************************************************************/
81 
83 {
84  if (!initialized_)
85  throw Exception("AbstractDiscreteRatesAcrossSitesTreeLikelihood::getNonDerivableParameters(). Object is not initialized.");
86  ParameterList tmp = getSubstitutionModelParameters();
87  tmp.addParameters(getRateDistributionParameters());
88  return tmp;
89 }
90 
91 /******************************************************************************/
92 
94 {
95  size_t nbSites = getNumberOfSites();
96  size_t nbClasses = getNumberOfClasses();
97  VVdouble l(nbSites);
98  for (size_t i = 0; i < nbSites; i++)
99  {
100  l[i].resize(nbClasses);
101  for (size_t j = 0; j < nbClasses; j++)
102  {
103  l[i][j] = getLikelihoodForASiteForARateClass(i, j);
104  }
105  }
106  return l;
107 }
108 
109 /******************************************************************************/
110 
112 {
113  size_t nbClasses = getNumberOfClasses();
114  double l = 0;
115  for (size_t i = 0; i < nbClasses; i++)
116  {
117  l += getLikelihoodForASiteForARateClassForAState(site, i, state) * rateDistribution_->getProbability(i);
118  }
119  return l;
120 }
121 
122 /******************************************************************************/
123 
125 {
126  size_t nbClasses = getNumberOfClasses();
127  double l = 0;
128  for (size_t i = 0; i < nbClasses; i++)
129  {
130  l += getLikelihoodForASiteForARateClassForAState(site, i, state) * rateDistribution_->getProbability(i);
131  }
132  // if(l <= 0.) cerr << "WARNING!!! Negative likelihood." << endl;
133  return log(l);
134 }
135 
136 /******************************************************************************/
137 
139 {
140  size_t nbSites = getNumberOfSites();
141  size_t nbClasses = getNumberOfClasses();
142  VVdouble l(nbSites);
143  for (size_t i = 0; i < nbSites; i++)
144  {
145  l[i] = Vdouble(nbClasses);
146  for (size_t j = 0; j < nbClasses; j++)
147  {
148  l[i][j] = getLogLikelihoodForASiteForARateClass(i, j);
149  }
150  }
151  return l;
152 }
153 
154 /******************************************************************************/
155 
157 {
158  size_t nbSites = getNumberOfSites();
159  size_t nbClasses = getNumberOfClasses();
160  size_t nbStates = getNumberOfStates();
161  VVVdouble l(nbSites);
162  for (size_t i = 0; i < nbSites; i++)
163  {
164  l[i].resize(nbClasses);
165  for (size_t j = 0; j < nbClasses; j++)
166  {
167  l[i][j].resize(nbStates);
168  for (size_t x = 0; x < nbStates; x++)
169  {
170  l[i][j][x] = getLikelihoodForASiteForARateClassForAState(i, j, static_cast<int>(x));
171  }
172  }
173  }
174  return l;
175 }
176 
177 /******************************************************************************/
178 
180 {
181  size_t nbSites = getNumberOfSites();
182  size_t nbClasses = getNumberOfClasses();
183  size_t nbStates = getNumberOfStates();
184  VVVdouble l(nbSites);
185  for (size_t i = 0; i < nbSites; i++)
186  {
187  l[i].resize(nbClasses);
188  for (size_t j = 0; j < nbClasses; j++)
189  {
190  l[i][j].resize(nbStates);
191  for (size_t x = 0; x < nbStates; x++)
192  {
193  l[i][j][x] = getLogLikelihoodForASiteForARateClassForAState(i, j, static_cast<int>(x));
194  }
195  }
196  }
197  return l;
198 }
199 
200 /*******************************************************************************/
201 
203 {
204  size_t nbSites = getNumberOfSites();
205  size_t nbClasses = getNumberOfClasses();
206  VVdouble pb = getLikelihoodForEachSiteForEachRateClass();
207  Vdouble l = getLikelihoodForEachSite();
208  for (size_t i = 0; i < nbSites; i++)
209  {
210  for (size_t j = 0; j < nbClasses; j++)
211  {
212  pb[i][j] = pb[i][j] * rateDistribution_->getProbability(j) / l[i];
213  }
214  }
215  return pb;
216 }
217 
218 /******************************************************************************/
219 
221 {
222  size_t nbSites = getNumberOfSites();
223  size_t nbClasses = getNumberOfClasses();
224  VVdouble lr = getLikelihoodForEachSiteForEachRateClass();
225  Vdouble l = getLikelihoodForEachSite();
226  Vdouble rates(nbSites, 0.);
227  for (size_t i = 0; i < nbSites; i++)
228  {
229  for (size_t j = 0; j < nbClasses; j++)
230  {
231  rates[i] += (lr[i][j] / l[i]) * rateDistribution_->getProbability(j) * rateDistribution_->getCategory(j);
232  }
233  }
234  return rates;
235 }
236 
237 /******************************************************************************/
238 
240 {
241  size_t nbSites = getNumberOfSites();
242  VVdouble l = getLikelihoodForEachSiteForEachRateClass();
243  vector<size_t> classes(nbSites);
244  for (size_t i = 0; i < nbSites; i++)
245  {
246  classes[i] = VectorTools::whichMax<double>(l[i]);
247  }
248  return classes;
249 }
250 
251 /******************************************************************************/
252 
254 {
255  size_t nbSites = getNumberOfSites();
256  VVdouble l = getLikelihoodForEachSiteForEachRateClass();
257  Vdouble rates(nbSites);
258  for (size_t i = 0; i < nbSites; i++)
259  {
260  rates[i] = rateDistribution_->getCategory(VectorTools::whichMax<double>(l[i]));
261  }
262  return rates;
263 }
264 
265 /******************************************************************************/
266 
268  VVVdouble& likelihoodArray)
269 {
270  size_t nbSites = likelihoodArray.size();
271  size_t nbClasses = likelihoodArray[0].size();
272  size_t nbStates = likelihoodArray[0][0].size();
273  for (size_t i = 0; i < nbSites; i++)
274  {
275  for (size_t c = 0; c < nbClasses; c++)
276  {
277  for (size_t s = 0; s < nbStates; s++)
278  {
279  likelihoodArray[i][c][s] = 1.;
280  }
281  }
282  }
283 }
284 
285 /******************************************************************************/
286 
288  const VVVdouble& likelihoodArray)
289 {
290  size_t nbSites = likelihoodArray.size();
291  size_t nbClasses = likelihoodArray[0].size();
292  size_t nbStates = likelihoodArray[0][0].size();
293  for (size_t i = 0; i < nbSites; i++)
294  {
295  cout << "Site " << i << ":" << endl;
296  for (size_t c = 0; c < nbClasses; c++)
297  {
298  cout << "Rate class " << c;
299  for (size_t s = 0; s < nbStates; s++)
300  {
301  cout << "\t" << likelihoodArray[i][c][s];
302  }
303  cout << endl;
304  }
305  cout << endl;
306  }
307 }
308 
309 /******************************************************************************/
310 
312 {
313  VVVdouble p3 = getTransitionProbabilitiesPerRateClass(nodeId, siteIndex);
314  VVdouble p2;
315  Vdouble probs = rateDistribution_->getProbabilities();
316  p2.resize(getNumberOfStates());
317  for (size_t i = 0; i < p2.size(); i++)
318  {
319  p2[i].resize(getNumberOfStates());
320  for (size_t j = 0; j < p2.size(); j++)
321  {
322  for (size_t k = 0; k < getNumberOfClasses(); k++)
323  {
324  p2[i][j] += p3[k][i][j] * probs[k];
325  }
326  }
327  }
328  return p2;
329 }
330 
331 /******************************************************************************/
332 
Vdouble getRateWithMaxPostProbOfEachSite() const
Get the posterior rate (the one with maximum posterior probability) for each site.
double getLogLikelihoodForASiteForAState(size_t site, int state) const
Get the logarithm of the likelihood for a site and for a state.
void enableDerivatives(bool yn)
Tell if derivatives must be computed.
STL namespace.
ParameterList getRateDistributionParameters() const
Get the parameters associated to the rate distirbution.
double getLikelihoodForASiteForAState(size_t site, int state) const
Get the likelihood for a site and for a state.
VVdouble getTransitionProbabilities(int nodeId, size_t siteIndex) const
Retrieves all Pij(t) for a particular branch, defined by the upper node and site. ...
static void displayLikelihoodArray(const VVVdouble &likelihoodArray)
Print the likelihood array to terminal (debugging tool).
VVdouble getPosteriorProbabilitiesOfEachRate() const
Get the posterior probability for each site of belonging to a particular rate class.
Vdouble getPosteriorRateOfEachSite() const
Get the posterior rate, i.e. averaged over all classes and weighted with posterior probabilities...
VVVdouble getLogLikelihoodForEachSiteForEachRateClassForEachState() const
Get the logarithm of the likelihood for each site and each rate class and each state.
std::vector< size_t > getRateClassWithMaxPostProbOfEachSite() const
Get the posterior rate class (the one with maximum posterior probability) for each site...
static void resetLikelihoodArray(VVVdouble &likelihoodArray)
Set all conditional likelihoods to 1.
ParameterList getNonDerivableParameters() const
All non derivable parameters.
VVVdouble getLikelihoodForEachSiteForEachRateClassForEachState() const
Get the likelihood for each site and each rate class and each state.
AbstractDiscreteRatesAcrossSitesTreeLikelihood(DiscreteDistribution *rDist, bool verbose=true)
VVdouble getLogLikelihoodForEachSiteForEachRateClass() const
Get the logarithm of the likelihood for each site and each rate class.
VVdouble getLikelihoodForEachSiteForEachRateClass() const
Get the likelihood for each site and each rate class.