Debian Codegraph @

src/dr/inference/model/TestStatistic.java

raw symbols SCIP
1 /*
2 * TestStatistic.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.inference.model;
27
28 import dr.util.Attribute;
29
30 /**
31 * @author Alexei Drummond
32 * @author Andrew Rambaut
33 * @version $Id: TestStatistic.java,v 1.12 2005/07/11 14:06:25 rambaut Exp $
34 */
35 public class TestStatistic extends Statistic.Abstract implements BooleanStatistic {
36
37 private Attribute attribute = null;
38 private Attribute attribute2 = null;
39 private double testValue1, testValue2;
40 private final int mode;
41
42 public static final int EQUALS = 0;
43 public static final int GREATER_THAN = 1;
44 public static final int LESS_THAN = 2;
45 public static final int INSIDE = 3;
46 public static final int OUTSIDE = 4;
47
48 public TestStatistic(String name, Attribute attr, double value, int mode) {
49 super(name);
50
51 attribute = attr;
52 testValue1 = value;
53 this.mode = mode;
54 }
55
56 /**
57 * A constructor for modes that require 2 values (i.e., INSIDE and OUTSIDE).
58 */
59 public TestStatistic(String name, Attribute attr, double value1, double value2, int mode) {
60 super(name);
61
62 attribute = attr;
63 testValue1 = value1;
64 testValue2 = value2;
65 this.mode = mode;
66 }
67
68 /**
69 * A constructor that compares 2 attributes (i.e., GREATER_THAN or LESS_THAN).
70 */
71 public TestStatistic(String name, Attribute attr1, Attribute attr2, int mode) {
72 super(name);
73
74 attribute = attr1;
75 attribute2 = attr2;
76 this.mode = mode;
77 }
78
79 public int getDimension() {
80 return 1;
81 }
82
83 /**
84 * @return boolean result of test.
85 */
86 public double getStatisticValue(int dim) {
87 return getBoolean(dim) ? 1.0 : 0.0;
88 }
89
90 /**
91 * @return boolean result of test.
92 */
93 public boolean getBoolean(int i) {
94
95 double num;
96 if (attribute instanceof Statistic) {
97 num = ((Statistic) attribute).getStatisticValue(0);
98 } else {
99 num = ((Number) attribute.getAttributeValue()).doubleValue();
100 }
101
102 if (attribute2 != null) {
103 if (attribute2 instanceof Statistic) {
104 testValue1 = ((Statistic) attribute2).getStatisticValue(0);
105 } else {
106 testValue1 = ((Number) attribute2.getAttributeValue()).doubleValue();
107 }
108 }
109
110 switch (mode) {
111 case EQUALS:
112 if (num == testValue1) return true;
113 break;
114 case GREATER_THAN:
115 if (num > testValue1) return true;
116 break;
117 case LESS_THAN:
118 if (num < testValue1) return true;
119 break;
120 case INSIDE:
121 if (num > testValue1 && num < testValue2) return true;
122 break;
123 case OUTSIDE:
124 if (num < testValue1 && num > testValue2) return true;
125 break;
126 }
127 return false;
128 }
129
130 }