| 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.ArrayList; |
| 29 |
import java.util.Collections; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public PiecewiseConstantPopulation(Type units) { |
| 48 |
super(units); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 141 |
|
| 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 |
|
| 193 |
|
| 194 |
protected double getDemographic(int epoch, double t) { |
| 195 |
return getEpochDemographic(epoch); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
protected double getIntensity(int epoch) { |
| 202 |
return getEpochDuration(epoch) / getEpochDemographic(epoch); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 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 |
|
| 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 |
|
| 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 |
} |