Debian Codegraph @

src/dr/inference/model/Statistic.java

raw symbols SCIP
1 /*
2 * Statistic.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.inference.loggers.LogColumn;
29 import dr.inference.loggers.Loggable;
30 import dr.inference.loggers.NumberColumn;
31 import dr.util.Attribute;
32 import dr.util.Identifiable;
33
34 /**
35 * @author Alexei Drummond
36 * @author Andrew Rambaut
37 * @version $Id: Statistic.java,v 1.8 2005/05/24 20:26:00 rambaut Exp $
38 */
39 public interface Statistic extends Attribute<double[]>, Identifiable, Loggable {
40
41 public static final String NAME = "name";
42
43 /**
44 * @return the name of this statistic
45 */
46 String getStatisticName();
47
48 /**
49 * @param dim the dimension to return name of
50 * @return the statistic's name for a given dimension
51 */
52 String getDimensionName(int dim);
53
54 /**
55 * Set the names of the dimensions (optional, by default they are named after the statistic).
56 * @param names
57 */
58 void setDimensionNames(String[] names) ;
59
60 /**
61 * @return the number of dimensions that this statistic has.
62 */
63 int getDimension();
64
65 /**
66 * @param dim the dimension to return value of
67 * @return the statistic's scalar value in the given dimension
68 */
69 double getStatisticValue(int dim);
70
71 /**
72 * @return the sum of all the elements
73 */
74 double getValueSum();
75
76 /**
77 * Abstract base class for Statistics
78 */
79 public abstract class Abstract implements Statistic {
80
81 private String name = null;
82
83 public Abstract() {
84 this.name = null;
85 }
86
87 public Abstract(String name) {
88 this.name = name;
89 }
90
91 @Override
92 public String getStatisticName() {
93 if (name != null) {
94 return name;
95 } else if (id != null) {
96 return id;
97 } else {
98 return getClass().toString();
99 }
100 }
101
102 @Override
103 public String getDimensionName(int dim) {
104 if (getDimension() == 1) {
105 return getStatisticName();
106 } else {
107 return getStatisticName() + Integer.toString(dim + 1);
108 }
109 }
110
111 @Override
112 public void setDimensionNames(String[] names) {
113 // do nothing
114 }
115
116 @Override
117 public double getValueSum() {
118 double sum = 0.0;
119 for (int i = 0; i < getDimension(); i++) {
120 sum += getStatisticValue(i);
121 }
122 return sum;
123 }
124
125 @Override
126 public String toString() {
127 StringBuffer buffer = new StringBuffer(String.valueOf(getStatisticValue(0)));
128
129 for (int i = 1; i < getDimension(); i++) {
130 buffer.append(", ").append(String.valueOf(getStatisticValue(i)));
131 }
132 return buffer.toString();
133 }
134
135 // **************************************************************
136 // Attribute IMPLEMENTATION
137 // **************************************************************
138
139 @Override
140 public final String getAttributeName() {
141 return getStatisticName();
142 }
143
144 @Override
145 public double[] getAttributeValue() {
146 double[] stats = new double[getDimension()];
147 for (int i = 0; i < stats.length; i++) {
148 stats[i] = getStatisticValue(i);
149 }
150
151 return stats;
152 }
153
154 // **************************************************************
155 // Identifiable IMPLEMENTATION
156 // **************************************************************
157
158 protected String id = null;
159
160 /**
161 * @return the id.
162 */
163 @Override
164 public String getId() {
165 return id;
166 }
167
168 /**
169 * Sets the id.
170 */
171 @Override
172 public void setId(String id) {
173 this.id = id;
174 }
175
176 // **************************************************************
177 // Loggable IMPLEMENTATION
178 // **************************************************************
179
180 /**
181 * @return the log columns.
182 */
183 @Override
184 public LogColumn[] getColumns() {
185 LogColumn[] columns = new LogColumn[getDimension()];
186 for (int i = 0; i < getDimension(); i++) {
187 columns[i] = new StatisticColumn(getDimensionName(i), i);
188 }
189 return columns;
190 }
191
192 private class StatisticColumn extends NumberColumn {
193 private final int dim;
194
195 public StatisticColumn(String label, int dim) {
196 super(label);
197 this.dim = dim;
198 }
199
200 public double getDoubleValue() {
201 return getStatisticValue(dim); }
202 }
203 }
204 }