Debian Codegraph @

src/dr/inference/operators/hmc/HamiltonianMonteCarloOperator.java

raw symbols SCIP
1 /*
2 * NewHamiltonianMonteCarloOperator.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.inference.operators.hmc;
27
28 import dr.inference.hmc.GradientWrtParameterProvider;
29 import dr.inference.model.Parameter;
30 import dr.inference.operators.AbstractAdaptableOperator;
31 import dr.inference.operators.AdaptationMode;
32 import dr.math.distributions.NormalDistribution;
33 import dr.util.Transform;
34
35 /**
36 * @author Max Tolkoff
37 * @author Marc A. Suchard
38 */
39
40 public class HamiltonianMonteCarloOperator extends AbstractAdaptableOperator {
41
42 final GradientWrtParameterProvider gradientProvider;
43 protected double stepSize;
44 protected final int nSteps;
45 final NormalDistribution drawDistribution;
46 final LeapFrogEngine leapFrogEngine;
47
48 public HamiltonianMonteCarloOperator(AdaptationMode mode, double weight, GradientWrtParameterProvider gradientProvider,
49 Parameter parameter, Transform transform,
50 double stepSize, int nSteps, double drawVariance) {
51 super(mode);
52 setWeight(weight);
53 setTargetAcceptanceProbability(0.8); // Stan default
54
55 this.gradientProvider = gradientProvider;
56 this.stepSize = stepSize;
57 this.nSteps = nSteps;
58 this.drawDistribution = new NormalDistribution(0, Math.sqrt(drawVariance));
59 this.leapFrogEngine = (transform != null ?
60 new LeapFrogEngine.WithTransform(parameter, transform) :
61 new LeapFrogEngine.Default(parameter));
62
63 }
64
65 @Override
66 public String getOperatorName() {
67 return "Vanilla HMC operator";
68 }
69
70 static double getScaledDotProduct(final double[] momentum,
71 final double sigmaSquared) {
72 double total = 0.0;
73 for (double m : momentum) {
74 total += m * m;
75 }
76
77 return total / (2 * sigmaSquared);
78 }
79
80 static double[] drawInitialMomentum(final NormalDistribution distribution, final int dim) {
81 double[] momentum = new double[dim];
82 for (int i = 0; i < dim; i++) {
83 momentum[i] = (Double) distribution.nextRandom();
84 }
85 return momentum;
86 }
87
88 @Override
89 public double doOperation() { return leapFrog(); }
90
91 private long count = 0;
92
93 private static final boolean DEBUG = false;
94
95 private double leapFrog() {
96
97 if (DEBUG) {
98 if (count % 5 == 0) {
99 System.err.println(stepSize);
100 }
101 ++count;
102 }
103
104 final int dim = gradientProvider.getDimension();
105
106 final double sigmaSquared = drawDistribution.getSD() * drawDistribution.getSD();
107
108 final double[] momentum = drawInitialMomentum(drawDistribution, dim);
109 final double[] position = leapFrogEngine.getInitialPosition();
110
111 final double prop = getScaledDotProduct(momentum, sigmaSquared) +
112 leapFrogEngine.getParameterLogJacobian();
113
114 leapFrogEngine.updateMomentum(position, momentum,
115 gradientProvider.getGradientLogDensity(), stepSize / 2);
116
117
118 if (DEBUG) {
119 System.err.println("nSteps = " + nSteps);
120 }
121
122 for (int i = 0; i < nSteps; i++) { // Leap-frog
123
124 leapFrogEngine.updatePosition(position, momentum, stepSize, sigmaSquared);
125
126 if (i < (nSteps - 1)) {
127 leapFrogEngine.updateMomentum(position, momentum,
128 gradientProvider.getGradientLogDensity(), stepSize);
129 }
130 }
131
132 leapFrogEngine.updateMomentum(position, momentum,
133 gradientProvider.getGradientLogDensity(), stepSize / 2);
134
135 final double res = getScaledDotProduct(momentum, sigmaSquared) +
136 leapFrogEngine.getParameterLogJacobian();
137
138 return prop - res; //hasting ratio
139 }
140
141 @Override
142 public double getAdaptableParameter() {
143 return Math.log(stepSize);
144 }
145
146 @Override
147 public void setAdaptableParameter(double value) {
148 if (DEBUG) {
149 System.err.println("Setting coercable paramter: " + getAdaptableParameter() + " -> " + value);
150 }
151 stepSize = Math.exp(value);
152 }
153
154 @Override
155 public double getRawParameter() {
156 return stepSize;
157 }
158
159 @Override
160 public String getAdaptableParameterName() {
161 return "stepSize";
162 }
163
164 interface LeapFrogEngine {
165
166 double[] getInitialPosition();
167
168 double getParameterLogJacobian();
169
170 void updateMomentum(final double[] position,
171 final double[] momentum,
172 final double[] gradient,
173 final double functionalStepSize);
174
175 void updatePosition(final double[] position,
176 final double[] momentum,
177 final double functionalStepSize,
178 final double sigmaSquared);
179
180 void setParameter(double[] position);
181
182 class Default implements LeapFrogEngine {
183
184 final protected Parameter parameter;
185
186 protected Default(Parameter parameter) {
187 this.parameter = parameter;
188 }
189
190 @Override
191 public double[] getInitialPosition() {
192 return parameter.getParameterValues();
193 }
194
195 @Override
196 public double getParameterLogJacobian() {
197 return 0;
198 }
199
200 @Override
201 public void updateMomentum(double[] position, double[] momentum, double[] gradient,
202 double functionalStepSize) {
203
204 final int dim = momentum.length;
205 for (int i = 0; i < dim; ++i) {
206 momentum[i] += functionalStepSize * gradient[i];
207 }
208 }
209
210 @Override
211 public void updatePosition(double[] position, double[] momentum,
212 double functionalStepSize, double sigmaSquared) {
213
214 final int dim = momentum.length;
215 for (int j = 0; j < dim; j++) {
216 position[j] += functionalStepSize * momentum[j] / sigmaSquared;
217 }
218
219 setParameter(position);
220 }
221
222
223 public void setParameter(double[] position) {
224
225 final int dim = position.length;
226 for (int j = 0; j < dim; ++j) {
227 parameter.setParameterValueQuietly(j, position[j]);
228 }
229 parameter.fireParameterChangedEvent(); // Does not seem to work with MaskedParameter
230 // parameter.setParameterValueNotifyChangedAll(0, position[0]);
231 }
232 }
233
234 class WithTransform extends Default {
235
236 final private Transform transform;
237 double[] unTransformedPosition;
238
239 private WithTransform(Parameter parameter, Transform transform) {
240 super(parameter);
241 this.transform = transform;
242 }
243
244 @Override
245 public double getParameterLogJacobian() {
246 return transform.getLogJacobian(unTransformedPosition,0, unTransformedPosition.length);
247 }
248
249 @Override
250 public double[] getInitialPosition() {
251 unTransformedPosition = super.getInitialPosition();
252 return transform.transform(unTransformedPosition, 0, unTransformedPosition.length);
253 }
254
255 @Override
256 public void updateMomentum(double[] position, double[] momentum, double[] gradient,
257 double functionalStepSize) {
258
259 gradient = transform.updateGradientLogDensity(gradient, unTransformedPosition,
260 0, unTransformedPosition.length);
261
262 super.updateMomentum(position, momentum, gradient, functionalStepSize);
263 }
264
265 @Override
266 public void setParameter(double[] position) {
267 unTransformedPosition = transform.inverse(position, 0, position.length);
268 super.setParameter(unTransformedPosition);
269 }
270 }
271 }
272 }