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.summary;
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.io.Serializable;
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.FastMath;
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Returns the sum of the natural logs for this collection of values.
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Uses {@link java.lang.Math#log(double)} to compute the logs.  Therefore,
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <ul>
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>If any of values are < 0, the result is <code>NaN.</code></li>
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>If all values are non-negative and less than
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>Double.POSITIVE_INFINITY</code>,  but at least one value is 0, the
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * result is <code>Double.NEGATIVE_INFINITY.</code></li>
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <li>If both <code>Double.POSITIVE_INFINITY</code> and
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>Double.NEGATIVE_INFINITY</code> are among the values, the result is
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>NaN.</code></li>
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </ul></p>
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <strong>Note that this implementation is not synchronized.</strong> If
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * multiple threads access an instance of this class concurrently, and at least
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * one of the threads invokes the <code>increment()</code> or
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>clear()</code> method, it must be synchronized externally.</p>
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1006299 $ $Date: 2010-10-10 16:47:17 +0200 (dim. 10 oct. 2010) $
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class SumOfLogs extends AbstractStorelessUnivariateStatistic implements Serializable {
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Serializable version identifier */
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final long serialVersionUID = -370076995648386763L;
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**Number of values that have been added */
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private int n;
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * The currently running value
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private double value;
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Create a SumOfLogs instance
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public SumOfLogs() {
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond       value = 0d;
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond       n = 0;
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Copy constructor, creates a new {@code SumOfLogs} identical
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * to the {@code original}
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param original the {@code SumOfLogs} instance to copy
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public SumOfLogs(SumOfLogs original) {
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        copy(original, this);
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public void increment(final double d) {
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        value += FastMath.log(d);
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        n++;
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double getResult() {
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n > 0) {
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return value;
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else {
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return Double.NaN;
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public long getN() {
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return n;
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public void clear() {
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        value = 0d;
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        n = 0;
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the sum of the natural logs of the entries in the specified portion of
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link SumOfLogs}.</p>
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum of the natural logs of the values or Double.NaN if
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * length = 0
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *  parameters are not valid
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double evaluate(final double[] values, final int begin, final int length) {
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double sumLog = Double.NaN;
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (test(values, begin, length)) {
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            sumLog = 0.0;
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            for (int i = begin; i < begin + length; i++) {
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                sumLog += FastMath.log(values[i]);
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return sumLog;
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public SumOfLogs copy() {
147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        SumOfLogs result = new SumOfLogs();
148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        copy(this, result);
149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Copies source to dest.
154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>Neither source nor dest can be null.</p>
155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param source SumOfLogs to copy
157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param dest SumOfLogs to copy to
158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws NullPointerException if either source or dest is null
159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static void copy(SumOfLogs source, SumOfLogs dest) {
161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.setData(source.getDataRef());
162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.n = source.n;
163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.value = source.value;
164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
166