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.math4.legacy.stat.descriptive.moment;

  18. import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
  19. import org.apache.commons.math4.legacy.exception.MathIllegalStateException;
  20. import org.apache.commons.math4.legacy.exception.NullArgumentException;
  21. import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
  22. import org.apache.commons.math4.legacy.stat.descriptive.AbstractStorelessUnivariateStatistic;
  23. import org.apache.commons.math4.legacy.stat.descriptive.StorelessUnivariateStatistic;
  24. import org.apache.commons.math4.legacy.stat.descriptive.summary.SumOfLogs;
  25. import org.apache.commons.math4.core.jdkmath.JdkMath;

  26. /**
  27.  * Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
  28.  * geometric mean </a> of the available values.
  29.  * <p>
  30.  * Uses a {@link SumOfLogs} instance to compute sum of logs and returns
  31.  * <code> exp( 1/n  (sum of logs) ).</code>  Therefore, </p>
  32.  * <ul>
  33.  * <li>If any of values are {@code < 0}, the result is <code>NaN.</code></li>
  34.  * <li>If all values are non-negative and less than
  35.  * <code>Double.POSITIVE_INFINITY</code>,  but at least one value is 0, the
  36.  * result is <code>0.</code></li>
  37.  * <li>If both <code>Double.POSITIVE_INFINITY</code> and
  38.  * <code>Double.NEGATIVE_INFINITY</code> are among the values, the result is
  39.  * <code>NaN.</code></li>
  40.  * </ul>
  41.  * <p>
  42.  * <strong>Note that this implementation is not synchronized.</strong> If
  43.  * multiple threads access an instance of this class concurrently, and at least
  44.  * one of the threads invokes the <code>increment()</code> or
  45.  * <code>clear()</code> method, it must be synchronized externally.</p>
  46.  */
  47. public class GeometricMean extends AbstractStorelessUnivariateStatistic {
  48.     /** Wrapped SumOfLogs instance. */
  49.     private StorelessUnivariateStatistic sumOfLogs;

  50.     /**
  51.      * Create a GeometricMean instance.
  52.      */
  53.     public GeometricMean() {
  54.         sumOfLogs = new SumOfLogs();
  55.     }

  56.     /**
  57.      * Copy constructor, creates a new {@code GeometricMean} identical
  58.      * to the {@code original}.
  59.      *
  60.      * @param original the {@code GeometricMean} instance to copy
  61.      * @throws NullArgumentException if original is null
  62.      */
  63.     public GeometricMean(GeometricMean original) throws NullArgumentException {
  64.         super();
  65.         copy(original, this);
  66.     }

  67.     /**
  68.      * Create a GeometricMean instance using the given SumOfLogs instance.
  69.      * @param sumOfLogs sum of logs instance to use for computation.
  70.      */
  71.     public GeometricMean(SumOfLogs sumOfLogs) {
  72.         this.sumOfLogs = sumOfLogs;
  73.     }

  74.     /**
  75.      * {@inheritDoc}
  76.      */
  77.     @Override
  78.     public GeometricMean copy() {
  79.         GeometricMean result = new GeometricMean();
  80.         // no try-catch or advertised exception because args guaranteed non-null
  81.         copy(this, result);
  82.         return result;
  83.     }

  84.     /**
  85.      * {@inheritDoc}
  86.      */
  87.     @Override
  88.     public void increment(final double d) {
  89.         sumOfLogs.increment(d);
  90.     }

  91.     /**
  92.      * {@inheritDoc}
  93.      */
  94.     @Override
  95.     public double getResult() {
  96.         if (sumOfLogs.getN() > 0) {
  97.             return JdkMath.exp(sumOfLogs.getResult() / sumOfLogs.getN());
  98.         } else {
  99.             return Double.NaN;
  100.         }
  101.     }

  102.     /**
  103.      * {@inheritDoc}
  104.      */
  105.     @Override
  106.     public void clear() {
  107.         sumOfLogs.clear();
  108.     }

  109.     /**
  110.      * Returns the geometric mean of the entries in the specified portion
  111.      * of the input array.
  112.      * <p>
  113.      * See {@link GeometricMean} for details on the computing algorithm.</p>
  114.      * <p>
  115.      * Throws <code>IllegalArgumentException</code> if the array is null.</p>
  116.      *
  117.      * @param values input array containing the values
  118.      * @param begin first array element to include
  119.      * @param length the number of elements to include
  120.      * @return the geometric mean or Double.NaN if length = 0 or
  121.      * any of the values are &lt;= 0.
  122.      * @throws MathIllegalArgumentException if the input array is null or the array
  123.      * index parameters are not valid
  124.      */
  125.     @Override
  126.     public double evaluate(final double[] values, final int begin, final int length)
  127.         throws MathIllegalArgumentException {
  128.         return JdkMath.exp(sumOfLogs.evaluate(values, begin, length) / length);
  129.     }

  130.     /**
  131.      * {@inheritDoc}
  132.      */
  133.     @Override
  134.     public long getN() {
  135.         return sumOfLogs.getN();
  136.     }

  137.     /**
  138.      * <p>Sets the implementation for the sum of logs.</p>
  139.      * <p>This method must be activated before any data has been added - i.e.,
  140.      * before {@link #increment(double) increment} has been used to add data;
  141.      * otherwise an IllegalStateException will be thrown.</p>
  142.      *
  143.      * @param sumLogImpl the StorelessUnivariateStatistic instance to use
  144.      * for computing the log sum
  145.      * @throws MathIllegalStateException if data has already been added
  146.      *  (i.e if n &gt; 0)
  147.      */
  148.     public void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl)
  149.         throws MathIllegalStateException {
  150.         checkEmpty();
  151.         this.sumOfLogs = sumLogImpl;
  152.     }

  153.     /**
  154.      * Returns the currently configured sum of logs implementation.
  155.      *
  156.      * @return the StorelessUnivariateStatistic implementing the log sum
  157.      */
  158.     public StorelessUnivariateStatistic getSumLogImpl() {
  159.         return sumOfLogs;
  160.     }

  161.     /**
  162.      * Copies source to dest.
  163.      * <p>Neither source nor dest can be null.</p>
  164.      *
  165.      * @param source GeometricMean to copy
  166.      * @param dest GeometricMean to copy to
  167.      * @throws NullArgumentException if either source or dest is null
  168.      */
  169.     public static void copy(GeometricMean source, GeometricMean dest)
  170.         throws NullArgumentException {
  171.         NullArgumentException.check(source);
  172.         NullArgumentException.check(dest);
  173.         dest.sumOfLogs = source.sumOfLogs.copy();
  174.     }

  175.     /**
  176.      * Throws MathIllegalStateException if n > 0.
  177.      * @throws MathIllegalStateException if data has been added to this statistic
  178.      */
  179.     private void checkEmpty() throws MathIllegalStateException {
  180.         if (getN() > 0) {
  181.             throw new MathIllegalStateException(
  182.                     LocalizedFormats.VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC,
  183.                     getN());
  184.         }
  185.     }
  186. }