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:
020: import org.apache.commons.discovery.tools.DiscoverClass;
021: import org.apache.commons.math.util.MathUtils;
022:
023: /**
024: * Abstract factory class for univariate statistical summaries.
025: *
026: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
027: */
028: public abstract class SummaryStatistics implements StatisticalSummary,
029: Serializable {
030:
031: /** Serialization UID */
032: private static final long serialVersionUID = -6400596334135654825L;
033:
034: /**
035: * Create an instance of a <code>SummaryStatistics</code>
036: *
037: * @param cls the type of <code>SummaryStatistics</code> object to
038: * create.
039: * @return a new factory.
040: * @throws InstantiationException is thrown if the object can not be
041: * created.
042: * @throws IllegalAccessException is thrown if the type's default
043: * constructor is not accessible.
044: */
045: public static SummaryStatistics newInstance(Class cls)
046: throws InstantiationException, IllegalAccessException {
047: return (SummaryStatistics) cls.newInstance();
048: }
049:
050: /**
051: * Create an instance of a <code>SummaryStatistics</code>
052: *
053: * @return a new SummaryStatistics instance.
054: */
055: public static SummaryStatistics newInstance() {
056: SummaryStatistics instance = null;
057: try {
058: DiscoverClass dc = new DiscoverClass();
059: instance = (SummaryStatistics) dc
060: .newInstance(SummaryStatistics.class,
061: "org.apache.commons.math.stat.descriptive.SummaryStatisticsImpl");
062: } catch (Throwable t) {
063: return new SummaryStatisticsImpl();
064: }
065: return instance;
066: }
067:
068: /**
069: * Return a {@link StatisticalSummaryValues} instance reporting current
070: * statistics.
071: *
072: * @return Current values of statistics
073: */
074: public StatisticalSummary getSummary() {
075: return new StatisticalSummaryValues(getMean(), getVariance(),
076: getN(), getMax(), getMin(), getSum());
077: }
078:
079: /**
080: * Adds the value to the data to be summarized
081: * @param v the value to be added
082: */
083: public abstract void addValue(double v);
084:
085: /**
086: * Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm">
087: * arithmetic mean </a> of the available values
088: * @return The mean or Double.NaN if no values have been added.
089: */
090: public abstract double getMean();
091:
092: /**
093: * Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
094: * geometric mean </a> of the available values
095: * @return The geometricMean, Double.NaN if no values have been added,
096: * or if the productof the available values is less than or equal to 0.
097: */
098: public abstract double getGeometricMean();
099:
100: /**
101: * Returns the variance of the available values.
102: * @return The variance, Double.NaN if no values have been added
103: * or 0.0 for a single value set.
104: */
105: public abstract double getVariance();
106:
107: /**
108: * Returns the standard deviation of the available values.
109: * @return The standard deviation, Double.NaN if no values have been added
110: * or 0.0 for a single value set.
111: */
112: public abstract double getStandardDeviation();
113:
114: /**
115: * Returns the maximum of the available values
116: * @return The max or Double.NaN if no values have been added.
117: */
118: public abstract double getMax();
119:
120: /**
121: * Returns the minimum of the available values
122: * @return The min or Double.NaN if no values have been added.
123: */
124: public abstract double getMin();
125:
126: /**
127: * Returns the number of available values
128: * @return The number of available values
129: */
130: public abstract long getN();
131:
132: /**
133: * Returns the sum of the values that have been added to Univariate.
134: * @return The sum or Double.NaN if no values have been added
135: */
136: public abstract double getSum();
137:
138: /**
139: * Returns the sum of the squares of the available values.
140: * @return The sum of the squares or Double.NaN if no
141: * values have been added.
142: */
143: public abstract double getSumsq();
144:
145: /**
146: * Resets all statistics
147: */
148: public abstract void clear();
149:
150: /**
151: * Returns true iff <code>object</code> is a <code>SummaryStatistics</code>
152: * instance and all statistics have the same values as this.
153: * @param object the object to test equality against.
154: * @return true if object equals this
155: */
156: public boolean equals(Object object) {
157: if (object == this ) {
158: return true;
159: }
160: if (object instanceof SummaryStatistics == false) {
161: return false;
162: }
163: SummaryStatistics stat = (SummaryStatistics) object;
164: return (MathUtils.equals(stat.getGeometricMean(), this
165: .getGeometricMean())
166: && MathUtils.equals(stat.getMax(), this .getMax())
167: && MathUtils.equals(stat.getMean(), this .getMean())
168: && MathUtils.equals(stat.getMin(), this .getMin())
169: && MathUtils.equals(stat.getN(), this .getN())
170: && MathUtils.equals(stat.getSum(), this .getSum())
171: && MathUtils.equals(stat.getSumsq(), this .getSumsq()) && MathUtils
172: .equals(stat.getVariance(), this .getVariance()));
173: }
174:
175: /**
176: * Returns hash code based on values of statistics
177: *
178: * @return hash code
179: */
180: public int hashCode() {
181: int result = 31 + MathUtils.hash(getGeometricMean());
182: result = result * 31 + MathUtils.hash(getGeometricMean());
183: result = result * 31 + MathUtils.hash(getMax());
184: result = result * 31 + MathUtils.hash(getMean());
185: result = result * 31 + MathUtils.hash(getMin());
186: result = result * 31 + MathUtils.hash(getN());
187: result = result * 31 + MathUtils.hash(getSum());
188: result = result * 31 + MathUtils.hash(getSumsq());
189: result = result * 31 + MathUtils.hash(getVariance());
190: return result;
191: }
192:
193: }
|