Debian Codegraph @

src/dr/oldevomodel/substmodel/SubstitutionEpochModel.java

raw symbols SCIP
1 /*
2 * SubstitutionEpochModel.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.DataType;
29 import dr.inference.model.Model;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32 import dr.math.matrixAlgebra.Vector;
33
34 import java.util.List;
35
36 /**
37 * @author Marc A. Suchard
38 */
39
40 public class SubstitutionEpochModel extends AbstractSubstitutionModel {
41
42 public static final boolean DEBUG = false;
43
44 public SubstitutionEpochModel(String name,
45 List<SubstitutionModel> modelList,
46 Parameter transitionTimes,
47 DataType dataType, FrequencyModel freqModel) {
48
49 super(name, dataType, freqModel);
50
51 this.modelList = modelList;
52 this.transitionTimesParameter = transitionTimes;
53 this.transitionTimes = transitionTimesParameter.getParameterValues();
54
55 addVariable(transitionTimes);
56
57 for (SubstitutionModel model : modelList)
58 addModel(model);
59
60 numberModels = modelList.size();
61 weight = new double[numberModels];
62 stateCount = dataType.getStateCount();
63 stepMatrix = new double[stateCount * stateCount];
64 productMatrix = new double[stateCount * stateCount];
65 resultMatrix = new double[stateCount * stateCount];
66 }
67
68
69 protected void frequenciesChanged() {
70 }
71
72 protected void handleModelChangedEvent(Model model, Object object, int index) {
73 if (model == freqModel)
74 frequenciesChanged();
75 else // This is an epoch model and I need to pass the info on
76 fireModelChanged(object, index);
77 }
78
79 protected void ratesChanged() {
80 }
81
82 protected void setupRelativeRates() {
83 }
84
85 public void getTransitionProbabilities(double startTime, double endTime, double distance, double[] matrix) {
86 int matrixCount = 0;
87
88 // System.out.println("startTime " + startTime );
89 // System.out.println("endTime " + endTime );
90 // EpochBranchSubstitutionModel.printMatrix(resultMatrix);
91
92 boolean oneMatrix = (getEpochWeights(startTime, endTime, weight) == 1);
93 for (int m = 0; m < numberModels; m++) {
94 if (weight[m] > 0) {
95 SubstitutionModel model = modelList.get(m);
96 if (matrixCount == 0) {
97 if (oneMatrix) {
98 model.getTransitionProbabilities(distance, matrix);
99 break;
100 } else
101 model.getTransitionProbabilities(distance * weight[m], resultMatrix);
102 matrixCount++;
103
104 // System.out.println("first " + weight[m] * (endTime - startTime) + " " + model.getVariable(0).getValue(0) + model.getId());
105 // EpochBranchSubstitutionModel.printMatrix(resultMatrix);
106
107 } else {
108 model.getTransitionProbabilities(distance * weight[m], stepMatrix);
109 // Sum over unobserved state
110 int index = 0;
111
112 // System.out.println("startTime " + startTime );
113 // System.out.println("endTime " + endTime );
114 // System.out.println("second " + weight[m] * (endTime - startTime) + " " + model.getVariable(0).getValue(0) + model.getId());
115 // EpochBranchSubstitutionModel.printMatrix(stepMatrix);
116
117 for (int i = 0; i < stateCount; i++) {
118 for (int j = 0; j < stateCount; j++) {
119 productMatrix[index] = 0;
120 for (int k = 0; k < stateCount; k++) {
121 productMatrix[index] += resultMatrix[i * stateCount + k] * stepMatrix[k * stateCount + j];
122 }
123 index++;
124 }
125 }
126
127 // EpochBranchSubstitutionModel.printMatrix(productMatrix);
128
129 // Swap pointers
130 double[] tmpMatrix = resultMatrix;
131 resultMatrix = productMatrix;
132 productMatrix = tmpMatrix;
133 }
134 }
135 }
136 if (!oneMatrix)
137 System.arraycopy(productMatrix, 0, matrix, 0, stateCount * stateCount);
138
139 // System.out.println("C:");
140 // EpochBranchSubstitutionModel.printMatrix(resultMatrix);
141 // System.exit(-1);
142
143 }
144
145 private int getEpochWeights(double startTime, double endTime, double[] weights) {
146
147 int matrixCount = 0;
148 final double lengthTime = endTime - startTime;
149 final int lastTime = numberModels - 2;
150
151 // model 0, 1, 2, ..., K-2, K-1
152 // times 0, 1, ..., K-2,
153 // where K = numberModels
154
155 // System.out.println(lengthTime);
156
157 // First epoch: 0 -> transitionTimes[0];
158 if (startTime <= transitionTimes[0]) {
159 if (endTime <= transitionTimes[0])
160 weights[0] = 1;
161 else
162 weights[0] = (transitionTimes[0] - startTime) / lengthTime;
163 matrixCount++;
164 } else
165 weights[0] = 0;
166
167 // Middle epoches:
168 for (int i = 1; i <= lastTime; i++) {
169 if (startTime <= transitionTimes[i]) {
170 double start = Math.max(startTime, transitionTimes[i - 1]);
171 double end = Math.min(endTime, transitionTimes[i]);
172 weights[i] = (end - start) / lengthTime;
173 matrixCount++;
174 } else
175 weights[i] = 0;
176 }
177
178 // Last epoch: transitionTimes[K-2] -> Infinity
179 if (lastTime >= 0) {
180 if (endTime > transitionTimes[lastTime]) {
181 double start = Math.max(startTime, transitionTimes[lastTime]);
182 weights[lastTime + 1] = (endTime - start) / lengthTime;
183 matrixCount++;
184 } else
185 weights[lastTime + 1] = 0;
186 }
187
188 if (DEBUG) {
189 double totalWeight = 0;
190 for (int i = 0; i < numberModels; i++)
191 totalWeight += weights[i];
192 System.err.println("Start: " + startTime + " End: " + endTime + " Count: " + matrixCount + " Weight: " + totalWeight + " - " + new Vector(weights));
193 if (totalWeight > 1.001) System.exit(-1);
194 if (totalWeight < 0.999) System.exit(-1);
195 }
196
197 // System.out.println(endTime-startTime);
198 // System.out.println(matrixCount);
199 // EpochBranchSubstitutionModel.printArray(weights);
200
201 return matrixCount;
202 }
203
204 public void getTransitionProbabilities(double distance, double[] matrix) {
205 throw new RuntimeException("Should not get here in a substitution epoch model.");
206 }
207
208 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
209 super.handleVariableChangedEvent(variable, index, type);
210
211 if (variable == transitionTimesParameter) {
212 transitionTimes = transitionTimesParameter.getParameterValues();
213 fireModelChanged(variable, index);
214 }
215 }
216
217 protected void storeState() {
218 }
219
220 protected void restoreState() {
221 }
222
223 private List<SubstitutionModel> modelList;
224 private Parameter transitionTimesParameter;
225 private double[] transitionTimes;
226 private double[] weight;
227 private double[] stepMatrix;
228 private double[] productMatrix;
229 private double[] resultMatrix;
230 private int numberModels;
231 private int stateCount;
232
233 }