| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evolution.coalescent; |
| 27 |
|
| 28 |
import java.util.Collections; |
| 29 |
import java.util.ArrayList; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class PiecewiseExponentialPopulation extends DemographicFunction.Abstract { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public PiecewiseExponentialPopulation(double[] intervals, double N0, double[] lambdas, Type units) { |
| 43 |
super(units); |
| 44 |
if (lambdas == null || intervals == null) { |
| 45 |
throw new IllegalArgumentException(); |
| 46 |
} |
| 47 |
if (lambdas.length != intervals.length + 1) { |
| 48 |
throw new IllegalArgumentException(); |
| 49 |
} |
| 50 |
|
| 51 |
this.thetas = new double[] { N0 }; |
| 52 |
this.intervals = intervals; |
| 53 |
this.lambdas = lambdas; |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public PiecewiseExponentialPopulation(double[] intervals, double[] thetas, double lambda, Type units) { |
| 63 |
super(units); |
| 64 |
if (thetas == null || intervals == null) { |
| 65 |
throw new IllegalArgumentException(); |
| 66 |
} |
| 67 |
if (thetas.length != intervals.length + 1) { |
| 68 |
throw new IllegalArgumentException(); |
| 69 |
} |
| 70 |
|
| 71 |
this.thetas = thetas; |
| 72 |
this.intervals = intervals; |
| 73 |
this.lambdas = new double[] { lambda }; |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public double getDemographic(double t) { |
| 82 |
if (thetas.length == 1) { |
| 83 |
int epoch = 0; |
| 84 |
double t1 = t; |
| 85 |
double N1 = thetas[0]; |
| 86 |
double dt = getEpochDuration(epoch); |
| 87 |
while (t1 > dt) { |
| 88 |
N1 = N1 * Math.exp(-lambdas[epoch] * dt); |
| 89 |
t1 -= dt; |
| 90 |
epoch ++; |
| 91 |
dt = getEpochDuration(epoch); |
| 92 |
} |
| 93 |
return N1 * Math.exp(-lambdas[epoch] * t1); |
| 94 |
} else { |
| 95 |
int epoch = 0; |
| 96 |
double t1 = t; |
| 97 |
double N1 = thetas[0]; |
| 98 |
double dt = getEpochDuration(epoch); |
| 99 |
while (t1 > dt) { |
| 100 |
epoch ++; |
| 101 |
N1 = thetas[epoch]; |
| 102 |
t1 -= dt; |
| 103 |
dt = getEpochDuration(epoch); |
| 104 |
} |
| 105 |
double r; |
| 106 |
if (epoch + 1 >= thetas.length) { |
| 107 |
r = lambdas[0]; |
| 108 |
} else { |
| 109 |
double N2 = thetas[epoch + 1]; |
| 110 |
if (N1 < N2) { |
| 111 |
r = -(Math.log(N2) - Math.log(N1)) / dt; |
| 112 |
} else if (N1 > N2) { |
| 113 |
r = (Math.log(N1) - Math.log(N2)) / dt; |
| 114 |
} else { |
| 115 |
r = 0.0; |
| 116 |
} |
| 117 |
} |
| 118 |
return N1 * Math.exp(-r * t1); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public double getIntensity(double t) { |
| 126 |
throw new RuntimeException("Not implemented!"); |
| 127 |
} |
| 128 |
|
| 129 |
public double getInverseIntensity(double x) { |
| 130 |
throw new RuntimeException("Not implemented!"); |
| 131 |
} |
| 132 |
|
| 133 |
public double getIntegral(double start, double finish) { |
| 134 |
|
| 135 |
|
| 136 |
final double numerical = getNumericalIntegral(start, finish); |
| 137 |
return numerical; |
| 138 |
} |
| 139 |
|
| 140 |
public double getUpperBound(int i) { |
| 141 |
return 1e9; |
| 142 |
} |
| 143 |
|
| 144 |
public double getLowerBound(int i) { |
| 145 |
return Double.MIN_VALUE; |
| 146 |
} |
| 147 |
|
| 148 |
public int getNumArguments() { |
| 149 |
return lambdas.length + 1; |
| 150 |
} |
| 151 |
|
| 152 |
public String getArgumentName(int i) { |
| 153 |
if (i < thetas.length) { |
| 154 |
return "theta" + i; |
| 155 |
} |
| 156 |
return "lambda" + (i - thetas.length); |
| 157 |
} |
| 158 |
|
| 159 |
public double getArgument(int i) { |
| 160 |
if (i < thetas.length) { |
| 161 |
return thetas[i]; |
| 162 |
} |
| 163 |
return lambdas[i - thetas.length]; |
| 164 |
} |
| 165 |
|
| 166 |
public void setArgument(int i, double value) { |
| 167 |
if (i < thetas.length) { |
| 168 |
thetas[i] = value; |
| 169 |
} else { |
| 170 |
lambdas[i - thetas.length] = value; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
public DemographicFunction getCopy() { |
| 175 |
PiecewiseExponentialPopulation df; |
| 176 |
|
| 177 |
if (thetas.length == 1) { |
| 178 |
df = new PiecewiseExponentialPopulation(new double[intervals.length], thetas[0], new double[lambdas.length], getUnits()); |
| 179 |
} else { |
| 180 |
df = new PiecewiseExponentialPopulation(new double[intervals.length], new double[thetas.length], lambdas[0], getUnits()); |
| 181 |
} |
| 182 |
System.arraycopy(intervals, 0, df.intervals, 0, intervals.length); |
| 183 |
System.arraycopy(thetas, 0, df.thetas, 0, thetas.length); |
| 184 |
System.arraycopy(lambdas, 0, df.lambdas, 0, lambdas.length); |
| 185 |
|
| 186 |
return df; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
protected double getDemographic(int epoch, double t) { |
| 194 |
return getEpochDemographic(epoch); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
protected double getIntensity(int epoch) { |
| 201 |
return getEpochDuration(epoch) / getEpochDemographic(epoch); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
protected double getIntensity(final int epoch, final double relativeTime) { |
| 208 |
assert 0 <= relativeTime && relativeTime <= getEpochDuration(epoch); |
| 209 |
|
| 210 |
return relativeTime / getEpochDemographic(epoch); |
| 211 |
} |
| 212 |
|
| 213 |
public int getEpochCount() { |
| 214 |
return intervals.length + 1; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public double getEpochDuration(int epoch) { |
| 221 |
if (epoch == intervals.length) { |
| 222 |
return Double.POSITIVE_INFINITY; |
| 223 |
} else return intervals[epoch]; |
| 224 |
} |
| 225 |
|
| 226 |
public void setEpochDuration(int epoch, double duration) { |
| 227 |
if (epoch < 0 || epoch >= intervals.length) { |
| 228 |
throw new IllegalArgumentException("epoch must be between 0 and " + (intervals.length - 1)); |
| 229 |
} |
| 230 |
if (duration < 0.0) { |
| 231 |
throw new IllegalArgumentException("duration must be positive."); |
| 232 |
} |
| 233 |
intervals[epoch] = duration; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public final double getEpochDemographic(int epoch) { |
| 240 |
if (epoch < 0 || epoch > intervals.length) { |
| 241 |
throw new IllegalArgumentException(); |
| 242 |
} |
| 243 |
|
| 244 |
if (thetas.length == 1) { |
| 245 |
int ep = 0; |
| 246 |
double N1 = thetas[0]; |
| 247 |
double dt = getEpochDuration(ep); |
| 248 |
while (epoch > ep) { |
| 249 |
N1 = N1 * Math.exp(-lambdas[ep] * dt); |
| 250 |
ep ++; |
| 251 |
dt = getEpochDuration(ep); |
| 252 |
} |
| 253 |
return N1; |
| 254 |
} else { |
| 255 |
return thetas[epoch]; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public final double getEpochGrowthRate(int epoch) { |
| 263 |
if (epoch < 0 || epoch > intervals.length) { |
| 264 |
throw new IllegalArgumentException(); |
| 265 |
} |
| 266 |
if (thetas.length == 1) { |
| 267 |
return lambdas[epoch]; |
| 268 |
} else { |
| 269 |
if (epoch >= thetas.length - 1) { |
| 270 |
return lambdas[0]; |
| 271 |
} else { |
| 272 |
double N1 = thetas[epoch]; |
| 273 |
double N2 = thetas[epoch + 1]; |
| 274 |
double dt = getEpochDuration(epoch); |
| 275 |
if (N1 < N2) { |
| 276 |
return -Math.log(N2 - N1) / dt; |
| 277 |
} else if (N1 > N2) { |
| 278 |
return Math.log(N1 - N2) / dt; |
| 279 |
} else { |
| 280 |
return 0.0; |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
public String toString() { |
| 287 |
StringBuffer buffer = new StringBuffer(); |
| 288 |
for (int i = 0; i < thetas.length; i++) { |
| 289 |
buffer.append("\t").append(thetas[i]); |
| 290 |
} |
| 291 |
for (int i = 0; i < lambdas.length; i++) { |
| 292 |
buffer.append("\t").append(lambdas[i]); |
| 293 |
} |
| 294 |
return buffer.toString(); |
| 295 |
} |
| 296 |
|
| 297 |
double[] intervals; |
| 298 |
double[] thetas; |
| 299 |
double[] lambdas; |
| 300 |
|
| 301 |
} |