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.math4.legacy.stat.descriptive;
18  
19  import org.apache.commons.math4.core.jdkmath.JdkMath;
20  import org.apache.commons.numbers.core.Precision;
21  
22  /**
23   *  Value object representing the results of a univariate statistical summary.
24   *
25   */
26  public class StatisticalSummaryValues
27      implements StatisticalSummary {
28      /** The sample mean. */
29      private final double mean;
30  
31      /** The sample variance. */
32      private final double variance;
33  
34      /** The number of observations in the sample. */
35      private final long n;
36  
37      /** The maximum value. */
38      private final double max;
39  
40      /** The minimum value. */
41      private final double min;
42  
43      /** The sum of the sample values. */
44      private final double sum;
45  
46      /**
47        * Constructor.
48        *
49        * @param mean  the sample mean
50        * @param variance  the sample variance
51        * @param n  the number of observations in the sample
52        * @param max  the maximum value
53        * @param min  the minimum value
54        * @param sum  the sum of the values
55       */
56      public StatisticalSummaryValues(double mean, double variance, long n,
57          double max, double min, double sum) {
58          super();
59          this.mean = mean;
60          this.variance = variance;
61          this.n = n;
62          this.max = max;
63          this.min = min;
64          this.sum = sum;
65      }
66  
67      /**
68       * @return Returns the max.
69       */
70      @Override
71      public double getMax() {
72          return max;
73      }
74  
75      /**
76       * @return Returns the mean.
77       */
78      @Override
79      public double getMean() {
80          return mean;
81      }
82  
83      /**
84       * @return Returns the min.
85       */
86      @Override
87      public double getMin() {
88          return min;
89      }
90  
91      /**
92       * @return Returns the number of values.
93       */
94      @Override
95      public long getN() {
96          return n;
97      }
98  
99      /**
100      * @return Returns the sum.
101      */
102     @Override
103     public double getSum() {
104         return sum;
105     }
106 
107     /**
108      * @return Returns the standard deviation
109      */
110     @Override
111     public double getStandardDeviation() {
112         return JdkMath.sqrt(variance);
113     }
114 
115     /**
116      * @return Returns the variance.
117      */
118     @Override
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)) {
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 + Double.hashCode(getMax());
156         result = result * 31 + Double.hashCode(getMean());
157         result = result * 31 + Double.hashCode(getMin());
158         result = result * 31 + Double.hashCode(getN());
159         result = result * 31 + Double.hashCode(getSum());
160         result = result * 31 + Double.hashCode(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 }