Debian Codegraph @

src/dr/inference/hmc/SumDerivative.java

raw symbols SCIP
1 /*
2 * SumDerivative.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.Likelihood;
31 import dr.inference.model.Parameter;
32 import dr.math.matrixAlgebra.Vector;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 /**
38 * @author Max Tolkoff
39 * @author Marc A. Suchard
40 */
41 public class SumDerivative implements GradientWrtParameterProvider {
42
43 private final int dimension;
44 private final Likelihood likelihood;
45 private final Parameter parameter;
46
47 List<GradientWrtParameterProvider> derivativeList;
48
49 public SumDerivative(List<GradientWrtParameterProvider> derivativeList){
50
51 // TODO Check that parameters are the same
52
53 this.derivativeList = derivativeList;
54
55 GradientWrtParameterProvider first = derivativeList.get(1);
56 dimension = first.getDimension();
57 parameter = first.getParameter();
58
59 if (derivativeList.size() == 1) {
60 likelihood = first.getLikelihood();
61 } else {
62 List<Likelihood> likelihoodList = new ArrayList<Likelihood>();
63
64 for (GradientWrtParameterProvider grad : derivativeList) {
65 if (grad.getDimension() != dimension) {
66 throw new RuntimeException("Unequal parameter dimensions");
67 }
68 likelihoodList.add(grad.getLikelihood());
69 }
70 likelihood = new CompoundLikelihood(likelihoodList);
71 }
72 }
73
74 @Override
75 public Likelihood getLikelihood() {
76 return likelihood;
77 }
78
79 @Override
80 public Parameter getParameter() {
81 return parameter;
82 }
83
84 @Override
85 public int getDimension() {
86 return dimension;
87 }
88
89 @Override
90 public double[] getGradientLogDensity() {
91 int size = derivativeList.size();
92
93 if (DEBUG) {
94 // start timer
95 }
96
97 final double[] derivative = derivativeList.get(0).getGradientLogDensity();
98
99 if (DEBUG) {
100 // stop timer
101
102 String name = derivativeList.get(0).getLikelihood().getId();
103 System.err.println(name);
104 System.err.println(new Vector(derivative));
105 }
106
107 for (int i = 1; i < size; i++) {
108
109 if (DEBUG) {
110 // start timer
111 }
112
113 final double[] temp = derivativeList.get(i).getGradientLogDensity();
114
115 if (DEBUG) {
116 // stop timer
117
118 String name = derivativeList.get(i).getLikelihood().getId();
119 System.err.println(name);
120 System.err.println(new Vector(temp));
121 }
122
123 for (int j = 0; j < temp.length; j++) {
124 derivative[j] += temp[j];
125 }
126 }
127
128 if (DEBUG) {
129 // print times
130
131 if (DEBUG_KILL) {
132 System.exit(-1);
133 }
134 }
135
136 return derivative;
137 }
138
139 private static final boolean DEBUG = false;
140 private static final boolean DEBUG_KILL = false;
141 }