bpp-phyl  2.2.0
FrequenciesSet.cpp
Go to the documentation of this file.
1 //
2 // File: FrequenciesSet.cpp
3 // Created by: Bastien Boussau
4 // Julien Dutheil
5 // Created on: Tue Aug 21 2007
6 //
7 
8 /*
9  Copyright or (c) or Copr. Bio++ Development Team, (November 16, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for phylogenetic data analysis.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39  */
40 
41 #include "FrequenciesSet.h"
42 
43 #include <Bpp/Numeric/NumConstants.h>
44 #include <Bpp/Numeric/Prob/Simplex.h>
45 
46 using namespace bpp;
47 
48 #include <cmath>
49 using namespace std;
50 
51 IntervalConstraint FrequenciesSet::FREQUENCE_CONSTRAINT_MILLI(NumConstants::MILLI(), 1 - NumConstants::MILLI(), false, false);
52 IntervalConstraint FrequenciesSet::FREQUENCE_CONSTRAINT_SMALL(NumConstants::SMALL(), 1 - NumConstants::SMALL(), false, false);
53 
54 // ///////////////////////////////////////
55 // AbstractFrequenciesSet
56 
57 
59 {
60  size_t s = stateMap_->getNumberOfModelStates();
61  vector<double> freq(s);
62  double x = 0;
63  for (size_t i = 0; i < s; ++i)
64  {
65  map<int, double>::const_iterator it = frequencies.find(stateMap_->getAlphabetStateAsInt(i));
66  if (it != frequencies.end())
67  freq[i] = it->second;
68  else
69  freq[i] = 0;
70  x += freq[i];
71  }
72  for (size_t i = 0; i < s; ++i)
73  {
74  freq[i] /= x;
75  }
76  setFrequencies(freq);
77 }
78 
79 const std::map<int, double> AbstractFrequenciesSet::getAlphabetStatesFrequencies() const
80 {
81  map<int, double> fmap;
82  for (size_t i = 0; i < stateMap_->getNumberOfModelStates(); ++i) {
83  fmap[stateMap_->getAlphabetStateAsInt(i)] += freq_[i];
84  }
85  return fmap;
86 }
87 
88 // ////////////////////////////
89 // FullFrequenciesSet
90 
91 FullFrequenciesSet::FullFrequenciesSet(StateMap* stateMap, bool allowNullFreqs, unsigned short method, const string& name) :
92  AbstractFrequenciesSet(stateMap, "Full.", name),
93  sFreq_(stateMap->getNumberOfModelStates(), method, allowNullFreqs, "Full.")
94 {
95  vector<double> vd;
96  double r = 1. / static_cast<double>(stateMap->getNumberOfModelStates());
97 
98  for (size_t i = 0; i < stateMap->getNumberOfModelStates(); i++)
99  vd.push_back(r);
100 
101  sFreq_.setFrequencies(vd);
102  addParameters_(sFreq_.getParameters());
103  updateFreq_();
104 }
105 
106 FullFrequenciesSet::FullFrequenciesSet(StateMap* stateMap, const vector<double>& initFreqs, bool allowNullFreqs, unsigned short method, const string& name) :
107  AbstractFrequenciesSet(stateMap, "Full.", name),
108  sFreq_(stateMap->getNumberOfModelStates(), method, allowNullFreqs, "Full.")
109 {
110  sFreq_.setFrequencies(initFreqs);
111  addParameters_(sFreq_.getParameters());
112  updateFreq_();
113 }
114 
115 void FullFrequenciesSet::setFrequencies(const vector<double>& frequencies)
116 {
117 
118  sFreq_.setFrequencies(frequencies);
119  setParametersValues(sFreq_.getParameters());
120 
121  updateFreq_();
122 }
123 
124 void FullFrequenciesSet::setNamespace(const std::string& nameSpace)
125 {
126  sFreq_.setNamespace(nameSpace);
127  AbstractFrequenciesSet::setNamespace(nameSpace);
128 }
129 
130 void FullFrequenciesSet::fireParameterChanged(const ParameterList& parameters)
131 {
132  sFreq_.matchParametersValues(parameters);
133  updateFreq_();
134 }
135 
137 {
138  for (size_t i = 0; i < getAlphabet()->getSize(); i++)
139  getFreq_(i)=sFreq_.prob(i);
140 }
141 
142 // ///////////////////////////////////////////
143 // / FixedFrequenciesSet
144 
145 FixedFrequenciesSet::FixedFrequenciesSet(StateMap* stateMap, const vector<double>& initFreqs, const string& name) throw (Exception) :
146  AbstractFrequenciesSet(stateMap, "Fixed.", name)
147 {
148  if (stateMap->getNumberOfModelStates() != initFreqs.size())
149  throw Exception("FixedFrequenciesSet::constructor. size of init vector does not match the number of states in the model.");
150  setFrequencies(initFreqs);
151 }
152 
153 FixedFrequenciesSet::FixedFrequenciesSet(StateMap* stateMap, const string& name) :
154  AbstractFrequenciesSet(stateMap, "Fixed.", name)
155 {
156  size_t n = stateMap->getNumberOfModelStates();
157  for (size_t i = 0; i < n; ++i)
158  {
159  getFreq_(i) = 1. / static_cast<double>(n);
160  }
161 }
162 
163 void FixedFrequenciesSet::setFrequencies(const vector<double>& frequencies)
164 {
165  if (frequencies.size() != getNumberOfFrequencies())
166  throw DimensionException("FixedFrequenciesSet::setFrequencies", frequencies.size(), getNumberOfFrequencies());
167  double sum = 0.0;
168  for (size_t i = 0; i < frequencies.size(); i++)
169  {
170  sum += frequencies[i];
171  }
172  if (fabs(1. - sum) > 0.00001)
173  throw Exception("FixedFrequenciesSet::setFrequencies. Frequencies sum must equal 1 (sum = " + TextTools::toString(sum) + ").");
174  setFrequencies_(frequencies);
175 }
176 
177 MarkovModulatedFrequenciesSet::MarkovModulatedFrequenciesSet(FrequenciesSet* freqSet, const std::vector<double>& rateFreqs) :
178  AbstractFrequenciesSet(new MarkovModulatedStateMap(freqSet->getStateMap(), static_cast<unsigned int>(rateFreqs.size())), "MarkovModulated.", "MarkovModulated." + freqSet->getName()),
179  freqSet_(freqSet),
180  rateFreqs_(rateFreqs)
181 {
182  freqSet_->setNamespace(std::string("MarkovModulated.") + freqSet_->getNamespace());
183  addParameters_(freqSet_->getParameters());
184  setFrequencies_(VectorTools::kroneckerMult(rateFreqs, freqSet_->getFrequencies()));
185 }
186 
MarkovModulatedFrequenciesSet(FrequenciesSet *freqSet, const std::vector< double > &rateFreqs)
FixedFrequenciesSet(StateMap *stateMap, const std::vector< double > &initFreqs, const std::string &name="Fixed")
Construction with user-defined frequencies on the states of the model.
virtual size_t getNumberOfModelStates() const =0
void setFrequencies_(const std::vector< double > &frequencies)
void setFrequencies(const std::vector< double > &frequencies)
Set the parameters in order to match a given set of frequencies.
STL namespace.
virtual const std::vector< double > getFrequencies() const =0
static IntervalConstraint FREQUENCE_CONSTRAINT_MILLI
void setNamespace(const std::string &nameSpace)
FullFrequenciesSet(StateMap *stateMap, bool allowNullFreqs=false, unsigned short method=1, const std::string &name="Full.")
Construction with uniform frequencies on the states of the alphabet.
Parametrize a set of state frequencies.
const std::map< int, double > getAlphabetStatesFrequencies() const
static IntervalConstraint FREQUENCE_CONSTRAINT_SMALL
const Alphabet * getAlphabet() const
size_t getNumberOfFrequencies() const
double & getFreq_(size_t i)
Simplex sFreq_
Simplex to handle the probabilities and the parameters.
void setFrequencies(const std::vector< double > &frequencies)
Set the parameters in order to match a given set of frequencies.
void setFrequenciesFromAlphabetStatesFrequencies(const std::map< int, double > &frequencies)
Set the Frequencies from the one of the map which keys match with a letter of the Alphabet...
void fireParameterChanged(const ParameterList &parameters)
This class implements a state map for Markov modulated models.
Definition: StateMap.h:185
Map the states of a given alphabet which have a model state.
Definition: StateMap.h:58
Basic implementation of the FrequenciesSet interface.