Debian Codegraph @

src/dr/evomodel/continuous/SemiConjugateMultivariateTraitLikelihood.java

raw symbols SCIP
1 /*
2 * SemiConjugateMultivariateTraitLikelihood.java
3 *
4 * Copyright (c) 2002-2015 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;
27
28 import dr.evolution.tree.MutableTreeModel;
29 import dr.evolution.tree.NodeRef;
30 import dr.evomodel.branchratemodel.BranchRateModel;
31 import dr.inference.model.CompoundParameter;
32 import dr.inference.model.Model;
33 import dr.math.distributions.MultivariateNormalDistribution;
34 import dr.math.matrixAlgebra.IllegalDimension;
35 import dr.math.matrixAlgebra.Matrix;
36
37 import java.util.List;
38
39 /**
40 * Integrated multivariate trait likelihood that assumes a semi-conjugate prior on the root.
41 * The semi-conjugate prior is a multivariate normal distribution with an independent precision
42 *
43 * @author Marc A. Suchard
44 */
45 public class SemiConjugateMultivariateTraitLikelihood extends IntegratedMultivariateTraitLikelihood {
46
47 public SemiConjugateMultivariateTraitLikelihood(String traitName,
48 MutableTreeModel treeModel,
49 MultivariateDiffusionModel diffusionModel,
50 CompoundParameter traitParameter,
51 List<Integer> missingIndices,
52 boolean cacheBranches,
53 boolean scaleByTime,
54 boolean useTreeLength,
55 BranchRateModel rateModel,
56 Model samplingDensity,
57 boolean reportAsMultivariate,
58 MultivariateNormalDistribution rootPrior,
59 boolean reciprocalRates,
60 List<RestrictedPartials> partials) {
61
62 super(traitName, treeModel, diffusionModel, traitParameter, null, missingIndices, cacheBranches, scaleByTime,
63 useTreeLength, rateModel, null, null, null, samplingDensity, partials, reportAsMultivariate, reciprocalRates);
64
65 setRootPrior(rootPrior); // Semi-conjugate multivariate normal with own mean and precision
66 }
67
68 @Override
69 public boolean getComputeWishartSufficientStatistics() {
70 return false; // No need for outer products, as Gibbs sampling of diffusion matrix is not possible
71 }
72
73 protected double calculateAscertainmentCorrection(int taxonIndex) {
74 throw new RuntimeException("Ascertainment correction not yet implemented for semi-conjugate trait likelihoods");
75 }
76
77 public double getRescaledLengthToRoot(NodeRef node) {
78 double length = 0;
79 final NodeRef root = treeModel.getRoot();
80 while (node != root) {
81 length += getRescaledBranchLengthForPrecision(node);
82 node = treeModel.getParent(node);
83 }
84 return length;
85 }
86
87 protected double integrateLogLikelihoodAtRoot(double[] y,
88 double[] Ay,
89 double[][] AplusB,
90 double[][] treePrecision, double rootPrecision) {
91 double detAplusB = 0;
92 double square = 0;
93
94 // square : (Ay + Bz)' (A+B)^{-1} (Ay + Bz)
95
96 if (dimTrait > 1) {
97 for (int i = 0; i < dimTrait; i++) {
98 Ay[i] += Bz[i]; // Ay is filled with sum, and original value is destroyed
99 for (int j = 0; j < dimTrait; j++) {
100 AplusB[i][j] = treePrecision[i][j] * rootPrecision + rootPriorPrecision[i][j];
101 }
102 }
103
104 Matrix mat = new Matrix(AplusB);
105
106 try {
107 detAplusB = mat.determinant();
108
109 } catch (IllegalDimension illegalDimension) {
110 illegalDimension.printStackTrace();
111 }
112
113 double[][] invAplusB = mat.inverse().toComponents();
114
115 for (int i = 0; i < dimTrait; i++) {
116 for (int j = 0; j < dimTrait; j++)
117 square += Ay[i] * invAplusB[i][j] * Ay[j];
118 }
119 } else {
120 // 1D is very simple
121 detAplusB = treePrecision[0][0] * rootPrecision + rootPriorPrecision[0][0];
122 Ay[0] += Bz[0];
123 square = Ay[0] * Ay[0] / detAplusB;
124 }
125
126 double retValue = 0.5 * (logRootPriorPrecisionDeterminant - Math.log(detAplusB) - zBz + square);
127
128 if (DEBUG) {
129 System.err.println("(Ay+Bz)(A+B)^{-1}(Ay+Bz) = " + square);
130 System.err.println("density = " + retValue);
131 System.err.println("zBz = " + zBz);
132 }
133
134 return retValue;
135 }
136
137 private void setRootPriorSumOfSquares() {
138 if (integrateRoot) {
139 Bz = new double[dimTrait];
140 // z'Bz -- sum-of-squares root contribution
141 zBz = computeWeightedAverageAndSumOfSquares(rootPriorMean, Bz, rootPriorPrecision, dimTrait, 1.0);
142 } else {
143 zBz = 0;
144 }
145 }
146
147 private void setRootPrior(MultivariateNormalDistribution rootPrior) {
148 rootPriorMean = rootPrior.getMean();
149 rootPriorPrecision = rootPrior.getScaleMatrix();
150
151 try {
152 logRootPriorPrecisionDeterminant = Math.log(new Matrix(rootPriorPrecision).determinant());
153 } catch (IllegalDimension illegalDimension) {
154 illegalDimension.printStackTrace();
155 }
156 setRootPriorSumOfSquares();
157 }
158
159 protected double[][] computeMarginalRootMeanAndVariance(double[] rootMean, double[][] treePrecision,
160 double[][] treeVariance, double rootPrecision) {
161
162 computeWeightedAverageAndSumOfSquares(rootMean, Ay, treePrecision, dimTrait, rootPrecision); // Fills in Ay
163
164 double[][] AplusB = tmpM;
165
166 for (int i = 0; i < dimTrait; i++) {
167 Ay[i] += Bz[i]; // Ay is filled with sum, and original value is destroyed
168 for (int j = 0; j < dimTrait; j++) {
169 AplusB[i][j] = treePrecision[i][j] * rootPrecision + rootPriorPrecision[i][j];
170 }
171 }
172 Matrix mat = new Matrix(AplusB);
173 double[][] invAplusB = mat.inverse().toComponents();
174
175 // Expected value: (A + B)^{-1}(Ay + Bz)
176 for (int i = 0; i < dimTrait; i++) {
177 rootMean[i] = 0.0;
178 for (int j = 0; j < dimTrait; j++) {
179 rootMean[i] += invAplusB[i][j] * Ay[j];
180 }
181 }
182 return invAplusB;
183 }
184
185 protected double[] rootPriorMean;
186 protected double[][] rootPriorPrecision;
187 protected double logRootPriorPrecisionDeterminant;
188 protected double[] Bz;
189 private double zBz; // Prior sum-of-squares contribution
190 }