1 | package geniusweb.ip.general;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Arrays;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * A class for tracking the average, variance and standard deviation of a
|
---|
8 | * sequence of numbers.
|
---|
9 | */
|
---|
10 |
|
---|
11 | /*
|
---|
12 | * ================================== General tips on how to use AvVar
|
---|
13 | * ==================================
|
---|
14 | *
|
---|
15 | * You will have to change the package to yours.
|
---|
16 | *
|
---|
17 | * Although it looks somewhat complicated, it is very easy really. To use,
|
---|
18 | * simply create an object e.g. like: AvVar myData = new AvVar()
|
---|
19 | *
|
---|
20 | * Every time you have a new data point (has to be a double value), you simply
|
---|
21 | * use the add() method. For instance, myData.add(data)
|
---|
22 | *
|
---|
23 | * Whenever you want to know the standard dev or variance, just invoke
|
---|
24 | * myData.stddev() and myData.variance() respectively.
|
---|
25 | *
|
---|
26 | * That's it. It uses very little memory.
|
---|
27 | *
|
---|
28 | * =============================================================================
|
---|
29 | * == How to compute the mean, standard error of the mean, and confidence
|
---|
30 | * intervals
|
---|
31 | * =============================================================================
|
---|
32 | * ==
|
---|
33 | *
|
---|
34 | * (1) The mean (i.e. average) can be computed as follows: average()
|
---|
35 | *
|
---|
36 | * (2) The standard error of the mean is the standard deviation devided by the
|
---|
37 | * square root of the number of values.
|
---|
38 | *
|
---|
39 | * This can be computed as follows: stddev() / Math.sqrt( num() )
|
---|
40 | *
|
---|
41 | * (3) The 95% confidence intervals are computed by adding/subtracting the
|
---|
42 | * standard error of the mean, multiplied by 1.96 The 99% confidence intervals
|
---|
43 | * are computed by adding/subtracting the standard error of the mean, multiplied
|
---|
44 | * by 2.58
|
---|
45 | *
|
---|
46 | * This can be computed as follows: 1.96 * (stddev() / Math.sqrt( num() )) 2.58
|
---|
47 | * * (stddev() / Math.sqrt( num() ))
|
---|
48 | */
|
---|
49 | public class AvVar implements Cloneable {
|
---|
50 |
|
---|
51 | public boolean checkValidity = true;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Counts the number of double values in the added sequence
|
---|
55 | *
|
---|
56 | * @see chm.math.AvVar#add
|
---|
57 | */
|
---|
58 | private long num;
|
---|
59 | /**
|
---|
60 | * This variable contain the sum of the added values
|
---|
61 | *
|
---|
62 | * @see chm.math.AvVar#add
|
---|
63 | */
|
---|
64 | private double av;
|
---|
65 | /**
|
---|
66 | * This variable contain the sum of the squares of the added values
|
---|
67 | *
|
---|
68 | * @see chm.math.AvVar#add
|
---|
69 | */
|
---|
70 | private double sav;
|
---|
71 | /**
|
---|
72 | * These variable are used to track the minimal and maximal value observed
|
---|
73 | * in the sequence of added values
|
---|
74 | *
|
---|
75 | * @see chm.math.AvVar#add
|
---|
76 | */
|
---|
77 | private double min, max;
|
---|
78 | private long indexMin, indexMax;
|
---|
79 | private boolean doMedian = false;
|
---|
80 | ArrayList values = null;
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * return new String("Aver. = ").concat( new
|
---|
84 | * String().valueOf(average())).concat( new String(" Var. = ")). concat( new
|
---|
85 | * String().valueOf(variance())); };
|
---|
86 | */
|
---|
87 | public AvVar() {
|
---|
88 | init();
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * return new String("Aver. = ").concat( new
|
---|
94 | * String().valueOf(average())).concat( new String(" Var. = ")). concat( new
|
---|
95 | * String().valueOf(variance())); };
|
---|
96 | */
|
---|
97 | public AvVar(boolean doMedian) {
|
---|
98 | init();
|
---|
99 | this.doMedian = doMedian;
|
---|
100 | if (doMedian)
|
---|
101 | values = new ArrayList();
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * adds a double to the sequence
|
---|
107 | *
|
---|
108 | * @param d the double value to be added
|
---|
109 | */
|
---|
110 | public void add(double d) {
|
---|
111 | num++;
|
---|
112 | av += d;
|
---|
113 | sav += d * d;
|
---|
114 | if (d < min) {
|
---|
115 | min = d;
|
---|
116 | indexMin = num;
|
---|
117 | }
|
---|
118 | if (d > max) {
|
---|
119 | max = d;
|
---|
120 | indexMax = num;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (doMedian)
|
---|
124 | values.add(new Double(d));
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Calculates the average of the current sequence
|
---|
129 | *
|
---|
130 | * @return the average
|
---|
131 | */
|
---|
132 | public double average() {
|
---|
133 | if (num == 0)
|
---|
134 | return 0;
|
---|
135 | else
|
---|
136 | return av / num;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Clone an <code>AvVar</code> object.
|
---|
141 | */
|
---|
142 | @Override
|
---|
143 | public Object clone() {
|
---|
144 | AvVar clone = null;
|
---|
145 | try {
|
---|
146 | clone = (AvVar) super.clone();
|
---|
147 | } catch (CloneNotSupportedException e) {
|
---|
148 | System.err.println("CloneNotSupportedException in AvVar.clone: "
|
---|
149 | + e.getMessage());
|
---|
150 | System.exit(-1);
|
---|
151 | }
|
---|
152 | return clone;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * First occurence of the current maximal value (first index)
|
---|
157 | */
|
---|
158 | public long indexMax() {
|
---|
159 | return indexMax;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * First occurence of the current minimal value (first index)
|
---|
164 | */
|
---|
165 | public long indexMin() {
|
---|
166 | return indexMin;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Initialize an already created AvVar.
|
---|
171 | */
|
---|
172 | public void init() {
|
---|
173 | num = 0;
|
---|
174 | av = 0.0;
|
---|
175 | sav = 0.0;
|
---|
176 | min = Double.MAX_VALUE;
|
---|
177 | max = -Double.MAX_VALUE; // MIN_VALUE is minimal positive val
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Determines the maximal value in the current sequence
|
---|
182 | *
|
---|
183 | * @return the maximal value
|
---|
184 | */
|
---|
185 | public double max() {
|
---|
186 | return max;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Insert the method's description here. Creation date: (4/9/03 16:41:11)
|
---|
191 | */
|
---|
192 | public double median() {
|
---|
193 | if (num == 0 || !doMedian)
|
---|
194 | return 0;
|
---|
195 |
|
---|
196 | Object[] dvalues = values.toArray();
|
---|
197 | Arrays.sort(dvalues);
|
---|
198 | if (num % 2 == 0) // even
|
---|
199 | return (((Double) dvalues[(int) (num / 2 - 1)]).doubleValue()
|
---|
200 | + ((Double) dvalues[(int) (num / 2)]).doubleValue()) / 2.0;
|
---|
201 | else // odd
|
---|
202 | return ((Double) dvalues[(int) (num / 2)]).doubleValue();
|
---|
203 |
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Determines the minimal value in the current sequence
|
---|
208 | *
|
---|
209 | * @return the minimal value
|
---|
210 | */
|
---|
211 | public double min() {
|
---|
212 | return min;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Determines the number of elements in the current sequence
|
---|
217 | *
|
---|
218 | * @return the number of elements
|
---|
219 | */
|
---|
220 | public long num() {
|
---|
221 | return num;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Calculates the standard deviation of the current sequence
|
---|
226 | *
|
---|
227 | * @return the variance
|
---|
228 | */
|
---|
229 | public double stddev() {
|
---|
230 | double var = variance();
|
---|
231 | double stddev = var;
|
---|
232 |
|
---|
233 | if ((var >= 0.0) || (checkValidity)) {
|
---|
234 | stddev = Math.sqrt(var);
|
---|
235 |
|
---|
236 | if (Double.isNaN(stddev)) {
|
---|
237 | // Assert.test(Math.abs(var) < 1e-10, " invalid variance: " + var +
|
---|
238 | // " num=" + num + " aver=" + av);
|
---|
239 | stddev = 0.0;
|
---|
240 | }
|
---|
241 | }
|
---|
242 | return stddev;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Returns a string containing the average and the standard deviation of the
|
---|
247 | * current sequence
|
---|
248 | *
|
---|
249 | * @return a string-representation
|
---|
250 | */
|
---|
251 | @Override
|
---|
252 | public String toString() {
|
---|
253 | return "Aver=" + average() + " std.=" + stddev();
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Calculates the variance of the current sequence
|
---|
258 | *
|
---|
259 | * @return the variance
|
---|
260 | */
|
---|
261 | public double variance() {
|
---|
262 | double res = 0;
|
---|
263 | if (num != 0) {
|
---|
264 | double d = av / num;
|
---|
265 | res = sav / num - d * d;
|
---|
266 | }
|
---|
267 | return res;
|
---|
268 | }
|
---|
269 | }
|
---|