001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat.descriptive.summary;
017:
018: import java.io.Serializable;
019:
020: import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
021:
022: /**
023: * Returns the sum of the natural logs for this collection of values.
024: * <p>
025: * Uses {@link java.lang.Math#log(double)} to compute the logs. Therefore,
026: * <ul>
027: * <li>If any of values are < 0, the result is <code>NaN.</code></li>
028: * <li>If all values are non-negative and less than
029: * <code>Double.POSITIVE_INFINITY</code>, but at least one value is 0, the
030: * result is <code>Double.NEGATIVE_INFINITY.</code></li>
031: * <li>If both <code>Double.POSITIVE_INFINITY</code> and
032: * <code>Double.NEGATIVE_INFINITY</code> are among the values, the result is
033: * <code>NaN.</code></li>
034: * </ul>
035: * <p>
036: * <strong>Note that this implementation is not synchronized.</strong> If
037: * multiple threads access an instance of this class concurrently, and at least
038: * one of the threads invokes the <code>increment()</code> or
039: * <code>clear()</code> method, it must be synchronized externally.
040: *
041: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
042: */
043: public class SumOfLogs extends AbstractStorelessUnivariateStatistic
044: implements Serializable {
045:
046: /** Serializable version identifier */
047: private static final long serialVersionUID = -370076995648386763L;
048:
049: /**Number of values that have been added */
050: private int n;
051:
052: /**
053: * The currently running value
054: */
055: private double value;
056:
057: /**
058: * Create a SumOfLogs instance
059: */
060: public SumOfLogs() {
061: value = 0d;
062: n = 0;
063: }
064:
065: /**
066: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
067: */
068: public void increment(final double d) {
069: value += Math.log(d);
070: n++;
071: }
072:
073: /**
074: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
075: */
076: public double getResult() {
077: if (n > 0) {
078: return value;
079: } else {
080: return Double.NaN;
081: }
082: }
083:
084: /**
085: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
086: */
087: public long getN() {
088: return n;
089: }
090:
091: /**
092: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
093: */
094: public void clear() {
095: value = 0d;
096: n = 0;
097: }
098:
099: /**
100: * Returns the sum of the natural logs of the entries in the specified portion of
101: * the input array, or <code>Double.NaN</code> if the designated subarray
102: * is empty.
103: * <p>
104: * Throws <code>IllegalArgumentException</code> if the array is null.
105: * <p>
106: * See {@link SumOfLogs}.
107: *
108: * @param values the input array
109: * @param begin index of the first array element to include
110: * @param length the number of elements to include
111: * @return the sum of the natural logs of the values or Double.NaN if
112: * length = 0
113: * @throws IllegalArgumentException if the array is null or the array index
114: * parameters are not valid
115: */
116: public double evaluate(final double[] values, final int begin,
117: final int length) {
118: double sumLog = Double.NaN;
119: if (test(values, begin, length)) {
120: sumLog = 0.0;
121: for (int i = begin; i < begin + length; i++) {
122: sumLog += Math.log(values[i]);
123: }
124: }
125: return sumLog;
126: }
127: }
|