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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.stat.descriptive.moment;
19
import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
20
import org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic;
21
import org.apache.commons.math.stat.descriptive.summary.SumOfLogs;
24
* Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
25
* geometric mean </a> of the available values.
27
* Uses a {@link SumOfLogs} instance to compute sum of logs and returns
28
* <code> exp( 1/n (sum of logs) ).</code> Therefore, </p>
30
* <li>If any of values are < 0, the result is <code>NaN.</code></li>
31
* <li>If all values are non-negative and less than
32
* <code>Double.POSITIVE_INFINITY</code>, but at least one value is 0, the
33
* result is <code>0.</code></li>
34
* <li>If both <code>Double.POSITIVE_INFINITY</code> and
35
* <code>Double.NEGATIVE_INFINITY</code> are among the values, the result is
36
* <code>NaN.</code></li>
39
* <strong>Note that this implementation is not synchronized.</strong> If
40
* multiple threads access an instance of this class concurrently, and at least
41
* one of the threads invokes the <code>increment()</code> or
42
* <code>clear()</code> method, it must be synchronized externally.</p>
45
* @version $Revision: 628352 $ $Date: 2008-02-16 09:39:03 -0700 (Sat, 16 Feb 2008) $
47
public class GeometricMean extends AbstractStorelessUnivariateStatistic {
49
/** Serializable version identifier */
50
private static final long serialVersionUID = -8178734905303459453L;
52
/** Wrapped SumOfLogs instance */
53
private StorelessUnivariateStatistic sumOfLogs;
56
* Create a GeometricMean instance
58
public GeometricMean() {
59
sumOfLogs = new SumOfLogs();
63
* Create a GeometricMean instance using the given SumOfLogs instance
65
public GeometricMean(SumOfLogs sumOfLogs) {
66
this.sumOfLogs = sumOfLogs;
70
* @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
72
public void increment(final double d) {
73
sumOfLogs.increment(d);
77
* @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
79
public double getResult() {
80
if (sumOfLogs.getN() > 0) {
81
return Math.exp(sumOfLogs.getResult() / (double) sumOfLogs.getN());
88
* @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
95
* Returns the geometric mean of the entries in the specified portion
98
* See {@link GeometricMean} for details on the computing algorithm.</p>
100
* Throws <code>IllegalArgumentException</code> if the array is null.</p>
102
* @param values input array containing the values
103
* @param begin first array element to include
104
* @param length the number of elements to include
105
* @return the geometric mean or Double.NaN if length = 0 or
106
* any of the values are <= 0.
107
* @throws IllegalArgumentException if the input array is null or the array
108
* index parameters are not valid
110
public double evaluate(
111
final double[] values, final int begin, final int length) {
113
sumOfLogs.evaluate(values, begin, length) / (double) length);
117
* @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
120
return sumOfLogs.getN();
124
* <p>Sets the implementation for the sum of logs.</p>
125
* <p>This method must be activated before any data has been added - i.e.,
126
* before {@link #increment(double) increment} has been used to add data;
127
* otherwise an IllegalStateException will be thrown.</p>
129
* @param sumLogImpl the StorelessUnivariateStatistic instance to use
130
* for computing the log sum
131
* @throws IllegalStateException if data has already been added
134
public void setSumLogImpl(
135
StorelessUnivariateStatistic sumLogImpl) {
137
this.sumOfLogs = sumLogImpl;
141
* Returns the currently configured sum of logs implementation
143
* @return the StorelessUnivariateStatistic implementing the log sum
145
public StorelessUnivariateStatistic getSumLogImpl() {
150
* Throws IllegalStateException if n > 0.
152
private void checkEmpty() {
154
throw new IllegalStateException(
155
"Implementation must be configured before values are added.");
b'\\ No newline at end of file'