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 java.io.Serializable;
20
import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
23
* Computes the first moment (arithmetic mean). Uses the definitional formula:
25
* mean = sum(x_i) / n </p>
27
* where <code>n</code> is the number of observations. </p>
29
* To limit numeric errors, the value of the statistic is computed using the
30
* following recursive updating algorithm: </p>
33
* <li>Initialize <code>m = </code> the first value</li>
34
* <li>For each additional value, update using <br>
35
* <code>m = m + (new value - m) / (number of observations)</code></li>
38
* Returns <code>Double.NaN</code> if the dataset is empty.</p>
40
* <strong>Note that this implementation is not synchronized.</strong> If
41
* multiple threads access an instance of this class concurrently, and at least
42
* one of the threads invokes the <code>increment()</code> or
43
* <code>clear()</code> method, it must be synchronized externally.</p>
45
* @version $Revision: 780541 $ $Date: 2009-05-31 20:47:02 -0400 (Sun, 31 May 2009) $
47
public class FirstMoment extends AbstractStorelessUnivariateStatistic
48
implements Serializable {
50
/** Serializable version identifier */
51
private static final long serialVersionUID = 6112755307178490473L;
54
/** Count of values that have been added */
57
/** First moment of values that have been added */
61
* Deviation of most recently added value from previous first moment.
62
* Retained to prevent repeated computation in higher order moments.
67
* Deviation of most recently added value from previous first moment,
68
* normalized by previous sample size. Retained to prevent repeated
69
* computation in higher order moments
71
protected double nDev;
74
* Create a FirstMoment instance
76
public FirstMoment() {
84
* Copy constructor, creates a new {@code FirstMoment} identical
85
* to the {@code original}
87
* @param original the {@code FirstMoment} instance to copy
89
public FirstMoment(FirstMoment original) {
98
public void increment(final double d) {
113
public void clear() {
124
public double getResult() {
139
public FirstMoment copy() {
140
FirstMoment result = new FirstMoment();
146
* Copies source to dest.
147
* <p>Neither source nor dest can be null.</p>
149
* @param source FirstMoment to copy
150
* @param dest FirstMoment to copy to
151
* @throws NullPointerException if either source or dest is null
153
public static void copy(FirstMoment source, FirstMoment dest) {
156
dest.dev = source.dev;
157
dest.nDev = dest.nDev;