Debian Codegraph @

src/dr/evomodel/operators/NNI.java

raw symbols SCIP
1 /*
2 * NNI.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 /**
27 *
28 */
29 package dr.evomodel.operators;
30
31 import dr.evolution.tree.NodeRef;
32 import dr.evomodel.tree.TreeModel;
33 import dr.evomodelxml.operators.NNIParser;
34 import dr.math.MathUtils;
35
36 /**
37 * Implements the Nearest Neighbor Interchange (NNI) operation. This particular
38 * implementation assumes explicitely bifurcating trees. It works similar to the
39 * Narrow Exchange but with manipulating the height of a node if necessary.
40 *
41 * @author Sebastian Hoehna
42 * @version 1.0
43 */
44 public class NNI extends AbstractTreeOperator {
45
46 private TreeModel tree = null;
47
48 /**
49 *
50 */
51 public NNI(TreeModel tree, double weight) {
52 this.tree = tree;
53 setWeight(weight);
54 }
55
56 /*
57 * (non-Javadoc)
58 *
59 * @see dr.inference.operators.SimpleMCMCOperator#doOperation()
60 */
61 @Override
62 public double doOperation() {
63 final int nNodes = tree.getNodeCount();
64 final NodeRef root = tree.getRoot();
65
66 NodeRef i;
67
68 // get a random node where neither you or your father is the root
69 do {
70 i = tree.getNode(MathUtils.nextInt(nNodes));
71 } while( root == i || tree.getParent(i) == root );
72
73 // get parent node
74 final NodeRef iParent = tree.getParent(i);
75 // get parent of parent -> grant parent :)
76 final NodeRef iGrandParent = tree.getParent(iParent);
77 // get left child of grant parent -> uncle
78 NodeRef iUncle = tree.getChild(iGrandParent, 0);
79 // check if uncle == father
80 if( iUncle == iParent ) {
81 // if so take right child -> sibling of father
82 iUncle = tree.getChild(iGrandParent, 1);
83 }
84
85 // change the height of my father to be randomly between my uncle's
86 // heights and my grandfather's height
87 // this is necessary for the hastings ratio to do also if the uncle is
88 // younger anyway
89
90 final double heightGrandfather = tree.getNodeHeight(iGrandParent);
91 final double heightUncle = tree.getNodeHeight(iUncle);
92 final double minHeightFather = Math.max(heightUncle, tree.getNodeHeight(getOtherChild(tree, iParent, i)));
93 final double heightI = tree.getNodeHeight(i);
94 final double minHeightReverse = Math.max(heightI, tree.getNodeHeight(getOtherChild(tree, iParent, i)));
95
96 double ran;
97 do {
98 ran = MathUtils.nextDouble();
99 } while( ran == 0.0 || ran == 1.0 );
100
101 // now calculate the new height for father between the height of the
102 // uncle and the grandparent
103 final double newHeightFather = minHeightFather + (ran * (heightGrandfather - minHeightFather));
104 // set the new height for the father
105 tree.setNodeHeight(iParent, newHeightFather);
106
107 // double prForward = 1 / (heightGrandfather - minHeightFather);
108 // double prBackward = 1 / (heightGrandfather - minHeightReverse);
109 // hastings ratio = backward Prob / forward Prob
110 final double hastingsRatio = Math.log((heightGrandfather - minHeightFather) / (heightGrandfather - minHeightReverse));
111 // now change the nodes
112 exchangeNodes(tree, i, iUncle, iParent, iGrandParent);
113
114 tree.pushTreeChangedEvent(iParent);
115 tree.pushTreeChangedEvent(iGrandParent);
116
117 return hastingsRatio;
118 }
119
120 /*
121 * (non-Javadoc)
122 *
123 * @see dr.inference.operators.SimpleMCMCOperator#getOperatorName()
124 */
125 @Override
126 public String getOperatorName() {
127 return NNIParser.NNI;
128 }
129
130 public double getMinimumAcceptanceLevel() {
131 return 0.025;
132 }
133
134 public double getMinimumGoodAcceptanceLevel() {
135 return 0.05;
136 }
137
138 /*
139 * (non-Javadoc)
140 *
141 * @see dr.inference.operators.MCMCOperator#getPerformanceSuggestion()
142 */
143 public String getPerformanceSuggestion() {
144 // TODO Auto-generated method stub
145 return null;
146 }
147
148 }