GeometricMean.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.statistics.descriptive;

  18. /**
  19.  * Computes the geometric mean of the available values. Uses the following definition
  20.  * of the geometric mean:
  21.  *
  22.  * <p>\[ \left(\prod_{i=1}^n x_i\right)^\frac{1}{n} \]
  23.  *
  24.  * <p>where \( n \) is the number of samples. This implementation uses the log scale:
  25.  *
  26.  * <p>\[ \exp{\left( {\frac{1}{n}\sum_{i=1}^n \ln x_i} \right)} \]
  27.  *
  28.  * <ul>
  29.  *   <li>The result is {@code NaN} if no values are added.
  30.  *   <li>The result is {@code NaN} if any of the values is {@code NaN}.
  31.  *   <li>The result is {@code NaN} if any of the values is negative.
  32.  *   <li>The result is {@code +infinity} if all values are in the range {@code (0, +infinity]}
  33.  *       and at least one value is {@code +infinity}.
  34.  *   <li>The result is {@code 0} if all values are in the range {@code [0, +infinity)}
  35.  *       and at least one value is zero.
  36.  *   <li>The result is {@code NaN} if all values are in the range {@code [0, +infinity]}
  37.  *       and at least one value is zero, and one value is {@code +infinity}.
  38.  * </ul>
  39.  *
  40.  * <p>Supports up to 2<sup>63</sup> (exclusive) observations.
  41.  * This implementation does not check for overflow of the count.
  42.  *
  43.  * <p>This class is designed to work with (though does not require)
  44.  * {@linkplain java.util.stream streams}.
  45.  *
  46.  * <p><strong>This instance is not thread safe.</strong>
  47.  * If multiple threads access an instance of this class concurrently,
  48.  * and at least one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or
  49.  * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
  50.  *
  51.  * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept}
  52.  * and {@link StatisticAccumulator#combine(StatisticResult) combine}
  53.  * as {@code accumulator} and {@code combiner} functions of
  54.  * {@link java.util.stream.Collector Collector} on a parallel stream,
  55.  * because the parallel instance of {@link java.util.stream.Stream#collect Stream.collect()}
  56.  * provides the necessary partitioning, isolation, and merging of results for
  57.  * safe and efficient parallel execution.
  58.  *
  59.  * @see <a href="https://en.wikipedia.org/wiki/Geometric_mean">Geometric mean (Wikipedia)</a>
  60.  * @see SumOfLogs
  61.  * @since 1.1
  62.  */
  63. public final class GeometricMean implements DoubleStatistic, StatisticAccumulator<GeometricMean> {
  64.     /** Count of values that have been added. */
  65.     private long n;

  66.     /**
  67.      * Sum of logs used to compute the geometric mean.
  68.      */
  69.     private final SumOfLogs sumOfLogs;

  70.     /**
  71.      * Create an instance.
  72.      */
  73.     private GeometricMean() {
  74.         this(SumOfLogs.create(), 0);
  75.     }

  76.     /**
  77.      * Create an instance.
  78.      *
  79.      * @param sumOfLogs Sum of logs.
  80.      * @param n Count of values.
  81.      */
  82.     private GeometricMean(SumOfLogs sumOfLogs, long n) {
  83.         this.sumOfLogs = sumOfLogs;
  84.         this.n = n;
  85.     }

  86.     /**
  87.      * Creates an instance.
  88.      *
  89.      * <p>The initial result is {@code NaN}.
  90.      *
  91.      * @return {@code GeometricMean} instance.
  92.      */
  93.     public static GeometricMean create() {
  94.         return new GeometricMean();
  95.     }

  96.     /**
  97.      * Returns an instance populated using the input {@code values}.
  98.      *
  99.      * <p>When the input is an empty array, the result is {@code NaN}.
  100.      *
  101.      * @param values Values.
  102.      * @return {@code GeometricMean} instance.
  103.      */
  104.     public static GeometricMean of(double... values) {
  105.         return new GeometricMean(SumOfLogs.of(values), values.length);
  106.     }

  107.     /**
  108.      * Returns an instance populated using the input {@code values}.
  109.      *
  110.      * <p>When the input is an empty array, the result is {@code NaN}.
  111.      *
  112.      * @param values Values.
  113.      * @return {@code GeometricMean} instance.
  114.      */
  115.     public static GeometricMean of(int... values) {
  116.         return new GeometricMean(SumOfLogs.of(values), values.length);
  117.     }

  118.     /**
  119.      * Returns an instance populated using the input {@code values}.
  120.      *
  121.      * <p>When the input is an empty array, the result is {@code NaN}.
  122.      *
  123.      * @param values Values.
  124.      * @return {@code GeometricMean} instance.
  125.      */
  126.     public static GeometricMean of(long... values) {
  127.         return new GeometricMean(SumOfLogs.of(values), values.length);
  128.     }

  129.     /**
  130.      * Updates the state of the statistic to reflect the addition of {@code value}.
  131.      *
  132.      * @param value Value.
  133.      */
  134.     @Override
  135.     public void accept(double value) {
  136.         n++;
  137.         sumOfLogs.accept(value);
  138.     }

  139.     /**
  140.      * Gets the geometric mean of all input values.
  141.      *
  142.      * <p>When no values have been added, the result is {@code NaN}.
  143.      *
  144.      * @return geometric mean of all values.
  145.      */
  146.     @Override
  147.     public double getAsDouble() {
  148.         return computeGeometricMean(n, sumOfLogs);
  149.     }

  150.     @Override
  151.     public GeometricMean combine(GeometricMean other) {
  152.         n += other.n;
  153.         sumOfLogs.combine(other.sumOfLogs);
  154.         return this;
  155.     }

  156.     /**
  157.      * Compute the geometric mean.
  158.      *
  159.      * @param n Count of values.
  160.      * @param sumOfLogs Sum of logs.
  161.      * @return the geometric mean
  162.      */
  163.     static double computeGeometricMean(long n, SumOfLogs sumOfLogs) {
  164.         return n == 0 ?
  165.             Double.NaN :
  166.             Math.exp(sumOfLogs.getAsDouble() / n);
  167.     }
  168. }