~ubuntu-branches/ubuntu/saucy/libbpp-phyl/saucy

« back to all changes in this revision

Viewing changes to src/Bpp/Phyl/Model/BinarySubstitutionModel.h

  • Committer: Package Import Robot
  • Author(s): Julien Dutheil
  • Date: 2013-02-09 16:00:00 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130209160000-5v65ba68z8032nzj
Tags: 2.0.3-1
* Reorganized model hierarchy
* New pairwise models
* Several bugs fixed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// File: BinarySubstitutionModel.h
 
3
// Created by: Laurent Gueguen
 
4
// Created on: 2009
 
5
//
 
6
 
 
7
/*
 
8
   Copyright or � or Copr. CNRS, (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
 
 
40
#ifndef _BINARYSUBSTITUTIONMODEL_H_
 
41
#define _BINARYSUBSTITUTIONMODEL_H_
 
42
 
 
43
#include "AbstractSubstitutionModel.h"
 
44
#include <Bpp/Seq/Alphabet/BinaryAlphabet.h>
 
45
 
 
46
namespace bpp
 
47
{
 
48
/**
 
49
 * @brief The Model on two states
 
50
 *
 
51
 * \f[
 
52
 * Q = r.\begin{pmatrix}
 
53
 * -\kappa & \kappa  \\
 
54
 * 1 & -1  \\
 
55
 * \end{pmatrix}
 
56
 * \f]
 
57
 * \f[
 
58
 * \pi = diag\left(\frac{1}{\kappa+1}, \frac{\kappa}{\kappa+1}\right)
 
59
 * \f]
 
60
 * Normalization: \f$r\f$ is set so that \f$\sum_i Q_{i,i}\pi_i = -1\f$:
 
61
 * \f[
 
62
 * Q = \begin{pmatrix}
 
63
 * -\frac{\kappa + 1}2 & \frac{\kappa + 1}2 \\
 
64
 * \frac{\kappa+1}{2\kappa} & -\frac{\kappa+1}{2\kappa}\\
 
65
 * \end{pmatrix}
 
66
 * \f]
 
67
 *
 
68
 * The eigen values are \f$\left(0, - \frac{(\kappa+1)^2}{2\kappa}\right)\f$,
 
69
 * and IF \f$\kappa \neq 1\f$, the left eigen vectors are, by row:
 
70
 * \f[
 
71
 * U = \begin{pmatrix}
 
72
 *  \frac{1}{1+\kappa} &  \frac{\kappa}{1+\kappa} \\
 
73
 *  \frac{\kappa-1}{\kappa+1} & -\frac{\kappa-1}{\kappa+1} \\
 
74
 * \end{pmatrix}
 
75
 * \f]
 
76
 * and the right eigen vectors are by column:
 
77
 * \f[
 
78
 * U^{-1} = \begin{pmatrix}
 
79
 *  1 &  \frac \kappa{\kappa-1} \\
 
80
 *  1 &  - \frac 1{\kappa-1} \\
 
81
 * \end{pmatrix}
 
82
 * \f]
 
83
 *
 
84
 * The probabilities of changes are computed analytically using the formulas, with \f$\lambda= \frac{(\kappa+1)^2}{2\kappa}\f$ :
 
85
 * \f[
 
86
 * P_{i,j}(t) = \begin{pmatrix}
 
87
 * \frac{1}{\kappa+1} + \frac{\kappa}{\kappa+1}e^{-\lambda t} & \frac{\kappa}{\kappa+1} - \frac{\kappa}{\kappa+1}e^{-\lambda t} \\
 
88
 * \frac{1}{\kappa+1} - \frac{1}{\kappa+1}e^{-\lambda t} & \frac{\kappa}{\kappa+1} + \frac{1}{\kappa+1}e^{-\lambda t} \\
 
89
 * \end{pmatrix}
 
90
 * \f]
 
91
 *
 
92
 * \f[
 
93
 * \frac{\partial P_{i,j}(t)}{\partial t} = \begin{pmatrix}
 
94
 * -\frac {\kappa+1} 2 e^{-\lambda t}  & \frac {\kappa+1} 2 e^{-\lambda t} \\
 
95
 * \frac {\kappa+1} {2\kappa} e^{-\lambda t}  & - \frac {\kappa+1} {2\kappa} e^{-\lambda t} \ \
 
96
 * \end{pmatrix}
 
97
 * \f]
 
98
 * \f{multline*}
 
99
 * \frac{\partial^2 P_{i,j}(t)}{\partial t^2} = \\
 
100
 * \begin{pmatrix}
 
101
 * \frac {\lambda (\kappa+1)} 2 e^{-\lambda t}  & -\ frac {\lambda (\kappa+1)} 2 e^{-\lambda t} \\
 
102
 * \frac {\lambda (\kappa+1)} {2\kappa} e^{-\lambda t}  & - \frac {\lambda (\kappa+1)} {2\kappa} e^{-\lambda t} \ \
 
103
 * \end{pmatrix}
 
104
 * \f}
 
105
 *
 
106
 * The parameter is named \c "kappa"
 
107
 * and its value may be retrieve with the command
 
108
 * \code
 
109
 * getParameterValue("kappa")
 
110
 * \endcode
 
111
 *
 
112
 */
 
113
 
 
114
class BinarySubstitutionModel :
 
115
  public AbstractSubstitutionModel
 
116
{
 
117
private:
 
118
  double _kappa;
 
119
 
 
120
protected:
 
121
  mutable double _lambda, _exp;
 
122
  mutable RowMatrix<double> _p;
 
123
 
 
124
public:
 
125
  BinarySubstitutionModel(const BinaryAlphabet* alpha, double kappa = 1.);
 
126
 
 
127
  virtual ~BinarySubstitutionModel() {}
 
128
 
 
129
  BinarySubstitutionModel* clone() const { return new BinarySubstitutionModel(*this); }
 
130
 
 
131
  
 
132
public:
 
133
  double Pij_t    (unsigned int i, unsigned int j, double d) const;
 
134
  double dPij_dt  (unsigned int i, unsigned int j, double d) const;
 
135
  double d2Pij_dt2(unsigned int i, unsigned int j, double d) const;
 
136
  const Matrix<double>& getPij_t    (double d) const;
 
137
  const Matrix<double>& getdPij_dt  (double d) const;
 
138
  const Matrix<double>& getd2Pij_dt2(double d) const;
 
139
 
 
140
  std::string getName() const { return "Binary"; }
 
141
 
 
142
  void setFreq(std::map<int, double>& freqs);
 
143
 
 
144
  unsigned int getNumberOfStates() const { return 2; }
 
145
 
 
146
protected:
 
147
  void updateMatrices();
 
148
};
 
149
} // end of namespace bpp.
 
150
 
 
151
#endif  // _BINARYSUBSTITUTIONMODEL_H_
 
152