| 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.continuous; |
| 27 |
|
| 28 |
import dr.geo.math.SphericalPolarCoordinates; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.xml.*; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class GreatCircleDiffusionModel extends MultivariateDiffusionModel { |
| 37 |
|
| 38 |
public static final String DIFFUSION_PROCESS = "greatCircleDiffusionModel"; |
| 39 |
public static final String DIFFUSION_CONSTANT = "precision"; |
| 40 |
public static final String COEFFICIENT = "diffusionCoefficient"; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public GreatCircleDiffusionModel(Parameter precision, Parameter coefficient) { |
| 45 |
super(); |
| 46 |
this.precision = precision; |
| 47 |
addVariable(precision); |
| 48 |
this.coefficient = coefficient; |
| 49 |
if (coefficient != null) |
| 50 |
addVariable(coefficient); |
| 51 |
} |
| 52 |
|
| 53 |
public GreatCircleDiffusionModel(Parameter precision) { |
| 54 |
this(precision, null); |
| 55 |
} |
| 56 |
|
| 57 |
protected double calculateLogDensity(double[] start, double[] stop, double time) { |
| 58 |
|
| 59 |
SphericalPolarCoordinates coord1 = new SphericalPolarCoordinates(start[0], start[1]); |
| 60 |
SphericalPolarCoordinates coord2 = new SphericalPolarCoordinates(stop[0], stop[1]); |
| 61 |
|
| 62 |
double distance = coord1.distance(coord2); |
| 63 |
|
| 64 |
double inverseVariance = precision.getParameterValue(0) / time; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if (coefficient == null) |
| 69 |
return -LOG2PI + Math.log(inverseVariance) - 0.5 * (distance * distance * inverseVariance); |
| 70 |
double coef = -coefficient.getParameterValue(0); |
| 71 |
return -LOG2PI + coef * Math.log(inverseVariance) - 0.5 * (distance * distance * Math.pow(inverseVariance, coef)); |
| 72 |
} |
| 73 |
|
| 74 |
protected void calculatePrecisionInfo() { |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 79 |
|
| 80 |
public String getParserName() { |
| 81 |
return DIFFUSION_PROCESS; |
| 82 |
} |
| 83 |
|
| 84 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 85 |
|
| 86 |
Parameter diffusionParam = (Parameter) xo.getChild(Parameter.class); |
| 87 |
Parameter coefficient = null; |
| 88 |
if (xo.hasChildNamed(COEFFICIENT)) |
| 89 |
coefficient = (Parameter) xo.getChild(COEFFICIENT).getChild(Parameter.class); |
| 90 |
|
| 91 |
return new GreatCircleDiffusionModel(diffusionParam, coefficient); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public String getParserDescription() { |
| 99 |
return "Describes a bivariate diffusion process using great circle distances."; |
| 100 |
} |
| 101 |
|
| 102 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 103 |
return rules; |
| 104 |
} |
| 105 |
|
| 106 |
private final XMLSyntaxRule[] rules = { |
| 107 |
new ElementRule(Parameter.class), |
| 108 |
new ElementRule(COEFFICIENT, |
| 109 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true), |
| 110 |
}; |
| 111 |
|
| 112 |
public Class getReturnType() { |
| 113 |
return MultivariateDiffusionModel.class; |
| 114 |
} |
| 115 |
}; |
| 116 |
|
| 117 |
private Parameter precision; |
| 118 |
private Parameter coefficient; |
| 119 |
|
| 120 |
} |