| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 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; |
| 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 |
|
| 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 |
} |