Stats.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 5k
Category:

Windows Develop

Development Platform:

Java

  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.  *    Stats.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.experiment;
  22. import weka.core.Utils;
  23. /**
  24.  * A class to store simple statistics
  25.  *
  26.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  27.  * @version $Revision: 1.7.2.1 $
  28.  */
  29. public class Stats {
  30.   
  31.   /** The number of values seen */
  32.   public double count = 0;
  33.   /** The sum of values seen */
  34.   public double sum = 0;
  35.   /** The sum of values squared seen */
  36.   public double sumSq = 0;
  37.   /** The std deviation of values at the last calculateDerived() call */    
  38.   public double stdDev = Double.NaN;
  39.   /** The mean of values at the last calculateDerived() call */    
  40.   public double mean = Double.NaN;
  41.   /** The minimum value seen, or Double.NaN if no values seen */
  42.   public double min = Double.NaN;
  43.   /** The maximum value seen, or Double.NaN if no values seen */
  44.   public double max = Double.NaN;
  45.     
  46.   /**
  47.    * Adds a value to the observed values
  48.    *
  49.    * @param value the observed value
  50.    */
  51.   public void add(double value) {
  52.     add(value, 1);
  53.   }
  54.   /**
  55.    * Adds a value that has been seen n times to the observed values
  56.    *
  57.    * @param value the observed value
  58.    * @param n the number of times to add value
  59.    */
  60.   public void add(double value, double n) {
  61.     sum += value * n;
  62.     sumSq += value * value * n;
  63.     count += n;
  64.     if (Double.isNaN(min)) {
  65.       min = max = value;
  66.     } else if (value < min) {
  67.       min = value;
  68.     } else if (value > max) {
  69.       max = value;
  70.     }
  71.   }
  72.   /**
  73.    * Removes a value to the observed values (no checking is done
  74.    * that the value being removed was actually added). 
  75.    *
  76.    * @param value the observed value
  77.    */
  78.   public void subtract(double value) {
  79.     subtract(value, 1);
  80.   }
  81.   /**
  82.    * Subtracts a value that has been seen n times from the observed values
  83.    *
  84.    * @param value the observed value
  85.    * @param n the number of times to subtract value
  86.    */
  87.   public void subtract(double value, double n) {
  88.     sum -= value * n;
  89.     sumSq -= value * value * n;
  90.     count -= n;
  91.   }
  92.   /**
  93.    * Tells the object to calculate any statistics that don't have their
  94.    * values automatically updated during add. Currently updates the mean
  95.    * and standard deviation.
  96.    */
  97.   public void calculateDerived() {
  98.     mean = Double.NaN;
  99.     stdDev = Double.NaN;
  100.     if (count > 0) {
  101.       mean = sum / count;
  102.       stdDev = Double.POSITIVE_INFINITY;
  103.       if (count > 1) {
  104. stdDev = sumSq - (sum * sum) / count;
  105. stdDev /= (count - 1);
  106.         if (stdDev < 0) {
  107.           System.err.println("Warning: stdDev value = " + stdDev 
  108.                              + " -- rounded to zero.");
  109.           stdDev = 0;
  110.         }
  111. stdDev = Math.sqrt(stdDev);
  112.       }
  113.     }
  114.   }
  115.     
  116.   /**
  117.    * Returns a string summarising the stats so far.
  118.    *
  119.    * @return the summary string
  120.    */
  121.   public String toString() {
  122.     calculateDerived();
  123.     return
  124.       "Count   " + Utils.doubleToString(count, 8) + 'n'
  125.       + "Min     " + Utils.doubleToString(min, 8) + 'n'
  126.       + "Max     " + Utils.doubleToString(max, 8) + 'n'
  127.       + "Sum     " + Utils.doubleToString(sum, 8) + 'n'
  128.       + "SumSq   " + Utils.doubleToString(sumSq, 8) + 'n'
  129.       + "Mean    " + Utils.doubleToString(mean, 8) + 'n'
  130.       + "StdDev  " + Utils.doubleToString(stdDev, 8) + 'n';
  131.   }
  132.   /**
  133.    * Tests the paired stats object from the command line.
  134.    * reads line from stdin, expecting two values per line.
  135.    *
  136.    * @param args ignored.
  137.    */
  138.   public static void main(String [] args) {
  139.     try {
  140.       Stats ps = new Stats();
  141.       java.io.LineNumberReader r = new java.io.LineNumberReader(
  142.    new java.io.InputStreamReader(System.in));
  143.       String line;
  144.       while ((line = r.readLine()) != null) {
  145.         line = line.trim();
  146.         if (line.equals("") || line.startsWith("@") || line.startsWith("%")) {
  147.           continue;
  148.         }
  149. java.util.StringTokenizer s 
  150.           = new java.util.StringTokenizer(line, " ,tnrf");
  151. int count = 0;
  152. double v1 = 0;
  153. while (s.hasMoreTokens()) {
  154.   double val = (new Double(s.nextToken())).doubleValue();
  155.   if (count == 0) {
  156.     v1 = val;
  157.   } else {
  158.             System.err.println("MSG: Too many values in line "" 
  159.                                + line + "", skipped.");
  160.     break;
  161.   }
  162.   count++;
  163. }
  164.         if (count == 1) {
  165.           ps.add(v1);
  166.         }
  167.       }
  168.       ps.calculateDerived();
  169.       System.err.println(ps);
  170.     } catch (Exception ex) {
  171.       ex.printStackTrace();
  172.       System.err.println(ex.getMessage());
  173.     }
  174.   }
  175. } // Stats