View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math3.stat.descriptive;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.math3.util.FastMath;
22  import org.apache.commons.math3.util.MathUtils;
23  import org.apache.commons.math3.util.Precision;
24  
25  /**
26   *  Value object representing the results of a univariate statistical summary.
27   *
28   * @version $Id: StatisticalSummaryValues.java 1416643 2012-12-03 19:37:14Z tn $
29   */
30  public class StatisticalSummaryValues implements Serializable,
31      StatisticalSummary {
32  
33      /** Serialization id */
34      private static final long serialVersionUID = -5108854841843722536L;
35  
36      /** The sample mean */
37      private final double mean;
38  
39      /** The sample variance */
40      private final double variance;
41  
42      /** The number of observations in the sample */
43      private final long n;
44  
45      /** The maximum value */
46      private final double max;
47  
48      /** The minimum value */
49      private final double min;
50  
51      /** The sum of the sample values */
52      private final double sum;
53  
54      /**
55        * Constructor
56        *
57        * @param mean  the sample mean
58        * @param variance  the sample variance
59        * @param n  the number of observations in the sample
60        * @param max  the maximum value
61        * @param min  the minimum value
62        * @param sum  the sum of the values
63       */
64      public StatisticalSummaryValues(double mean, double variance, long n,
65          double max, double min, double sum) {
66          super();
67          this.mean = mean;
68          this.variance = variance;
69          this.n = n;
70          this.max = max;
71          this.min = min;
72          this.sum = sum;
73      }
74  
75      /**
76       * @return Returns the max.
77       */
78      public double getMax() {
79          return max;
80      }
81  
82      /**
83       * @return Returns the mean.
84       */
85      public double getMean() {
86          return mean;
87      }
88  
89      /**
90       * @return Returns the min.
91       */
92      public double getMin() {
93          return min;
94      }
95  
96      /**
97       * @return Returns the number of values.
98       */
99      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 }