StatisticalSummaryValues.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.descriptive;

  22. import java.io.Serializable;

  23. import org.hipparchus.util.FastMath;
  24. import org.hipparchus.util.MathUtils;
  25. import org.hipparchus.util.Precision;

  26. /**
  27.  * Value object representing the results of a univariate
  28.  * statistical summary.
  29.  */
  30. public class StatisticalSummaryValues
  31.     implements Serializable, StatisticalSummary {

  32.     /** Serialization id */
  33.     private static final long serialVersionUID = 20160406L;

  34.     /** The sample mean */
  35.     private final double mean;

  36.     /** The sample variance */
  37.     private final double variance;

  38.     /** The number of observations in the sample */
  39.     private final long n;

  40.     /** The maximum value */
  41.     private final double max;

  42.     /** The minimum value */
  43.     private final double min;

  44.     /** The sum of the sample values */
  45.     private final double sum;

  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.      * @return Returns the max.
  68.      */
  69.     @Override
  70.     public double getMax() {
  71.         return max;
  72.     }

  73.     /**
  74.      * @return Returns the mean.
  75.      */
  76.     @Override
  77.     public double getMean() {
  78.         return mean;
  79.     }

  80.     /**
  81.      * @return Returns the min.
  82.      */
  83.     @Override
  84.     public double getMin() {
  85.         return min;
  86.     }

  87.     /**
  88.      * @return Returns the number of values.
  89.      */
  90.     @Override
  91.     public long getN() {
  92.         return n;
  93.     }

  94.     /**
  95.      * @return Returns the sum.
  96.      */
  97.     @Override
  98.     public double getSum() {
  99.         return sum;
  100.     }

  101.     /**
  102.      * @return Returns the standard deviation
  103.      */
  104.     @Override
  105.     public double getStandardDeviation() {
  106.         return FastMath.sqrt(variance);
  107.     }

  108.     /**
  109.      * @return Returns the variance.
  110.      */
  111.     @Override
  112.     public double getVariance() {
  113.         return variance;
  114.     }

  115.     /**
  116.      * Returns true iff <code>object</code> is a
  117.      * <code>StatisticalSummary</code> instance and all
  118.      * statistics have the same values as this.
  119.      *
  120.      * @param object the object to test equality against.
  121.      * @return true if object equals this
  122.      */
  123.     @Override
  124.     public boolean equals(Object object) {
  125.         if (object == this) {
  126.             return true;
  127.         }
  128.         if (!(object instanceof StatisticalSummaryValues)) {
  129.             return false;
  130.         }
  131.         StatisticalSummary other = (StatisticalSummary) object;
  132.         return Precision.equalsIncludingNaN(other.getMax(),      getMax())  &&
  133.                Precision.equalsIncludingNaN(other.getMean(),     getMean()) &&
  134.                Precision.equalsIncludingNaN(other.getMin(),      getMin())  &&
  135.                Precision.equalsIncludingNaN(other.getN(),        getN())    &&
  136.                Precision.equalsIncludingNaN(other.getSum(),      getSum())  &&
  137.                Precision.equalsIncludingNaN(other.getVariance(), getVariance());
  138.     }

  139.     /**
  140.      * Returns hash code based on values of statistics
  141.      *
  142.      * @return hash code
  143.      */
  144.     @Override
  145.     public int hashCode() {
  146.         int result = 31 + MathUtils.hash(getMax());
  147.         result = result * 31 + MathUtils.hash(getMean());
  148.         result = result * 31 + MathUtils.hash(getMin());
  149.         result = result * 31 + MathUtils.hash(getN());
  150.         result = result * 31 + MathUtils.hash(getSum());
  151.         result = result * 31 + MathUtils.hash(getVariance());
  152.         return result;
  153.     }

  154.     /**
  155.      * Generates a text report displaying values of statistics.
  156.      * Each statistic is displayed on a separate line.
  157.      *
  158.      * @return String with line feeds displaying statistics
  159.      */
  160.     @Override
  161.     public String toString() {
  162.         StringBuilder outBuffer = new StringBuilder(200); // the size is just a wild guess
  163.         String endl = "\n";
  164.         outBuffer.append("StatisticalSummaryValues:").append(endl).
  165.                   append("n: ").append(getN()).append(endl).
  166.                   append("min: ").append(getMin()).append(endl).
  167.                   append("max: ").append(getMax()).append(endl).
  168.                   append("mean: ").append(getMean()).append(endl).
  169.                   append("std dev: ").append(getStandardDeviation()).append(endl).
  170.                   append("variance: ").append(getVariance()).append(endl).
  171.                   append("sum: ").append(getSum()).append(endl);
  172.         return outBuffer.toString();
  173.     }

  174. }