001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math3.stat.descriptive;
018
019 import java.io.Serializable;
020
021 import org.apache.commons.math3.util.FastMath;
022 import org.apache.commons.math3.util.MathUtils;
023 import org.apache.commons.math3.util.Precision;
024
025 /**
026 * Value object representing the results of a univariate statistical summary.
027 *
028 * @version $Id: StatisticalSummaryValues.java 1416643 2012-12-03 19:37:14Z tn $
029 */
030 public class StatisticalSummaryValues implements Serializable,
031 StatisticalSummary {
032
033 /** Serialization id */
034 private static final long serialVersionUID = -5108854841843722536L;
035
036 /** The sample mean */
037 private final double mean;
038
039 /** The sample variance */
040 private final double variance;
041
042 /** The number of observations in the sample */
043 private final long n;
044
045 /** The maximum value */
046 private final double max;
047
048 /** The minimum value */
049 private final double min;
050
051 /** The sum of the sample values */
052 private final double sum;
053
054 /**
055 * Constructor
056 *
057 * @param mean the sample mean
058 * @param variance the sample variance
059 * @param n the number of observations in the sample
060 * @param max the maximum value
061 * @param min the minimum value
062 * @param sum the sum of the values
063 */
064 public StatisticalSummaryValues(double mean, double variance, long n,
065 double max, double min, double sum) {
066 super();
067 this.mean = mean;
068 this.variance = variance;
069 this.n = n;
070 this.max = max;
071 this.min = min;
072 this.sum = sum;
073 }
074
075 /**
076 * @return Returns the max.
077 */
078 public double getMax() {
079 return max;
080 }
081
082 /**
083 * @return Returns the mean.
084 */
085 public double getMean() {
086 return mean;
087 }
088
089 /**
090 * @return Returns the min.
091 */
092 public double getMin() {
093 return min;
094 }
095
096 /**
097 * @return Returns the number of values.
098 */
099 public long getN() {
100 return n;
101 }
102
103 /**
104 * @return Returns the sum.
105 */
106 public double getSum() {
107 return sum;
108 }
109
110 /**
111 * @return Returns the standard deviation
112 */
113 public double getStandardDeviation() {
114 return FastMath.sqrt(variance);
115 }
116
117 /**
118 * @return Returns the variance.
119 */
120 public double getVariance() {
121 return variance;
122 }
123
124 /**
125 * Returns true iff <code>object</code> is a
126 * <code>StatisticalSummaryValues</code> instance and all statistics have
127 * the same values as this.
128 *
129 * @param object the object to test equality against.
130 * @return true if object equals this
131 */
132 @Override
133 public boolean equals(Object object) {
134 if (object == this ) {
135 return true;
136 }
137 if (object instanceof StatisticalSummaryValues == false) {
138 return false;
139 }
140 StatisticalSummaryValues stat = (StatisticalSummaryValues) object;
141 return Precision.equalsIncludingNaN(stat.getMax(), getMax()) &&
142 Precision.equalsIncludingNaN(stat.getMean(), getMean()) &&
143 Precision.equalsIncludingNaN(stat.getMin(), getMin()) &&
144 Precision.equalsIncludingNaN(stat.getN(), getN()) &&
145 Precision.equalsIncludingNaN(stat.getSum(), getSum()) &&
146 Precision.equalsIncludingNaN(stat.getVariance(), getVariance());
147 }
148
149 /**
150 * Returns hash code based on values of statistics
151 *
152 * @return hash code
153 */
154 @Override
155 public int hashCode() {
156 int result = 31 + MathUtils.hash(getMax());
157 result = result * 31 + MathUtils.hash(getMean());
158 result = result * 31 + MathUtils.hash(getMin());
159 result = result * 31 + MathUtils.hash(getN());
160 result = result * 31 + MathUtils.hash(getSum());
161 result = result * 31 + MathUtils.hash(getVariance());
162 return result;
163 }
164
165 /**
166 * Generates a text report displaying values of statistics.
167 * Each statistic is displayed on a separate line.
168 *
169 * @return String with line feeds displaying statistics
170 */
171 @Override
172 public String toString() {
173 StringBuffer outBuffer = new StringBuffer();
174 String endl = "\n";
175 outBuffer.append("StatisticalSummaryValues:").append(endl);
176 outBuffer.append("n: ").append(getN()).append(endl);
177 outBuffer.append("min: ").append(getMin()).append(endl);
178 outBuffer.append("max: ").append(getMax()).append(endl);
179 outBuffer.append("mean: ").append(getMean()).append(endl);
180 outBuffer.append("std dev: ").append(getStandardDeviation())
181 .append(endl);
182 outBuffer.append("variance: ").append(getVariance()).append(endl);
183 outBuffer.append("sum: ").append(getSum()).append(endl);
184 return outBuffer.toString();
185 }
186
187 }