Debian Codegraph @

src/dr/evolution/coalescent/EmpiricalPiecewiseConstant.java

raw symbols SCIP
1 /*
2 * EmpiricalPiecewiseConstant.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 *
30 * @version $Id: EmpiricalPiecewiseConstant.java,v 1.4 2004/10/01 23:30:16 alexei Exp $
31 *
32 * @author Alexei Drummond
33 * @author Andrew Rambaut
34 */
35 public class EmpiricalPiecewiseConstant extends DemographicFunction.Abstract {
36
37 /**
38 * Creates a piecewise constant model with the given break points.
39 * @param intervals an array of intervals, Each interval represents time
40 * over which a different population size is assumed, the first interval starts from the
41 * present and proceeds back into the past.
42 * @param popSizes the effective population sizes of each interval. The last population size represents the
43 * time from the end of the last interval out to infinity.
44 * @param lag a lag to align the break points with the most recent sample time (must be positive)
45 */
46 public EmpiricalPiecewiseConstant(double[] intervals, double[] popSizes, double lag, Type units) {
47 super(units);
48 if (popSizes == null || intervals == null) { throw new IllegalArgumentException(); }
49 if (popSizes.length != intervals.length + 1) { throw new IllegalArgumentException(); }
50 if (lag < 0.0) throw new IllegalArgumentException("Lag must be greater than 1.");
51
52 this.intervals = intervals;
53 this.popSizes = popSizes;
54 this.lag = lag;
55 }
56
57 public void setLag(double lag) {
58 this.lag = lag;
59 }
60
61 public void setPopulationSizes(double[] popSizes) {
62 this.popSizes = popSizes;
63 }
64
65 // **************************************************************
66 // Implementation of abstract methods
67 // **************************************************************
68
69 public double getDemographic(double t) {
70 int epoch = 0;
71 double t1 = t+lag;
72 while (t1 > getEpochDuration(epoch)) {
73 t1 -= getEpochDuration(epoch);
74 epoch += 1;
75 }
76 return getDemographic(epoch, t1);
77 }
78
79 public double getIntensity(double t) {
80
81 // find the first epoch that is involved
82 double t2 = lag;
83 int epoch = 0;
84 while (t2 > getEpochDuration(epoch)) {
85 t2 -= getEpochDuration(epoch);
86 epoch += 1;
87 }
88
89 // add last fraction of first epoch
90 double intensity = getIntensity(epoch)-getIntensity(epoch, t2);
91
92 double t1 = t-(getEpochDuration(epoch)-t2);
93 epoch += 1;
94 while (t1 > getEpochDuration(epoch)) {
95 t1 -= getEpochDuration(epoch);
96 intensity += getIntensity(epoch);
97 epoch += 1;
98 }
99 // add last fraction of intensity
100 // when t1 may be negative (for example when t is in the first epoch) the intensity need
101 // to be substracted
102 intensity += t1 >= 0 ? getIntensity(epoch, t1) : getIntensity(epoch-1, t1);
103
104 return intensity;
105 }
106
107 public double getInverseIntensity(double x) {
108 throw new RuntimeException("Not implemented!");
109 }
110
111 public double getUpperBound(int i) { return 1e9;}
112 public double getLowerBound(int i) { return Double.MIN_VALUE;}
113
114 public int getNumArguments() { return 1; }
115
116 public String getArgumentName(int i) {
117 return "lag";
118 }
119
120 public double getArgument(int i) {
121 return lag;
122 }
123
124 public void setArgument(int i, double value) {
125 lag = value;
126 }
127
128 public DemographicFunction getCopy() {
129 EmpiricalPiecewiseConstant df = new EmpiricalPiecewiseConstant(new double[intervals.length], new double[popSizes.length], lag, getUnits());
130 System.arraycopy(intervals, 0, df.intervals, 0, intervals.length);
131 System.arraycopy(popSizes, 0, df.popSizes, 0, popSizes.length);
132
133 return df;
134 }
135
136
137 /**
138 * @return the value of the demographic function for the given epoch and time relative to start of epoch.
139 */
140 protected double getDemographic(int epoch, double t) {
141 return getEpochDemographic(epoch);
142 }
143
144 /**
145 * @return the value of the intensity function for the given epoch.
146 */
147 protected double getIntensity(int epoch) {
148 return getEpochDuration(epoch) / getEpochDemographic(epoch);
149 }
150
151 /**
152 * @return the value of the intensity function for the given epoch and time relative to start of epoch.
153 */
154 protected double getIntensity(int epoch, double relativeTime) {
155 return relativeTime / getEpochDemographic(epoch);
156 }
157
158 /**
159 * @return the duration of the specified epoch (in whatever units this demographic model is specified in).
160 */
161 public double getEpochDuration(int epoch) {
162 if (epoch < intervals.length) {
163 return intervals[epoch];
164 }
165 return Double.POSITIVE_INFINITY;
166 }
167
168 /**
169 * @return the pop size of a given epoch.
170 */
171 public double getEpochDemographic(int epoch) {
172 if (epoch >= popSizes.length) { throw new IllegalArgumentException(); }
173 return popSizes[epoch];
174 }
175
176 public String toString() {
177 StringBuffer buffer = new StringBuffer();
178 buffer.append(popSizes[0]);
179 for (int i =1; i < popSizes.length; i++) {
180 buffer.append("\t").append(popSizes[i]);
181 }
182 return buffer.toString();
183 }
184
185 double[] intervals;
186 double[] popSizes;
187 double lag;
188 }