| 1 |
/* |
| 2 |
* EllipticalSliceOperatorParser.java |
| 3 |
* |
| 4 |
* Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 |
* |
| 6 |
* This file is part of BEAST. |
| 7 |
* See the NOTICE file distributed with this work for additional |
| 8 |
* information regarding copyright ownership and licensing. |
| 9 |
* |
| 10 |
* BEAST is free software; you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU Lesser General Public License as |
| 12 |
* published by the Free Software Foundation; either version 2 |
| 13 |
* of the License, or (at your option) any later version. |
| 14 |
* |
| 15 |
* BEAST is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU Lesser General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public |
| 21 |
* License along with BEAST; if not, write to the |
| 22 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 |
* Boston, MA 02110-1301 USA |
| 24 |
*/ |
| 25 |
|
| 26 |
package dr.inferencexml.operators; |
| 27 |
|
| 28 |
import dr.inference.distribution.MultivariateDistributionLikelihood; |
| 29 |
import dr.inference.distribution.MultivariateNormalDistributionModel; |
| 30 |
import dr.inference.model.CompoundParameter; |
| 31 |
import dr.inference.model.MaskedParameter; |
| 32 |
import dr.inference.model.Parameter; |
| 33 |
import dr.inference.operators.EllipticalSliceOperator; |
| 34 |
import dr.inference.operators.MCMCOperator; |
| 35 |
import dr.math.distributions.GaussianProcessRandomGenerator; |
| 36 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 37 |
import dr.xml.*; |
| 38 |
import org.ejml.ops.CommonOps; |
| 39 |
|
| 40 |
/** |
| 41 |
*/ |
| 42 |
public class EllipticalSliceOperatorParser extends AbstractXMLObjectParser { |
| 43 |
|
| 44 |
public static final String ELLIPTICAL_SLICE_SAMPLER = "ellipticalSliceSampler"; |
| 45 |
public static final String SIGNAL_CONSTITUENT_PARAMETERS = "signalConstituentParameters"; |
| 46 |
public static final String BRACKET_ANGLE = "bracketAngle"; |
| 47 |
public static final String TRANSLATION_INVARIANT = "translationInvariant"; |
| 48 |
public static final String ROTATION_INVARIANT = "rotationInvariant"; |
| 49 |
|
| 50 |
public static final String DRAW_BY_ROW = "drawByRow"; // TODO What is this? |
| 51 |
|
| 52 |
public String getParserName() { |
| 53 |
return ELLIPTICAL_SLICE_SAMPLER; |
| 54 |
} |
| 55 |
|
| 56 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 57 |
|
| 58 |
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 59 |
final Parameter variable = (Parameter) xo.getChild(Parameter.class); |
| 60 |
boolean drawByRowTemp = false; |
| 61 |
if (xo.hasAttribute(DRAW_BY_ROW)) { |
| 62 |
drawByRowTemp = xo.getBooleanAttribute(DRAW_BY_ROW); |
| 63 |
} |
| 64 |
final boolean drawByRow = drawByRowTemp; |
| 65 |
|
| 66 |
boolean signal = xo.getAttribute(SIGNAL_CONSTITUENT_PARAMETERS, true); |
| 67 |
if (!signal) { |
| 68 |
Parameter possiblyCompound = variable; |
| 69 |
if (variable instanceof MaskedParameter) { |
| 70 |
possiblyCompound = ((MaskedParameter) variable).getUnmaskedParameter(); |
| 71 |
} |
| 72 |
if (!(possiblyCompound instanceof CompoundParameter)) { |
| 73 |
signal = true; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
double bracketAngle = xo.getAttribute(BRACKET_ANGLE, 0.0); |
| 78 |
|
| 79 |
boolean translationInvariant = xo.getAttribute(TRANSLATION_INVARIANT, false); |
| 80 |
boolean rotationInvariant = xo.getAttribute(ROTATION_INVARIANT, false); |
| 81 |
|
| 82 |
GaussianProcessRandomGenerator gaussianProcess = (GaussianProcessRandomGenerator) |
| 83 |
xo.getChild(GaussianProcessRandomGenerator.class); |
| 84 |
|
| 85 |
if (gaussianProcess == null) { |
| 86 |
|
| 87 |
final MultivariateDistributionLikelihood likelihood = |
| 88 |
(MultivariateDistributionLikelihood) xo.getChild(MultivariateDistributionLikelihood.class); |
| 89 |
|
| 90 |
if (!(likelihood.getDistribution() instanceof GaussianProcessRandomGenerator)) { |
| 91 |
throw new XMLParseException("Elliptical slice sampling only works for multivariate normally distributed random variables"); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
if (likelihood.getDistribution() instanceof MultivariateNormalDistribution) |
| 96 |
gaussianProcess = (MultivariateNormalDistribution) likelihood.getDistribution(); |
| 97 |
|
| 98 |
if (likelihood.getDistribution() instanceof MultivariateNormalDistributionModel) |
| 99 |
gaussianProcess = (MultivariateNormalDistributionModel) likelihood.getDistribution(); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
EllipticalSliceOperator operator = new EllipticalSliceOperator(variable, gaussianProcess, |
| 104 |
drawByRow, signal, bracketAngle, |
| 105 |
translationInvariant, rotationInvariant); |
| 106 |
operator.setWeight(weight); |
| 107 |
return operator; |
| 108 |
} |
| 109 |
|
| 110 |
//************************************************************************ |
| 111 |
// AbstractXMLObjectParser implementation |
| 112 |
//************************************************************************ |
| 113 |
|
| 114 |
public String getParserDescription() { |
| 115 |
return "An elliptical slice sampler for parameters with Gaussian priors."; |
| 116 |
} |
| 117 |
|
| 118 |
public Class getReturnType() { |
| 119 |
return EllipticalSliceOperator.class; |
| 120 |
} |
| 121 |
|
| 122 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 123 |
return rules; |
| 124 |
} |
| 125 |
|
| 126 |
private final XMLSyntaxRule[] rules = { |
| 127 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 128 |
AttributeRule.newBooleanRule(SIGNAL_CONSTITUENT_PARAMETERS, true), |
| 129 |
AttributeRule.newDoubleRule(BRACKET_ANGLE, true), |
| 130 |
|
| 131 |
AttributeRule.newBooleanRule(TRANSLATION_INVARIANT, true), |
| 132 |
AttributeRule.newBooleanRule(ROTATION_INVARIANT, true), |
| 133 |
|
| 134 |
new ElementRule(Parameter.class), |
| 135 |
new XORRule( |
| 136 |
new ElementRule(GaussianProcessRandomGenerator.class), |
| 137 |
new ElementRule(MultivariateDistributionLikelihood.class) |
| 138 |
), |
| 139 |
AttributeRule.newBooleanRule(DRAW_BY_ROW, true), |
| 140 |
// new ElementRule(MultivariateNormalDistribution.class), |
| 141 |
}; |
| 142 |
} |