Debian Codegraph @

src/dr/oldevomodel/substmodel/TwoStateCovarionModel.java

raw symbols SCIP
1 /*
2 * TwoStateCovarionModel.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.oldevomodel.substmodel;
27
28 import dr.evolution.datatype.TwoStateCovarion;
29 import dr.oldevomodelxml.substmodel.TwoStateCovarionModelParser;
30 import dr.inference.model.Parameter;
31
32 /**
33 * @author Helen Shearman
34 * @author Alexei Drummond
35 * @version $Id$
36 */
37 public class TwoStateCovarionModel extends AbstractSubstitutionModel {
38
39 /**
40 * constructor
41 *
42 * @param dataType the data type
43 * @param freqModel the frequencies
44 * @param alphaParameter the rate of evolution in slow mode
45 * @param switchingParameter the rate of flipping between slow and fast modes
46 */
47 public TwoStateCovarionModel(TwoStateCovarion dataType, FrequencyModel freqModel,
48 Parameter alphaParameter,
49 Parameter switchingParameter) {
50 super(TwoStateCovarionModelParser.COVARION_MODEL, dataType, freqModel);
51
52 alpha = alphaParameter;
53 this.switchingParameter = switchingParameter;
54
55 addVariable(alpha);
56 addVariable(switchingParameter);
57 setupRelativeRates();
58 }
59
60 protected void frequenciesChanged() {
61 // DO NOTHING
62 }
63
64 protected void ratesChanged() {
65 setupRelativeRates();
66 }
67
68 protected void setupRelativeRates() {
69
70 relativeRates[0] = alpha.getParameterValue(0);
71 relativeRates[1] = switchingParameter.getParameterValue(0);
72 relativeRates[2] = 0.0;
73 relativeRates[3] = 0.0;
74 relativeRates[4] = switchingParameter.getParameterValue(0);
75 relativeRates[5] = 1.0;
76 }
77
78 public String toString() {
79
80 return SubstitutionModelUtils.toString(relativeRates, dataType, true, 2);
81 }
82
83 /**
84 * Normalize rate matrix to one expected substitution per unit time
85 *
86 * @param matrix the matrix to normalize to one expected substitution
87 * @param pi the equilibrium distribution of states
88 */
89 void normalize(double[][] matrix, double[] pi) {
90
91 if (isNormalized) {
92
93 double subst = 0.0;
94 int dimension = pi.length;
95
96 for (int i = 0; i < dimension; i++) {
97 subst += -matrix[i][i] * pi[i];
98 }
99
100 // normalize, including switches
101 for (int i = 0; i < dimension; i++) {
102 for (int j = 0; j < dimension; j++) {
103 matrix[i][j] = matrix[i][j] / subst;
104 }
105 }
106
107 double switchingProportion = 0.0;
108 switchingProportion += matrix[0][2] * pi[2];
109 switchingProportion += matrix[2][0] * pi[0];
110 switchingProportion += matrix[1][3] * pi[3];
111 switchingProportion += matrix[3][1] * pi[1];
112
113 //System.out.println("switchingProportion=" + switchingProportion);
114
115 // normalize, removing switches
116 for (int i = 0; i < dimension; i++) {
117 for (int j = 0; j < dimension; j++) {
118 matrix[i][j] = matrix[i][j] / (1.0 - switchingProportion);
119 }
120 }
121 }
122 }
123
124 private Parameter alpha;
125 private Parameter switchingParameter;
126
127 // if true then matrix will be normalized to output 1 substitution per unit time
128 private boolean isNormalized = true;
129
130 }