source: trunk/gsdl/packages/kea/kea-3.0/weka/experiment/Stats.java@ 8815

Last change on this file since 8815 was 8815, checked in by mdewsnip, 19 years ago

Kea 3.0, as downloaded from http://www.nzdl.org/kea but with CSTR_abstracts_test, CSTR_abstracts_train, Chinese_test, and Chinese_train directories removed.

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