Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * FuLiD.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.NodeRef;
29 import dr.evolution.tree.Tree;
30
31 /**
32 * @author Alexei Drummond
33 *
34 * @version $Id: FuLiD.java,v 1.2 2005/09/28 13:50:56 rambaut Exp $
35 */
36 public class FuLiD extends AbstractTreeSummaryStatistic {
37
38 private FuLiD() { }
39
40 public double[] getSummaryStatistic(Tree tree) {
41
42 double externalLength = 0.0;
43 double internalLength = 0.0;
44
45 int externalNodeCount = tree.getExternalNodeCount();
46 for (int i = 0; i < externalNodeCount; i++) {
47 NodeRef node = tree.getExternalNode(i);
48 NodeRef parent = tree.getParent(node);
49 externalLength += tree.getNodeHeight(parent) - tree.getNodeHeight(node);
50 }
51
52 int internalNodeCount = tree.getInternalNodeCount();
53 for (int i = 0; i < internalNodeCount; i++) {
54 NodeRef node = tree.getInternalNode(i);
55 if (!tree.isRoot(node)) {
56 NodeRef parent = tree.getParent(node);
57 internalLength += tree.getNodeHeight(parent) - tree.getNodeHeight(node);
58 }
59 }
60
61 int n = externalNodeCount;
62
63 double total = externalLength + internalLength;
64
65 // difference in expectations
66 double D = total - a(n)*externalLength;
67
68 // normalized
69 D /= Math.sqrt(u(n)*total + (v(n)*(total*total)));
70
71 return new double[] { D };
72 }
73
74 private double a(int n) {
75 double a = 0.0;
76 for (int k = 1; k < n; k++) {
77 a += 1/(double)k;
78 }
79 return a;
80 }
81
82 private double b(int n) {
83 double b = 0.0;
84 for (int k = 1; k < n; k++) {
85 b += 1/(double)(k*k);
86 }
87 return b;
88 }
89
90 private double c(int n) {
91
92 if (n==2) return 1.0;
93
94 double an = a(n);
95
96 double c = 2.0 * (n * an - 2.0 * (n - 1.0));
97 c /= (n-1)*(n-2);
98
99 return c;
100 }
101
102 private double v(int n) {
103 double an2 = a(n);
104 an2 *= an2;
105
106 double v = 1 + (an2/(b(n)+an2)) * (c(n)-((n+1)/(n-1)));
107
108 return v;
109 }
110
111 private double u(int n) {
112 return a(n) - 1 - v(n);
113 }
114
115 public String getSummaryStatisticName() { return FACTORY.getSummaryStatisticName(); }
116 public String getSummaryStatisticDescription() { return FACTORY.getSummaryStatisticDescription(); }
117 public String getSummaryStatisticReference() { return FACTORY.getSummaryStatisticReference(); }
118 public boolean allowsPolytomies() { return FACTORY.allowsPolytomies(); }
119 public boolean allowsNonultrametricTrees() { return FACTORY.allowsNonultrametricTrees(); }
120 public boolean allowsUnrootedTrees() { return FACTORY.allowsUnrootedTrees(); }
121 public SummaryStatisticDescription.Category getCategory() { return FACTORY.getCategory(); }
122
123 public static final TreeSummaryStatistic.Factory FACTORY = new TreeSummaryStatistic.Factory() {
124
125 public TreeSummaryStatistic createStatistic() {
126 return new FuLiD();
127 }
128
129 public String getSummaryStatisticName() {
130 return "Fu & Li's D";
131 }
132
133 public String getSummaryStatisticDescription() {
134
135 return "The normalized difference between total tree length and external branch tree length. " +
136 "Note that this implementation uses branch lengths rather than the counts of mutations (this " +
137 "has a nice side-effect of taking into account complex mutation models.)";
138 }
139
140 public String getSummaryStatisticReference() {
141
142 return "Fu & Li (1993)";
143 }
144
145 public boolean allowsPolytomies() { return false; }
146
147 public boolean allowsNonultrametricTrees() { return false; }
148
149 public boolean allowsUnrootedTrees() { return false; }
150
151 public SummaryStatisticDescription.Category getCategory() { return SummaryStatisticDescription.Category.POPULATION_GENETIC; }
152 };
153 }