Debian Codegraph @

src/dr/evolution/coalescent/TwoEpochDemographic.java

raw symbols SCIP
1 /*
2 * TwoEpochDemographic.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.evolution.coalescent;
27
28 /**
29 * This class models a demographic function that contains two epochs.
30 *
31 * @author Alexei Drummond
32 *
33 * @version $Id: TwoEpochDemographic.java,v 1.7 2005/05/24 20:25:56 rambaut Exp $
34 */
35 public class TwoEpochDemographic extends DemographicFunction.Abstract {
36
37 private DemographicFunction epoch1;
38 private DemographicFunction epoch2;
39 private double transitionTime;
40
41 /**
42 * Construct demographic model with default settings.
43 */
44 public TwoEpochDemographic(DemographicFunction epoch1, DemographicFunction epoch2, Type units) {
45
46 super(units);
47
48 this.epoch1 = epoch1;
49 this.epoch2 = epoch2;
50 }
51
52 /**
53 * @return the time of the transition between the two demographic functions.
54 */
55 public final double getTransitionTime() { return transitionTime; }
56
57 public final void setTransitionTime(double t) {
58 if (t < 0.0 || t > Double.MAX_VALUE) {
59 throw new IllegalArgumentException("transition time out of bounds.");
60 }
61 transitionTime = t;
62 }
63
64 public final DemographicFunction getFirstEpochDemography() {
65 return epoch1;
66 }
67
68 public final DemographicFunction getSecondEpochDemography() {
69 return epoch2;
70 }
71
72 // Implementation of abstract methods
73
74 public final double getDemographic(double t) {
75
76 if (t < transitionTime) {
77 return epoch1.getDemographic(t);
78 } else {
79 return epoch2.getDemographic(t-transitionTime);
80 }
81 }
82
83 public final double getIntensity(double t) {
84
85 if (t < transitionTime) {
86 return epoch1.getIntensity(t);
87 } else {
88 return epoch1.getIntensity(transitionTime) + epoch2.getIntensity(t-transitionTime);
89 }
90 }
91
92 /**
93 * By overriding this function we ensure that the integral can still be found when one of the sub-demographic models does not implement getIntensity.
94 * @param start
95 * @param finish
96 * @return the integral of the demographic function between start and finish
97 */
98 public final double getIntegral(double start, double finish) {
99
100 if (start < transitionTime) {
101 if (finish < transitionTime) {
102 return epoch1.getIntegral(start, finish);
103 } else {
104 return epoch1.getIntegral(start, transitionTime) + epoch2.getIntegral(0, finish-transitionTime);
105 }
106 } else {
107 return epoch2.getIntegral(start-transitionTime, finish-transitionTime);
108 }
109 }
110
111 /**
112 * @param x a value of intensity
113 * @return the time in the past that corresponds to the given intensity.
114 */
115 public final double getInverseIntensity(double x) {
116
117 double time = epoch1.getInverseIntensity(x);
118 if (time < transitionTime) {
119 return time;
120 }
121 x -= epoch1.getIntensity(transitionTime);
122 return transitionTime + epoch2.getInverseIntensity(x);
123 }
124
125 public int getNumArguments() {
126 return epoch1.getNumArguments() + epoch2.getNumArguments() + 1;
127 }
128
129 public final String getArgumentName(int n) {
130
131 if (n < epoch1.getNumArguments()) {
132 return epoch1.getArgumentName(n);
133 }
134 n -= epoch1.getNumArguments();
135 if (n < epoch2.getNumArguments()) {
136 return epoch2.getArgumentName(n);
137 }
138 n -= epoch2.getNumArguments();
139 if (n == 0) return "transitionTime";
140 throw new IllegalArgumentException();
141 }
142
143 public final double getArgument(int n) {
144
145 if (n < epoch1.getNumArguments()) {
146 return epoch1.getArgument(n);
147 }
148 n -= epoch1.getNumArguments();
149 if (n < epoch2.getNumArguments()) {
150 return epoch2.getArgument(n);
151 }
152 n -= epoch2.getNumArguments();
153 if (n == 0) return transitionTime;
154 throw new IllegalArgumentException();
155 }
156
157 public final void setArgument(int n, double value) {
158
159 if (n < epoch1.getNumArguments()) {
160 epoch1.setArgument(n, value);
161 }
162 n -= epoch1.getNumArguments();
163 if (n < epoch2.getNumArguments()) {
164 epoch2.setArgument(n, value);
165 }
166 n -= epoch2.getNumArguments();
167 if (n == 0) transitionTime = value;
168 throw new IllegalArgumentException();
169 }
170
171 /**
172 * @return the lower bound of the nth argument of this function.
173 */
174 public final double getLowerBound(int n) {
175
176 if (n < epoch1.getNumArguments()) {
177 return epoch1.getLowerBound(n);
178 }
179 n -= epoch1.getNumArguments();
180 if (n < epoch2.getNumArguments()) {
181 return epoch2.getLowerBound(n);
182 }
183 n -= epoch2.getNumArguments();
184 if (n == 0) return 0.0;
185 throw new IllegalArgumentException();
186 }
187
188 /**
189 * @return the upper bound of the nth argument of this function.
190 */
191 public final double getUpperBound(int n) {
192
193 if (n < epoch1.getNumArguments()) {
194 return epoch1.getUpperBound(n);
195 }
196 n -= epoch1.getNumArguments();
197 if (n < epoch2.getNumArguments()) {
198 return epoch2.getUpperBound(n);
199 }
200 n -= epoch2.getNumArguments();
201 if (n == 0) return Double.MAX_VALUE;
202 throw new IllegalArgumentException();
203 }
204
205 public final DemographicFunction getCopy() {
206 TwoEpochDemographic df = new TwoEpochDemographic(epoch1, epoch2, getUnits());
207 df.setTransitionTime(transitionTime);
208 return df;
209 }
210 }