Debian Codegraph @

src/dr/evomodel/operators/RateExchangeOperator.java

raw symbols SCIP
1 /*
2 * RateExchangeOperator.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.operators;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evomodel.tree.TreeModel;
30 import dr.evomodelxml.operators.RateExchangeOperatorParser;
31 import dr.inference.operators.MCMCOperator;
32 import dr.inference.operators.SimpleMCMCOperator;
33 import dr.math.MathUtils;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36
37 /**
38 * Implements the RateExchange move.
39 *
40 * @author Alexei Drummond
41 * @version $Id: RateExchangeOperator.java,v 1.3 2005/01/06 14:46:36 rambaut Exp $
42 */
43 public class RateExchangeOperator extends SimpleMCMCOperator {
44
45 private static final String TRAIT = "trait";
46
47 private final TreeModel tree;
48 private final boolean swapRates;
49 private final boolean swapTraits;
50 private final boolean swapAtRoot;
51 private final boolean moveHeight;
52
53 public RateExchangeOperator(TreeModel tree, double weight, boolean swapRates, boolean swapTraits, boolean swapAtRoot, boolean moveHeight) {
54 this.tree = tree;
55 setWeight(weight);
56 this.swapRates = swapRates;
57 this.swapTraits = swapTraits;
58 this.swapAtRoot = swapAtRoot;
59 this.moveHeight = moveHeight;
60 }
61
62 /**
63 * Do a probablistic subtree slide move.
64 *
65 * @return the log-transformed hastings ratio
66 */
67 public double doOperation() {
68
69 NodeRef node0 = tree.getInternalNode(MathUtils.nextInt(tree.getInternalNodeCount()));
70 NodeRef node1 = tree.getChild(node0, 0);
71 NodeRef node2 = tree.getChild(node0, 1);
72
73 if (swapRates) {
74 if (swapAtRoot) {
75 double[] rates = new double[]{tree.getNodeRate(node0), tree.getNodeRate(node1), tree.getNodeRate(node2)};
76
77 int r1 = MathUtils.nextInt(3);
78 tree.setNodeRate(node0, rates[r1]);
79 // swap down the top trait
80 rates[r1] = rates[2];
81
82 int r2 = MathUtils.nextInt(2);
83 tree.setNodeRate(node1, rates[r2]);
84 // swap down the top trait
85 rates[r2] = rates[1];
86
87 tree.setNodeRate(node2, rates[0]);
88 } else {
89 // just swap the two child rates...
90 double tmp = tree.getNodeRate(node1);
91 tree.setNodeRate(node1, tree.getNodeRate(node2));
92 tree.setNodeRate(node2, tmp);
93 }
94 }
95
96 if (swapTraits) {
97 if (swapAtRoot) {
98 double[] traits = new double[]{tree.getNodeTrait(node0, TRAIT), tree.getNodeTrait(node1, TRAIT), tree.getNodeTrait(node2, TRAIT)};
99
100 int r1 = MathUtils.nextInt(3);
101 tree.setNodeTrait(node0, TRAIT, traits[r1]);
102 // swap down the top trait
103 traits[r1] = traits[2];
104
105 int r2 = MathUtils.nextInt(2);
106 tree.setNodeTrait(node1, TRAIT, traits[r2]);
107 // swap down the top trait
108 traits[r2] = traits[1];
109
110 tree.setNodeTrait(node2, TRAIT, traits[0]);
111 } else {
112 // just swap the two child traits...
113 double tmp = tree.getNodeTrait(node1, TRAIT);
114 tree.setNodeTrait(node1, TRAIT, tree.getNodeTrait(node2, TRAIT));
115 tree.setNodeTrait(node2, TRAIT, tmp);
116 }
117 }
118
119 // If the node is not the root, do a uniform pick of its height
120 if (!tree.isRoot(node0) && moveHeight) {
121 double lower = tree.getNodeHeightLower(node0);
122 double upper = tree.getNodeHeightUpper(node0);
123 double newValue = (MathUtils.nextDouble() * (upper - lower)) + lower;
124 tree.setNodeHeight(node0, newValue);
125 }
126
127 return 0.0;
128 }
129
130 public double getTargetAcceptanceProbability() {
131 return 0.234;
132 }
133
134
135 public String getOperatorName() {
136 return RateExchangeOperatorParser.RATE_EXCHANGE;
137 }
138
139 public Element createOperatorElement(Document d) {
140 return d.createElement(RateExchangeOperatorParser.RATE_EXCHANGE);
141 }
142
143 }