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