| 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.inferencexml.distribution; |
| 27 |
|
| 28 |
import dr.inference.distribution.DistributionLikelihood; |
| 29 |
import dr.inference.distribution.MultivariateDistributionLikelihood; |
| 30 |
import dr.inference.model.Likelihood; |
| 31 |
import dr.inference.model.Statistic; |
| 32 |
import dr.inference.trace.LogFileTraces; |
| 33 |
import dr.inference.trace.TraceException; |
| 34 |
import dr.math.distributions.*; |
| 35 |
import dr.util.FileHelpers; |
| 36 |
import dr.xml.*; |
| 37 |
|
| 38 |
import java.io.File; |
| 39 |
import java.io.FileNotFoundException; |
| 40 |
import java.util.StringTokenizer; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class WorkingPriorParsers { |
| 47 |
public final static boolean DEBUG = true; |
| 48 |
|
| 49 |
public static final String NORMAL_REFERENCE_PRIOR = "normalReferencePrior"; |
| 50 |
public static final String NORMAL_WORKING_PRIOR = "normalWorkingPrior"; |
| 51 |
public static final String LOG_TRANSFORMED_NORMAL_REFERENCE_PRIOR = "logTransformedNormalReferencePrior"; |
| 52 |
public static final String LOG_TRANSFORMED_NORMAL_WORKING_PRIOR = "logTransformedNormalWorkingPrior"; |
| 53 |
public static final String LOGIT_TRANSFORMED_NORMAL_REFERENCE_PRIOR = "logitTransformedNormalReferencePrior"; |
| 54 |
public static final String LOGIT_TRANSFORMED_NORMAL_WORKING_PRIOR = "logitTransformedNormalWorkingPrior"; |
| 55 |
public static final String GAMMA_REFERENCE_PRIOR = "gammaReferencePrior"; |
| 56 |
public static final String GAMMA_WORKING_PRIOR = "gammaWorkingPrior"; |
| 57 |
public static final String PARAMETER_COLUMN = "parameterColumn"; |
| 58 |
public static final String DIMENSION = "dimension"; |
| 59 |
public static final String UPPERLIMIT = "upperLimit"; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static XMLObjectParser GAMMA_REFERENCE_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 65 |
|
| 66 |
public String getParserName() { |
| 67 |
return GAMMA_REFERENCE_PRIOR; |
| 68 |
} |
| 69 |
|
| 70 |
public String[] getParserNames() { |
| 71 |
return new String[]{getParserName(), GAMMA_WORKING_PRIOR}; |
| 72 |
} |
| 73 |
|
| 74 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 75 |
|
| 76 |
String fileName = xo.getStringAttribute(FileHelpers.FILE_NAME); |
| 77 |
|
| 78 |
try { |
| 79 |
|
| 80 |
File file = new File(fileName); |
| 81 |
String parent = file.getParent(); |
| 82 |
|
| 83 |
if (!file.isAbsolute()) { |
| 84 |
parent = System.getProperty("user.dir"); |
| 85 |
} |
| 86 |
file = new File(parent, fileName); |
| 87 |
fileName = file.getAbsolutePath(); |
| 88 |
|
| 89 |
String parameterName = xo.getStringAttribute(PARAMETER_COLUMN); |
| 90 |
|
| 91 |
int dimension = 1; |
| 92 |
if (xo.hasAttribute(DIMENSION)) { |
| 93 |
dimension = xo.getIntegerAttribute(DIMENSION); |
| 94 |
} |
| 95 |
if (dimension <= 0) { |
| 96 |
throw new XMLParseException("Column '" + parameterName + "' has dimension smaller than 1."); |
| 97 |
} |
| 98 |
|
| 99 |
LogFileTraces traces = new LogFileTraces(fileName, file); |
| 100 |
traces.loadTraces(); |
| 101 |
long maxState = traces.getMaxState(); |
| 102 |
|
| 103 |
|
| 104 |
long burnin = xo.getAttribute("burnin", maxState / 10); |
| 105 |
if (burnin < 0 || burnin >= maxState) { |
| 106 |
burnin = maxState / 10; |
| 107 |
System.out.println("WARNING: Burn-in larger than total number of states - using 10%"); |
| 108 |
} |
| 109 |
traces.setBurnIn(burnin); |
| 110 |
|
| 111 |
if (dimension == 1) { |
| 112 |
|
| 113 |
int traceIndexParameter = -1; |
| 114 |
for (int i = 0; i < traces.getTraceCount(); i++) { |
| 115 |
String traceName = traces.getTraceName(i); |
| 116 |
if (traceName.trim().equals(parameterName)) { |
| 117 |
traceIndexParameter = i; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if (traceIndexParameter == -1) { |
| 122 |
throw new XMLParseException("GammaKDEDistribution: Column '" + parameterName + "' can not be found for " + getParserName() + " element."); |
| 123 |
} |
| 124 |
|
| 125 |
Double[] parameterSamples = new Double[traces.getStateCount()]; |
| 126 |
|
| 127 |
DistributionLikelihood likelihood = new DistributionLikelihood(new GammaKDEDistribution((Double[]) traces.getValues(traceIndexParameter).toArray(parameterSamples))); |
| 128 |
for (int j = 0; j < xo.getChildCount(); j++) { |
| 129 |
if (xo.getChild(j) instanceof Statistic) { |
| 130 |
likelihood.addData((Statistic) xo.getChild(j)); |
| 131 |
} else { |
| 132 |
throw new XMLParseException("illegal element in " + xo.getName() + " element"); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return likelihood; |
| 137 |
|
| 138 |
} else { |
| 139 |
|
| 140 |
|
| 141 |
GammaKDEDistribution[] arrayKDE = new GammaKDEDistribution[dimension]; |
| 142 |
|
| 143 |
for (int i = 0; i < dimension; i++) { |
| 144 |
|
| 145 |
String newParameterName = parameterName + (i+1); |
| 146 |
int traceIndexParameter = -1; |
| 147 |
for (int j = 0; j < traces.getTraceCount(); j++) { |
| 148 |
String traceName = traces.getTraceName(j); |
| 149 |
if (traceName.trim().equals(newParameterName)) { |
| 150 |
traceIndexParameter = j; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
if (traceIndexParameter == -1) { |
| 155 |
throw new XMLParseException("GammaKDEDistribution: Column '" + newParameterName + "' can not be found for " + getParserName() + " element."); |
| 156 |
} |
| 157 |
|
| 158 |
Double[] parameterSamples = new Double[traces.getStateCount()]; |
| 159 |
traces.getValues(traceIndexParameter).toArray(parameterSamples); |
| 160 |
|
| 161 |
arrayKDE[i] = new GammaKDEDistribution(parameterSamples); |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(new MultivariateKDEDistribution(arrayKDE)); |
| 166 |
|
| 167 |
for (int j = 0; j < xo.getChildCount(); j++) { |
| 168 |
if (xo.getChild(j) instanceof Statistic) { |
| 169 |
if (DEBUG) { |
| 170 |
System.out.println(((Statistic) xo.getChild(j)).toString()); |
| 171 |
System.out.println(((Statistic) xo.getChild(j)).getDimension()); |
| 172 |
} |
| 173 |
likelihood.addData((Statistic) xo.getChild(j)); |
| 174 |
} else { |
| 175 |
throw new XMLParseException("illegal element in " + xo.getName() + " element"); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return likelihood; |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
} catch (FileNotFoundException fnfe) { |
| 184 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 185 |
} catch (java.io.IOException ioe) { |
| 186 |
throw new XMLParseException(ioe.getMessage()); |
| 187 |
} catch (TraceException e) { |
| 188 |
throw new XMLParseException(e.getMessage()); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 193 |
return rules; |
| 194 |
} |
| 195 |
|
| 196 |
private final XMLSyntaxRule[] rules = { |
| 197 |
AttributeRule.newStringRule("fileName"), |
| 198 |
AttributeRule.newStringRule("parameterColumn"), |
| 199 |
AttributeRule.newIntegerRule("burnin"), |
| 200 |
new ElementRule(Statistic.class, 1, Integer.MAX_VALUE) |
| 201 |
}; |
| 202 |
|
| 203 |
public String getParserDescription() { |
| 204 |
return "Calculates the reference prior probability of some data under a given normal distribution."; |
| 205 |
} |
| 206 |
|
| 207 |
public Class getReturnType() { |
| 208 |
return Likelihood.class; |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public static XMLObjectParser LOG_TRANSFORMED_NORMAL_REFERENCE_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 216 |
|
| 217 |
public String getParserName() { |
| 218 |
return LOG_TRANSFORMED_NORMAL_REFERENCE_PRIOR; |
| 219 |
} |
| 220 |
|
| 221 |
public String[] getParserNames() { |
| 222 |
return new String[]{getParserName(), LOG_TRANSFORMED_NORMAL_WORKING_PRIOR}; |
| 223 |
} |
| 224 |
|
| 225 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 226 |
|
| 227 |
String fileName = xo.getStringAttribute(FileHelpers.FILE_NAME); |
| 228 |
|
| 229 |
try { |
| 230 |
|
| 231 |
File file = new File(fileName); |
| 232 |
String parent = file.getParent(); |
| 233 |
|
| 234 |
if (!file.isAbsolute()) { |
| 235 |
parent = System.getProperty("user.dir"); |
| 236 |
} |
| 237 |
file = new File(parent, fileName); |
| 238 |
fileName = file.getAbsolutePath(); |
| 239 |
|
| 240 |
String parameterName = xo.getStringAttribute(PARAMETER_COLUMN); |
| 241 |
|
| 242 |
int dimension = 1; |
| 243 |
if (xo.hasAttribute(DIMENSION)) { |
| 244 |
dimension = xo.getIntegerAttribute(DIMENSION); |
| 245 |
} |
| 246 |
if (dimension <= 0) { |
| 247 |
throw new XMLParseException("Column '" + parameterName + "' has dimension smaller than 1."); |
| 248 |
} |
| 249 |
|
| 250 |
LogFileTraces traces = new LogFileTraces(fileName, file); |
| 251 |
traces.loadTraces(); |
| 252 |
long maxState = traces.getMaxState(); |
| 253 |
|
| 254 |
|
| 255 |
long burnin = xo.getAttribute("burnin", maxState / 10); |
| 256 |
if (burnin < 0 || burnin >= maxState) { |
| 257 |
burnin = maxState / 10; |
| 258 |
System.out.println("WARNING: Burn-in larger than total number of states - using 10%"); |
| 259 |
} |
| 260 |
traces.setBurnIn(burnin); |
| 261 |
|
| 262 |
if (dimension == 1) { |
| 263 |
|
| 264 |
int traceIndexParameter = -1; |
| 265 |
for (int i = 0; i < traces.getTraceCount(); i++) { |
| 266 |
String traceName = traces.getTraceName(i); |
| 267 |
if (traceName.trim().equals(parameterName)) { |
| 268 |
traceIndexParameter = i; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
if (traceIndexParameter == -1) { |
| 273 |
throw new XMLParseException("LogTransformedNormalKDEDistribution: Column '" + parameterName + "' can not be found for " + getParserName() + " element."); |
| 274 |
} |
| 275 |
|
| 276 |
Double[] parameterSamples = new Double[traces.getStateCount()]; |
| 277 |
traces.getValues(traceIndexParameter).toArray(parameterSamples); |
| 278 |
|
| 279 |
DistributionLikelihood likelihood = new DistributionLikelihood(new LogTransformedNormalKDEDistribution(parameterSamples)); |
| 280 |
for (int j = 0; j < xo.getChildCount(); j++) { |
| 281 |
if (xo.getChild(j) instanceof Statistic) { |
| 282 |
if (DEBUG) { |
| 283 |
System.out.println(((Statistic) xo.getChild(j)).toString()); |
| 284 |
System.out.println(((Statistic) xo.getChild(j)).getDimension()); |
| 285 |
} |
| 286 |
likelihood.addData((Statistic) xo.getChild(j)); |
| 287 |
} else { |
| 288 |
throw new XMLParseException("illegal element in " + xo.getName() + " element"); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
return likelihood; |
| 293 |
|
| 294 |
} else { |
| 295 |
|
| 296 |
|
| 297 |
LogTransformedNormalKDEDistribution[] arrayKDE = new LogTransformedNormalKDEDistribution[dimension]; |
| 298 |
|
| 299 |
for (int i = 0; i < dimension; i++) { |
| 300 |
|
| 301 |
String newParameterName = parameterName + (i+1); |
| 302 |
int traceIndexParameter = -1; |
| 303 |
for (int j = 0; j < traces.getTraceCount(); j++) { |
| 304 |
String traceName = traces.getTraceName(j); |
| 305 |
if (traceName.trim().equals(newParameterName)) { |
| 306 |
traceIndexParameter = j; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
if (traceIndexParameter == -1) { |
| 311 |
throw new XMLParseException("LogTransformedNormalKDEDistribution: Column '" + newParameterName + "' can not be found for " + getParserName() + " element."); |
| 312 |
} |
| 313 |
|
| 314 |
Double[] parameterSamples = new Double[traces.getStateCount()]; |
| 315 |
traces.getValues(traceIndexParameter).toArray(parameterSamples); |
| 316 |
|
| 317 |
arrayKDE[i] = new LogTransformedNormalKDEDistribution(parameterSamples); |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(new MultivariateKDEDistribution(arrayKDE)); |
| 322 |
|
| 323 |
for (int j = 0; j < xo.getChildCount(); j++) { |
| 324 |
if (xo.getChild(j) instanceof Statistic) { |
| 325 |
if (DEBUG) { |
| 326 |
System.out.println(((Statistic) xo.getChild(j)).toString()); |
| 327 |
System.out.println(((Statistic) xo.getChild(j)).getDimension()); |
| 328 |
} |
| 329 |
likelihood.addData((Statistic) xo.getChild(j)); |
| 330 |
} else { |
| 331 |
throw new XMLParseException("illegal element in " + xo.getName() + " element"); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
return likelihood; |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
} catch (FileNotFoundException fnfe) { |
| 340 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 341 |
} catch (java.io.IOException ioe) { |
| 342 |
throw new XMLParseException(ioe.getMessage()); |
| 343 |
} catch (TraceException e) { |
| 344 |
throw new XMLParseException(e.getMessage()); |
| 345 |
} |
| 346 |
|
| 347 |
} |
| 348 |
|
| 349 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 350 |
return rules; |
| 351 |
} |
| 352 |
|
| 353 |
private final XMLSyntaxRule[] rules = { |
| 354 |
AttributeRule.newStringRule("fileName"), |
| 355 |
AttributeRule.newStringRule("parameterColumn"), |
| 356 |
AttributeRule.newIntegerRule("burnin"), |
| 357 |
new ElementRule(Statistic.class, 1, Integer.MAX_VALUE) |
| 358 |
}; |
| 359 |
|
| 360 |
public String getParserDescription() { |
| 361 |
return "Calculates the reference prior probability of some data under log transformed normal distribution."; |
| 362 |
} |
| 363 |
|
| 364 |
public Class getReturnType() { |
| 365 |
return Likelihood.class; |
| 366 |
} |
| 367 |
|
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public static XMLObjectParser LOGIT_TRANSFORMED_NORMAL_REFERENCE_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 374 |
|
| 375 |
public String getParserName() { |
| 376 |
return LOGIT_TRANSFORMED_NORMAL_REFERENCE_PRIOR; |
| 377 |
} |
| 378 |
|
| 379 |
public String[] getParserNames() { |
| 380 |
return new String[]{getParserName(), LOGIT_TRANSFORMED_NORMAL_WORKING_PRIOR}; |
| 381 |
} |
| 382 |
|
| 383 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 384 |
|
| 385 |
String fileName = xo.getStringAttribute(FileHelpers.FILE_NAME); |
| 386 |
|
| 387 |
try { |
| 388 |
|
| 389 |
File file = new File(fileName); |
| 390 |
String parent = file.getParent(); |
| 391 |
|
| 392 |
if (!file.isAbsolute()) { |
| 393 |
parent = System.getProperty("user.dir"); |
| 394 |
} |
| 395 |
file = new File(parent, fileName); |
| 396 |
fileName = file.getAbsolutePath(); |
| 397 |
|
| 398 |
|
| 399 |
String parameterName = xo.getStringAttribute(PARAMETER_COLUMN); |
| 400 |
|
| 401 |
int dimension = 1; |
| 402 |
if (xo.hasAttribute(DIMENSION)) { |
| 403 |
dimension = xo.getIntegerAttribute(DIMENSION); |
| 404 |
} |
| 405 |
if (dimension <= 0) { |
| 406 |
throw new XMLParseException("Column '" + parameterName + "' has dimension smaller than 1."); |
| 407 |
} |
| 408 |
|
| 409 |
double upperlimit = 1.0; |
| 410 |
if (xo.hasAttribute(UPPERLIMIT)) { |
| 411 |
upperlimit = xo.getDoubleAttribute(UPPERLIMIT); |
| 412 |
} |
| 413 |
if (upperlimit <= 0.0) { |
| 414 |
throw new XMLParseException("Positive upper bound expected for logit transformed normal KDE distribution."); |
| 415 |
} |
| 416 |
|
| 417 |
LogFileTraces traces = new LogFileTraces(fileName, file); |
| 418 |
traces.loadTraces(); |
| 419 |
long maxState = traces.getMaxState(); |
| 420 |
|
| 421 |
|
| 422 |
long burnin = xo.getAttribute("burnin", maxState / 10); |
| 423 |
if (burnin < 0 || burnin >= maxState) { |
| 424 |
burnin = maxState / 10; |
| 425 |
System.out.println("WARNING: Burn-in larger than total number of states - using 10%"); |
| 426 |
} |
| 427 |
traces.setBurnIn(burnin); |
| 428 |
|
| 429 |
if (dimension == 1) { |
| 430 |
|
| 431 |
int traceIndexParameter = -1; |
| 432 |
for (int i = 0; i < traces.getTraceCount(); i++) { |
| 433 |
String traceName = traces.getTraceName(i); |
| 434 |
if (traceName.trim().equals(parameterName)) { |
| 435 |
traceIndexParameter = i; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
if (traceIndexParameter == -1) { |
| 440 |
throw new XMLParseException("LogitTransformedNormalKDEDistribution: Column '" + parameterName + "' can not be found for " + getParserName() + " element."); |
| 441 |
} |
| 442 |
|
| 443 |
Double[] parameterSamples = new Double[traces.getStateCount()]; |
| 444 |
traces.getValues(traceIndexParameter).toArray(parameterSamples); |
| 445 |
|
| 446 |
DistributionLikelihood likelihood = new DistributionLikelihood(new LogitTransformedNormalKDEDistribution(parameterSamples, upperlimit)); |
| 447 |
for (int j = 0; j < xo.getChildCount(); j++) { |
| 448 |
if (xo.getChild(j) instanceof Statistic) { |
| 449 |
if (DEBUG) { |
| 450 |
System.out.println(((Statistic) xo.getChild(j)).toString()); |
| 451 |
System.out.println(((Statistic) xo.getChild(j)).getDimension()); |
| 452 |
} |
| 453 |
likelihood.addData((Statistic) xo.getChild(j)); |
| 454 |
} else { |
| 455 |
throw new XMLParseException("illegal element in " + xo.getName() + " element"); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
return likelihood; |
| 460 |
|
| 461 |
} else { |
| 462 |
|
| 463 |
|
| 464 |
LogitTransformedNormalKDEDistribution[] arrayKDE = new LogitTransformedNormalKDEDistribution[dimension]; |
| 465 |
String[] newParameterName = new String[dimension]; |
| 466 |
|
| 467 |
upperlimit = dimension; |
| 468 |
|
| 469 |
if (xo.hasAttribute("parameterNames")) { |
| 470 |
String temp = (String)xo.getAttribute("parameterNames"); |
| 471 |
StringTokenizer token = new StringTokenizer(temp); |
| 472 |
for (int i = 0; i < dimension; i++) { |
| 473 |
newParameterName[i] = token.nextToken(); |
| 474 |
} |
| 475 |
} else { |
| 476 |
for (int i = 0; i < dimension; i++) { |
| 477 |
newParameterName[i] = parameterName + (i+1); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
for (int i = 0; i < dimension; i++) { |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
int traceIndexParameter = -1; |
| 486 |
for (int j = 0; j < traces.getTraceCount(); j++) { |
| 487 |
String traceName = traces.getTraceName(j); |
| 488 |
if (traceName.trim().equals(newParameterName[i])) { |
| 489 |
traceIndexParameter = j; |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
if (traceIndexParameter == -1) { |
| 494 |
throw new XMLParseException("LogitTransformedNormalKDEDistribution: Column '" + newParameterName[i] + "' can not be found for " + getParserName() + " element."); |
| 495 |
} |
| 496 |
|
| 497 |
Double[] parameterSamples = new Double[traces.getStateCount()]; |
| 498 |
traces.getValues(traceIndexParameter).toArray(parameterSamples); |
| 499 |
|
| 500 |
arrayKDE[i] = new LogitTransformedNormalKDEDistribution(parameterSamples, upperlimit); |
| 501 |
|
| 502 |
} |
| 503 |
|
| 504 |
MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(new MultivariateKDEDistribution(arrayKDE)); |
| 505 |
|
| 506 |
for (int j = 0; j < xo.getChildCount(); j++) { |
| 507 |
if (xo.getChild(j) instanceof Statistic) { |
| 508 |
if (DEBUG) { |
| 509 |
System.out.println(((Statistic) xo.getChild(j)).toString()); |
| 510 |
System.out.println(((Statistic) xo.getChild(j)).getDimension()); |
| 511 |
} |
| 512 |
likelihood.addData((Statistic) xo.getChild(j)); |
| 513 |
} else { |
| 514 |
throw new XMLParseException("illegal element in " + xo.getName() + " element"); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
return likelihood; |
| 519 |
|
| 520 |
} |
| 521 |
|
| 522 |
} catch (FileNotFoundException fnfe) { |
| 523 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 524 |
} catch (java.io.IOException ioe) { |
| 525 |
throw new XMLParseException(ioe.getMessage()); |
| 526 |
} catch (TraceException e) { |
| 527 |
throw new XMLParseException(e.getMessage()); |
| 528 |
} |
| 529 |
|
| 530 |
} |
| 531 |
|
| 532 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 533 |
return rules; |
| 534 |
} |
| 535 |
|
| 536 |
private final XMLSyntaxRule[] rules = { |
| 537 |
AttributeRule.newStringRule("fileName"), |
| 538 |
AttributeRule.newStringRule("parameterColumn"), |
| 539 |
AttributeRule.newIntegerRule("burnin"), |
| 540 |
|
| 541 |
AttributeRule.newIntegerRule("dimension", true), |
| 542 |
|
| 543 |
AttributeRule.newDoubleRule("upperLimit", true), |
| 544 |
new ElementRule(Statistic.class, 1, Integer.MAX_VALUE) |
| 545 |
}; |
| 546 |
|
| 547 |
public String getParserDescription() { |
| 548 |
return "Calculates the reference prior probability of some data under logit transformed normal distribution."; |
| 549 |
} |
| 550 |
|
| 551 |
public Class getReturnType() { |
| 552 |
return Likelihood.class; |
| 553 |
} |
| 554 |
|
| 555 |
}; |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
public static XMLObjectParser NORMAL_REFERENCE_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 561 |
|
| 562 |
public String getParserName() { |
| 563 |
return NORMAL_REFERENCE_PRIOR; |
| 564 |
} |
| 565 |
|
| 566 |
public String[] getParserNames() { |
| 567 |
return new String[]{getParserName(), NORMAL_WORKING_PRIOR}; |
| 568 |
} |
| 569 |
|
| 570 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 571 |
|
| 572 |
String fileName = xo.getStringAttribute(FileHelpers.FILE_NAME); |
| 573 |
|
| 574 |
try { |
| 575 |
|
| 576 |
File file = new File(fileName); |
| 577 |
String parent = file.getParent(); |
| 578 |
|
| 579 |
if (!file.isAbsolute()) { |
| 580 |
parent = System.getProperty("user.dir"); |
| 581 |
} |
| 582 |
file = new File(parent, fileName); |
| 583 |
fileName = file.getAbsolutePath(); |
| 584 |
|
| 585 |
String parameterName = xo.getStringAttribute(PARAMETER_COLUMN); |
| 586 |
|
| 587 |
int dimension = 1; |
| 588 |
if (xo.hasAttribute(DIMENSION)) { |
| 589 |
dimension = xo.getIntegerAttribute(DIMENSION); |
| 590 |
} |
| 591 |
if (dimension <= 0) { |
| 592 |
throw new XMLParseException("Column '" + parameterName + "' has dimension smaller than 1."); |
| 593 |
} |
| 594 |
|
| 595 |
LogFileTraces traces = new LogFileTraces(fileName, file); |
| 596 |
traces.loadTraces(); |
| 597 |
long maxState = traces.getMaxState(); |
| 598 |
|
| 599 |
|
| 600 |
long burnin = xo.getAttribute("burnin", maxState / 10); |
| 601 |
if (burnin < 0 || burnin >= maxState) { |
| 602 |
burnin = maxState / 10; |
| 603 |
System.out.println("WARNING: Burn-in larger than total number of states - using 10%"); |
| 604 |
} |
| 605 |
traces.setBurnIn(burnin); |
| 606 |
|
| 607 |
if (dimension == 1) { |
| 608 |
|
| 609 |
int traceIndexParameter = -1; |
| 610 |
for (int i = 0; i < traces.getTraceCount(); i++) { |
| 611 |
String traceName = traces.getTraceName(i); |
| 612 |
if (traceName.trim().equals(parameterName)) { |
| 613 |
traceIndexParameter = i; |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
if (traceIndexParameter == -1) { |
| 618 |
throw new XMLParseException("NormalKDEDistribution: Column '" + parameterName + "' can not be found for " + getParserName() + " element."); |
| 619 |
} |
| 620 |
|
| 621 |
Double[] parameterSamples = new Double[traces.getStateCount()]; |
| 622 |
|
| 623 |
DistributionLikelihood likelihood = new DistributionLikelihood(new NormalKDEDistribution((Double[]) traces.getValues(traceIndexParameter).toArray(parameterSamples))); |
| 624 |
for (int j = 0; j < xo.getChildCount(); j++) { |
| 625 |
if (xo.getChild(j) instanceof Statistic) { |
| 626 |
likelihood.addData((Statistic) xo.getChild(j)); |
| 627 |
} else { |
| 628 |
throw new XMLParseException("illegal element in " + xo.getName() + " element"); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
return likelihood; |
| 633 |
|
| 634 |
} else { |
| 635 |
|
| 636 |
|
| 637 |
NormalKDEDistribution[] arrayKDE = new NormalKDEDistribution[dimension]; |
| 638 |
|
| 639 |
for (int i = 0; i < dimension; i++) { |
| 640 |
|
| 641 |
String newParameterName = parameterName + (i+1); |
| 642 |
int traceIndexParameter = -1; |
| 643 |
for (int j = 0; j < traces.getTraceCount(); j++) { |
| 644 |
String traceName = traces.getTraceName(j); |
| 645 |
if (traceName.trim().equals(newParameterName)) { |
| 646 |
traceIndexParameter = j; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
if (traceIndexParameter == -1) { |
| 651 |
throw new XMLParseException("NormalKDEDistribution: Column '" + newParameterName + "' can not be found for " + getParserName() + " element."); |
| 652 |
} |
| 653 |
|
| 654 |
Double[] parameterSamples = new Double[traces.getStateCount()]; |
| 655 |
traces.getValues(traceIndexParameter).toArray(parameterSamples); |
| 656 |
|
| 657 |
arrayKDE[i] = new NormalKDEDistribution(parameterSamples); |
| 658 |
|
| 659 |
} |
| 660 |
|
| 661 |
MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(new MultivariateKDEDistribution(arrayKDE)); |
| 662 |
|
| 663 |
for (int j = 0; j < xo.getChildCount(); j++) { |
| 664 |
if (xo.getChild(j) instanceof Statistic) { |
| 665 |
if (DEBUG) { |
| 666 |
System.out.println(((Statistic) xo.getChild(j)).toString()); |
| 667 |
System.out.println(((Statistic) xo.getChild(j)).getDimension()); |
| 668 |
} |
| 669 |
likelihood.addData((Statistic) xo.getChild(j)); |
| 670 |
} else { |
| 671 |
throw new XMLParseException("illegal element in " + xo.getName() + " element"); |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
return likelihood; |
| 676 |
|
| 677 |
} |
| 678 |
|
| 679 |
} catch (FileNotFoundException fnfe) { |
| 680 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 681 |
} catch (java.io.IOException ioe) { |
| 682 |
throw new XMLParseException(ioe.getMessage()); |
| 683 |
} catch (TraceException e) { |
| 684 |
throw new XMLParseException(e.getMessage()); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 689 |
return rules; |
| 690 |
} |
| 691 |
|
| 692 |
private final XMLSyntaxRule[] rules = { |
| 693 |
AttributeRule.newStringRule("fileName"), |
| 694 |
AttributeRule.newStringRule("parameterColumn"), |
| 695 |
AttributeRule.newIntegerRule("burnin"), |
| 696 |
new ElementRule(Statistic.class, 1, Integer.MAX_VALUE) |
| 697 |
}; |
| 698 |
|
| 699 |
public String getParserDescription() { |
| 700 |
return "Calculates the reference prior probability of some data under a given normal distribution."; |
| 701 |
} |
| 702 |
|
| 703 |
public Class getReturnType() { |
| 704 |
return Likelihood.class; |
| 705 |
} |
| 706 |
}; |
| 707 |
|
| 708 |
} |