Debian Codegraph @

src/dr/evomodel/substmodel/MarkovModulatedFrequencyModel.java

raw symbols SCIP
1 /*
2 * MarkovModulatedFrequencyModel.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.evomodel.substmodel;
27
28 import cern.colt.matrix.DoubleMatrix2D;
29 import cern.colt.matrix.impl.DenseDoubleMatrix2D;
30 import cern.colt.matrix.linalg.Algebra;
31 import cern.colt.matrix.linalg.LUDecomposition;
32 import dr.inference.model.Model;
33 import dr.inference.model.Parameter;
34 import dr.inference.model.Variable;
35
36 import java.util.List;
37
38 /**
39 * @author Marc A. Suchard
40 * @author Vladimir Minin
41 * <p/>
42 * A class for implementing a kronecker sum of CTMC models in BEAST using BEAGLE
43 * This work is supported by NSF grant 0856099
44 * <p/>
45 * O'Brien JD, Minin VN and Suchard MA (2009) Learning to count: robust estimates for labeled distances between
46 * molecular sequences. Molecular Biology and Evolution, 26, 801-814
47 */
48
49 public class MarkovModulatedFrequencyModel extends FrequencyModel {
50
51 public MarkovModulatedFrequencyModel(String name, List<FrequencyModel> freqModels, Parameter switchingRates) {
52 super(name);
53 this.freqModels = freqModels;
54 int freqCount = 0;
55 stateCount = freqModels.get(0).getFrequencyCount();
56 numBaseModel = freqModels.size();
57 for (int i = 0; i < numBaseModel; i++) {
58 int size = freqModels.get(i).getFrequencyCount();
59 if (stateCount != size) {
60 throw new RuntimeException("MarkovModulatedFrequencyModel requires all frequencies model to have the same dimension");
61 }
62 addModel(freqModels.get(i));
63
64 freqCount += size;
65 }
66 totalFreqCount = freqCount;
67 this.switchingRates = switchingRates;
68 addVariable(switchingRates);
69
70 // if (switchingRates.getDimension() > 2 || numBaseModel > 2) {
71 // throw new RuntimeException("MarkovModulatedFrequencyModel not yet implemented for more than 2 hidden classes");
72 // }
73
74 baseStationaryDistribution = new double[numBaseModel];
75 storedBaseStationaryDistribution = new double[numBaseModel];
76 stationaryDistributionKnown = false;
77
78 d = new DenseDoubleMatrix2D(numBaseModel, numBaseModel);
79 d.set(0, 0, 1.0);
80 }
81
82 public void setFrequency(int i, double value) {
83 throw new RuntimeException("Not implemented");
84 }
85
86 public double getFrequency(int index) {
87 int whichModel = index / stateCount;
88 int whichState = index % stateCount;
89 double relativeFreq = freqModels.get(whichModel).getFrequency(whichState) / numBaseModel;
90
91 // Scale by stationary distribution over hidden classes
92 if (numBaseModel > 1) {
93 if (!stationaryDistributionKnown) {
94 computeStationaryDistribution(baseStationaryDistribution);
95 stationaryDistributionKnown = true;
96 }
97 // relativeFreq *= baseStationaryDistribution[whichModel]; // Try: no adjustment, appears to cause store/restore issue
98 }
99
100 return relativeFreq;
101 }
102
103 private void computeStationaryDistribution(double[] statDistr) {
104
105 // // Uses an eigendecomposition and matrix inverse
106 // DoubleMatrix2D mat = new DenseDoubleMatrix2D(numBaseModel, numBaseModel);
107 // int index = 0;
108 // for (int i = 0; i < numBaseModel; ++i) {
109 // for (int j = i + 1; j < numBaseModel; ++j) {
110 // mat.set(i, j, switchingRates.getParameterValue(index));
111 // index++;
112 // }
113 // }
114 // for (int j = 0; j < numBaseModel; ++j) {
115 // for (int i = j + 1; i < numBaseModel; ++i) {
116 // mat.set(i, j, switchingRates.getParameterValue(index));
117 // index++;
118 // }
119 // }
120 // for (int i = 0; i < numBaseModel; ++i) {
121 // double rowTotal = 0.0;
122 // for (int j = 0; j < numBaseModel; ++j) {
123 // if (i != j) {
124 // rowTotal += mat.get(i,j);
125 // }
126 // }
127 // mat.set(i,i, -rowTotal);
128 // }
129 //
130 // EigenvalueDecomposition ed = new EigenvalueDecomposition(mat);
131 // DoubleMatrix2D eigenVectors = ed.getV();
132 // DoubleMatrix2D b = alg.mult(eigenVectors, alg.mult(d, alg.inverse(eigenVectors)));
133 //
134 // for (int i = 0; i < numBaseModel; ++i) {
135 // statDistr[i] = b.get(0,i);
136 // }
137 // System.err.println(new Vector(statDistr));
138
139 if (allRatesAreZero(switchingRates)) {
140 return;
141 }
142
143 // Uses an LU decomposition to solve Q^t \pi = 0 and \sum \pi_i = 1
144 DoubleMatrix2D mat2 = new DenseDoubleMatrix2D(numBaseModel + 1, numBaseModel);
145 int index2 = 0;
146 for (int i = 0; i < numBaseModel; ++i) {
147 for (int j = i + 1; j < numBaseModel; ++j) {
148 mat2.set(j, i, switchingRates.getParameterValue(index2)); // Transposed
149 index2++;
150 }
151 }
152 for (int j = 0; j < numBaseModel; ++j) {
153 for (int i = j + 1; i < numBaseModel; ++i) {
154 mat2.set(j, i, switchingRates.getParameterValue(index2)); // Transposed
155 index2++;
156 }
157 }
158 for (int i = 0; i < numBaseModel; ++i) {
159 double rowTotal = 0.0;
160 for (int j = 0; j < numBaseModel; ++j) {
161 if (i != j) {
162 rowTotal += mat2.get(j, i); // Transposed
163 }
164
165 }
166 mat2.set(i, i, -rowTotal);
167 }
168
169 // Add row for sum-to-one constraint
170 for (int i = 0; i < numBaseModel; ++i) {
171 mat2.set(numBaseModel, i, 1.0);
172 }
173
174 LUDecomposition decomp = new LUDecomposition(mat2);
175 DoubleMatrix2D x = new DenseDoubleMatrix2D(numBaseModel + 1, 1);
176 x.set(numBaseModel, 0, 1.0);
177 DoubleMatrix2D y = decomp.solve(x);
178 for (int i = 0; i < numBaseModel; ++i) {
179 statDistr[i] = y.get(i, 0);
180 }
181 //System.err.println(new Vector(statDistr));
182 }
183
184 private static boolean allRatesAreZero(Parameter rates) {
185 for (int i = 0; i < rates.getDimension(); ++i) {
186 if (rates.getParameterValue(i) != 0.0) {
187 return false;
188 }
189 }
190 return true;
191 }
192
193 protected void storeState() {
194 System.arraycopy(baseStationaryDistribution, 0, storedBaseStationaryDistribution, 0, numBaseModel);
195 storedStationaryDistributionKnown = stationaryDistributionKnown;
196 }
197
198 protected void restoreState() {
199 double[] tmp = baseStationaryDistribution;
200 baseStationaryDistribution = storedBaseStationaryDistribution;
201 storedBaseStationaryDistribution = tmp;
202
203 stationaryDistributionKnown = storedStationaryDistributionKnown;
204 }
205
206 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
207 if (variable == switchingRates) {
208 stationaryDistributionKnown = false;
209 }
210 }
211
212 protected void handleModelChangedEvent(Model model, Object object, int index) {
213 // System.err.println("MMFM.hMCE : " + model.getId() + " : " + model.getClass().getCanonicalName());
214 fireModelChanged();
215 }
216
217 public int getFrequencyCount() {
218 return totalFreqCount;
219 }
220
221 public Parameter getFrequencyParameter() {
222 throw new RuntimeException("Not implemented");
223 }
224
225 private List<FrequencyModel> freqModels;
226
227 private final int numBaseModel;
228 private final int totalFreqCount;
229 private final int stateCount;
230 private final Parameter switchingRates;
231
232 private double[] baseStationaryDistribution;
233 private double[] storedBaseStationaryDistribution;
234
235 private boolean stationaryDistributionKnown;
236 private boolean storedStationaryDistributionKnown;
237
238 private final Algebra alg = new Algebra();
239 private final DoubleMatrix2D d;
240 }