Debian Codegraph @

src/dr/evomodel/continuous/hmc/FullyConjugateTreeTipsPotentialDerivative.java

raw symbols SCIP
1 /*
2 * FullyConjugateTreeTipsPotentialDerivative.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.evomodel.continuous.hmc;
27
28 import dr.evomodel.continuous.FullyConjugateMultivariateTraitLikelihood;
29 import dr.inference.hmc.GradientWrtParameterProvider;
30 import dr.inference.model.Likelihood;
31 import dr.inference.model.Parameter;
32 import dr.xml.Reportable;
33
34 /**
35 * @author Max Tolkoff
36 * @author Marc A. Suchard
37 */
38 public class FullyConjugateTreeTipsPotentialDerivative implements GradientWrtParameterProvider, Reportable {
39
40 private final FullyConjugateMultivariateTraitLikelihood treeLikelihood;
41 private final Parameter traitParameter;
42 private final Parameter mask;
43
44 public FullyConjugateTreeTipsPotentialDerivative(FullyConjugateMultivariateTraitLikelihood treeLikelihood,
45 Parameter mask){
46 this.treeLikelihood = treeLikelihood;
47 traitParameter = treeLikelihood.getTraitParameter();
48 this.mask = mask;
49
50 if (mask != null) {
51 if (traitParameter.getDimension() != mask.getDimension()) {
52 throw new IllegalArgumentException("Trait and mask parameters have differing dimension");
53 }
54 }
55 }
56
57 @Override
58 public Likelihood getLikelihood() {
59 return treeLikelihood;
60 }
61
62 @Override
63 public Parameter getParameter() {
64 return traitParameter;
65 }
66
67 @Override
68 public int getDimension() {
69 return traitParameter.getDimension();
70 }
71
72 @Override
73 public double[] getGradientLogDensity() {
74
75 final int dimTraits = treeLikelihood.getDimTrait() * treeLikelihood.getNumData();
76 final int ntaxa = traitParameter.getDimension() / dimTraits;
77
78 final double[] derivative = new double[traitParameter.getDimension()];
79
80 final double[][] allMeans = treeLikelihood.getConditionalMeans();
81 final double[] allScalars = treeLikelihood.getPrecisionFactors();
82 final double[][] precisionMatrix = treeLikelihood.getDiffusionModel().getPrecisionmatrix();
83
84 for (int i = 0; i < ntaxa; ++i) {
85
86 final double[] mean = allMeans[i];
87 final double scale = allScalars[i];
88
89 for (int j = 0; j < dimTraits; ++j) {
90
91 double sum = 0.0;
92 for (int k = 0; k < dimTraits; ++k) {
93 sum += (mean[k] - traitParameter.getParameterValue(i * dimTraits + k)) *
94 scale * precisionMatrix[j][k];
95 }
96 derivative[i * dimTraits + j] = sum;
97 }
98 }
99
100 if (mask != null) {
101 for (int i = 0; i < mask.getDimension(); ++i) {
102 if (mask.getParameterValue(i) == 0.0) {
103 derivative[i] = 0.0;
104 }
105 }
106 }
107
108 return derivative;
109 }
110
111 @Override
112 public String getReport() {
113 return (new dr.math.matrixAlgebra.Vector(getGradientLogDensity())).toString();
114 }
115 }