StatisticalSummary.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.util.Arrays;

  23. import org.hipparchus.util.MathUtils;

  24. /**
  25.  * Reporting interface for basic univariate statistics.
  26.  */
  27. public interface StatisticalSummary {

  28.     /**
  29.      * Computes aggregated statistical summaries.
  30.      * <p>
  31.      * This method can be used to combine statistics computed over partitions or
  32.      * subsamples - i.e., the returned StatisticalSummary should contain
  33.      * the same values that would have been obtained by computing a single
  34.      * StatisticalSummary over the combined dataset.
  35.      *
  36.      * @param statistics StatisticalSummary instances to aggregate
  37.      * @return summary statistics for the combined dataset
  38.      * @throws org.hipparchus.exception.NullArgumentException if the input is null
  39.      */
  40.     static StatisticalSummary aggregate(StatisticalSummary... statistics) {
  41.         MathUtils.checkNotNull(statistics);
  42.         return aggregate(Arrays.asList(statistics));
  43.     }

  44.     /**
  45.      * Computes aggregated statistical summaries.
  46.      * <p>
  47.      * This method can be used to combine statistics computed over partitions or
  48.      * subsamples - i.e., the returned StatisticalSummary should contain
  49.      * the same values that would have been obtained by computing a single
  50.      * StatisticalSummary over the combined dataset.
  51.      *
  52.      * @param statistics iterable of StatisticalSummary instances to aggregate
  53.      * @return summary statistics for the combined dataset
  54.      * @throws org.hipparchus.exception.NullArgumentException if the input is null
  55.      */
  56.     static StatisticalSummary aggregate(Iterable<? extends StatisticalSummary> statistics) {
  57.         MathUtils.checkNotNull(statistics);

  58.         long n = 0;
  59.         double min = Double.NaN;
  60.         double max = Double.NaN;
  61.         double sum = Double.NaN;
  62.         double mean = Double.NaN;
  63.         double m2 = Double.NaN;

  64.         for (StatisticalSummary current : statistics) {
  65.             if (current.getN() == 0) {
  66.                 continue;
  67.             }

  68.             if (n == 0) {
  69.                 n = current.getN();
  70.                 min = current.getMin();
  71.                 sum = current.getSum();
  72.                 max = current.getMax();
  73.                 m2 = current.getVariance() * (n - 1);
  74.                 mean = current.getMean();
  75.             } else {
  76.                 if (current.getMin() < min) {
  77.                     min = current.getMin();
  78.                 }
  79.                 if (current.getMax() > max) {
  80.                     max = current.getMax();
  81.                 }

  82.                 sum += current.getSum();
  83.                 final double oldN = n;
  84.                 final double curN = current.getN();
  85.                 n += curN;
  86.                 final double meanDiff = current.getMean() - mean;
  87.                 mean = sum / n;
  88.                 final double curM2 = current.getVariance() * (curN - 1d);
  89.                 m2 = m2 + curM2 + meanDiff * meanDiff * oldN * curN / n;
  90.             }
  91.         }

  92.         final double variance = n == 0 ? Double.NaN :
  93.                                 n == 1 ? 0d         :
  94.                                          m2 / (n - 1);

  95.         return new StatisticalSummaryValues(mean, variance, n, max, min, sum);
  96.     }

  97.     /**
  98.      * Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm">
  99.      * arithmetic mean </a> of the available values
  100.      * @return The mean or Double.NaN if no values have been added.
  101.      */
  102.     double getMean();

  103.     /**
  104.      * Returns the variance of the available values.
  105.      * @return The variance, Double.NaN if no values have been added
  106.      * or 0.0 for a single value set.
  107.      */
  108.     double getVariance();

  109.     /**
  110.      * Returns the standard deviation of the available values.
  111.      * @return The standard deviation, Double.NaN if no values have been added
  112.      * or 0.0 for a single value set.
  113.      */
  114.     double getStandardDeviation();

  115.     /**
  116.      * Returns the maximum of the available values
  117.      * @return The max or Double.NaN if no values have been added.
  118.      */
  119.     double getMax();

  120.     /**
  121.     * Returns the minimum of the available values
  122.     * @return The min or Double.NaN if no values have been added.
  123.     */
  124.     double getMin();

  125.     /**
  126.      * Returns the number of available values
  127.      * @return The number of available values
  128.      */
  129.     long getN();

  130.     /**
  131.      * Returns the sum of the values that have been added to Univariate.
  132.      * @return The sum or Double.NaN if no values have been added
  133.      */
  134.     double getSum();

  135. }