Debian Codegraph @

src/dr/evomodel/branchratemodel/ArbitraryBranchRates.java

raw symbols SCIP
1 /*
2 * ArbitraryBranchRates.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.tree.TreeModel;
31 import dr.evomodel.tree.TreeParameterModel;
32 import dr.evomodelxml.branchratemodel.ArbitraryBranchRatesParser;
33 import dr.inference.model.Model;
34 import dr.inference.model.Parameter;
35 import dr.inference.model.Variable;
36
37 /**
38 * Allows branch rates to take on any double value
39 * This is useful for forming a scaled mixture of normals for the continuous diffusion model
40 *
41 * @author Marc A. Suchard
42 * @author Alexei Drummond
43 */
44 public class ArbitraryBranchRates extends AbstractBranchRateModel {
45
46 // The rates of each branch
47 final TreeParameterModel rates;
48 final Parameter rateParameter;
49 final boolean reciprocal;
50 final boolean exp;
51
52 public ArbitraryBranchRates(TreeModel tree, Parameter rateParameter, boolean reciprocal, boolean exp, boolean setRates) {
53
54 super(ArbitraryBranchRatesParser.ARBITRARY_BRANCH_RATES);
55
56 if (setRates) {
57 double value = exp ? 0.0 : 1.0;
58 for (int i = 0; i < rateParameter.getDimension(); i++) {
59 rateParameter.setValue(i, value);
60 }
61 }
62 //Force the boundaries of rate
63 Parameter.DefaultBounds bounds = exp ?
64 new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY,
65 rateParameter.getDimension()) :
66 new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0,
67 rateParameter.getDimension());
68 rateParameter.addBounds(bounds);
69
70 this.rates = new TreeParameterModel(tree, rateParameter, false);
71 this.rateParameter = rateParameter;
72
73 addModel(rates);
74
75 this.reciprocal = reciprocal;
76 this.exp = exp;
77 }
78
79 public void setBranchRate(Tree tree, NodeRef node, double value) {
80 rates.setNodeValue(tree, node, value);
81 }
82
83 public double getBranchRate(final Tree tree, final NodeRef node) {
84 // Branch rates are proportional to time.
85 // In the traitLikelihoods, time is proportional to variance
86 // Fernandez and Steel (2000) shows the sampling density with the scalar proportional to precision
87 double rate = rates.getNodeValue(tree, node);
88
89 if (exp) {
90 rate = Math.exp(rate);
91 }
92
93 if (reciprocal) {
94 rate = 1.0 / rate;
95 }
96
97 return rate;
98 }
99
100 public boolean usingReciprocal() {
101 return reciprocal;
102 }
103
104 public void handleModelChangedEvent(Model model, Object object, int index) {
105 // Should be called by TreeParameterModel
106 fireModelChanged(object, index);
107 }
108
109 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
110 // Changes to rateParameter are handled by model changed events
111 }
112
113 protected void storeState() {
114 }
115
116 protected void restoreState() {
117 }
118
119 protected void acceptState() {
120 }
121
122 }