Debian Codegraph @

src/test/dr/evomodel/substmodel/TwoStateOccupancyMarkovRewardsTest.java

raw symbols SCIP
1 /*
2 * TwoStateOccupancyMarkovRewardsTest.java
3 *
4 * Copyright (c) 2002-2014 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 test.dr.evomodel.substmodel;
27
28 import dr.evomodel.branchratemodel.LatentStateBranchRateModel;
29 import dr.inference.markovjumps.MarkovReward;
30 import dr.inference.markovjumps.SericolaSeriesMarkovReward;
31 import dr.inference.markovjumps.TwoStateOccupancyMarkovReward;
32 import dr.inference.model.Parameter;
33 import dr.math.matrixAlgebra.Matrix;
34 import dr.math.matrixAlgebra.Vector;
35 import org.apache.commons.math.FunctionEvaluationException;
36 import org.apache.commons.math.MaxIterationsExceededException;
37 import org.apache.commons.math.analysis.UnivariateRealFunction;
38 import org.apache.commons.math.analysis.integration.TrapezoidIntegrator;
39 import test.dr.math.MathTestCase;
40
41 /**
42 * @author Marc A. Suchard
43 */
44
45 public class TwoStateOccupancyMarkovRewardsTest extends MathTestCase {
46
47 private static final double tolerance = 10E-3;
48
49 private double sum(double[] v) {
50 double sum = 0.0;
51 for (double x : v) {
52 sum += x;
53 }
54 return sum;
55 }
56
57 public void testNew() {
58
59 // Equal rates
60 double rate = 2.1;
61 double prop = 0.5;
62 double eps = 0.3;
63 double branchLength = 2.4;
64
65 MarkovReward markovReward1 = createMarkovReward(rate, prop);
66 MarkovReward markovReward2 = createSericolaMarkovReward(rate, prop);
67
68 double r1 = markovReward1.computePdf(eps * branchLength, branchLength, 0, 0);
69 double r2 = markovReward2.computePdf(eps * branchLength, branchLength, 0, 0);
70
71 assertEquals(r1, r2, tolerance);
72
73 // Unequal rates
74 prop = 0.501;
75 MarkovReward markovReward3 = createMarkovReward(rate, prop);
76 MarkovReward markovReward4 = createSericolaMarkovReward(rate, prop);
77
78 double r3 = markovReward3.computePdf(eps * branchLength, branchLength, 0, 0);
79 double r4 = markovReward4.computePdf(eps * branchLength, branchLength, 0, 0);
80
81 assertEquals(r3, r4, tolerance);
82
83
84
85 System.exit(-1);
86 }
87
88 public void testTwoStateSericolaRewards1() {
89 // final double rate = 0.0015;
90 // final double prop = 0.5;
91 // final double eps = 0.01;
92 // final double branchLength = 2000.0;
93
94 final double rate = 1;
95 final double prop = 0.5;
96 final double eps = 0.1;
97 final double branchLength = 1.2;
98
99 final boolean print = false;
100
101 //
102 // TwoStateOccupancyMarkovReward two = (TwoStateOccupancyMarkovReward) markovReward;
103 //
104 //// run(markovReward, rate, prop, branchLength, print, 1000);
105 //
106 // System.err.println(markovReward.computePdf(0.5 * branchLength, branchLength, 0, 0));
107 //
108 // System.err.println(new Vector(two.getJumpProbabilities()));
109 //
110 // MarkovReward markovReward2 = createMarkovReward(rate, prop + eps);
111 // TwoStateOccupancyMarkovReward two2 = (TwoStateOccupancyMarkovReward) markovReward2;
112 //
113 // System.err.println(markovReward2.computePdf(0.5 * branchLength, branchLength, 0, 0));
114 // System.err.println(new Vector(two2.getJumpProbabilities()));
115 // System.err.println("");
116
117 // double[][] C = two2.getC();
118 // double[][] D = two2.getD();
119 //
120 // System.err.println("C:\n" + new Matrix(C));
121 //
122 // System.err.println("D:\n" + new Matrix(D));
123
124 }
125
126 // public void testTwoStateSericolaRewards1() {
127 // final double rate = 0.0015;
128 //// final double prop = 0.5;
129 // final double prop = 0.66666;
130 //
131 // final double branchLength = 2000.0;
132 // final boolean print = false;
133 //
134 //// MarkovReward markovReward = createMarkovReward(rate, prop);
135 // MarkovReward markovReward = createSericolaMarkovReward(rate, prop);
136 //
137 // run(markovReward, rate, prop, branchLength, print, 1000);
138 // }
139
140 // public void testTwoStateSericolaRewards2() {
141 // final double rate = 0.0015;
142 // final double prop = 0.5;
143 //// final double prop = 0.66666;
144 // final double branchLength = 1000.0;
145 // final boolean print = false;
146 //
147 // MarkovReward markovReward = createMarkovReward(rate, prop);
148 //// MarkovReward markovReward = createSericolaMarkovReward(rate, prop);
149 //
150 // run(markovReward, rate, prop, branchLength, print, 1000);
151 // }
152
153 // public void testLatentStateBranchRateModel() throws FunctionEvaluationException, MaxIterationsExceededException {
154 //
155 // LatentStateBranchRateModel model = new LatentStateBranchRateModel(
156 // new Parameter.Default(0.001), new Parameter.Default(0.5));
157 //
158 // TrapezoidIntegrator integator = new TrapezoidIntegrator();
159 //
160 // final double branchLength = 2000;
161 // double integral = integator.integrate(new LatentStateDensityFunction(model, branchLength), 0.0, 1.0);
162 //
163 // System.out.println("testLatentStateBeanchRateModel");
164 // System.out.println("Integral = " + integral);
165 //
166 // assertEquals(integral, 1.0, tolerance);
167 // }
168
169 private void run(MarkovReward markovReward, double rate, double prop, double branchLength,
170 boolean print, int length) {
171 DensityFunction densityFunction = new DensityFunction(markovReward, branchLength, rate, prop);
172
173 final double step = branchLength / length;
174 int i = 0;
175 double sum = 0.0;
176 double modeY = 0.0;
177 double modeX = 0.0;
178
179 for (double x = 0.0; x <= branchLength; x += step, ++i) {
180
181 double density = 0;
182 density = densityFunction.value(x);
183
184 if (x == 0.0) {
185 modeY = density;
186 } else {
187 if (density > modeY) {
188 modeY = density;
189 modeX = x;
190 }
191 }
192
193 if (x == 0.0 || x == branchLength) {
194 sum += density;
195 } else {
196 sum += 2.0 * density;
197 }
198
199 if (print) {
200 System.out.println(i + "\t" + String.format("%3.2f", x) + "\t" + String.format("%5.3e", density));
201 }
202 }
203 sum *= (branchLength / 2.0 / length);
204
205 // TODO Normalization is missing in LatentBranchRateModel
206 System.out.println("branchLength = " + branchLength);
207 System.out.println("rate = " + rate);
208 System.out.println("prop = " + prop);
209 System.out.println("Integral = " + sum);
210 System.out.println("Mode = " + String.format("%3.2e", modeY) + " at " + modeX);
211
212 assertEquals(sum, 1.0, tolerance);
213
214 TrapezoidIntegrator integrator = new TrapezoidIntegrator();
215 double integral = 0.0;
216 try {
217 integral = integrator.integrate(new UnitDensityFunction(markovReward, branchLength, rate, prop), 0.0, 1.0);
218 } catch (MaxIterationsExceededException e) {
219 e.printStackTrace();
220 } catch (FunctionEvaluationException e) {
221 e.printStackTrace();
222 }
223 System.out.println("unt int = " + integral);
224 assertEquals(integral, 1.0, tolerance);
225
226 System.out.println("\n");
227 }
228
229 private class LatentStateDensityFunction implements UnivariateRealFunction {
230
231 private final LatentStateBranchRateModel model;
232 private final double branchLength;
233
234 LatentStateDensityFunction(LatentStateBranchRateModel model, double branchLength) {
235 this.model = model;
236 this.branchLength = branchLength;
237 }
238
239 public double value(double prop) {
240 return model.getBranchRewardDensity(prop, branchLength);
241 }
242 }
243
244 private class DensityFunction implements UnivariateRealFunction {
245
246 private final MarkovReward markovReward;
247 private final double branchLength;
248 private final double rate;
249 private final double prop;
250
251 DensityFunction(MarkovReward markovReward, double branchLength, double rate, double prop) {
252 this.markovReward = markovReward;
253 this.branchLength = branchLength;
254 this.rate = rate;
255 this.prop = prop;
256 }
257
258 @Override
259 public double value(double v) { //throws FunctionEvaluationException {
260 return markovReward.computePdf(v, branchLength, 0, 0) /
261 (markovReward.computeConditionalProbability(branchLength, 0, 0)
262 - Math.exp(-rate * prop * branchLength));
263 }
264 }
265
266 private class UnitDensityFunction implements UnivariateRealFunction {
267
268 private final MarkovReward markovReward;
269 private final double branchLength;
270 private final double rate;
271 private final double prop;
272
273 UnitDensityFunction(MarkovReward markovReward, double branchLength, double rate, double prop) {
274 this.markovReward = markovReward;
275 this.branchLength = branchLength;
276 this.rate = rate;
277 this.prop = prop;
278 }
279
280 @Override
281 public double value(double v) { //throws FunctionEvaluationException {
282 double density = markovReward.computePdf(v * branchLength, branchLength, 0, 0) /
283 (markovReward.computeConditionalProbability(branchLength, 0, 0)
284 - Math.exp(-rate * prop * branchLength));
285 return density * branchLength;
286 }
287 }
288
289 private double[] createLatentInfinitesimalMatrix(final double rate, final double prop) {
290 double[] mat = new double[]{
291 -rate * prop, rate * prop,
292 rate * (1.0 - prop), -rate * (1.0 - prop)
293 };
294 return mat;
295 }
296
297 private SericolaSeriesMarkovReward createSericolaMarkovReward(final double rate, final double prop) {
298 double[] r = new double[]{0.0, 1.0};
299 return new SericolaSeriesMarkovReward(createLatentInfinitesimalMatrix(rate, prop), r, 2);
300 }
301
302 private TwoStateOccupancyMarkovReward createMarkovReward(final double rate, final double prop) {
303 TwoStateOccupancyMarkovReward markovReward = new
304 TwoStateOccupancyMarkovReward(
305 createLatentInfinitesimalMatrix(rate, prop)
306 );
307 return markovReward;
308 }
309 }