Debian Codegraph @

src/dr/evolution/coalescent/PiecewiseConstantPopulation.java

raw symbols SCIP
1 /*
2 * PiecewiseConstantPopulation.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 import java.util.ArrayList;
29 import java.util.Collections;
30
31 /**
32 * @author Alexei Drummond
33 * @author Andrew Rambaut
34 */
35 public class PiecewiseConstantPopulation extends DemographicFunction.Abstract {
36
37 ArrayList<Double> cif = null;
38 ArrayList<Double> endTime = null;
39 final boolean cacheCumulativeIntensities = true;
40 private boolean intensitiesKnown = false;
41
42 /**
43 * Construct demographic model with default settings
44 *
45 * @param units of time
46 */
47 public PiecewiseConstantPopulation(Type units) {
48 super(units);
49 }
50
51 /**
52 * Creates a piecewise constant model with the given break points.
53 *
54 * @param intervals an array of intervals, Each interval represents time
55 * over which a different population size is assumed, the first interval starts from the
56 * present and proceeds back into the past.
57 * @param thetas the population sizes of each interval. The last population size represents the
58 * time from the end of the last interval out to infinity.
59 */
60 public PiecewiseConstantPopulation(double[] intervals, double[] thetas, Type units) {
61 super(units);
62 setIntervals(intervals, thetas);
63 }
64
65 public void setIntervals(final double[] intervals, final double[] thetas) {
66 if (thetas == null || intervals == null) {
67 throw new IllegalArgumentException();
68 }
69 if (thetas.length != intervals.length + 1) {
70 throw new IllegalArgumentException();
71 }
72
73 this.intervals = intervals;
74 this.thetas = thetas;
75
76 if (cacheCumulativeIntensities) {
77 cif = new ArrayList<Double>(intervals.length);
78 endTime = new ArrayList<Double>(intervals.length);
79 cif.add(getIntensity(0));
80 endTime.add(intervals[0]);
81 for (int i = 1; i < intervals.length; i++) {
82 cif.add(getIntensity(i) + cif.get(i - 1));
83 endTime.add(intervals[i] + endTime.get(i - 1));
84 }
85
86 intensitiesKnown = true;
87 }
88 }
89
90 // **************************************************************
91 // Implementation of abstract methods
92 // **************************************************************
93
94 public double getDemographic(double t) {
95 int epoch = 0;
96 double t1 = t;
97 while (t1 > getEpochDuration(epoch)) {
98 t1 -= getEpochDuration(epoch);
99 epoch += 1;
100 }
101 return getDemographic(epoch, t1);
102 }
103
104 /**
105 * Gets the integral of intensity function from time 0 to time t.
106 */
107 public double getIntensity(double t) {
108
109 if (cif != null) {
110 if (!intensitiesKnown) {
111 setIntervals(intervals, thetas);
112 }
113
114 int epoch = Collections.binarySearch(endTime, t);
115
116 if (epoch < 0) {
117 epoch = -epoch - 1;
118
119 if (epoch > 0) {
120
121 return cif.get(epoch - 1) + getIntensity(epoch, t - endTime.get(epoch - 1));
122 } else {
123 assert epoch == 0;
124 return getIntensity(0, t);
125 }
126 } else {
127 return cif.get(epoch);
128 }
129 } else {
130
131 double intensity = 0.0;
132 int epoch = 0;
133 double t1 = t;
134 while (t1 > getEpochDuration(epoch)) {
135 t1 -= getEpochDuration(epoch);
136 intensity += getIntensity(epoch);
137 epoch += 1;
138 }
139
140 // probably same bug as in EmpiricalPiecewiseConstant when t1 goes negative
141 // add last fraction of intensity
142 intensity += getIntensity(epoch, t1);
143
144 return intensity;
145 }
146 }
147
148 public double getInverseIntensity(double x) {
149 throw new RuntimeException("Not implemented!");
150 }
151
152 public double getUpperBound(int i) {
153 return 1e9;
154 }
155
156 public double getLowerBound(int i) {
157 return Double.MIN_VALUE;
158 }
159
160 public int getNumArguments() {
161 return thetas.length;
162 }
163
164 public String getArgumentName(int i) {
165 return "theta" + i;
166 }
167
168 public double getArgument(int i) {
169 return thetas[i];
170 }
171
172 public void setArgument(int i, double value) {
173 thetas[i] = value;
174
175 intensitiesKnown = false;
176 }
177
178 public DemographicFunction getCopy() {
179 PiecewiseConstantPopulation df = new PiecewiseConstantPopulation(new double[intervals.length], new double[thetas.length], getUnits());
180 System.arraycopy(intervals, 0, df.intervals, 0, intervals.length);
181 System.arraycopy(thetas, 0, df.thetas, 0, thetas.length);
182
183 if (cacheCumulativeIntensities) {
184 df.setIntervals(intervals, thetas);
185 }
186
187 return df;
188 }
189
190
191 /**
192 * @return the value of the demographic function for the given epoch and time relative to start of epoch.
193 */
194 protected double getDemographic(int epoch, double t) {
195 return getEpochDemographic(epoch);
196 }
197
198 /**
199 * @return the value of the intensity function for the given epoch.
200 */
201 protected double getIntensity(int epoch) {
202 return getEpochDuration(epoch) / getEpochDemographic(epoch);
203 }
204
205 /**
206 * @return the value of the intensity function for the given epoch and time relative to start of epoch.
207 */
208 protected double getIntensity(final int epoch, final double relativeTime) {
209 assert 0 <= relativeTime && relativeTime <= getEpochDuration(epoch);
210
211 return relativeTime / getEpochDemographic(epoch);
212 }
213
214 /**
215 * @return the duration of the specified epoch (in whatever units this demographic model is specified in).
216 */
217 public double getEpochDuration(int epoch) {
218 if (epoch == intervals.length) {
219 return Double.POSITIVE_INFINITY;
220 } else return intervals[epoch];
221 }
222
223 public void setEpochDuration(int epoch, double duration) {
224 if (epoch < 0 || epoch >= intervals.length) {
225 throw new IllegalArgumentException("epoch must be between 0 and " + (intervals.length - 1));
226 }
227 if (duration < 0.0) {
228 throw new IllegalArgumentException("duration must be positive.");
229 }
230 intervals[epoch] = duration;
231 }
232
233 /**
234 * @return the pop size of a given epoch.
235 */
236 public final double getEpochDemographic(int epoch) {
237 if (epoch < 0 || epoch >= thetas.length) {
238 throw new IllegalArgumentException();
239 }
240 return thetas[epoch];
241 }
242
243 public String toString() {
244 StringBuffer buffer = new StringBuffer();
245 buffer.append(thetas[0]);
246 for (int i = 1; i < thetas.length; i++) {
247 buffer.append("\t").append(thetas[i]);
248 }
249 return buffer.toString();
250 }
251
252 double[] intervals;
253 double[] thetas;
254 }