source: src/main/java/weka/experiment/Stats.java @ 26

Last change on this file since 26 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 5.6 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    Stats.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.experiment;
24
25import weka.core.RevisionHandler;
26import weka.core.RevisionUtils;
27import weka.core.Utils;
28
29import java.io.Serializable;
30
31/**
32 * A class to store simple statistics
33 *
34 * @author Len Trigg (trigg@cs.waikato.ac.nz)
35 * @version $Revision: 1.12 $
36 */
37public class Stats
38  implements Serializable, RevisionHandler {
39
40  /** for serialization */
41  private static final long serialVersionUID = -8610544539090024102L;
42 
43  /** The number of values seen */
44  public double count = 0;
45
46  /** The sum of values seen */
47  public double sum = 0;
48
49  /** The sum of values squared seen */
50  public double sumSq = 0;
51
52  /** The std deviation of values at the last calculateDerived() call */   
53  public double stdDev = Double.NaN;
54
55  /** The mean of values at the last calculateDerived() call */   
56  public double mean = Double.NaN;
57
58  /** The minimum value seen, or Double.NaN if no values seen */
59  public double min = Double.NaN;
60
61  /** The maximum value seen, or Double.NaN if no values seen */
62  public double max = Double.NaN;
63   
64  /**
65   * Adds a value to the observed values
66   *
67   * @param value the observed value
68   */
69  public void add(double value) {
70
71    add(value, 1);
72  }
73
74  /**
75   * Adds a value that has been seen n times to the observed values
76   *
77   * @param value the observed value
78   * @param n the number of times to add value
79   */
80  public void add(double value, double n) {
81
82    sum += value * n;
83    sumSq += value * value * n;
84    count += n;
85    if (Double.isNaN(min)) {
86      min = max = value;
87    } else if (value < min) {
88      min = value;
89    } else if (value > max) {
90      max = value;
91    }
92  }
93
94  /**
95   * Removes a value to the observed values (no checking is done
96   * that the value being removed was actually added).
97   *
98   * @param value the observed value
99   */
100  public void subtract(double value) {
101    subtract(value, 1);
102  }
103
104  /**
105   * Subtracts a value that has been seen n times from the observed values
106   *
107   * @param value the observed value
108   * @param n the number of times to subtract value
109   */
110  public void subtract(double value, double n) {
111    sum -= value * n;
112    sumSq -= value * value * n;
113    count -= n;
114  }
115
116  /**
117   * Tells the object to calculate any statistics that don't have their
118   * values automatically updated during add. Currently updates the mean
119   * and standard deviation.
120   */
121  public void calculateDerived() {
122
123    mean = Double.NaN;
124    stdDev = Double.NaN;
125    if (count > 0) {
126      mean = sum / count;
127      stdDev = Double.POSITIVE_INFINITY;
128      if (count > 1) {
129        stdDev = sumSq - (sum * sum) / count;
130        stdDev /= (count - 1);
131        if (stdDev < 0) {
132          //          System.err.println("Warning: stdDev value = " + stdDev
133          //                             + " -- rounded to zero.");
134          stdDev = 0;
135        }
136        stdDev = Math.sqrt(stdDev);
137      }
138    }
139  }
140   
141  /**
142   * Returns a string summarising the stats so far.
143   *
144   * @return the summary string
145   */
146  public String toString() {
147
148    calculateDerived();
149    return
150      "Count   " + Utils.doubleToString(count, 8) + '\n'
151      + "Min     " + Utils.doubleToString(min, 8) + '\n'
152      + "Max     " + Utils.doubleToString(max, 8) + '\n'
153      + "Sum     " + Utils.doubleToString(sum, 8) + '\n'
154      + "SumSq   " + Utils.doubleToString(sumSq, 8) + '\n'
155      + "Mean    " + Utils.doubleToString(mean, 8) + '\n'
156      + "StdDev  " + Utils.doubleToString(stdDev, 8) + '\n';
157  }
158 
159  /**
160   * Returns the revision string.
161   *
162   * @return            the revision
163   */
164  public String getRevision() {
165    return RevisionUtils.extract("$Revision: 1.12 $");
166  }
167
168  /**
169   * Tests the paired stats object from the command line.
170   * reads line from stdin, expecting two values per line.
171   *
172   * @param args ignored.
173   */
174  public static void main(String [] args) {
175
176    try {
177      Stats ps = new Stats();
178      java.io.LineNumberReader r = new java.io.LineNumberReader(
179                                   new java.io.InputStreamReader(System.in));
180      String line;
181      while ((line = r.readLine()) != null) {
182        line = line.trim();
183        if (line.equals("") || line.startsWith("@") || line.startsWith("%")) {
184          continue;
185        }
186        java.util.StringTokenizer s
187          = new java.util.StringTokenizer(line, " ,\t\n\r\f");
188        int count = 0;
189        double v1 = 0;
190        while (s.hasMoreTokens()) {
191          double val = (new Double(s.nextToken())).doubleValue();
192          if (count == 0) {
193            v1 = val;
194          } else {
195            System.err.println("MSG: Too many values in line \"" 
196                               + line + "\", skipped.");
197            break;
198          }
199          count++;
200        }
201        if (count == 1) {
202          ps.add(v1);
203        }
204      }
205      ps.calculateDerived();
206      System.err.println(ps);
207    } catch (Exception ex) {
208      ex.printStackTrace();
209      System.err.println(ex.getMessage());
210    }
211  }
212
213} // Stats
214
Note: See TracBrowser for help on using the repository browser.