Debian Codegraph @

src/dr/inference/hmc/CompoundGradient.java

raw symbols SCIP
1 /*
2 * CompoundGradient.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.hmc.GradientWrtParameterProvider;
29 import dr.inference.model.CompoundLikelihood;
30 import dr.inference.model.CompoundParameter;
31 import dr.inference.model.Likelihood;
32 import dr.inference.model.Parameter;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 /**
38 * @author Max Tolkoff
39 * @author Marc A. Suchard
40 */
41
42 public class CompoundGradient implements GradientWrtParameterProvider {
43
44 private final int dimension;
45 private final List<GradientWrtParameterProvider> derivativeList;
46 private final Likelihood likelihood;
47 private final Parameter parameter;
48
49 public CompoundGradient(List<GradientWrtParameterProvider> derivativeList) {
50
51 this.derivativeList = derivativeList;
52
53 if (derivativeList.size() == 1) {
54 likelihood = derivativeList.get(0).getLikelihood();
55 parameter = derivativeList.get(0).getParameter();
56 dimension = parameter.getDimension();
57 } else {
58 List<Likelihood> likelihoodList = new ArrayList<Likelihood>();
59 CompoundParameter compoundParameter = new CompoundParameter("hmc");
60
61 int dim = 0;
62 for (GradientWrtParameterProvider grad : derivativeList) {
63 likelihoodList.add(grad.getLikelihood());
64 Parameter p = grad.getParameter();
65 compoundParameter.addParameter(p);
66
67 dim += p.getDimension();
68 }
69
70 likelihood = new CompoundLikelihood(likelihoodList);
71 parameter = compoundParameter;
72 dimension = dim;
73 }
74 }
75
76 @Override
77 public Likelihood getLikelihood() {
78 return likelihood;
79 }
80
81 @Override
82 public Parameter getParameter() {
83 return parameter;
84 }
85
86 @Override
87 public int getDimension() {
88 return dimension;
89 }
90
91 // @Override
92 // public void getGradientLogDensity(final double[] destination, final int offset) {
93 // double[] grad = getGradientLogDensity();
94 // System.arraycopy(grad, 0, destination, offset, grad.length);
95 // }
96
97 @Override
98 public double[] getGradientLogDensity() {
99
100 double[] result = new double[dimension];
101
102 int offset = 0;
103 for (GradientWrtParameterProvider grad : derivativeList) {
104
105 System.err.println(grad.getLikelihood().getId() + " " + grad.getParameter().getId());
106
107 double[] tmp = grad.getGradientLogDensity();
108 System.arraycopy(tmp, 0, result, offset, grad.getDimension());
109 offset += grad.getDimension();
110 }
111
112 return result;
113 }
114 }