2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * A class that can keep track of some statistics like variance, average, min,
23 * max incrementally. That is, when adding a new data item, it updates the
26 public class IncrementalStats {
29 // ===================== fields ========================================
30 // =====================================================================
39 private double sqrsum;
47 // ====================== initialization ==============================
48 // ====================================================================
51 /** Calls {@link #reset}. */
52 public IncrementalStats() { reset(); }
54 // --------------------------------------------------------------------
56 /** Resets the statistics to reflect the zero elements set.
57 * Min and max are set to positive and negative infinity, respectively.
63 min = Double.POSITIVE_INFINITY;
64 max = Double.NEGATIVE_INFINITY;
71 // ======================== methods ===================================
72 // ====================================================================
75 /** Updates the statistics according to this element. It calls
76 * <code>add(item,1)</code>.
77 * @see #add(double,int) */
78 public final void add( double item ) { add(item,1); }
80 // --------------------------------------------------------------------
82 /** Updates the statistics assuming element <code>item</code> is added
83 * <code>k</code> times.*/
84 public void add( double item, int k ) {
91 if( item == min ) countmin+=k;
97 if(item == max) countmax+=k;
107 sqrsum += item*item*k;
111 // --------------------------------------------------------------------
113 /** The number of data items processed so far */
114 public int getN() { return n; }
116 // --------------------------------------------------------------------
118 /** The maximum of the data items */
119 public double getMax() { return max; }
121 // --------------------------------------------------------------------
123 /** The minimum of the data items */
124 public double getMin() { return min; }
126 // --------------------------------------------------------------------
128 /** Returns the number of data items whose value equals the maximum. */
129 public int getMaxCount() { return countmax; }
131 // --------------------------------------------------------------------
133 /** Returns the number of data items whose value equals the minimum. */
134 public int getMinCount() { return countmin; }
136 // --------------------------------------------------------------------
138 /** The sum of the data items */
139 public double getSum() { return sum; }
141 // --------------------------------------------------------------------
143 /** The sum of the squares of the data items */
144 public double getSqrSum() { return sqrsum; }
146 // --------------------------------------------------------------------
148 /** The average of the data items */
149 public double getAverage() { return sum/n; }
151 // --------------------------------------------------------------------
153 /** The empirical variance of the data items. Guaranteed to be larger or
154 equal to 0.0. If due to rounding errors the value becomes negative,
156 public double getVar() {
159 (((double)n) / (n-1)) * (sqrsum/n - getAverage()*getAverage());
160 return (var>=0.0?var:0.0);
161 // XXX note that we have very little possibility to increase numeric
162 // stability if this class is "greedy", ie, if it has no memory
163 // In a more precise implementation we could delay the calculation of
164 // statistics and store the data in some intelligent structure
167 // --------------------------------------------------------------------
169 /** the empirical standard deviation of the data items */
170 public double getStD() { return Math.sqrt(getVar()); }
172 // --------------------------------------------------------------------
175 * Prints the following quantities separated by spaces in a single line
177 * Minimum, maximum, number of items, average, variance, number of minimal
178 * items, number of maximal items.
180 public String toString() {
182 return min+" "+max+" "+n+" "+sum/n+" "+getVar()+" "+
183 countmin+" "+countmax;