Debian Codegraph @

src/dr/app/treestat/statistics/GammaStatistic.java

raw symbols SCIP
1 /*
2 * GammaStatistic.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.app.treestat.statistics;
27
28 import dr.evolution.tree.Tree;
29
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33
34 /**
35 * @author Alexei Drummond
36 *
37 * @version $Id: GammaStatistic.java,v 1.5 2006/05/09 10:24:27 rambaut Exp $
38 */
39 public class GammaStatistic extends AbstractTreeSummaryStatistic {
40
41 private GammaStatistic() { }
42
43 public double[] getSummaryStatistic(Tree tree) {
44
45 int n = tree.getExternalNodeCount();
46 double[] g = getIntervals(tree);
47
48 double T = 0; // total branch length
49 for (int j = 2; j <= n; j++) {
50 T += j * g[j-2];
51 }
52
53 double sum = 0.0;
54 for (int i = 2; i < n; i++) {
55 for (int k = 2; k <= i; k++) {
56 sum += k * g[k-2];
57 }
58 }
59
60 double gamma = ((sum / (n-2.0)) - (T / 2.0)) / (T * Math.sqrt(1.0 / (12.0 * (n - 2.0))));
61 return new double[] { gamma };
62 }
63
64 /**
65 * @return the intervals in an ultrametric tree in order from root to tips.
66 */
67 private static double[] getIntervals(Tree tree) {
68
69 List<Double> heights = new ArrayList<Double>();
70
71 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
72 heights.add(tree.getNodeHeight(tree.getInternalNode(i)));
73 }
74 Collections.sort(heights, Collections.reverseOrder());
75
76 double[] intervals = new double[heights.size()];
77 for (int i = 0; i < intervals.length - 1; i++) {
78 double height1 = heights.get(i);
79 double height2 = heights.get(i + 1);
80
81 intervals[i] = height1 - height2;
82 }
83 intervals[intervals.length - 1] = heights.get(intervals.length - 1);
84
85 return intervals;
86
87 }
88
89 public String getSummaryStatisticName() { return FACTORY.getSummaryStatisticName(); }
90 public String getSummaryStatisticDescription() { return FACTORY.getSummaryStatisticDescription(); }
91 public String getSummaryStatisticReference() { return FACTORY.getSummaryStatisticReference(); }
92 public boolean allowsPolytomies() { return FACTORY.allowsPolytomies(); }
93 public boolean allowsNonultrametricTrees() { return FACTORY.allowsNonultrametricTrees(); }
94 public boolean allowsUnrootedTrees() { return FACTORY.allowsUnrootedTrees(); }
95 public SummaryStatisticDescription.Category getCategory() { return FACTORY.getCategory(); }
96
97 public static final TreeSummaryStatistic.Factory FACTORY = new TreeSummaryStatistic.Factory() {
98
99 public TreeSummaryStatistic createStatistic() {
100 return new GammaStatistic();
101 }
102
103 public String getSummaryStatisticName() {
104 return "Gamma";
105 }
106
107 public String getSummaryStatisticDescription() {
108 return "The gamma-statistic is a summary of the information contained in the inter-node " +
109 "intervals of a phylogeny; under the assumption that the clade diversified " +
110 "with constant rates, it follows a normal distribution with mean of zero and a standard-deviation " +
111 "of one " +
112 "(Pybus and Harvey 2000). Thus, the null hypothesis that the clade diversified with constant " +
113 "rates may be tested with 1 - 2*pnorm(abs(gamma.stat(phy))) for a two-tailed test, " +
114 "or 1 - pnorm(abs(gamma.stat(phy))) for a one-tailed test, both returning the corresponding P-value.";
115 }
116
117 public String getSummaryStatisticReference() {
118 return "Pybus & Harvey (2000)";
119 }
120
121 public boolean allowsPolytomies() { return true; }
122
123 public boolean allowsNonultrametricTrees() { return false; }
124
125 public boolean allowsUnrootedTrees() { return false; }
126
127 public SummaryStatisticDescription.Category getCategory() { return SummaryStatisticDescription.Category.SPECIATION; }
128 };
129 }