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.  *      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. import org.apache.commons.math4.core.jdkmath.JdkMath;
  19. import org.apache.commons.numbers.core.Precision;

  20. /**
  21.  *  Value object representing the results of a univariate statistical summary.
  22.  *
  23.  */
  24. public class StatisticalSummaryValues
  25.     implements StatisticalSummary {
  26.     /** The sample mean. */
  27.     private final double mean;

  28.     /** The sample variance. */
  29.     private final double variance;

  30.     /** The number of observations in the sample. */
  31.     private final long n;

  32.     /** The maximum value. */
  33.     private final double max;

  34.     /** The minimum value. */
  35.     private final double min;

  36.     /** The sum of the sample values. */
  37.     private final double sum;

  38.     /**
  39.       * Constructor.
  40.       *
  41.       * @param mean  the sample mean
  42.       * @param variance  the sample variance
  43.       * @param n  the number of observations in the sample
  44.       * @param max  the maximum value
  45.       * @param min  the minimum value
  46.       * @param sum  the sum of the values
  47.      */
  48.     public StatisticalSummaryValues(double mean, double variance, long n,
  49.         double max, double min, double sum) {
  50.         super();
  51.         this.mean = mean;
  52.         this.variance = variance;
  53.         this.n = n;
  54.         this.max = max;
  55.         this.min = min;
  56.         this.sum = sum;
  57.     }

  58.     /**
  59.      * @return Returns the max.
  60.      */
  61.     @Override
  62.     public double getMax() {
  63.         return max;
  64.     }

  65.     /**
  66.      * @return Returns the mean.
  67.      */
  68.     @Override
  69.     public double getMean() {
  70.         return mean;
  71.     }

  72.     /**
  73.      * @return Returns the min.
  74.      */
  75.     @Override
  76.     public double getMin() {
  77.         return min;
  78.     }

  79.     /**
  80.      * @return Returns the number of values.
  81.      */
  82.     @Override
  83.     public long getN() {
  84.         return n;
  85.     }

  86.     /**
  87.      * @return Returns the sum.
  88.      */
  89.     @Override
  90.     public double getSum() {
  91.         return sum;
  92.     }

  93.     /**
  94.      * @return Returns the standard deviation
  95.      */
  96.     @Override
  97.     public double getStandardDeviation() {
  98.         return JdkMath.sqrt(variance);
  99.     }

  100.     /**
  101.      * @return Returns the variance.
  102.      */
  103.     @Override
  104.     public double getVariance() {
  105.         return variance;
  106.     }

  107.     /**
  108.      * Returns true iff <code>object</code> is a
  109.      * <code>StatisticalSummaryValues</code> instance and all statistics have
  110.      *  the same values as this.
  111.      *
  112.      * @param object the object to test equality against.
  113.      * @return true if object equals this
  114.      */
  115.     @Override
  116.     public boolean equals(Object object) {
  117.         if (object == this ) {
  118.             return true;
  119.         }
  120.         if (!(object instanceof StatisticalSummaryValues)) {
  121.             return false;
  122.         }
  123.         StatisticalSummaryValues stat = (StatisticalSummaryValues) object;
  124.         return Precision.equalsIncludingNaN(stat.getMax(),      getMax())  &&
  125.                Precision.equalsIncludingNaN(stat.getMean(),     getMean()) &&
  126.                Precision.equalsIncludingNaN(stat.getMin(),      getMin())  &&
  127.                Precision.equalsIncludingNaN(stat.getN(),        getN())    &&
  128.                Precision.equalsIncludingNaN(stat.getSum(),      getSum())  &&
  129.                Precision.equalsIncludingNaN(stat.getVariance(), getVariance());
  130.     }

  131.     /**
  132.      * Returns hash code based on values of statistics.
  133.      *
  134.      * @return hash code
  135.      */
  136.     @Override
  137.     public int hashCode() {
  138.         int result = 31 + Double.hashCode(getMax());
  139.         result = result * 31 + Double.hashCode(getMean());
  140.         result = result * 31 + Double.hashCode(getMin());
  141.         result = result * 31 + Double.hashCode(getN());
  142.         result = result * 31 + Double.hashCode(getSum());
  143.         result = result * 31 + Double.hashCode(getVariance());
  144.         return result;
  145.     }

  146.     /**
  147.      * Generates a text report displaying values of statistics.
  148.      * Each statistic is displayed on a separate line.
  149.      *
  150.      * @return String with line feeds displaying statistics
  151.      */
  152.     @Override
  153.     public String toString() {
  154.         StringBuffer outBuffer = new StringBuffer();
  155.         String endl = "\n";
  156.         outBuffer.append("StatisticalSummaryValues:").append(endl);
  157.         outBuffer.append("n: ").append(getN()).append(endl);
  158.         outBuffer.append("min: ").append(getMin()).append(endl);
  159.         outBuffer.append("max: ").append(getMax()).append(endl);
  160.         outBuffer.append("mean: ").append(getMean()).append(endl);
  161.         outBuffer.append("std dev: ").append(getStandardDeviation())
  162.             .append(endl);
  163.         outBuffer.append("variance: ").append(getVariance()).append(endl);
  164.         outBuffer.append("sum: ").append(getSum()).append(endl);
  165.         return outBuffer.toString();
  166.     }
  167. }