Debian Codegraph @

src/dr/evomodel/treedatalikelihood/preorder/ProcessSimulationDelegate.java

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * DataSimulationDelegate.java
3 *
4 * Copyright (c) 2002-2016 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.treedatalikelihood.preorder;
27
28 import dr.evolution.tree.*;
29 import dr.evomodel.continuous.MultivariateDiffusionModel;
30 import dr.evomodel.treedatalikelihood.ProcessOnTreeDelegate;
31 import dr.evomodel.treedatalikelihood.ProcessSimulation;
32 import dr.evomodel.treedatalikelihood.TreeTraversal;
33 import dr.evomodel.treedatalikelihood.continuous.*;
34 import dr.evomodel.treedatalikelihood.continuous.cdi.ContinuousDiffusionIntegrator;
35 import dr.inference.model.Model;
36 import dr.inference.model.ModelListener;
37 import dr.math.matrixAlgebra.*;
38 import org.ejml.data.DenseMatrix64F;
39
40 import java.util.List;
41 import java.util.Map;
42
43 import static dr.math.matrixAlgebra.missingData.MissingOps.*;
44
45 /**
46 * ProcessSimulationDelegate - interface for a plugin delegate for data simulation on a tree.
47 *
48 * @author Andrew Rambaut
49 * @author Marc Suchard
50 * @version $Id$
51 */
52 public interface ProcessSimulationDelegate extends ProcessOnTreeDelegate, TreeTraitProvider, ModelListener {
53
54 void simulate(int[] operations, int operationCount, int rootNodeNumber);
55
56 void setCallback(ProcessSimulation simulationProcess);
57
58 int vectorizeNodeOperations(List<ProcessOnTreeDelegate.NodeOperation> nodeOperations, int[] operations);
59
60 abstract class AbstractDelegate implements ProcessSimulationDelegate {
61
62 AbstractDelegate(String name, Tree tree) {
63 this.name = name;
64 this.tree = tree;
65 this.baseTree = getBaseTree(tree);
66 constructTraits(treeTraitHelper);
67 }
68
69 protected abstract void constructTraits(Helper treeTraitHelper);
70
71 @Override
72 public final TreeTraversal.TraversalType getOptimalTraversalType() {
73 return TreeTraversal.TraversalType.PRE_ORDER;
74 }
75
76 @Override
77 public final void setCallback(ProcessSimulation simulationProcess) {
78 this.simulationProcess = simulationProcess;
79 }
80
81 @Override
82 public void simulate(final int[] operations, final int operationCount,
83 final int rootNodeNumber) {
84
85 setupStatistics();
86
87 simulateRoot(rootNodeNumber);
88
89 int k = 0;
90 for (int i = 0; i < operationCount; ++i) {
91 simulateNode(
92 operations[k ],
93 operations[k + 1],
94 operations[k + 2],
95 operations[k + 3],
96 operations[k + 4]
97 );
98
99 k += ContinuousDiffusionIntegrator.OPERATION_TUPLE_SIZE;
100 }
101 }
102
103 private static Tree getBaseTree(Tree derived) {
104 while (derived instanceof TransformableTree) {
105 derived = ((TransformableTree) derived).getOriginalTree();
106 }
107 return derived;
108 }
109
110 static NodeRef getBaseNode(Tree derived, NodeRef node) {
111 while (derived instanceof TransformableTree) {
112 derived = ((TransformableTree) derived).getOriginalTree();
113 node = ((TransformableTree) derived).getOriginalNode(node);
114 }
115 return node;
116 }
117
118 protected double getNormalization() {
119 return 1.0;
120 }
121
122 @Override
123 public final TreeTrait[] getTreeTraits() {
124 return treeTraitHelper.getTreeTraits();
125 }
126
127 @Override
128 public final TreeTrait getTreeTrait(String key) {
129 return treeTraitHelper.getTreeTrait(key);
130 }
131
132 protected abstract void setupStatistics();
133
134 protected abstract void simulateRoot(final int rootNumber);
135
136 protected abstract void simulateNode(final int v0,
137 final int v1,
138 final int v2,
139 final int v3,
140 final int v4);
141
142 final TreeTraitProvider.Helper treeTraitHelper = new Helper();
143
144 ProcessSimulation simulationProcess = null;
145
146 final Tree tree;
147 final Tree baseTree;
148 final String name;
149 }
150
151 abstract class AbstractContinuousTraitDelegate extends AbstractDelegate {
152
153 final int dimTrait;
154 final int numTraits;
155 final int dimNode;
156
157 final MultivariateDiffusionModel diffusionModel;
158 final ContinuousTraitPartialsProvider dataModel;
159 final ConjugateRootTraitPrior rootPrior;
160 final RootProcessDelegate rootProcessDelegate;
161 final ContinuousDataLikelihoodDelegate likelihoodDelegate;
162
163 double[] diffusionVariance;
164 DenseMatrix64F Vd;
165 DenseMatrix64F Pd;
166
167 double[][] cholesky;
168 Map<PartiallyMissingInformation.HashedIntArray,
169 ConditionalVarianceAndTransform> conditionalMap;
170
171 AbstractContinuousTraitDelegate(String name,
172 Tree tree,
173 MultivariateDiffusionModel diffusionModel,
174 ContinuousTraitPartialsProvider dataModel,
175 ConjugateRootTraitPrior rootPrior,
176 ContinuousRateTransformation rateTransformation,
177 ContinuousDataLikelihoodDelegate likelihoodDelegate) {
178 super(name, tree);
179
180 dimTrait = likelihoodDelegate.getTraitDim();
181 numTraits = likelihoodDelegate.getTraitCount();
182 dimNode = dimTrait * numTraits;
183 this.diffusionModel = diffusionModel;
184 this.dataModel = dataModel;
185 this.rateTransformation = rateTransformation;
186 this.rootPrior = rootPrior;
187 this.rootProcessDelegate = likelihoodDelegate.getRootProcessDelegate();
188 this.likelihoodDelegate = likelihoodDelegate;
189
190 diffusionModel.addModelListener(this);
191 }
192
193 @Override
194 protected final double getNormalization() {
195 return rateTransformation.getNormalization();
196 }
197
198 final private ContinuousRateTransformation rateTransformation;
199
200 protected boolean isLoggable() {
201 return true;
202 }
203
204 @Override
205 public void modelChangedEvent(Model model, Object object, int index) {
206 if (model == diffusionModel) {
207 clearCache();
208 } else {
209 throw new IllegalArgumentException("Unknown model");
210 }
211 }
212
213 @Override
214 public void modelRestored(Model model) {
215
216 }
217
218 @Override
219 protected void setupStatistics() {
220 if (diffusionVariance == null) {
221 double[][] diffusionPrecision = diffusionModel.getPrecisionmatrix();
222 diffusionVariance = getVectorizedVarianceFromPrecision(diffusionPrecision);
223 Vd = wrap(diffusionVariance, 0, dimTrait, dimTrait);
224 Pd = new DenseMatrix64F(diffusionPrecision);
225 }
226 if (cholesky == null) {
227 cholesky = getCholeskyOfVariance(diffusionVariance, dimTrait);
228 }
229 }
230
231 void clearCache() {
232 diffusionVariance = null;
233 Vd = null;
234 Pd = null;
235 cholesky = null;
236 conditionalMap = null;
237 }
238
239 static double[][] getCholeskyOfVariance(Matrix variance) {
240 final double[][] cholesky;
241 try {
242 cholesky = new CholeskyDecomposition(variance).getL();
243 } catch (IllegalDimension illegalDimension) {
244 throw new RuntimeException("Attempted Cholesky decomposition on non-square matrix");
245 }
246 return cholesky;
247 }
248
249 static double[][] getCholeskyOfVariance(double[] variance, final int dim) {
250 return CholeskyDecomposition.execute(variance, 0, dim);
251 }
252
253 private static double[] getVectorizedVarianceFromPrecision(double[][] precision) {
254 return new SymmetricMatrix(precision).inverse().toVectorizedComponents();
255 }
256
257 }
258 }