View Javadoc
1   package org.djutils.means;
2   
3   /**
4    * Compute the geometric (weighted) mean of a set of values. Geometric mean can not handle negative or zero values.
5    * <p>
6    * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
7    * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
8    * </p>
9    * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
10   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
11   * @param <V> value type
12   * @param <W> weight type
13   */
14  public class GeometricMean<V extends Number, W extends Number> extends AbstractMean<GeometricMean<V, W>, V, W>
15  {
16  
17      @Override
18      public final double getMean()
19      {
20          return Math.exp(getSum() / getSumOfWeights());
21      }
22  
23      @Override
24      public final GeometricMean<V, W> addImpl(final V value, final Number weight)
25      {
26          increment(Math.log(value.doubleValue()) * weight.doubleValue(), weight.doubleValue());
27          return this;
28      }
29  
30      @Override
31      public final String toString()
32      {
33          return "GeometricMean [current sum of logarithmic values=" + getSum() + ", current sum of weights=" + getSumOfWeights()
34                  + ", current geometric mean=" + getMean() + "]";
35      }
36  
37  }