Debian Codegraph @

src/dr/evomodel/speciation/BetaSplittingModel.java

raw symbols SCIP
1 /*
2 * BetaSplittingModel.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.speciation;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeUtils;
31 import dr.evomodelxml.speciation.BetaSplittingModelParser;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34 import dr.math.GammaFunction;
35
36 /**
37 * This class contains methods that describe a Beta-splitting branching model (Aldous 1996, 2001).
38 *
39 * @author Alexei Drummond
40 */
41 public class BetaSplittingModel extends BranchingModel {
42
43 public BetaSplittingModel(Parameter phiParameter, Tree tree) {
44
45 super(BetaSplittingModelParser.BETA_SPLITTING_MODEL);
46
47 this.phiParameter = phiParameter;
48 addVariable(phiParameter);
49 phiParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
50 N = tree.getExternalNodeCount();
51 logProbs = new double[N + 1][N + 1];
52 storedLogProbs = new double[N + 1][N + 1];
53 makeSplitProbs(logProbs);
54 }
55
56 /**
57 * Returns the phi parameter, which can range from -infinity to +infinity.
58 *
59 * @return phi phi = log(beta/2.0 + 1.0)
60 */
61 public double getPhi() {
62 return phiParameter.getParameterValue(0);
63 }
64
65 /**
66 * Sets the phi parameter, which can range from -infinity to +infinity.
67 *
68 * @param phi = log(beta/2.0 + 1.0)
69 */
70 public void setPhi(double phi) {
71
72 phiParameter.setParameterValue(0, phi);
73 }
74
75 /**
76 * Returns the beta parameter, which can range from -2 to positive infinity.
77 *
78 * @return beta
79 */
80 public double getBeta() {
81 return (Math.exp(getPhi()) - 1) * 2.0;
82 }
83
84 /**
85 * Sets the beta parameter, which can range from -2 to positive infinity
86 *
87 * @param beta
88 */
89 public void setBeta(double beta) {
90 if (beta < -2.0) throw new IllegalArgumentException();
91 setPhi(Math.log(beta / 2.0 + 1));
92 }
93
94 /**
95 * Return the probability of this node producing subtrees of the given sizes.
96 *
97 * @param tree the tree of the node for which the probability will be calculated
98 * @param node the node for which the probability will be calculated
99 * @return the log of the probability of the split below the given node
100 */
101 public double logNodeProbability(Tree tree, NodeRef node) {
102
103 if (tree.isExternal(node)) return 0.0;
104
105 int leftChildren = TreeUtils.getLeafCount(tree, tree.getChild(node, 0));
106 int rightChildren = TreeUtils.getLeafCount(tree, tree.getChild(node, 1));
107
108 // calculate the probability of this pair..
109
110 return logProbs[leftChildren + rightChildren][leftChildren] +
111 logProbs[leftChildren + rightChildren][rightChildren];
112 }
113
114 // **************************************************************
115 // XMLElement IMPLEMENTATION
116 // **************************************************************
117
118 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
119 //System.out.println("parameter changed:" + parameter.getParameterName());
120 makeSplitProbs(logProbs);
121 }
122
123 protected void storeState() {
124 //copy the current logProbs into the storedLogProbs
125 for (int i = 0; i < logProbs.length; i++) {
126 System.arraycopy(logProbs[i], 0, storedLogProbs[i], 0, logProbs[i].length);
127 }
128 }
129
130 /**
131 * Restore the stored state
132 */
133 protected void restoreState() {
134
135 // swap the logProbs arrays
136 double[][] tmp = logProbs;
137 logProbs = storedLogProbs;
138 storedLogProbs = tmp;
139 }
140
141 private void makeSplitProbs(double[][] logProbs) {
142
143 // code modified from original code by Marc Suchard
144
145 double beta = getBeta();
146 //double[][] prob = new double[bigN+1][bigN+1];
147 logProbs[2][1] = 0;
148 logProbs[3][1] = logProbs[3][2] = Math.log(0.5);
149 //if( beta < 90 ) {
150 double[] logGammaBeta = new double[N];
151 double[] logGammaNone = new double[N];
152 for (int i = 1; i < N; i++) {
153 logGammaBeta[i] = GammaFunction.lnGamma(beta + i + 1.0); //Sfun.gamma(beta + i + 1.0);
154 logGammaNone[i] = GammaFunction.lnGamma(i + 1.0); //Sfun.gamma(i + 1.0);
155 }
156 for (int n = 4; n <= N; n++) {
157 double end = (n / 2.0) + 0.5;
158 for (int i = 1; i <= end; i++)
159 logProbs[n][i] = logProbs[n][n - i] =
160 logGammaBeta[i] + logGammaBeta[n - i] - logGammaNone[i] - logGammaNone[n - i];
161
162 // Normalize
163 double sum = 0;
164 for (int i = 1; i < n; i++) {
165 sum += Math.exp(logProbs[n][i]);
166 }
167 double logSum = Math.log(sum);
168 // divide the probabilities through by the sum
169 for (int i = 1; i < n; i++) {
170 logProbs[n][i] -= logSum;
171 //System.out.println("logProbs[" + n + "][" + i + "]=" + logProbs[n][i] );
172 }
173 }
174 /*} else { // calculate on log scale for numerical stability
175 //System.err.println("Log scale calculations");
176 double[] logGammaBeta = new double[bigN];
177 double[] logGammaNone = new double[bigN];
178 for(int i=1; i<bigN; i++) {
179 logGammaBeta[i] = Sfun.logGamma(beta + i + 1.0);
180 logGammaNone[i] = Sfun.logGamma(i + 1.0);
181 //System.err.print(beta+" "+logGammaBeta[i] + " "+logGammaNone[i]);
182 //System.err.println(" "+gammln(beta + i + 1.0));
183 }
184 double c = logGammaBeta[1];
185 logGammaBeta[1] = 0;
186 for(int i=2; i<bigN; i++)
187 logGammaBeta[i] -= c;
188 for(int n=4; n<=bigN; n++) {
189 double end = (n / 2.0) + 0.5;
190 for(int i=1; i<=end; i++) {
191 prob[n][i] = prob[n][n-i] = Math.exp(logGammaBeta[i] + logGammaBeta[n-i] - logGammaNone[i] - logGammaNone[n-i]);
192 //System.err.println(prob[n][i]);
193 }
194 double sum = 0;
195 for(int i=1; i<n; i++)
196 sum += prob[n][i];
197 for(int i=1; i<n; i++)
198 prob[n][i] /= sum;
199 }
200 } */
201 //return prob;
202 }
203
204 //Protected stuff
205 final Parameter phiParameter;
206
207 double[][] logProbs;
208 double[][] storedLogProbs;
209
210 final int N;
211 }