Debian Codegraph @

src/dr/inference/hmc/MaskedGradient.java

raw symbols SCIP
1 /*
2 * MaskedGradient.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.hmc;
27
28 import dr.inference.model.*;
29
30 /**
31 * @author Max Tolkoff
32 * @author Marc A. Suchard
33 */
34
35 public class MaskedGradient implements GradientWrtParameterProvider, VariableListener {
36
37 private final int dimension;
38 private final GradientWrtParameterProvider gradient;
39 private final Parameter parameter;
40
41 private final int[] map;
42
43 public MaskedGradient(GradientWrtParameterProvider gradient, Parameter mask) {
44 this.gradient = gradient;
45 mask.addVariableListener(this);
46
47 final Parameter originalParameter = gradient.getParameter();
48 this.parameter = new MaskedParameter(originalParameter, mask, true);
49
50 this.map = new int[originalParameter.getDimension()];
51 int[] inverseMap = new int[originalParameter.getDimension()];
52
53 this.dimension = MaskedParameter.updateMask(mask, map, inverseMap, 1);
54 }
55
56 @Override
57 public Likelihood getLikelihood() {
58 return gradient.getLikelihood();
59 }
60
61 @Override
62 public Parameter getParameter() {
63 return parameter;
64 }
65
66 @Override
67 public int getDimension() {
68 return dimension;
69 }
70
71 @Override
72 public double[] getGradientLogDensity() {
73
74 double[] originalGradient = gradient.getGradientLogDensity();
75 double[] result = new double[dimension];
76
77 for (int i = 0; i < dimension; ++i) {
78 result[i] = originalGradient[map[i]];
79 }
80
81 return result;
82 }
83
84 @Override
85 public void variableChangedEvent(Variable variable, int index, Variable.ChangeType type) {
86 throw new RuntimeException("Changing mask is not implemented");
87 }
88 }