001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat.descriptive;
017:
018: import java.io.Serializable;
019: import org.apache.commons.math.stat.descriptive.moment.SecondMoment;
020: import org.apache.commons.math.stat.descriptive.moment.GeometricMean;
021: import org.apache.commons.math.stat.descriptive.moment.Mean;
022: import org.apache.commons.math.stat.descriptive.moment.Variance;
023: import org.apache.commons.math.stat.descriptive.rank.Max;
024: import org.apache.commons.math.stat.descriptive.rank.Min;
025: import org.apache.commons.math.stat.descriptive.summary.Sum;
026: import org.apache.commons.math.stat.descriptive.summary.SumOfLogs;
027: import org.apache.commons.math.stat.descriptive.summary.SumOfSquares;
028:
029: /**
030: * Provides a default {@link SummaryStatistics} implementation.
031: *
032: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
033: */
034: public class SummaryStatisticsImpl extends SummaryStatistics implements
035: Serializable {
036:
037: /** Serializable version identifier */
038: private static final long serialVersionUID = 8787174276883311692L;
039:
040: /** count of values that have been added */
041: protected long n = 0;
042:
043: /** SecondMoment is used to compute the mean and variance */
044: protected SecondMoment secondMoment = null;
045:
046: /** sum of values that have been added */
047: protected Sum sum = null;
048:
049: /** sum of the square of each value that has been added */
050: protected SumOfSquares sumsq = null;
051:
052: /** min of values that have been added */
053: protected Min min = null;
054:
055: /** max of values that have been added */
056: protected Max max = null;
057:
058: /** sumLog of values that have been added */
059: protected SumOfLogs sumLog = null;
060:
061: /** geoMean of values that have been added */
062: protected GeometricMean geoMean = null;
063:
064: /** mean of values that have been added */
065: protected Mean mean = null;
066:
067: /** variance of values that have been added */
068: protected Variance variance = null;
069:
070: /**
071: * Construct a SummaryStatistics
072: */
073: public SummaryStatisticsImpl() {
074: sum = new Sum();
075: sumsq = new SumOfSquares();
076: min = new Min();
077: max = new Max();
078: sumLog = new SumOfLogs();
079: geoMean = new GeometricMean();
080: secondMoment = new SecondMoment();
081: }
082:
083: /**
084: * Add a value to the data
085: *
086: * @param value the value to add
087: */
088: public void addValue(double value) {
089: sum.increment(value);
090: sumsq.increment(value);
091: min.increment(value);
092: max.increment(value);
093: sumLog.increment(value);
094: geoMean.increment(value);
095: secondMoment.increment(value);
096: n++;
097: }
098:
099: /**
100: * Returns the number of available values
101: * @return The number of available values
102: */
103: public long getN() {
104: return n;
105: }
106:
107: /**
108: * Returns the sum of the values that have been added to Univariate.
109: * @return The sum or Double.NaN if no values have been added
110: */
111: public double getSum() {
112: return sum.getResult();
113: }
114:
115: /**
116: * Returns the sum of the squares of the values that have been added.
117: * <p>
118: * Double.NaN is returned if no values have been added.</p>
119: *
120: * @return The sum of squares
121: */
122: public double getSumsq() {
123: return sumsq.getResult();
124: }
125:
126: /**
127: * Returns the mean of the values that have been added.
128: * <p>
129: * Double.NaN is returned if no values have been added.</p>
130: *
131: * @return the mean
132: */
133: public double getMean() {
134: return new Mean(secondMoment).getResult();
135: }
136:
137: /**
138: * Returns the standard deviation of the values that have been added.
139: * <p>
140: * Double.NaN is returned if no values have been added.</p>
141: *
142: * @return the standard deviation
143: */
144: public double getStandardDeviation() {
145: double stdDev = Double.NaN;
146: if (getN() > 0) {
147: if (getN() > 1) {
148: stdDev = Math.sqrt(getVariance());
149: } else {
150: stdDev = 0.0;
151: }
152: }
153: return (stdDev);
154: }
155:
156: /**
157: * Returns the variance of the values that have been added.
158: * <p>
159: * Double.NaN is returned if no values have been added.</p>
160: *
161: * @return the variance
162: */
163: public double getVariance() {
164: return new Variance(secondMoment).getResult();
165: }
166:
167: /**
168: * Returns the maximum of the values that have been added.
169: * <p>
170: * Double.NaN is returned if no values have been added.</p>
171: *
172: * @return the maximum
173: */
174: public double getMax() {
175: return max.getResult();
176: }
177:
178: /**
179: * Returns the minimum of the values that have been added.
180: * <p>
181: * Double.NaN is returned if no values have been added.</p>
182: *
183: * @return the minimum
184: */
185: public double getMin() {
186: return min.getResult();
187: }
188:
189: /**
190: * Returns the geometric mean of the values that have been added.
191: * <p>
192: * Double.NaN is returned if no values have been added.</p>
193: *
194: * @return the geometric mean
195: */
196: public double getGeometricMean() {
197: return geoMean.getResult();
198: }
199:
200: /**
201: * Generates a text report displaying
202: * summary statistics from values that
203: * have been added.
204: * @return String with line feeds displaying statistics
205: */
206: public String toString() {
207: StringBuffer outBuffer = new StringBuffer();
208: outBuffer.append("SummaryStatistics:\n");
209: outBuffer.append("n: " + getN() + "\n");
210: outBuffer.append("min: " + getMin() + "\n");
211: outBuffer.append("max: " + getMax() + "\n");
212: outBuffer.append("mean: " + getMean() + "\n");
213: outBuffer
214: .append("geometric mean: " + getGeometricMean() + "\n");
215: outBuffer.append("variance: " + getVariance() + "\n");
216: outBuffer.append("sum of squares: " + getSumsq() + "\n");
217: outBuffer.append("standard deviation: "
218: + getStandardDeviation() + "\n");
219: return outBuffer.toString();
220: }
221:
222: /**
223: * Resets all statistics and storage
224: */
225: public void clear() {
226: this .n = 0;
227: min.clear();
228: max.clear();
229: sum.clear();
230: sumLog.clear();
231: sumsq.clear();
232: geoMean.clear();
233: secondMoment.clear();
234: }
235:
236: }
|