View Javadoc

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.math3.stat.descriptive.moment;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.math3.exception.MathIllegalArgumentException;
22  import org.apache.commons.math3.exception.MathIllegalStateException;
23  import org.apache.commons.math3.exception.NullArgumentException;
24  import org.apache.commons.math3.exception.util.LocalizedFormats;
25  import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic;
26  import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic;
27  import org.apache.commons.math3.stat.descriptive.summary.SumOfLogs;
28  import org.apache.commons.math3.util.FastMath;
29  import org.apache.commons.math3.util.MathUtils;
30  
31  /**
32   * Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
33   * geometric mean </a> of the available values.
34   * <p>
35   * Uses a {@link SumOfLogs} instance to compute sum of logs and returns
36   * <code> exp( 1/n  (sum of logs) ).</code>  Therefore, </p>
37   * <ul>
38   * <li>If any of values are < 0, the result is <code>NaN.</code></li>
39   * <li>If all values are non-negative and less than
40   * <code>Double.POSITIVE_INFINITY</code>,  but at least one value is 0, the
41   * result is <code>0.</code></li>
42   * <li>If both <code>Double.POSITIVE_INFINITY</code> and
43   * <code>Double.NEGATIVE_INFINITY</code> are among the values, the result is
44   * <code>NaN.</code></li>
45   * </ul> </p>
46   * <p>
47   * <strong>Note that this implementation is not synchronized.</strong> If
48   * multiple threads access an instance of this class concurrently, and at least
49   * one of the threads invokes the <code>increment()</code> or
50   * <code>clear()</code> method, it must be synchronized externally.</p>
51   *
52   *
53   * @version $Id: GeometricMean.java 1416643 2012-12-03 19:37:14Z tn $
54   */
55  public class GeometricMean extends AbstractStorelessUnivariateStatistic implements Serializable {
56  
57      /** Serializable version identifier */
58      private static final long serialVersionUID = -8178734905303459453L;
59  
60      /** Wrapped SumOfLogs instance */
61      private StorelessUnivariateStatistic sumOfLogs;
62  
63      /**
64       * Create a GeometricMean instance
65       */
66      public GeometricMean() {
67          sumOfLogs = new SumOfLogs();
68      }
69  
70      /**
71       * Copy constructor, creates a new {@code GeometricMean} identical
72       * to the {@code original}
73       *
74       * @param original the {@code GeometricMean} instance to copy
75       * @throws NullArgumentException if original is null
76       */
77      public GeometricMean(GeometricMean original) throws NullArgumentException {
78          super();
79          copy(original, this);
80      }
81  
82      /**
83       * Create a GeometricMean instance using the given SumOfLogs instance
84       * @param sumOfLogs sum of logs instance to use for computation
85       */
86      public GeometricMean(SumOfLogs sumOfLogs) {
87          this.sumOfLogs = sumOfLogs;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public GeometricMean copy() {
95          GeometricMean result = new GeometricMean();
96          // no try-catch or advertised exception because args guaranteed non-null
97          copy(this, result);
98          return result;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public void increment(final double d) {
106         sumOfLogs.increment(d);
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public double getResult() {
114         if (sumOfLogs.getN() > 0) {
115             return FastMath.exp(sumOfLogs.getResult() / sumOfLogs.getN());
116         } else {
117             return Double.NaN;
118         }
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public void clear() {
126         sumOfLogs.clear();
127     }
128 
129     /**
130      * Returns the geometric mean of the entries in the specified portion
131      * of the input array.
132      * <p>
133      * See {@link GeometricMean} for details on the computing algorithm.</p>
134      * <p>
135      * Throws <code>IllegalArgumentException</code> if the array is null.</p>
136      *
137      * @param values input array containing the values
138      * @param begin first array element to include
139      * @param length the number of elements to include
140      * @return the geometric mean or Double.NaN if length = 0 or
141      * any of the values are &lt;= 0.
142      * @throws MathIllegalArgumentException if the input array is null or the array
143      * index parameters are not valid
144      */
145     @Override
146     public double evaluate(
147         final double[] values, final int begin, final int length)
148     throws MathIllegalArgumentException {
149         return FastMath.exp(
150             sumOfLogs.evaluate(values, begin, length) / length);
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     public long getN() {
157         return sumOfLogs.getN();
158     }
159 
160     /**
161      * <p>Sets the implementation for the sum of logs.</p>
162      * <p>This method must be activated before any data has been added - i.e.,
163      * before {@link #increment(double) increment} has been used to add data;
164      * otherwise an IllegalStateException will be thrown.</p>
165      *
166      * @param sumLogImpl the StorelessUnivariateStatistic instance to use
167      * for computing the log sum
168      * @throws MathIllegalStateException if data has already been added
169      *  (i.e if n > 0)
170      */
171     public void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl)
172     throws MathIllegalStateException {
173         checkEmpty();
174         this.sumOfLogs = sumLogImpl;
175     }
176 
177     /**
178      * Returns the currently configured sum of logs implementation
179      *
180      * @return the StorelessUnivariateStatistic implementing the log sum
181      */
182     public StorelessUnivariateStatistic getSumLogImpl() {
183         return sumOfLogs;
184     }
185 
186     /**
187      * Copies source to dest.
188      * <p>Neither source nor dest can be null.</p>
189      *
190      * @param source GeometricMean to copy
191      * @param dest GeometricMean to copy to
192      * @throws NullArgumentException if either source or dest is null
193      */
194     public static void copy(GeometricMean source, GeometricMean dest)
195         throws NullArgumentException {
196         MathUtils.checkNotNull(source);
197         MathUtils.checkNotNull(dest);
198         dest.setData(source.getDataRef());
199         dest.sumOfLogs = source.sumOfLogs.copy();
200     }
201 
202 
203     /**
204      * Throws MathIllegalStateException if n > 0.
205      * @throws MathIllegalStateException if data has been added to this statistic
206      */
207     private void checkEmpty() throws MathIllegalStateException {
208         if (getN() > 0) {
209             throw new MathIllegalStateException(
210                     LocalizedFormats.VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC,
211                     getN());
212         }
213     }
214 
215 }