Debian Codegraph @

src/dr/evolution/coalescent/PiecewiseLinearPopulation.java

raw symbols SCIP
1 /*
2 * PiecewiseLinearPopulation.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.Collections;
29
30 /**
31 * @author Alexei Drummond
32 * @author Andrew Rambaut
33 */
34 public class PiecewiseLinearPopulation extends PiecewiseConstantPopulation {
35
36 /**
37 * Construct demographic model with default settings
38 *
39 * @param units of time
40 */
41 public PiecewiseLinearPopulation(Type units) {
42 super(units);
43 }
44
45 /**
46 * Construct demographic model with default settings
47 */
48 public PiecewiseLinearPopulation(double[] intervals, double[] thetas, Type units) {
49
50 super(intervals, thetas, units);
51 }
52
53 // **************************************************************
54 // Implementation of abstract methods
55 // **************************************************************
56
57 /**
58 * @return the value of the demographic function for the given epoch and time relative to start of epoch.
59 */
60 protected final double getDemographic(int epoch, double t) {
61
62 // if in last epoch then the population is flat.
63 if (epoch == (thetas.length - 1)) {
64 return getEpochDemographic(epoch);
65 }
66
67 final double popSize1 = getEpochDemographic(epoch);
68 final double popSize2 = getEpochDemographic(epoch + 1);
69
70 final double width = getEpochDuration(epoch);
71
72 assert 0 <= t && t <= width;
73
74 return popSize1 + (t / width) * (popSize2 - popSize1);
75 }
76
77 public DemographicFunction getCopy() {
78 PiecewiseLinearPopulation df = new PiecewiseLinearPopulation(new double[intervals.length], new double[thetas.length], getUnits());
79 System.arraycopy(intervals, 0, df.intervals, 0, intervals.length);
80 System.arraycopy(thetas, 0, df.thetas, 0, thetas.length);
81
82 return df;
83 }
84
85 /**
86 * @return the value of the intensity function for the given epoch.
87 */
88 protected final double getIntensity(int epoch) {
89
90 final double N1 = getEpochDemographic(epoch);
91 final double N2 = getEpochDemographic(epoch + 1);
92 final double w = getEpochDuration(epoch);
93
94 if (N1 != N2) {
95 return w * Math.log(N2 / N1) / (N2 - N1);
96 } else {
97 return w / N1;
98 }
99 }
100
101 /**
102 * @return the value of the intensity function for the given epoch and time relative to start of epoch.
103 */
104 protected final double getIntensity(int epoch, double relativeTime) {
105 assert relativeTime <= getEpochDuration(epoch);
106
107 final double N1 = getEpochDemographic(epoch);
108 final double N2 = getEpochDemographic(epoch + 1);
109 final double w = getEpochDuration(epoch);
110
111 if (N1 != N2) {
112 return w * Math.log(N1 * w / (N2 * relativeTime + N1 * (w - relativeTime))) / (N1 - N2);
113 } else {
114 return relativeTime / N1;
115 }
116 }
117
118 /**
119 * @param targetI the intensity
120 * @return the time corresponding to the given target intensity
121 */
122 public final double getInverseIntensity(double targetI) {
123
124 if (cif != null) {
125
126 int epoch = Collections.binarySearch(cif, targetI);
127
128 if (epoch < 0) {
129 epoch = -epoch - 1;
130
131 if (epoch > 0) {
132
133 return endTime.get(epoch - 1) +
134 getInverseIntensity(epoch, targetI - cif.get(epoch - 1));
135 } else {
136 assert epoch == 0;
137 return getInverseIntensity(0, targetI);
138 }
139 } else {
140 return endTime.get(epoch);
141 }
142 } else {
143
144 int epoch = 0;
145 double cI = 0;
146 double eI = getIntensity(epoch);
147 double time = 0.0;
148 while (cI + eI < targetI) {
149 cI += eI;
150 time += getEpochDuration(epoch);
151
152 epoch += 1;
153 eI = getIntensity(epoch);
154 }
155
156 time += getInverseIntensity(epoch, targetI - cI);
157
158 return time;
159 }
160 }
161
162 /**
163 * @param epoch the epoch
164 * @param I intensity corresponding to relative time within this epoch
165 * @return the relative time within the given epoch to accumulate the given residual intensity
166 */
167 private double getInverseIntensity(int epoch, double I) {
168
169 final double N1 = getEpochDemographic(epoch);
170 final double N2 = getEpochDemographic(epoch + 1);
171 final double w = getEpochDuration(epoch);
172
173 final double dn = N2 - N1;
174 double time;
175 if (dn != 0.0) {
176 time = N1 * w * (Math.exp(I * dn / w) - 1.0) / dn;
177 } else {
178 time = I * N1;
179 }
180
181 return time;
182 }
183 }