Debian Codegraph @

src/dr/evomodel/arg/operators/ARGReassortmentOperator.java

raw symbols SCIP
1 /*
2 * ARGReassortmentOperator.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.arg.operators;
27
28 import dr.evomodel.arg.ARGModel;
29 import dr.evomodelxml.tree.TreeModelParser;
30 import dr.inference.model.CompoundParameter;
31 import dr.inference.operators.SimpleMCMCOperator;
32 import dr.math.MathUtils;
33
34 import java.util.logging.Logger;
35
36 public class ARGReassortmentOperator extends SimpleMCMCOperator {
37
38 public static final String ADD_PROBABILITY = "addProbability";
39 public static final String ARG_REASSORTMENT_OPERATOR = "argReassortmentOperator";
40 public static final String INTERNAL_NODES = "internalNodes";
41 public static final String INTERNAL_AND_ROOT = "internalNodesPlusRoot";
42 public static final String NODE_RATES = TreeModelParser.NODE_RATES;
43 public static final String CHOOSE_BRANCHES_FIRST = "chooseBranchesFirst";
44 public static final double LOG_TWO = Math.log(2.0);
45
46 private double singlePartitionProbability = 0.0;
47 private double probBelowRoot = 0.9;
48 private double size = 0.0; //Used to choose add step
49 private boolean branchesFirst;
50
51 private ARGModel arg;
52
53 private CompoundParameter internalNodeParameters;
54 private CompoundParameter internalAndRootNodeParameters;
55 private CompoundParameter nodeRates;
56
57 public ARGReassortmentOperator(ARGModel arg, int weight, boolean branchesFirst,
58 double singlePartProb, double addProbability,
59 CompoundParameter internalNodeParameters,
60 CompoundParameter internalAndRootNodeParameters,
61 CompoundParameter nodeRates) {
62 this.arg = arg;
63 this.internalNodeParameters = internalNodeParameters;
64 this.internalAndRootNodeParameters = internalAndRootNodeParameters;
65 this.nodeRates = nodeRates;
66
67 this.branchesFirst = branchesFirst;
68 this.singlePartitionProbability = singlePartProb;
69
70 setWeight(weight);
71
72 this.size = addProbability;
73
74 //Transformed for computation reasons
75 probBelowRoot = -Math.log(1 - Math.sqrt(probBelowRoot));
76 }
77
78
79 public double doOperation() {
80 double logHastings = 0;
81
82 if (MathUtils.nextDouble() < 1.0 / (1 + Math.exp(-size)))
83 logHastings = addOperation() - size;
84 else
85 logHastings = removeOperation() + size;
86
87 return logHastings;
88 }
89
90 private double addOperation() {
91 if (branchesFirst)
92 return addOperationBranchesFirst();
93
94 return addOperationHeightsFirst();
95 }
96
97 private double addOperationBranchesFirst() {
98
99 double logHastings = 0;
100 double treeHeight = arg.getNodeHeight(arg.getRoot());
101 double newBifurcationHeight = Double.POSITIVE_INFINITY;
102 double newReassortmentHeight = Double.POSITIVE_INFINITY;
103
104 double theta = probBelowRoot / treeHeight;
105
106 while (newBifurcationHeight > treeHeight && newReassortmentHeight > treeHeight) {
107 newBifurcationHeight = MathUtils.nextExponential(theta);
108 newReassortmentHeight = MathUtils.nextExponential(theta);
109 }
110
111 logHastings += theta * (newBifurcationHeight + newReassortmentHeight) - LOG_TWO
112 - 2.0 * Math.log(theta) + Math.log(1 - Math.exp(-2.0 * treeHeight * theta));
113
114 if (newBifurcationHeight < newReassortmentHeight) {
115 double temp = newBifurcationHeight;
116 newBifurcationHeight = newReassortmentHeight;
117 newReassortmentHeight = temp;
118 }
119
120 return 0;
121 }
122
123 private double addOperationHeightsFirst() {
124
125 return 0;
126 }
127
128 private double removeOperation() {
129
130 return 0;
131 }
132
133 public String getOperatorName() {
134 return ARG_REASSORTMENT_OPERATOR;
135 }
136
137 public String getPerformanceSuggestion() {
138 return "Try changing the add probability probability";
139 }
140
141 private class NoReassortmentEventException extends Exception {
142 private static final long serialVersionUID = 1L;
143
144 public NoReassortmentEventException() {
145 super("");
146 }
147 }
148 }