| 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 |
import dr.math.Binomial; |
| 30 |
|
| 31 |
import java.util.ArrayList; |
| 32 |
import java.util.Collections; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class DeltaStatistic extends AbstractTreeSummaryStatistic { |
| 40 |
|
| 41 |
private DeltaStatistic() { } |
| 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 += Binomial.choose2(j) * g[j-2]; |
| 51 |
} |
| 52 |
|
| 53 |
double sum = 0.0; |
| 54 |
for (int i = n; i > 2; i--) { |
| 55 |
for (int k = n; k >= i; k--) { |
| 56 |
sum += Binomial.choose2(k) * g[k-2]; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
double delta = ((T / 2.0) - (sum * (1.0 / (n - 2.0)))) / (T * Math.sqrt(1.0/(12.0*(n - 2.0)))); |
| 61 |
return new double[] { delta }; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private static double[] getIntervals(Tree tree) { |
| 68 |
|
| 69 |
ArrayList<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 DeltaStatistic.FACTORY.getSummaryStatisticName(); } |
| 90 |
public String getSummaryStatisticDescription() { return DeltaStatistic.FACTORY.getSummaryStatisticDescription(); } |
| 91 |
public String getSummaryStatisticReference() { return DeltaStatistic.FACTORY.getSummaryStatisticReference(); } |
| 92 |
public boolean allowsPolytomies() { return DeltaStatistic.FACTORY.allowsPolytomies(); } |
| 93 |
public boolean allowsNonultrametricTrees() { return DeltaStatistic.FACTORY.allowsNonultrametricTrees(); } |
| 94 |
public boolean allowsUnrootedTrees() { return DeltaStatistic.FACTORY.allowsUnrootedTrees(); } |
| 95 |
public Category getCategory() { return DeltaStatistic.FACTORY.getCategory(); } |
| 96 |
|
| 97 |
public static final Factory FACTORY = new Factory() { |
| 98 |
|
| 99 |
public TreeSummaryStatistic createStatistic() { |
| 100 |
return new DeltaStatistic(); |
| 101 |
} |
| 102 |
|
| 103 |
public String getSummaryStatisticName() { |
| 104 |
return "Delta"; |
| 105 |
} |
| 106 |
|
| 107 |
public String getSummaryStatisticDescription() { |
| 108 |
return "The delta-statistic is a summary of the information contained in the inter-node " + |
| 109 |
"intervals of a genealogy; under the assumption of a constant-size population, " + |
| 110 |
"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 population has a constant " + |
| 113 |
"population size may be tested with 1 - 2*pnorm(abs(delta.stat(phy))) for a two-tailed test, " + |
| 114 |
"or 1 - pnorm(abs(delta.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 Category getCategory() { return Category.POPULATION_GENETIC; } |
| 128 |
}; |
| 129 |
} |