| 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.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.MathUtils; |
| 33 |
import dr.xml.*; |
| 34 |
import org.w3c.dom.Document; |
| 35 |
import org.w3c.dom.Element; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class DiffusionModel extends AbstractModel { |
| 45 |
|
| 46 |
public static final String DIFFUSION_PROCESS = "diffusionProcess"; |
| 47 |
public static final String DIFFUSION_CONSTANT = "D"; |
| 48 |
public static final String BIAS = "mu"; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public DiffusionModel(Parameter diffusionRateParameter) { |
| 54 |
|
| 55 |
super(DIFFUSION_PROCESS); |
| 56 |
|
| 57 |
this.diffusionRateParameter = diffusionRateParameter; |
| 58 |
addVariable(diffusionRateParameter); |
| 59 |
diffusionRateParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public DiffusionModel(Parameter diffusionRateParameter, Parameter biasParameter) { |
| 66 |
|
| 67 |
super(DIFFUSION_PROCESS); |
| 68 |
|
| 69 |
this.diffusionRateParameter = diffusionRateParameter; |
| 70 |
this.biasParameter = biasParameter; |
| 71 |
addVariable(diffusionRateParameter); |
| 72 |
addVariable(biasParameter); |
| 73 |
diffusionRateParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 74 |
} |
| 75 |
|
| 76 |
public double getD() { |
| 77 |
return diffusionRateParameter.getParameterValue(0); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public double getLogLikelihood(double start, double stop, double time) { |
| 84 |
|
| 85 |
double D = diffusionRateParameter.getParameterValue(0); |
| 86 |
double bias = getBias(); |
| 87 |
|
| 88 |
|
| 89 |
double Dtime = D * time; |
| 90 |
|
| 91 |
double unbiasedDistance = (stop - start) - (bias * time); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
return -0.5 * Math.log(Dtime) - ((unbiasedDistance * unbiasedDistance) / (Dtime)); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public double simulateForward(double value, double time) { |
| 105 |
double D = diffusionRateParameter.getParameterValue(0); |
| 106 |
double delta = MathUtils.nextGaussian(); |
| 107 |
|
| 108 |
delta *= Math.sqrt(D * time); |
| 109 |
delta += getBias() * time; |
| 110 |
|
| 111 |
return value + delta; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
private double getBias() { |
| 119 |
if (biasParameter == null) return 0.0; |
| 120 |
return biasParameter.getParameterValue(0); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public void handleModelChangedEvent(Model model, Object object, int index) { |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
public void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
protected void storeState() { |
| 136 |
} |
| 137 |
|
| 138 |
protected void restoreState() { |
| 139 |
} |
| 140 |
|
| 141 |
protected void acceptState() { |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public Element createElement(Document document) { |
| 149 |
throw new RuntimeException("Not implemented!"); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 157 |
|
| 158 |
public String getParserName() { |
| 159 |
return DIFFUSION_PROCESS; |
| 160 |
} |
| 161 |
|
| 162 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 163 |
|
| 164 |
XMLObject cxo = xo.getChild(DIFFUSION_CONSTANT); |
| 165 |
Parameter diffusionParam = (Parameter) cxo.getChild(Parameter.class); |
| 166 |
|
| 167 |
Parameter biasParam = null; |
| 168 |
if (xo.hasAttribute(BIAS)) { |
| 169 |
cxo = xo.getChild(BIAS); |
| 170 |
biasParam = (Parameter) cxo.getChild(Parameter.class); |
| 171 |
} |
| 172 |
|
| 173 |
if (biasParam == null) { |
| 174 |
return new DiffusionModel(diffusionParam); |
| 175 |
} |
| 176 |
return new DiffusionModel(diffusionParam, biasParam); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public String getParserDescription() { |
| 184 |
return "Describes a diffusion process."; |
| 185 |
} |
| 186 |
|
| 187 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 188 |
return rules; |
| 189 |
} |
| 190 |
|
| 191 |
private final XMLSyntaxRule[] rules = { |
| 192 |
new ElementRule(DIFFUSION_CONSTANT, |
| 193 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 194 |
new ElementRule(BIAS, |
| 195 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}) |
| 196 |
}; |
| 197 |
|
| 198 |
public Class getReturnType() { |
| 199 |
return DiffusionModel.class; |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
private final Parameter diffusionRateParameter; |
| 208 |
private Parameter biasParameter; |
| 209 |
} |