Debian Codegraph @

src/dr/stats/Variate.java

raw symbols SCIP
1 /*
2 * Variate.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.stats;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 /**
32 * interface for a continuous variate.
33 *
34 * @author Andrew Rambaut
35 * @version $Id: Variate.java,v 1.15 2006/02/20 17:36:23 rambaut Exp $
36 */
37 public interface Variate<T> {
38 /**
39 * @return the number of values in this Variate
40 */
41 int getCount();
42
43 /**
44 * @return the ith value. Returns NaN if it is missing
45 */
46 T get(int index);
47
48 /**
49 * @return the minimum value.
50 */
51 T getMin();
52
53 /**
54 * @return the maximum value.
55 */
56 T getMax();
57
58 /**
59 * @return the range.
60 */
61 T getRange();
62
63 /**
64 * @return the sum.
65 */
66 T getSum();
67
68 /**
69 * @return the mean.
70 */
71 T getMean();
72
73 /**
74 * compute the q-th quantile for the distribution
75 * (= inverse cdf)
76 *
77 * @param q quantile (0 <= q <= 1)
78 */
79 double getQuantile(T q);
80
81 /**
82 * add a value
83 */
84 void add(T value);
85
86 /**
87 * add all the values
88 */
89 void add(Variate values);
90
91 /**
92 * add all the values
93 */
94 void add(T[] values);
95
96 /**
97 * remove a value
98 */
99 void remove(int index);
100
101 /**
102 * remove all values
103 */
104 void removeAll();
105
106
107 abstract class N<Number> implements Variate<Number> {
108 protected List<Number> values = new ArrayList<Number>();
109
110 // public N() {}
111 // public N(List<Number> values) {
112 // add(values);
113 // }
114
115 public int getCount() {
116 return values.size();
117 }
118
119 public void add(Variate values) {
120 for (int i = 0; i < values.getCount(); i++) {
121 add((Number) values.get(i));
122 }
123 }
124
125 public void add(Number value) {
126 values.add(value);
127 }
128
129 public Number get(int index) {
130 return values.get(index);
131 }
132
133 public void remove(int index) {
134 values.remove(index);
135 }
136
137 public void removeAll() {
138 values.clear();
139 }
140
141 public void add(List<Number> values) {
142 this.values.addAll(values);
143 }
144
145 public void add(Number[] values) {
146 for (Number value : values) {
147 add(value);
148 }
149 }
150 }
151
152 /**
153 * A double precision concrete implementation of Variate
154 */
155 class D extends N<Double> {
156 public D() {}
157
158 public D(List<Double> values) {
159 // super(values);
160 add(values);
161 }
162
163 public D(Double[] values) {
164 add(values);
165 }
166
167 /**
168 * @return the minimum value.
169 */
170 public Double getMin() {
171 Double minValue = java.lang.Double.POSITIVE_INFINITY;
172 for (Double value : values) {
173 if (value < minValue)
174 minValue = value;
175 }
176 return minValue;
177 }
178
179 /**
180 * @return the maximum value.
181 */
182 public Double getMax() {
183 Double maxValue = java.lang.Double.NEGATIVE_INFINITY;
184 for (Double value : values) {
185 if (value > maxValue)
186 maxValue = value;
187 }
188 return maxValue;
189 }
190
191 /**
192 * @return the range.
193 */
194 public Double getRange() {
195 return getMin() - getMax();
196 }
197
198 /**
199 * @return the mean.
200 */
201 public Double getMean() {
202 return getSum() / getCount();
203 }
204
205 /**
206 * @return the sum.
207 */
208 public Double getSum() {
209 Double sum = 0.0;
210 for (Double value : values) {
211 sum += value;
212 }
213 return sum;
214 }
215
216 /**
217 * compute the q-th quantile for the distribution
218 * (= inverse cdf)
219 *
220 * @param q quantile (0 <= q <= 1)
221 */
222 public double getQuantile(Double q) {
223 double[] dv = new double[values.size()];
224 for (int i = 0; i < values.size(); i++) {
225 dv[i] = values.get(i);
226 }
227 return DiscreteStatistics.quantile(q.doubleValue(), dv);
228 }
229
230 }
231
232 /**
233 * A single precision concrete implementation of Variate
234 */
235 class I extends N<Integer> {
236 public I() {}
237
238 public I(List<Integer> values) {
239 // super(values);
240 add(values);
241 }
242
243 public I(Integer[] values) {
244 add(values);
245 }
246
247 /**
248 * @return the minimum value.
249 */
250 public Integer getMin() {
251 Integer minValue = java.lang.Integer.MAX_VALUE;
252 for (Number value : values) {
253 if (value.intValue() < minValue)
254 minValue = value.intValue();
255 }
256 return minValue;
257 }
258
259 /**
260 * @return the maximum value.
261 */
262 public Integer getMax() {
263 Integer maxValue = java.lang.Integer.MIN_VALUE;
264 for (Number value : values) {
265 if (value.intValue() > maxValue)
266 maxValue = value.intValue();
267 }
268 return maxValue;
269 }
270
271 /**
272 * @return the range.
273 */
274 public Integer getRange() {
275 return getMin() - getMax();
276 }
277
278 /**
279 * @return the mean.
280 */
281 public Integer getMean() {
282 return getSum() / getCount();
283 }
284
285 /**
286 * @return the sum.
287 */
288 public Integer getSum() {
289 Integer sum = 0;
290 for (Integer value : values) {
291 sum += value;
292 }
293 return sum;
294 }
295
296 /**
297 * compute the q-th quantile for the distribution
298 * (= inverse cdf)
299 *
300 * @param q quantile (0 <= q <= 1)
301 */
302 public double getQuantile(Integer q) {
303 double[] dv = new double[values.size()];
304 for (int i = 0; i < values.size(); i++) {
305 dv[i] = values.get(i).doubleValue();
306 }
307 return DiscreteStatistics.quantile(q.doubleValue(), dv);
308 }
309
310 }
311 }