Debian Codegraph @

src/test/dr/evomodel/coalescent/MultiEpochExponentialTest.java

raw symbols SCIP
1 /*
2 * MultiEpochExponentialTest.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 test.dr.evomodel.coalescent;
27
28 import dr.evolution.coalescent.ExponentialExponential;
29 import dr.evolution.coalescent.ExponentialGrowth;
30 import dr.evolution.coalescent.MultiEpochExponential;
31 import dr.evolution.util.Units;
32 import dr.evomodel.coalescent.VDdemographicFunction;
33 import dr.evomodel.coalescent.VariableDemographicModel;
34 import junit.framework.TestCase;
35
36 /**
37 * @author Marc A. Suchard
38 */
39 public class MultiEpochExponentialTest extends TestCase {
40
41 public void testExponentialExponential() {
42
43 Units.Type units = Units.Type.YEARS;
44
45 ExponentialExponential ee = new ExponentialExponential(units);
46 ee.setN0(N0);
47 ee.setGrowthRate(rates[0]);
48 ee.setAncestralGrowthRate(rates[1]);
49 ee.setTransitionTime(transitionTimes[0]);
50
51 MultiEpochExponential mee = new MultiEpochExponential(units, 2);
52 mee.setN0(N0);
53 for (int i = 0;i < rates.length; ++i) {
54 mee.setGrowthRate(i, rates[i]);
55 }
56 for (int i = 0; i < transitionTimes.length; ++i) {
57 mee.setTransitionTime(i, transitionTimes[i]);
58 }
59
60 for (double time = 0; time < 20; time += 1.0) {
61 double eeDemo = ee.getDemographic(time);
62 double meeDemo = mee.getDemographic(time);
63 assertEquals(eeDemo, meeDemo, tolerance1);
64 }
65
66 double start = 0.0;
67 double finish = 1.0;
68
69 for (; finish < 20.0; finish += 1.0) {
70 double eeInt = ee.getIntegral(start, finish);
71 double meeIntN = mee.getNumericalIntegral(start, finish);
72 double meeIntA = mee.getAnalyticIntegral(start, finish);
73 // System.err.println(finish + ": " + eeInt + " " + meeIntN + " " + meeIntA);
74 assertEquals(eeInt, meeIntN, tolerance1);
75 assertEquals(meeIntN, meeIntA, tolerance2);
76 }
77
78
79 start = 0.5;
80 finish = 1.0;
81
82 for (; finish < 20.0; finish += 1.0) {
83 double eeInt = ee.getIntegral(start, finish);
84 double meeIntN = mee.getNumericalIntegral(start, finish);
85 double meeIntA = mee.getAnalyticIntegral(start, finish);
86 // System.err.println(finish + ": " + eeInt + " " + meeIntN + " " + meeIntA);
87 assertEquals(eeInt, meeIntN, tolerance1);
88 assertEquals(meeIntN, meeIntA, tolerance2);
89 }
90
91 start = 11.0;
92 finish = 11.0;
93
94 for (; finish < 20.0; finish += 1.0) {
95 double eeInt = ee.getIntegral(start, finish);
96 double meeIntN = mee.getNumericalIntegral(start, finish);
97 double meeIntA = mee.getAnalyticIntegral(start, finish);
98 // System.err.println(finish + ": " + eeInt + " " + meeIntN + " " + meeIntA);
99 assertEquals(eeInt, meeIntN, tolerance1);
100 assertEquals(meeIntN, meeIntA, tolerance2);
101 }
102 }
103
104
105 public void testThreeExponential() {
106
107 Units.Type units = Units.Type.YEARS;
108
109 ExponentialGrowth e = new ExponentialGrowth(units);
110 e.setN0(N0);
111 e.setGrowthRate(rates3[0]);
112
113 MultiEpochExponential mee = new MultiEpochExponential(units, 3);
114 mee.setN0(N0);
115 for (int i = 0; i < rates3.length; ++i) {
116 mee.setGrowthRate(i, rates3[i]);
117 }
118 for (int i = 0; i < transitionTimes3.length; ++i) {
119 mee.setTransitionTime(i, transitionTimes3[i]);
120 }
121
122 double start = 0.0;
123 double finish = 1.0;
124
125 for (; finish < 20.0; finish += 1.0) {
126 double eeInt = e.getIntegral(start, finish);
127 double meeIntN = mee.getNumericalIntegral(start, finish);
128 double meeIntA = mee.getAnalyticIntegral(start, finish);
129 // System.err.println(finish + ": " + eeInt + " " + meeIntN + " " + meeIntA);
130 assertEquals(eeInt, meeIntN, tolerance1);
131 assertEquals(meeIntN, meeIntA, tolerance2);
132 }
133
134
135 start = 0.5;
136 finish = 1.0;
137
138 for (; finish < 20.0; finish += 1.0) {
139 double eeInt = e.getIntegral(start, finish);
140 double meeIntN = mee.getNumericalIntegral(start, finish);
141 double meeIntA = mee.getAnalyticIntegral(start, finish);
142 // System.err.println(finish + ": " + eeInt + " " + meeIntN + " " + meeIntA);
143 assertEquals(eeInt, meeIntN, tolerance1);
144 assertEquals(meeIntN, meeIntA, tolerance2);
145 }
146
147 start = 11.0;
148 finish = 11.0;
149
150 for (; finish < 20.0; finish += 1.0) {
151 double eeInt = e.getIntegral(start, finish);
152 double meeIntN = mee.getNumericalIntegral(start, finish);
153 double meeIntA = mee.getAnalyticIntegral(start, finish);
154 // System.err.println(finish + ": " + eeInt + " " + meeIntN + " " + meeIntA);
155 assertEquals(eeInt, meeIntN, tolerance1);
156 assertEquals(meeIntN, meeIntA, tolerance2);
157 }
158 }
159
160
161 double N0 = 100;
162
163 double[] rates = new double[] { 0.2, -0.2 };
164 double[] transitionTimes = new double[] { 10.0 };
165
166 double[] rates3 = new double[] { 0.1, 0.1, 0.1 };
167 double[] transitionTimes3 = new double[] { 10.0, 15.0 };
168
169 private final static double tolerance1 = 1E-10;
170 private final static double tolerance2 = 1E-5;
171 }