| 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.Parameter; |
| 29 |
import dr.xml.*; |
| 30 |
|
| 31 |
import java.io.File; |
| 32 |
import java.io.FileNotFoundException; |
| 33 |
import java.io.FileReader; |
| 34 |
import java.io.IOException; |
| 35 |
import java.util.HashMap; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class BivariateDiscreteDiffusionModel extends MultivariateDiffusionModel { |
| 45 |
|
| 46 |
public static final String DISCRETE_DIFFUSION_PROCESS = "multivariateDiscreteDiffusionModel"; |
| 47 |
public static final String GRID_X_DIMENSION = "xGridDimension"; |
| 48 |
public static final String GRID_Y_DIMENSION = "yGridDimension"; |
| 49 |
|
| 50 |
public static final String EVEC_NAME = "eigenvectorsFileName"; |
| 51 |
public static final String EVAL_NAME = "eigenvaluesFileName"; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public BivariateDiscreteDiffusionModel(Parameter graphRate, |
| 57 |
int xDim, int yDim, |
| 58 |
double[] eVal, double[][] eVec) { |
| 59 |
super(); |
| 60 |
|
| 61 |
this.graphRate = graphRate; |
| 62 |
this.xDim = xDim; |
| 63 |
this.yDim = yDim; |
| 64 |
this.totalDim = xDim * yDim; |
| 65 |
this.eVal = eVal; |
| 66 |
this.eVec = eVec; |
| 67 |
|
| 68 |
|
| 69 |
addVariable(graphRate); |
| 70 |
|
| 71 |
System.err.println("TEST00 = " + getCTMCProbability(0, 0, 0.0)); |
| 72 |
System.err.println("TEST01 = " + getCTMCProbability(0, 1, 0.0)); |
| 73 |
} |
| 74 |
|
| 75 |
private int getIndex(int x, int y) { |
| 76 |
return x * yDim + y; |
| 77 |
} |
| 78 |
|
| 79 |
private int[] getXY(int index) { |
| 80 |
int[] xy = new int[2]; |
| 81 |
xy[0] = index / yDim; |
| 82 |
xy[1] = index - xy[0] * yDim; |
| 83 |
return xy; |
| 84 |
} |
| 85 |
|
| 86 |
private double getProbability(int I, int J, double time) { |
| 87 |
double probability = 0; |
| 88 |
for (int k = 0; k < totalDim; k++) { |
| 89 |
probability += eVec[I][k] * Math.exp(time * eVal[k]) * eVec[J][k]; |
| 90 |
} |
| 91 |
return probability; |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
public void handleParameterChangedEvent(Parameter parameter, int index) { |
| 96 |
|
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
private double getCTMCProbability(int I, int J, double time) { |
| 101 |
double probability = 0; |
| 102 |
for (int k = 0; k < totalDim; k++) { |
| 103 |
probability += eVec[I][k] * Math.exp(time * eVal[k]) * eVec[J][k]; |
| 104 |
} |
| 105 |
return probability; |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public double getLogLikelihood(double[] start, double[] stop, double time) { |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
int startIndex = getIndex((int) start[0], (int) start[1]); |
| 118 |
int stopIndex = getIndex((int) stop[0], (int) stop[1]); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
return Math.log(getCTMCProbability(startIndex, stopIndex, time)); |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 129 |
|
| 130 |
public String getParserName() { |
| 131 |
return DISCRETE_DIFFUSION_PROCESS; |
| 132 |
} |
| 133 |
|
| 134 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 135 |
|
| 136 |
int xDim = xo.getIntegerAttribute(GRID_X_DIMENSION); |
| 137 |
int yDim = xo.getIntegerAttribute(GRID_Y_DIMENSION); |
| 138 |
|
| 139 |
String evecFileName = xo.getStringAttribute(EVEC_NAME); |
| 140 |
String evalFileName = xo.getStringAttribute(EVAL_NAME); |
| 141 |
|
| 142 |
File evecFile; |
| 143 |
File evalFile; |
| 144 |
|
| 145 |
try { |
| 146 |
File file = new File(evecFileName); |
| 147 |
String name = file.getName(); |
| 148 |
String parent = file.getParent(); |
| 149 |
|
| 150 |
if (!file.isAbsolute()) { |
| 151 |
parent = System.getProperty("user.dir"); |
| 152 |
} |
| 153 |
evecFile = new File(parent, name); |
| 154 |
new FileReader(evecFile); |
| 155 |
} catch (FileNotFoundException fnfe) { |
| 156 |
throw new XMLParseException("File '" + evecFileName + "' can not be opened for " + getParserName() + " element."); |
| 157 |
} |
| 158 |
|
| 159 |
try { |
| 160 |
File file = new File(evalFileName); |
| 161 |
String name = file.getName(); |
| 162 |
String parent = file.getParent(); |
| 163 |
|
| 164 |
if (!file.isAbsolute()) { |
| 165 |
parent = System.getProperty("user.dir"); |
| 166 |
} |
| 167 |
evalFile = new File(parent, name); |
| 168 |
new FileReader(evalFile); |
| 169 |
} catch (FileNotFoundException fnfe) { |
| 170 |
throw new XMLParseException("File '" + evalFileName + "' can not be opened for " + getParserName() + " element."); |
| 171 |
} |
| 172 |
|
| 173 |
double[] eval = null; |
| 174 |
double[][] evec = null; |
| 175 |
|
| 176 |
try { |
| 177 |
eval = TopographicalMap.readEigenvalues(evalFile.getAbsolutePath()); |
| 178 |
evec = TopographicalMap.readEigenvectors(evecFile.getAbsolutePath()); |
| 179 |
} catch (IOException e) { |
| 180 |
e.printStackTrace(); |
| 181 |
assert false; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
Parameter graphRate = (Parameter) xo.getChild(Parameter.class); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
final int totalDim = xDim * yDim; |
| 193 |
if (totalDim != evec.length || totalDim != eval.length) |
| 194 |
throw new XMLParseException("Number of eigenvalues and eigenvectors must match the map grid dimensions"); |
| 195 |
|
| 196 |
return new BivariateDiscreteDiffusionModel(graphRate, xDim, yDim, eval, evec); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public String getParserDescription() { |
| 204 |
return "Describes a multivariate discrete diffusion process."; |
| 205 |
} |
| 206 |
|
| 207 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 208 |
return rules; |
| 209 |
} |
| 210 |
|
| 211 |
private final XMLSyntaxRule[] rules = { |
| 212 |
AttributeRule.newStringRule(EVEC_NAME), |
| 213 |
AttributeRule.newStringRule(EVAL_NAME), |
| 214 |
AttributeRule.newIntegerRule(GRID_X_DIMENSION), |
| 215 |
AttributeRule.newIntegerRule(GRID_Y_DIMENSION), |
| 216 |
new ElementRule(Parameter.class), |
| 217 |
|
| 218 |
|
| 219 |
}; |
| 220 |
|
| 221 |
public Class getReturnType() { |
| 222 |
return BivariateDiscreteDiffusionModel.class; |
| 223 |
} |
| 224 |
|
| 225 |
}; |
| 226 |
|
| 227 |
private final Parameter graphRate; |
| 228 |
private final int xDim; |
| 229 |
private final int yDim; |
| 230 |
private final int totalDim; |
| 231 |
HashMap<Integer, Double> probabilityCache; |
| 232 |
|
| 233 |
private final double[] eVal; |
| 234 |
private final double[][] eVec; |
| 235 |
|
| 236 |
} |