Debian Codegraph @

src/dr/evomodel/treedatalikelihood/continuous/DriftDiffusionModelDelegate.java

raw symbols SCIP
1 /*
2 * HomogenousDiffusionModelDelegate.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.continuous;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evomodel.branchratemodel.BranchRateModel;
31 import dr.evomodel.continuous.MultivariateDiffusionModel;
32 import dr.inference.model.Model;
33
34 import java.util.List;
35
36 /**
37 * A simple diffusion model delegate with branch-specific drift and constant diffusion
38 * @author Marc A. Suchard
39 * @version $Id$
40 */
41 public final class DriftDiffusionModelDelegate extends AbstractDiffusionModelDelegate {
42
43 // private static final boolean DEBUG = true;
44
45 private final int dim;
46 private final List<BranchRateModel> branchRateModels;
47
48 public DriftDiffusionModelDelegate(Tree tree,
49 MultivariateDiffusionModel diffusionModel,
50 List<BranchRateModel> branchRateModels) {
51 this(tree, diffusionModel, branchRateModels, 0);
52 }
53
54 private DriftDiffusionModelDelegate(Tree tree,
55 MultivariateDiffusionModel diffusionModel,
56 List<BranchRateModel> branchRateModels,
57 int partitionNumber) {
58 super(tree, diffusionModel, partitionNumber);
59 this.branchRateModels = branchRateModels;
60
61 dim = diffusionModel.getPrecisionParameter().getColumnDimension();
62
63 if (branchRateModels != null) {
64
65 for (BranchRateModel rateModel : branchRateModels) {
66 addModel(rateModel);
67 }
68
69 if (branchRateModels.size() != dim) {
70 throw new IllegalArgumentException("Invalid dimensions");
71 }
72 }
73 }
74
75 @Override
76 protected void handleModelChangedEvent(Model model, Object object, int index) {
77
78 if (branchRateModels.contains(model)) {
79 fireModelChanged(model);
80 } else {
81 super.handleModelChangedEvent(model, object, index);
82 }
83 }
84
85 @Override
86 public boolean hasDrift() { return true; }
87
88 @Override
89 protected double[] getDriftRates(int[] branchIndices, int updateCount) {
90
91 final double[] drift = new double[updateCount * dim]; // TODO Reuse?
92
93 if (branchRateModels != null) {
94
95 int offset = 0;
96 for (int i = 0; i < updateCount; ++i) {
97
98 final NodeRef node = tree.getNode(branchIndices[i]); // TODO Check if correct node
99
100 for (int model = 0; model < dim; ++model) {
101 drift[offset] = branchRateModels.get(model).getBranchRate(tree, node);
102 ++offset;
103 }
104 }
105 }
106 return drift;
107 }
108
109 double[] getAccumulativeDrift(final NodeRef node) {
110 final double[] drift = new double[dim];
111 recursivelyAccumulateDrift(node, drift);
112 return drift;
113 }
114
115 private void recursivelyAccumulateDrift(final NodeRef node, final double[] drift) {
116 if (!tree.isRoot(node)) {
117
118 final double length = tree.getBranchLength(node);
119
120 for (int model = 0; model < dim; ++model) {
121 drift[model] += branchRateModels.get(model).getBranchRate(tree, node) * length;
122 }
123
124 recursivelyAccumulateDrift(tree.getParent(node), drift);
125 }
126 }
127 }