1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.stat.descriptive.moment;
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.io.Serializable;
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.MathRuntimeException;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.LocalizedFormats;
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic;
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.summary.SumOfLogs;
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.FastMath;
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * geometric mean </a> of the available values.
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Uses a {@link SumOfLogs} instance to compute sum of logs and returns
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code> exp( 1/n  (sum of logs) ).</code>  Therefore, </p>
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul>
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>If any of values are < 0, the result is <code>NaN.</code></li>
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>If all values are non-negative and less than
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>Double.POSITIVE_INFINITY</code>,  but at least one value is 0, the
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * result is <code>0.</code></li>
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>If both <code>Double.POSITIVE_INFINITY</code> and
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>Double.NEGATIVE_INFINITY</code> are among the values, the result is
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>NaN.</code></li>
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul> </p>
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <strong>Note that this implementation is not synchronized.</strong> If
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * multiple threads access an instance of this class concurrently, and at least
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * one of the threads invokes the <code>increment()</code> or
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>clear()</code> method, it must be synchronized externally.</p>
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1006299 $ $Date: 2010-10-10 16:47:17 +0200 (dim. 10 oct. 2010) $
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class GeometricMean extends AbstractStorelessUnivariateStatistic implements Serializable {
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Serializable version identifier */
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final long serialVersionUID = -8178734905303459453L;
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Wrapped SumOfLogs instance */
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private StorelessUnivariateStatistic sumOfLogs;
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Create a GeometricMean instance
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public GeometricMean() {
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        sumOfLogs = new SumOfLogs();
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Copy constructor, creates a new {@code GeometricMean} identical
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * to the {@code original}
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param original the {@code GeometricMean} instance to copy
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public GeometricMean(GeometricMean original) {
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        super();
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        copy(original, this);
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Create a GeometricMean instance using the given SumOfLogs instance
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param sumOfLogs sum of logs instance to use for computation
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public GeometricMean(SumOfLogs sumOfLogs) {
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.sumOfLogs = sumOfLogs;
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public GeometricMean copy() {
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        GeometricMean result = new GeometricMean();
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        copy(this, result);
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public void increment(final double d) {
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        sumOfLogs.increment(d);
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double getResult() {
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (sumOfLogs.getN() > 0) {
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return FastMath.exp(sumOfLogs.getResult() / sumOfLogs.getN());
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else {
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return Double.NaN;
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public void clear() {
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        sumOfLogs.clear();
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the geometric mean of the entries in the specified portion
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * of the input array.
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link GeometricMean} for details on the computing algorithm.</p>
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values input array containing the values
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin first array element to include
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the geometric mean or Double.NaN if length = 0 or
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * any of the values are &lt;= 0.
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the input array is null or the array
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * index parameters are not valid
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double evaluate(
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final double[] values, final int begin, final int length) {
143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return FastMath.exp(
144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            sumOfLogs.evaluate(values, begin, length) / length);
145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public long getN() {
151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return sumOfLogs.getN();
152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>Sets the implementation for the sum of logs.</p>
156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>This method must be activated before any data has been added - i.e.,
157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * before {@link #increment(double) increment} has been used to add data;
158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * otherwise an IllegalStateException will be thrown.</p>
159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param sumLogImpl the StorelessUnivariateStatistic instance to use
161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * for computing the log sum
162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalStateException if data has already been added
163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *  (i.e if n > 0)
164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public void setSumLogImpl(
166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            StorelessUnivariateStatistic sumLogImpl) {
167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        checkEmpty();
168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.sumOfLogs = sumLogImpl;
169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the currently configured sum of logs implementation
173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the StorelessUnivariateStatistic implementing the log sum
175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public StorelessUnivariateStatistic getSumLogImpl() {
177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return sumOfLogs;
178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Copies source to dest.
182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>Neither source nor dest can be null.</p>
183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
184dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param source GeometricMean to copy
185dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param dest GeometricMean to copy to
186dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws NullPointerException if either source or dest is null
187dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
188dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static void copy(GeometricMean source, GeometricMean dest) {
189dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.setData(source.getDataRef());
190dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.sumOfLogs = source.sumOfLogs.copy();
191dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
192dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
193dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
194dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
195dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws IllegalStateException if n > 0.
196dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
197dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private void checkEmpty() {
198dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (getN() > 0) {
199dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalStateException(
200dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    LocalizedFormats.VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC,
201dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    getN());
202dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
203dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
204dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
205dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
206