| 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.casetocase; |
| 27 |
|
| 28 |
import dr.inference.model.AbstractModel; |
| 29 |
import dr.inference.model.Model; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.model.Variable; |
| 32 |
import dr.math.IntegrableUnivariateFunction; |
| 33 |
import dr.math.RiemannApproximation; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
import java.lang.reflect.Constructor; |
| 37 |
import java.lang.reflect.InvocationTargetException; |
| 38 |
import java.util.ArrayList; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public abstract class SpatialKernel extends AbstractModel implements IntegrableUnivariateFunction { |
| 47 |
|
| 48 |
private ArrayList<Parameter> params; |
| 49 |
|
| 50 |
public SpatialKernel(String name){ |
| 51 |
super(name); |
| 52 |
} |
| 53 |
|
| 54 |
public ArrayList<Parameter> getParams(){ |
| 55 |
return params; |
| 56 |
} |
| 57 |
|
| 58 |
public Parameter getParam(int index){ |
| 59 |
return params.get(index); |
| 60 |
} |
| 61 |
|
| 62 |
public void setParam(int index, Parameter value){ |
| 63 |
params.set(index, value); |
| 64 |
} |
| 65 |
|
| 66 |
public static final String SPATIAL_KERNEL_FUNCTION = "spatialKernelFunction"; |
| 67 |
public static final String PARAMETERS = "parameters"; |
| 68 |
public static final String KERNEL_TYPE = "type"; |
| 69 |
public static final String INTEGRATOR_STEPS = "integratorSteps"; |
| 70 |
public static final String ALPHA = "kernel.alpha"; |
| 71 |
public static final String R_0 = "kernel.r0"; |
| 72 |
|
| 73 |
public enum Type{ |
| 74 |
EXPONENTIAL ("exponential", Exponential.class), |
| 75 |
POWER_LAW ("powerLaw", PowerLaw.class), |
| 76 |
GAUSSIAN ("gaussian", Gaussian.class), |
| 77 |
LOGISTIC ("logistic", Logistic.class); |
| 78 |
|
| 79 |
private final String xmlName; |
| 80 |
private final Class kernelClass; |
| 81 |
|
| 82 |
String getXmlName(){ |
| 83 |
return xmlName; |
| 84 |
} |
| 85 |
|
| 86 |
SpatialKernel makeKernelFunction(ArrayList<Parameter> parameters) throws IllegalAccessException, |
| 87 |
InstantiationException, InvocationTargetException, NoSuchMethodException { |
| 88 |
|
| 89 |
|
| 90 |
Constructor constructor = kernelClass.getConstructor(SpatialKernel.class, String.class, ArrayList.class); |
| 91 |
|
| 92 |
return (SpatialKernel)constructor.newInstance(null, xmlName, parameters); |
| 93 |
} |
| 94 |
|
| 95 |
Type(String xmlName, Class kernelClass){ |
| 96 |
this.xmlName = xmlName; |
| 97 |
this.kernelClass = kernelClass; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public static double EuclideanDistance(double[] point1, double[] point2){ |
| 102 |
return Math.sqrt(Math.pow(point1[0]-point2[0],2) + Math.pow(point1[1]-point2[1], 2)); |
| 103 |
} |
| 104 |
|
| 105 |
public double getUpperBound(){ |
| 106 |
return Double.POSITIVE_INFINITY; |
| 107 |
} |
| 108 |
|
| 109 |
public double getLowerBound(){ |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
protected void handleModelChangedEvent(Model model, Object object, int index){ |
| 114 |
fireModelChanged(); |
| 115 |
} |
| 116 |
|
| 117 |
protected void storeState(){ |
| 118 |
} |
| 119 |
|
| 120 |
protected void restoreState(){ |
| 121 |
} |
| 122 |
|
| 123 |
public double value(double distance){ |
| 124 |
return evaluate(distance); |
| 125 |
} |
| 126 |
|
| 127 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type){ |
| 128 |
fireModelChanged(); |
| 129 |
} |
| 130 |
|
| 131 |
protected void acceptState(){ |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public void configureIntegrator(int sampleSize){} |
| 138 |
|
| 139 |
public abstract SpatialKernel newInstance(ArrayList<Parameter> params) throws InstantiationException; |
| 140 |
|
| 141 |
public class Exponential extends SpatialKernel { |
| 142 |
|
| 143 |
private Parameter alpha; |
| 144 |
|
| 145 |
public SpatialKernel newInstance(ArrayList<Parameter> params) throws InstantiationException { |
| 146 |
return new Exponential(Type.EXPONENTIAL.getXmlName(), params); |
| 147 |
} |
| 148 |
|
| 149 |
public Exponential(String name, ArrayList<Parameter> params) throws InstantiationException { |
| 150 |
super(name); |
| 151 |
|
| 152 |
if(params.size()!=1){ |
| 153 |
throw new InstantiationException("Wrong number of parameters for this spatial kernal function"); |
| 154 |
} |
| 155 |
|
| 156 |
if(!params.get(0).getId().equals(ALPHA)){ |
| 157 |
throw new InstantiationException("No parameter named alpha"); |
| 158 |
} |
| 159 |
|
| 160 |
this.alpha = params.get(0); |
| 161 |
addVariable(alpha); |
| 162 |
} |
| 163 |
|
| 164 |
public Exponential() throws InstantiationException { |
| 165 |
this(Type.EXPONENTIAL.getXmlName(), null); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public double evaluate(double argument) { |
| 171 |
return evaluate(argument, alpha.getParameterValue(0)); |
| 172 |
} |
| 173 |
|
| 174 |
public double evaluate(double argument, double alpha){ |
| 175 |
return Math.exp(-argument * alpha); |
| 176 |
} |
| 177 |
|
| 178 |
public double evaluateIntegral(double a, double b){ |
| 179 |
double aValue = alpha.getParameterValue(0); |
| 180 |
return -(1/aValue)*Math.exp(-aValue*b) + (1/aValue)*Math.exp(-aValue*a); |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
public class PowerLaw extends SpatialKernel { |
| 186 |
|
| 187 |
private Parameter alpha; |
| 188 |
|
| 189 |
public SpatialKernel newInstance(ArrayList<Parameter> params) throws InstantiationException{ |
| 190 |
return new PowerLaw(Type.POWER_LAW.getXmlName(), params); |
| 191 |
} |
| 192 |
|
| 193 |
public PowerLaw(String name, ArrayList<Parameter> params) throws InstantiationException { |
| 194 |
super(name); |
| 195 |
if(params.size()!=1){ |
| 196 |
throw new InstantiationException("Wrong number of parameters for this spatial kernal function"); |
| 197 |
} |
| 198 |
|
| 199 |
if(!params.get(0).getId().equals(ALPHA)){ |
| 200 |
throw new InstantiationException("No parameter named alpha"); |
| 201 |
} |
| 202 |
this.alpha = params.get(0); |
| 203 |
addVariable(alpha); |
| 204 |
} |
| 205 |
|
| 206 |
public PowerLaw() throws InstantiationException { |
| 207 |
this(Type.POWER_LAW.getXmlName(), null); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public double evaluate(double argument) { |
| 213 |
return evaluate(argument, alpha.getParameterValue(0)); |
| 214 |
} |
| 215 |
|
| 216 |
public double evaluate(double argument, double alpha){ |
| 217 |
return Math.pow(argument, -alpha); |
| 218 |
} |
| 219 |
|
| 220 |
public double evaluateIntegral(double a, double b){ |
| 221 |
double aValue = params.get(0).getParameterValue(0); |
| 222 |
return -aValue*Math.pow(b, -aValue-1) + -aValue*Math.pow(a, -aValue-1); |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
public class Gaussian extends SpatialKernel { |
| 228 |
|
| 229 |
private Parameter alpha; |
| 230 |
|
| 231 |
RiemannApproximation integrator; |
| 232 |
|
| 233 |
public SpatialKernel newInstance(ArrayList<Parameter> params) throws InstantiationException{ |
| 234 |
return new Gaussian(Type.GAUSSIAN.getXmlName(), params); |
| 235 |
} |
| 236 |
|
| 237 |
public Gaussian(String name, ArrayList<Parameter> params) throws InstantiationException { |
| 238 |
this(name, params, 25); |
| 239 |
} |
| 240 |
|
| 241 |
public Gaussian() throws InstantiationException { |
| 242 |
this(Type.GAUSSIAN.getXmlName(), null); |
| 243 |
} |
| 244 |
|
| 245 |
public void configureIntegrator(int sampleSize){ |
| 246 |
integrator = new RiemannApproximation(sampleSize); |
| 247 |
} |
| 248 |
|
| 249 |
public Gaussian(String name, ArrayList<Parameter> params, int steps) throws InstantiationException { |
| 250 |
super(name); |
| 251 |
|
| 252 |
if(params.size()!=1){ |
| 253 |
throw new InstantiationException("Wrong number of parameters for this spatial kernal function"); |
| 254 |
} |
| 255 |
|
| 256 |
if(!params.get(0).getId().equals(ALPHA)){ |
| 257 |
throw new InstantiationException("No parameter named alpha"); |
| 258 |
} |
| 259 |
|
| 260 |
this.alpha = params.get(0); |
| 261 |
addVariable(alpha); |
| 262 |
|
| 263 |
|
| 264 |
integrator = new RiemannApproximation(steps); |
| 265 |
} |
| 266 |
|
| 267 |
public double evaluate(double argument) { |
| 268 |
return evaluate(argument, alpha.getParameterValue(0)); |
| 269 |
} |
| 270 |
|
| 271 |
public double evaluate(double argument, double alpha){ |
| 272 |
return Math.exp(-Math.pow(argument, 2) * alpha); |
| 273 |
} |
| 274 |
|
| 275 |
public double evaluateIntegral(double a, double b){ |
| 276 |
return integrator.integrate(this, a, b); |
| 277 |
} |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
public class Logistic extends SpatialKernel { |
| 282 |
|
| 283 |
private Parameter alpha; |
| 284 |
private Parameter r_0; |
| 285 |
|
| 286 |
RiemannApproximation integrator; |
| 287 |
|
| 288 |
public SpatialKernel newInstance(ArrayList<Parameter> params) throws InstantiationException{ |
| 289 |
|
| 290 |
return new Logistic(Type.LOGISTIC.getXmlName(), params); |
| 291 |
} |
| 292 |
|
| 293 |
public Logistic() throws InstantiationException { |
| 294 |
this(Type.GAUSSIAN.getXmlName(), null); |
| 295 |
} |
| 296 |
|
| 297 |
public Logistic(String name, ArrayList<Parameter> params) throws InstantiationException { |
| 298 |
this(name, params, 25); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
public void configureIntegrator(int sampleSize){ |
| 303 |
integrator = new RiemannApproximation(sampleSize); |
| 304 |
} |
| 305 |
|
| 306 |
public Logistic(String name, ArrayList<Parameter> params, int steps) throws InstantiationException { |
| 307 |
super(name); |
| 308 |
|
| 309 |
if(params.size()!=2){ |
| 310 |
throw new InstantiationException("Wrong number of parameters for this spatial kernal function"); |
| 311 |
} |
| 312 |
|
| 313 |
boolean hasAlpha = false; |
| 314 |
boolean hasR0 = false; |
| 315 |
|
| 316 |
for(Parameter parameter : params){ |
| 317 |
if(parameter.getId().equals(ALPHA)){ |
| 318 |
hasAlpha = true; |
| 319 |
this.alpha = parameter; |
| 320 |
} |
| 321 |
if( parameter.getId().equals(R_0)){ |
| 322 |
hasR0 = true; |
| 323 |
this.r_0 = parameter; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
if(!hasAlpha || !hasR0){ |
| 328 |
throw new InstantiationException("Kernel function does not have the required parameters"); |
| 329 |
} |
| 330 |
|
| 331 |
addVariable(alpha); |
| 332 |
addVariable(r_0); |
| 333 |
|
| 334 |
integrator = new RiemannApproximation(steps); |
| 335 |
} |
| 336 |
|
| 337 |
public double evaluate(double argument) { |
| 338 |
return evaluate(argument, alpha.getParameterValue(0), r_0.getParameterValue(0)); |
| 339 |
} |
| 340 |
|
| 341 |
public double evaluate(double argument, double alpha, double r_0){ |
| 342 |
return 1/(1+Math.pow((argument/r_0), alpha)); |
| 343 |
} |
| 344 |
|
| 345 |
public double evaluateIntegral(double a, double b){ |
| 346 |
return integrator.integrate(this, a, b); |
| 347 |
} |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 356 |
|
| 357 |
public String getParserName(){ |
| 358 |
return SPATIAL_KERNEL_FUNCTION; |
| 359 |
} |
| 360 |
|
| 361 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 362 |
try{ |
| 363 |
String type = (String)xo.getAttribute("type"); |
| 364 |
XMLObject parameters = xo.getChild(PARAMETERS); |
| 365 |
ArrayList<Parameter> params = new ArrayList<Parameter>(); |
| 366 |
for(int i=0; i<parameters.getChildCount(); i++){ |
| 367 |
Parameter cxo = (Parameter)parameters.getChild(i); |
| 368 |
params.add(cxo); |
| 369 |
} |
| 370 |
|
| 371 |
SpatialKernel kernelFunction = null; |
| 372 |
for(Type value : Type.values()){ |
| 373 |
if(value.getXmlName().equals(type)){ |
| 374 |
kernelFunction = value.makeKernelFunction(params); |
| 375 |
} |
| 376 |
} |
| 377 |
if(kernelFunction==null){ |
| 378 |
throw new XMLParseException("Unknown spatial kernel type"); |
| 379 |
} |
| 380 |
|
| 381 |
if(xo.hasAttribute(INTEGRATOR_STEPS)){ |
| 382 |
kernelFunction.configureIntegrator(Integer.parseInt((String)xo.getAttribute(INTEGRATOR_STEPS))); |
| 383 |
} |
| 384 |
return kernelFunction; |
| 385 |
} catch(Exception e){ |
| 386 |
throw new XMLParseException("Failed to initiate spatial kernel ("+e.toString()+")"); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
@Override |
| 391 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 392 |
return rules; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
public final XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 397 |
new ElementRule(PARAMETERS, new XMLSyntaxRule[]{ |
| 398 |
new ElementRule(Parameter.class, 1, Integer.MAX_VALUE) |
| 399 |
}, 1, 1), |
| 400 |
AttributeRule.newStringRule(KERNEL_TYPE), |
| 401 |
AttributeRule.newIntegerRule(INTEGRATOR_STEPS, true) |
| 402 |
}; |
| 403 |
|
| 404 |
public String getParserDescription() { |
| 405 |
return "This element represents a spatial kernel function with a single parameter."; |
| 406 |
} |
| 407 |
|
| 408 |
public Class getReturnType() { |
| 409 |
return SpatialKernel.class; |
| 410 |
} |
| 411 |
}; |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
} |