| 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.evomodel.coalescent; |
| 27 |
|
| 28 |
import dr.evolution.coalescent.DemographicFunction; |
| 29 |
import dr.evolution.coalescent.PiecewiseConstantPopulation; |
| 30 |
import dr.evolution.coalescent.PiecewiseExponentialPopulation; |
| 31 |
import dr.evolution.coalescent.PiecewiseLinearPopulation; |
| 32 |
import dr.inference.model.Model; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.inference.model.Statistic; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class PiecewisePopulationModel extends DemographicModel { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public PiecewisePopulationModel(String name, Parameter N0Parameter, double[] epochLengths, boolean isLinear, Type units) { |
| 51 |
|
| 52 |
super(name); |
| 53 |
|
| 54 |
this.epochCount = epochLengths.length + 1; |
| 55 |
|
| 56 |
if (N0Parameter.getDimension() != epochCount) { |
| 57 |
throw new IllegalArgumentException( |
| 58 |
"epochSize parameter must have the same dimensions as the number of epochs: (" + epochCount + |
| 59 |
") but instead has " + N0Parameter.getDimension() + "!" |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
this.N0Parameter = N0Parameter; |
| 64 |
addVariable(N0Parameter); |
| 65 |
N0Parameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, N0Parameter.getDimension())); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
setUnits(units); |
| 71 |
|
| 72 |
if (isLinear) { |
| 73 |
piecewiseFunction = new PiecewiseLinearPopulation(epochLengths, new double[N0Parameter.getDimension()], units); |
| 74 |
} else { |
| 75 |
piecewiseFunction = new PiecewiseConstantPopulation(epochLengths, new double[N0Parameter.getDimension()], units); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public PiecewisePopulationModel(String name, Parameter N0Parameter, Parameter growthRatesParameter, |
| 83 |
double[] epochLengths, Type units) { |
| 84 |
|
| 85 |
super(name); |
| 86 |
|
| 87 |
this.epochCount = epochLengths.length + 1; |
| 88 |
|
| 89 |
this.N0Parameter = N0Parameter; |
| 90 |
this.growthRatesParameter = growthRatesParameter; |
| 91 |
addVariable(N0Parameter); |
| 92 |
N0Parameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, N0Parameter.getDimension())); |
| 93 |
|
| 94 |
addVariable(growthRatesParameter); |
| 95 |
growthRatesParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, growthRatesParameter.getDimension())); |
| 96 |
|
| 97 |
setUnits(units); |
| 98 |
|
| 99 |
int popSizeCount = N0Parameter.getDimension(); |
| 100 |
int growthRateCount = growthRatesParameter.getDimension(); |
| 101 |
|
| 102 |
if (popSizeCount == epochCount && growthRateCount == 1) { |
| 103 |
piecewiseFunction = new PiecewiseExponentialPopulation(epochLengths, |
| 104 |
new double[N0Parameter.getDimension()], |
| 105 |
growthRatesParameter.getParameterValue(0), |
| 106 |
units); |
| 107 |
} else if (popSizeCount == 1 && growthRateCount == epochCount) { |
| 108 |
piecewiseFunction = new PiecewiseExponentialPopulation(epochLengths, |
| 109 |
N0Parameter.getParameterValue(0), |
| 110 |
new double[growthRatesParameter.getDimension()], units); |
| 111 |
} else { |
| 112 |
if (growthRatesParameter.getDimension() != epochCount) { |
| 113 |
throw new IllegalArgumentException( |
| 114 |
"growthRate parameter must have the same dimension as the number of epochs: (" + epochCount + |
| 115 |
") but instead has " + N0Parameter.getDimension() + "!" |
| 116 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
addStatistic(new GrowthRateStatistic()); |
| 122 |
} |
| 123 |
|
| 124 |
public DemographicFunction getDemographicFunction() { |
| 125 |
if (growthRatesParameter != null) { |
| 126 |
|
| 127 |
for (int i = 0; i < N0Parameter.getDimension(); i++) { |
| 128 |
piecewiseFunction.setArgument(i, N0Parameter.getParameterValue(i)); |
| 129 |
} |
| 130 |
for (int i = 0; i < growthRatesParameter.getDimension(); i++) { |
| 131 |
piecewiseFunction.setArgument(i + N0Parameter.getDimension(), growthRatesParameter.getParameterValue(i)); |
| 132 |
} |
| 133 |
} else { |
| 134 |
|
| 135 |
for (int i = 0; i < N0Parameter.getDimension(); i++) { |
| 136 |
piecewiseFunction.setArgument(i, N0Parameter.getParameterValue(i)); |
| 137 |
} |
| 138 |
} |
| 139 |
return piecewiseFunction; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
protected void handleParameterChangedEvent(Parameter parameter, int index) { |
| 151 |
|
| 152 |
if (parameter == N0Parameter) { |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
protected void storeState() { |
| 160 |
} |
| 161 |
|
| 162 |
protected void restoreState() { |
| 163 |
} |
| 164 |
|
| 165 |
protected void acceptState() { |
| 166 |
} |
| 167 |
|
| 168 |
public class GrowthRateStatistic extends Statistic.Abstract { |
| 169 |
|
| 170 |
public GrowthRateStatistic() { |
| 171 |
super("growthRate"); |
| 172 |
} |
| 173 |
|
| 174 |
public int getDimension() { |
| 175 |
return ((PiecewiseExponentialPopulation) piecewiseFunction).getEpochCount(); |
| 176 |
} |
| 177 |
|
| 178 |
public double getStatisticValue(int i) { |
| 179 |
return ((PiecewiseExponentialPopulation) piecewiseFunction).getEpochGrowthRate(i); |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private Parameter N0Parameter; |
| 189 |
private Parameter growthRatesParameter; |
| 190 |
private DemographicFunction piecewiseFunction = null; |
| 191 |
|
| 192 |
private final int epochCount; |
| 193 |
} |