Debian Codegraph @

src/dr/evomodel/branchratemodel/ContinuousTraitBranchRateModel.java

raw symbols SCIP
1 /*
2 * ContinuousTraitBranchRateModel.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.branchratemodel;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evomodel.continuous.SampledMultivariateTraitLikelihood;
31 import dr.evomodel.tree.TreeModel;
32 import dr.evomodelxml.branchratemodel.ContinuousTraitBranchRateModelParser;
33 import dr.inference.model.Model;
34 import dr.inference.model.Parameter;
35 import dr.inference.model.Variable;
36
37 /**
38 * Takes the log rates at each node provided by a specified rate and give the branch rate as the average.
39 *
40 * @author Andrew Rambaut
41 * @author Marc Suchard
42 */
43 public class ContinuousTraitBranchRateModel extends AbstractBranchRateModel {
44
45 private final String trait;
46 private final int dimension;
47 private final Parameter rateParameter;
48 private final Parameter ratioParameter;
49 private SampledMultivariateTraitLikelihood traitLikelihood;
50
51 public ContinuousTraitBranchRateModel(SampledMultivariateTraitLikelihood traitLikelihood, int dimension) {
52 super(ContinuousTraitBranchRateModelParser.TRAIT_BRANCH_RATES);
53
54 this.traitLikelihood = traitLikelihood;
55 this.trait = traitLikelihood.getTraitName();
56 this.dimension = dimension;
57
58 this.rateParameter = null;
59 this.ratioParameter = null;
60 addModel(traitLikelihood);
61 }
62
63 public ContinuousTraitBranchRateModel(String trait, Parameter rateParameter, Parameter ratioParameter) {
64 super(ContinuousTraitBranchRateModelParser.TRAIT_BRANCH_RATES);
65
66 this.trait = trait;
67 dimension = 0;
68 this.rateParameter = rateParameter;
69 this.ratioParameter = ratioParameter;
70
71 if (rateParameter != null) {
72 addVariable(rateParameter);
73 }
74
75 if (ratioParameter != null) {
76 addVariable(ratioParameter);
77 }
78 }
79
80 public void handleModelChangedEvent(Model model, Object object, int index) {
81 fireModelChanged();
82 }
83
84 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
85 fireModelChanged();
86 }
87
88 protected void storeState() {
89 }
90
91 protected void restoreState() {
92 }
93
94 protected void acceptState() {
95 }
96
97
98 public double getBranchRate(final Tree tree, final NodeRef node) {
99 NodeRef parent = tree.getParent(node);
100 if (parent == null) {
101 throw new IllegalArgumentException("Root does not have a valid rate");
102 }
103
104 double rate = 1.0;
105 TreeModel treeModel = (TreeModel) tree;
106
107 if (rateParameter != null) {
108 double scale = 1.0;
109 double ratio = 1.0;
110
111 if (rateParameter != null) {
112 scale = rateParameter.getParameterValue(0);
113 }
114
115 if (ratioParameter != null) {
116 ratio = ratioParameter.getParameterValue(0);
117 }
118
119
120 // get the log rate for the node and its parent
121 double rate1 = ratio * treeModel.getMultivariateNodeTrait(node, trait)[0];
122 double rate2 = ratio * treeModel.getMultivariateNodeTrait(parent, trait)[0];
123
124 if (rate1 == rate2) {
125 return scale * Math.exp(rate1);
126 }
127
128 rate = scale * (Math.exp(rate2) - Math.exp(rate1)) / (rate2 - rate1);
129 } else {
130 double rate1 = treeModel.getMultivariateNodeTrait(node, trait)[dimension];
131 double rate2 = treeModel.getMultivariateNodeTrait(parent, trait)[dimension];
132
133 if (rate1 == rate2) {
134 return Math.exp(rate1);
135 }
136
137 rate = (Math.exp(rate2) - Math.exp(rate1)) / (rate2 - rate1); // TODO Should this not be averaged on the log-scale?
138 }
139
140 return rate;
141 }
142
143 }