Debian Codegraph @

src/dr/inferencexml/operators/hmc/HamiltonianMonteCarloOperatorParser.java

raw symbols SCIP
1 /*
2 * HamiltonianMonteCarloOperatorParser.java
3 *
4 * Copyright (c) 2002-2017 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.hmc;
27
28 import dr.inference.hmc.GradientWrtParameterProvider;
29 import dr.inference.model.Parameter;
30 import dr.inference.operators.AdaptableMCMCOperator;
31 import dr.inference.operators.AdaptationMode;
32 import dr.inference.operators.hmc.HamiltonianMonteCarloOperator;
33 import dr.inference.operators.MCMCOperator;
34 import dr.inference.operators.hmc.NoUTurnOperator;
35 import dr.util.Transform;
36 import dr.xml.*;
37
38 /**
39 * @author Max Tolkoff
40 * @author Marc A. Suchard
41 */
42
43 public class HamiltonianMonteCarloOperatorParser extends AbstractXMLObjectParser {
44
45 private final static String HMC_OPERATOR = "hamiltonianMonteCarloOperator";
46 private final static String N_STEPS = "nSteps";
47 private final static String STEP_SIZE = "stepSize";
48 private final static String DRAW_VARIANCE = "drawVariance";
49 private final static String MODE = "mode";
50 private final static String NUTS = "nuts";
51 private final static String VANILLA = "vanilla";
52
53 @Override
54 public String getParserName() {
55 return HMC_OPERATOR;
56 }
57
58 private int parseRunMode(XMLObject xo) throws XMLParseException {
59 int mode = 0;
60 if (xo.getAttribute(MODE, VANILLA).toLowerCase().compareTo(NUTS) == 0) {
61 mode = 1;
62 }
63 return mode;
64 }
65
66 @Override
67 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
68
69 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
70 int nSteps = xo.getIntegerAttribute(N_STEPS);
71 double stepSize = xo.getDoubleAttribute(STEP_SIZE);
72 double drawVariance = xo.getDoubleAttribute(DRAW_VARIANCE);
73 int runMode = parseRunMode(xo);
74
75 AdaptationMode adaptationMode = AdaptationMode.parseMode(xo);
76
77 GradientWrtParameterProvider derivative =
78 (GradientWrtParameterProvider) xo.getChild(GradientWrtParameterProvider.class);
79
80 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
81
82 Transform transform = (Transform.MultivariableTransformWithParameter)
83 xo.getChild(Transform.MultivariableTransformWithParameter.class);
84
85 if (derivative.getDimension() != parameter.getDimension()) {
86 throw new XMLParseException("Gradient (" + derivative.getDimension() +
87 ") must be the same dimensions as the parameter (" + parameter.getDimension() + ")");
88 }
89
90
91 if (runMode == 0) {
92 return new HamiltonianMonteCarloOperator(adaptationMode, weight, derivative, parameter, transform,
93 stepSize, nSteps, drawVariance);
94 } else {
95 return new NoUTurnOperator(adaptationMode, weight, derivative, parameter,transform,
96 stepSize, nSteps, drawVariance);
97 }
98 }
99
100 @Override
101 public XMLSyntaxRule[] getSyntaxRules() {
102 return rules;
103 }
104
105 private final XMLSyntaxRule[] rules = {
106 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
107 AttributeRule.newIntegerRule(N_STEPS),
108 AttributeRule.newDoubleRule(STEP_SIZE),
109 AttributeRule.newDoubleRule(DRAW_VARIANCE),
110 AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true),
111 AttributeRule.newStringRule(MODE, true),
112 new ElementRule(Parameter.class),
113 new ElementRule(Transform.MultivariableTransformWithParameter.class, true),
114 new ElementRule(GradientWrtParameterProvider.class),
115 };
116
117 @Override
118 public String getParserDescription() {
119 return "Returns a Hamiltonian Monte Carlo transition kernel";
120 }
121
122 @Override
123 public Class getReturnType() {
124 return HamiltonianMonteCarloOperator.class;
125 }
126 }