| 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.epidemiology; |
| 27 |
|
| 28 |
import dr.evolution.coalescent.DemographicFunction; |
| 29 |
import dr.evomodel.coalescent.DemographicModel; |
| 30 |
import dr.inference.loggers.LogColumn; |
| 31 |
import dr.inference.loggers.NumberColumn; |
| 32 |
import dr.inference.model.*; |
| 33 |
import dr.math.distributions.NormalDistribution; |
| 34 |
|
| 35 |
import java.util.Arrays; |
| 36 |
import java.util.HashSet; |
| 37 |
import java.util.Set; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public class SIRModel extends DemographicModel implements Likelihood { |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public SIRModel(Parameter reproductiveNumberParameter, |
| 63 |
Parameter recoveryRateParameter, |
| 64 |
Parameter hostPopulationSizeParameter, |
| 65 |
Parameter proportionsParameter, |
| 66 |
Type units) { |
| 67 |
|
| 68 |
this(SIRModelParser.SIR_MODEL, reproductiveNumberParameter, recoveryRateParameter, |
| 69 |
hostPopulationSizeParameter, proportionsParameter, units); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public SIRModel(String name, |
| 76 |
Parameter reproductiveNumberParameter, |
| 77 |
Parameter recoveryRateParameter, |
| 78 |
Parameter hostPopulationSizeParameter, |
| 79 |
Parameter proportionsParameter, |
| 80 |
Type units) { |
| 81 |
|
| 82 |
super(name); |
| 83 |
|
| 84 |
this.reproductiveNumberParameter = reproductiveNumberParameter; |
| 85 |
addVariable(reproductiveNumberParameter); |
| 86 |
reproductiveNumberParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 1.0, 1)); |
| 87 |
|
| 88 |
this.recoveryRateParameter = recoveryRateParameter; |
| 89 |
addVariable(recoveryRateParameter); |
| 90 |
recoveryRateParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1)); |
| 91 |
|
| 92 |
this.hostPopulationSizeParameter = hostPopulationSizeParameter; |
| 93 |
addVariable(hostPopulationSizeParameter); |
| 94 |
hostPopulationSizeParameter.addBounds(new Parameter.DefaultBounds(Double.MAX_VALUE, 0.0, 1)); |
| 95 |
|
| 96 |
this.proportionsParameter = proportionsParameter; |
| 97 |
addVariable(proportionsParameter); |
| 98 |
|
| 99 |
demographicFunction = new SIRDemographicFunction(units); |
| 100 |
setUnits(units); |
| 101 |
|
| 102 |
addStatistic(new TimeseriesStatistic("susceptibles")); |
| 103 |
addStatistic(new TimeseriesStatistic("infecteds")); |
| 104 |
addStatistic(new TimeseriesStatistic("recovereds")); |
| 105 |
addStatistic(new TimeseriesStatistic("effectivePopulationSize")); |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
protected void handleVariableChangedEvent(final Variable variable, final int index, final Variable.ChangeType type) { |
| 111 |
demographicFunction.reset(); |
| 112 |
fireModelChanged(); |
| 113 |
likelihoodKnown = false; |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
protected void storeState() { |
| 118 |
storedLogLikelihood = logLikelihood; |
| 119 |
demographicFunction.store(); |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
protected void restoreState() { |
| 124 |
logLikelihood = storedLogLikelihood; |
| 125 |
likelihoodKnown = true; |
| 126 |
demographicFunction.restore(); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
public double getSusceptibles(final double t) { |
| 131 |
return demographicFunction.getSusceptibles(t); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
public double getInfecteds(final double t) { |
| 136 |
return demographicFunction.getInfecteds(t); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
public double getRecovereds(final double t) { |
| 141 |
return demographicFunction.getRecovereds(t); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
public double getEffectivePopulationSize(final double t) { |
| 146 |
return demographicFunction.getDemographic(t); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public String prettyName() { |
| 152 |
return Likelihood.Abstract.getPrettyName(this); |
| 153 |
} |
| 154 |
|
| 155 |
public final double getLogLikelihood() { |
| 156 |
if (!getLikelihoodKnown()) { |
| 157 |
logLikelihood = calculateLogLikelihood(); |
| 158 |
likelihoodKnown = true; |
| 159 |
} |
| 160 |
return logLikelihood; |
| 161 |
} |
| 162 |
|
| 163 |
protected boolean getLikelihoodKnown() { |
| 164 |
return likelihoodKnown; |
| 165 |
} |
| 166 |
|
| 167 |
public boolean evaluateEarly() { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public Set<Likelihood> getLikelihoodSet() { |
| 173 |
return new HashSet<Likelihood>(Arrays.asList(this)); |
| 174 |
} |
| 175 |
|
| 176 |
public boolean isUsed() { |
| 177 |
return isUsed; |
| 178 |
} |
| 179 |
|
| 180 |
public void setUsed() { |
| 181 |
isUsed = true; |
| 182 |
} |
| 183 |
|
| 184 |
public Model getModel() { |
| 185 |
return this; |
| 186 |
} |
| 187 |
|
| 188 |
public void makeDirty() { |
| 189 |
likelihoodKnown = false; |
| 190 |
} |
| 191 |
|
| 192 |
public LogColumn[] getColumns() { |
| 193 |
return new LogColumn[]{ |
| 194 |
new LikelihoodColumn(getId()) |
| 195 |
}; |
| 196 |
} |
| 197 |
|
| 198 |
protected class LikelihoodColumn extends NumberColumn { |
| 199 |
public LikelihoodColumn(String label) { |
| 200 |
super(label); |
| 201 |
} |
| 202 |
|
| 203 |
public double getDoubleValue() { |
| 204 |
return getLogLikelihood(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
public double calculateLogLikelihood() { |
| 209 |
double r = demographicFunction.getRecovereds(endTime); |
| 210 |
return NormalDistribution.logPdf(r, 0, 100); |
| 211 |
} |
| 212 |
|
| 213 |
private boolean isUsed = false; |
| 214 |
private boolean likelihoodKnown = false; |
| 215 |
private double logLikelihood; |
| 216 |
private double storedLogLikelihood; |
| 217 |
|
| 218 |
private double stepSize = 0.01; |
| 219 |
private double endTime = 5; |
| 220 |
|
| 221 |
public DemographicFunction getDemographicFunction() { |
| 222 |
return demographicFunction; |
| 223 |
} |
| 224 |
|
| 225 |
Parameter reproductiveNumberParameter = null; |
| 226 |
Parameter recoveryRateParameter = null; |
| 227 |
Parameter hostPopulationSizeParameter = null; |
| 228 |
Parameter proportionsParameter = null; |
| 229 |
|
| 230 |
SIRDemographicFunction demographicFunction = null; |
| 231 |
|
| 232 |
class SIRDemographicFunction extends DemographicFunction.Abstract { |
| 233 |
|
| 234 |
DynamicalSystem syst = new DynamicalSystem(0, 0.01); |
| 235 |
|
| 236 |
public SIRDemographicFunction(Type units) { |
| 237 |
|
| 238 |
super(units); |
| 239 |
|
| 240 |
double hostPop = hostPopulationSizeParameter.getParameterValue(0); |
| 241 |
double initialS = hostPop * proportionsParameter.getParameterValue(0); |
| 242 |
double initialI = hostPop * proportionsParameter.getParameterValue(1); |
| 243 |
double initialR = hostPop * proportionsParameter.getParameterValue(2); |
| 244 |
|
| 245 |
syst.addVariable("susceptibles", initialS); |
| 246 |
syst.addVariable("infecteds", initialI); |
| 247 |
syst.addVariable("recovereds", initialR); |
| 248 |
syst.addVariable("total", hostPop); |
| 249 |
syst.addForce("contact", reproductiveNumberParameter.getParameterValue(0) * recoveryRateParameter.getParameterValue(0), |
| 250 |
new String[]{"infecteds","susceptibles"}, new String[]{"total"}, "susceptibles", "infecteds"); |
| 251 |
syst.addForce("recovery", recoveryRateParameter.getParameterValue(0), new String[]{"infecteds"}, |
| 252 |
new String[]{}, "infecteds", "recovereds"); |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
public void reset() { |
| 257 |
|
| 258 |
double hostPop = hostPopulationSizeParameter.getParameterValue(0); |
| 259 |
double initialS = hostPop * proportionsParameter.getParameterValue(0); |
| 260 |
double initialI = hostPop * proportionsParameter.getParameterValue(1); |
| 261 |
double initialR = hostPop * proportionsParameter.getParameterValue(2); |
| 262 |
|
| 263 |
syst.resetVar("susceptibles", initialS); |
| 264 |
syst.resetVar("infecteds", initialI); |
| 265 |
syst.resetVar("recovereds", initialR); |
| 266 |
syst.resetVar("total", hostPop); |
| 267 |
syst.resetForce("contact", reproductiveNumberParameter.getParameterValue(0) * recoveryRateParameter.getParameterValue(0)); |
| 268 |
syst.resetForce("recovery", recoveryRateParameter.getParameterValue(0)); |
| 269 |
syst.resetTime(); |
| 270 |
} |
| 271 |
|
| 272 |
public void store () { |
| 273 |
syst.store(); |
| 274 |
} |
| 275 |
|
| 276 |
public void restore () { |
| 277 |
syst.restore(); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
public double getDemographic(final double t) { |
| 282 |
|
| 283 |
double beta = reproductiveNumberParameter.getParameterValue(0) * recoveryRateParameter.getParameterValue(0); |
| 284 |
double numer = getInfecteds(t) * hostPopulationSizeParameter.getParameterValue(0); |
| 285 |
double denom = 2.0 * beta * getSusceptibles(t); |
| 286 |
double ne = numer / denom; |
| 287 |
|
| 288 |
if (ne < 0.001) { |
| 289 |
ne = 0.001; |
| 290 |
} |
| 291 |
|
| 292 |
return ne; |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
public double getLogDemographic(final double t) { |
| 298 |
return Math.log(getDemographic(t)); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
public double getSusceptibles(final double t) { |
| 303 |
return syst.getValue("susceptibles", t); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
public double getInfecteds(final double t) { |
| 308 |
return syst.getValue("infecteds", t); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
public double getRecovereds(final double t) { |
| 313 |
return syst.getValue("recovereds", t); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
public double getIntensity(final double t) { |
| 318 |
return 1.0; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
public double getInverseIntensity(final double x) { |
| 323 |
return 1.0; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
public double getIntegral(final double start, final double finish) { |
| 328 |
|
| 329 |
double neAvg = 0.5 * (getDemographic(start) + getDemographic(finish)); |
| 330 |
double integral = (finish-start)*(1.0 / neAvg); |
| 331 |
return integral ; |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
public int getNumArguments() { |
| 337 |
return 0; |
| 338 |
} |
| 339 |
|
| 340 |
public String getArgumentName(final int n) { |
| 341 |
return null; |
| 342 |
} |
| 343 |
|
| 344 |
public double getArgument(final int n) { |
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
public void setArgument(final int n, final double value) { |
| 349 |
} |
| 350 |
|
| 351 |
public double getLowerBound(final int n) { |
| 352 |
return 0; |
| 353 |
} |
| 354 |
|
| 355 |
public double getUpperBound(final int n) { |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
public DemographicFunction getCopy() { |
| 360 |
return null; |
| 361 |
} |
| 362 |
|
| 363 |
public double getThreshold() { |
| 364 |
return 0; |
| 365 |
} |
| 366 |
|
| 367 |
} |
| 368 |
|
| 369 |
public class TimeseriesStatistic extends Statistic.Abstract { |
| 370 |
|
| 371 |
public TimeseriesStatistic(String name) { |
| 372 |
super(name); |
| 373 |
} |
| 374 |
|
| 375 |
@Override |
| 376 |
public String getDimensionName(final int i) { |
| 377 |
double t = (double) i * stepSize; |
| 378 |
return Double.toString(t); |
| 379 |
} |
| 380 |
|
| 381 |
public int getDimension() { |
| 382 |
return (int) (endTime / stepSize); |
| 383 |
} |
| 384 |
|
| 385 |
public double getStatisticValue(final int i) { |
| 386 |
double t = (double) i * stepSize; |
| 387 |
if (getStatisticName().equals("susceptibles")) { |
| 388 |
return getSusceptibles(t); |
| 389 |
} |
| 390 |
else if (getStatisticName().equals("infecteds")) { |
| 391 |
return getInfecteds(t); |
| 392 |
} |
| 393 |
else if (getStatisticName().equals("recovereds")) { |
| 394 |
return getRecovereds(t); |
| 395 |
} |
| 396 |
else if (getStatisticName().equals("effectivePopulationSize")) { |
| 397 |
return getEffectivePopulationSize(t); |
| 398 |
} |
| 399 |
else { |
| 400 |
return 0.0; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
} |