KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > stat > descriptive > SummaryStatisticsImpl


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.math.stat.descriptive;
17
18 import java.io.Serializable JavaDoc;
19 import org.apache.commons.math.stat.descriptive.moment.SecondMoment;
20 import org.apache.commons.math.stat.descriptive.moment.GeometricMean;
21 import org.apache.commons.math.stat.descriptive.moment.Mean;
22 import org.apache.commons.math.stat.descriptive.moment.Variance;
23 import org.apache.commons.math.stat.descriptive.rank.Max;
24 import org.apache.commons.math.stat.descriptive.rank.Min;
25 import org.apache.commons.math.stat.descriptive.summary.Sum;
26 import org.apache.commons.math.stat.descriptive.summary.SumOfLogs;
27 import org.apache.commons.math.stat.descriptive.summary.SumOfSquares;
28
29 /**
30  * Provides a default {@link SummaryStatistics} implementation.
31  *
32  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
33  */

34 public class SummaryStatisticsImpl extends SummaryStatistics implements Serializable JavaDoc {
35
36     /** Serializable version identifier */
37     static final long serialVersionUID = 8787174276883311692L;
38
39     /** count of values that have been added */
40     protected long n = 0;
41     
42     /** SecondMoment is used to compute the mean and variance */
43     protected SecondMoment secondMoment = null;
44     
45     /** sum of values that have been added */
46     protected Sum sum = null;
47
48     /** sum of the square of each value that has been added */
49     protected SumOfSquares sumsq = null;
50
51     /** min of values that have been added */
52     protected Min min = null;
53
54     /** max of values that have been added */
55     protected Max max = null;
56
57     /** sumLog of values that have been added */
58     protected SumOfLogs sumLog = null;
59
60     /** geoMean of values that have been added */
61     protected GeometricMean geoMean = null;
62
63     /** mean of values that have been added */
64     protected Mean mean = null;
65
66     /** variance of values that have been added */
67     protected Variance variance = null;
68
69     /**
70      * Construct a SummaryStatistics
71      */

72     public SummaryStatisticsImpl() {
73         sum = new Sum();
74         sumsq = new SumOfSquares();
75         min = new Min();
76         max = new Max();
77         sumLog = new SumOfLogs();
78         geoMean = new GeometricMean();
79         secondMoment = new SecondMoment();
80     }
81
82     /**
83      * Add a value to the data
84      *
85      * @param value the value to add
86      */

87     public void addValue(double value) {
88         sum.increment(value);
89         sumsq.increment(value);
90         min.increment(value);
91         max.increment(value);
92         sumLog.increment(value);
93         geoMean.increment(value);
94         secondMoment.increment(value);
95         n++;
96     }
97
98     /**
99      * Returns the number of available values
100      * @return The number of available values
101      */

102     public long getN() {
103         return n;
104     }
105
106     /**
107      * Returns the sum of the values that have been added to Univariate.
108      * @return The sum or Double.NaN if no values have been added
109      */

110     public double getSum() {
111         return sum.getResult();
112     }
113
114     /**
115      * Returns the sum of the squares of the values that have been added.
116      * <p>
117      * Double.NaN is returned if no values have been added.</p>
118      *
119      * @return The sum of squares
120      */

121     public double getSumsq() {
122         return sumsq.getResult();
123     }
124
125     /**
126      * Returns the mean of the values that have been added.
127      * <p>
128      * Double.NaN is returned if no values have been added.</p>
129      *
130      * @return the mean
131      */

132     public double getMean() {
133       return new Mean(secondMoment).getResult();
134     }
135
136     /**
137      * Returns the standard deviation of the values that have been added.
138      * <p>
139      * Double.NaN is returned if no values have been added.</p>
140      *
141      * @return the standard deviation
142      */

143     public double getStandardDeviation() {
144         double stdDev = Double.NaN;
145         if (getN() > 0) {
146             if (getN() > 1) {
147                 stdDev = Math.sqrt(getVariance());
148             } else {
149                 stdDev = 0.0;
150             }
151         }
152         return (stdDev);
153     }
154
155     /**
156      * Returns the variance of the values that have been added.
157      * <p>
158      * Double.NaN is returned if no values have been added.</p>
159      *
160      * @return the variance
161      */

162     public double getVariance() {
163         return new Variance(secondMoment).getResult();
164     }
165
166     /**
167      * Returns the maximum of the values that have been added.
168      * <p>
169      * Double.NaN is returned if no values have been added.</p>
170      *
171      * @return the maximum
172      */

173     public double getMax() {
174         return max.getResult();
175     }
176
177     /**
178      * Returns the minimum of the values that have been added.
179      * <p>
180      * Double.NaN is returned if no values have been added.</p>
181      *
182      * @return the minimum
183      */

184     public double getMin() {
185         return min.getResult();
186     }
187
188     /**
189      * Returns the geometric mean of the values that have been added.
190      * <p>
191      * Double.NaN is returned if no values have been added.</p>
192      *
193      * @return the geometric mean
194      */

195     public double getGeometricMean() {
196         return geoMean.getResult();
197     }
198     
199     /**
200      * Generates a text report displaying
201      * summary statistics from values that
202      * have been added.
203      * @return String with line feeds displaying statistics
204      */

205     public String JavaDoc toString() {
206         StringBuffer JavaDoc outBuffer = new StringBuffer JavaDoc();
207         outBuffer.append("SummaryStatistics:\n");
208         outBuffer.append("n: " + getN() + "\n");
209         outBuffer.append("min: " + getMin() + "\n");
210         outBuffer.append("max: " + getMax() + "\n");
211         outBuffer.append("mean: " + getMean() + "\n");
212         outBuffer.append("geometric mean: " + getGeometricMean() + "\n");
213         outBuffer.append("variance: " + getVariance() + "\n");
214         outBuffer.append("sum of squares: " + getSumsq() + "\n");
215         outBuffer.append("standard deviation: " + getStandardDeviation() + "\n");
216         return outBuffer.toString();
217     }
218
219     /**
220      * Resets all statistics and storage
221      */

222     public void clear() {
223         this.n = 0;
224         min.clear();
225         max.clear();
226         sum.clear();
227         sumLog.clear();
228         sumsq.clear();
229         geoMean.clear();
230         secondMoment.clear();
231     }
232
233 }
Popular Tags