Revision: 6735
Author:
bael...@gmail.com
Date: Tue Apr 14 09:33:53 2015 UTC
Log: Adding working priors for generalized stepping stone sampling.
https://code.google.com/p/beast-mcmc/source/detail?r=6735
Modified:
/trunk/src/dr/inferencexml/distribution/WorkingPriorParsers.java
/trunk/src/dr/math/distributions/LogTransformedNormalKDEDistribution.java
=======================================
--- /trunk/src/dr/inferencexml/distribution/WorkingPriorParsers.java Tue
Oct 8 07:53:23 2013 UTC
+++ /trunk/src/dr/inferencexml/distribution/WorkingPriorParsers.java Tue
Apr 14 09:33:53 2015 UTC
@@ -25,9 +25,6 @@
package dr.inferencexml.distribution;
-import java.io.File;
-import java.io.FileNotFoundException;
-
import dr.inference.distribution.DistributionLikelihood;
import dr.inference.model.Likelihood;
import dr.inference.model.Statistic;
@@ -35,15 +32,13 @@
import dr.inference.trace.TraceException;
import dr.math.distributions.GammaKDEDistribution;
import dr.math.distributions.LogTransformedNormalKDEDistribution;
+import dr.math.distributions.LogitTransformedNormalKDEDistribution;
import dr.math.distributions.NormalKDEDistribution;
import dr.util.FileHelpers;
-import dr.xml.AbstractXMLObjectParser;
-import dr.xml.AttributeRule;
-import dr.xml.ElementRule;
-import dr.xml.XMLObject;
-import dr.xml.XMLObjectParser;
-import dr.xml.XMLParseException;
-import dr.xml.XMLSyntaxRule;
+import dr.xml.*;
+
+import java.io.File;
+import java.io.FileNotFoundException;
/**
* @author Guy Baele
@@ -54,6 +49,7 @@
public static final String NORMAL_REFERENCE_PRIOR
= "normalReferencePrior";
public static final String LOG_TRANSFORMED_NORMAL_REFERENCE_PRIOR
= "logTransformedNormalReferencePrior";
+ public static final String LOGIT_TRANSFORMED_NORMAL_REFERENCE_PRIOR
= "logitTransformedNormalReferencePrior";
public static final String GAMMA_REFERENCE_PRIOR
= "gammaReferencePrior";
public static final String PARAMETER_COLUMN = "parameterColumn";
@@ -237,6 +233,101 @@
public String getParserDescription() {
return "Calculates the reference prior probability of some
data under log transformed normal distribution.";
}
+
+ public Class getReturnType() {
+ return Likelihood.class;
+ }
+
+ };
+
+ /**
+ * A special parser that reads a convenient short form of reference
priors on parameters.
+ */
+ public static XMLObjectParser
LOGIT_TRANSFORMED_NORMAL_REFERENCE_PRIOR_PARSER = new
AbstractXMLObjectParser() {
+
+ public String getParserName() {
+ return LOGIT_TRANSFORMED_NORMAL_REFERENCE_PRIOR;
+ }
+
+ public Object parseXMLObject(XMLObject xo) throws
XMLParseException {
+
+ String fileName = xo.getStringAttribute(FileHelpers.FILE_NAME);
+
+ try {
+
+ File file = new File(fileName);
+ String parent = file.getParent();
+
+ if (!file.isAbsolute()) {
+ parent = System.getProperty("user.dir");
+ }
+ file = new File(parent, fileName);
+ fileName = file.getAbsolutePath();
+
+ String parameterName =
xo.getStringAttribute(PARAMETER_COLUMN);
+
+ LogFileTraces traces = new LogFileTraces(fileName, file);
+ traces.loadTraces();
+ int maxState = traces.getMaxState();
+
+ // leaving the burnin attribute off will result in 10%
being used
+ int burnin = xo.getAttribute("burnin", maxState / 10);
+ if (burnin < 0 || burnin >= maxState) {
+ burnin = maxState / 10;
+ System.out.println("WARNING: Burn-in larger than total
number of states - using 10%");
+ }
+ traces.setBurnIn(burnin);
+
+ int traceIndexParameter = -1;
+ for (int i = 0; i < traces.getTraceCount(); i++) {
+ String traceName = traces.getTraceName(i);
+ if (traceName.trim().equals(parameterName)) {
+ traceIndexParameter = i;
+ }
+ }
+
+ if (traceIndexParameter == -1) {
+ throw new XMLParseException("Column '" + parameterName
+ "' can not be found for " + getParserName() + " element.");
+ }
+
+ Double[] parameterSamples = new
Double[traces.getStateCount()];
+
traces.getValues(traceIndexParameter).toArray(parameterSamples);
+
+ DistributionLikelihood likelihood = new
DistributionLikelihood(new
LogitTransformedNormalKDEDistribution(parameterSamples));
+ for (int j = 0; j < xo.getChildCount(); j++) {
+ if (xo.getChild(j) instanceof Statistic) {
+ likelihood.addData((Statistic) xo.getChild(j));
+ } else {
+ throw new XMLParseException("illegal element in "
+ xo.getName() + " element");
+ }
+ }
+
+ return likelihood;
+
+ } catch (FileNotFoundException fnfe) {
+ throw new XMLParseException("File '" + fileName + "' can
not be opened for " + getParserName() + " element.");
+ } catch (java.io.IOException ioe) {
+ throw new XMLParseException(ioe.getMessage());
+ } catch (TraceException e) {
+ throw new XMLParseException(e.getMessage());
+ }
+
+ }
+
+ public XMLSyntaxRule[] getSyntaxRules() {
+ return rules;
+ }
+
+ private final XMLSyntaxRule[] rules = {
+ AttributeRule.newStringRule("fileName"),
+ AttributeRule.newStringRule("parameterColumn"),
+ AttributeRule.newIntegerRule("burnin"),
+ new ElementRule(Statistic.class, 1, Integer.MAX_VALUE)
+ };
+
+ public String getParserDescription() {
+ return "Calculates the reference prior probability of some
data under logit transformed normal distribution.";
+ }
public Class getReturnType() {
return Likelihood.class;
=======================================
---
/trunk/src/dr/math/distributions/LogTransformedNormalKDEDistribution.java
Mon Apr 13 12:47:30 2015 UTC
+++
/trunk/src/dr/math/distributions/LogTransformedNormalKDEDistribution.java
Tue Apr 14 09:33:53 2015 UTC
@@ -452,7 +452,7 @@
for (int i = 0; i < samples.length-1; i++) {
System.out.print(samples[i] + ",");
}
- System.out.println(samples[samples.length-1] + ")\n\n");
+ System.out.println(samples[samples.length-1] + ")\n");
System.out.println("hist(samples,
200)\nminimum=min(samples)\nabline(v=minimum,col=2,lty=2)\n");
System.out.println("plot(density(samples))\nabline(v=minimum,col=2,lty=2)\n");