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   */
29  public class StatisticalSummaryValues implements Serializable,
30      StatisticalSummary {
31  
32      /** Serialization id */
33      private static final long serialVersionUID = -5108854841843722536L;
34  
35      /** The sample mean */
36      private final double mean;
37  
38      /** The sample variance */
39      private final double variance;
40  
41      /** The number of observations in the sample */
42      private final long n;
43  
44      /** The maximum value */
45      private final double max;
46  
47      /** The minimum value */
48      private final double min;
49  
50      /** The sum of the sample values */
51      private final double sum;
52  
53      /**
54        * Constructor
55        *
56        * @param mean  the sample mean
57        * @param variance  the sample variance
58        * @param n  the number of observations in the sample
59        * @param max  the maximum value
60        * @param min  the minimum value
61        * @param sum  the sum of the values
62       */
63      public StatisticalSummaryValues(double mean, double variance, long n,
64          double max, double min, double sum) {
65          super();
66          this.mean = mean;
67          this.variance = variance;
68          this.n = n;
69          this.max = max;
70          this.min = min;
71          this.sum = sum;
72      }
73  
74      /**
75       * @return Returns the max.
76       */
77      public double getMax() {
78          return max;
79      }
80  
81      /**
82       * @return Returns the mean.
83       */
84      public double getMean() {
85          return mean;
86      }
87  
88      /**
89       * @return Returns the min.
90       */
91      public double getMin() {
92          return min;
93      }
94  
95      /**
96       * @return Returns the number of values.
97       */
98      public long getN() {
99          return n;
100     }
101 
102     /**
103      * @return Returns the sum.
104      */
105     public double getSum() {
106         return sum;
107     }
108 
109     /**
110      * @return Returns the standard deviation
111      */
112     public double getStandardDeviation() {
113         return FastMath.sqrt(variance);
114     }
115 
116     /**
117      * @return Returns the variance.
118      */
119     public double getVariance() {
120         return variance;
121     }
122 
123     /**
124      * Returns true iff <code>object</code> is a
125      * <code>StatisticalSummaryValues</code> instance and all statistics have
126      *  the same values as this.
127      *
128      * @param object the object to test equality against.
129      * @return true if object equals this
130      */
131     @Override
132     public boolean equals(Object object) {
133         if (object == this ) {
134             return true;
135         }
136         if (object instanceof StatisticalSummaryValues == false) {
137             return false;
138         }
139         StatisticalSummaryValues stat = (StatisticalSummaryValues) object;
140         return Precision.equalsIncludingNaN(stat.getMax(),      getMax())  &&
141                Precision.equalsIncludingNaN(stat.getMean(),     getMean()) &&
142                Precision.equalsIncludingNaN(stat.getMin(),      getMin())  &&
143                Precision.equalsIncludingNaN(stat.getN(),        getN())    &&
144                Precision.equalsIncludingNaN(stat.getSum(),      getSum())  &&
145                Precision.equalsIncludingNaN(stat.getVariance(), getVariance());
146     }
147 
148     /**
149      * Returns hash code based on values of statistics
150      *
151      * @return hash code
152      */
153     @Override
154     public int hashCode() {
155         int result = 31 + MathUtils.hash(getMax());
156         result = result * 31 + MathUtils.hash(getMean());
157         result = result * 31 + MathUtils.hash(getMin());
158         result = result * 31 + MathUtils.hash(getN());
159         result = result * 31 + MathUtils.hash(getSum());
160         result = result * 31 + MathUtils.hash(getVariance());
161         return result;
162     }
163 
164     /**
165      * Generates a text report displaying values of statistics.
166      * Each statistic is displayed on a separate line.
167      *
168      * @return String with line feeds displaying statistics
169      */
170     @Override
171     public String toString() {
172         StringBuffer outBuffer = new StringBuffer();
173         String endl = "\n";
174         outBuffer.append("StatisticalSummaryValues:").append(endl);
175         outBuffer.append("n: ").append(getN()).append(endl);
176         outBuffer.append("min: ").append(getMin()).append(endl);
177         outBuffer.append("max: ").append(getMax()).append(endl);
178         outBuffer.append("mean: ").append(getMean()).append(endl);
179         outBuffer.append("std dev: ").append(getStandardDeviation())
180             .append(endl);
181         outBuffer.append("variance: ").append(getVariance()).append(endl);
182         outBuffer.append("sum: ").append(getSum()).append(endl);
183         return outBuffer.toString();
184     }
185 
186 }