001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.stat.descriptive.moment;
018
019import java.io.Serializable;
020
021import org.apache.commons.math3.exception.NullArgumentException;
022import org.apache.commons.math3.util.MathUtils;
023
024/**
025 * Computes a statistic related to the Second Central Moment.  Specifically,
026 * what is computed is the sum of squared deviations from the sample mean.
027 * <p>
028 * The following recursive updating formula is used:</p>
029 * <p>
030 * Let <ul>
031 * <li> dev = (current obs - previous mean) </li>
032 * <li> n = number of observations (including current obs) </li>
033 * </ul>
034 * Then</p>
035 * <p>
036 * new value = old value + dev^2 * (n -1) / n.</p>
037 * <p>
038 * Returns <code>Double.NaN</code> if no data values have been added and
039 * returns <code>0</code> if there is just one value in the data set.
040 * Note that Double.NaN may also be returned if the input includes NaN
041 * and / or infinite values.</p>
042 * <p>
043 * <strong>Note that this implementation is not synchronized.</strong> If
044 * multiple threads access an instance of this class concurrently, and at least
045 * one of the threads invokes the <code>increment()</code> or
046 * <code>clear()</code> method, it must be synchronized externally.</p>
047 *
048 */
049public class SecondMoment extends FirstMoment implements Serializable {
050
051    /** Serializable version identifier */
052    private static final long serialVersionUID = 3942403127395076445L;
053
054    /** second moment of values that have been added */
055    protected double m2;
056
057    /**
058     * Create a SecondMoment instance
059     */
060    public SecondMoment() {
061        super();
062        m2 = Double.NaN;
063    }
064
065    /**
066     * Copy constructor, creates a new {@code SecondMoment} identical
067     * to the {@code original}
068     *
069     * @param original the {@code SecondMoment} instance to copy
070     * @throws NullArgumentException if original is null
071     */
072    public SecondMoment(SecondMoment original)
073    throws NullArgumentException {
074        super(original);
075        this.m2 = original.m2;
076    }
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public void increment(final double d) {
083        if (n < 1) {
084            m1 = m2 = 0.0;
085        }
086        super.increment(d);
087        m2 += ((double) n - 1) * dev * nDev;
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public void clear() {
095        super.clear();
096        m2 = Double.NaN;
097    }
098
099    /**
100     * {@inheritDoc}
101     */
102    @Override
103    public double getResult() {
104        return m2;
105    }
106
107    /**
108     * {@inheritDoc}
109     */
110    @Override
111    public SecondMoment copy() {
112        SecondMoment result = new SecondMoment();
113        // no try-catch or advertised NAE because args are guaranteed non-null
114        copy(this, result);
115        return result;
116    }
117
118    /**
119     * Copies source to dest.
120     * <p>Neither source nor dest can be null.</p>
121     *
122     * @param source SecondMoment to copy
123     * @param dest SecondMoment to copy to
124     * @throws NullArgumentException if either source or dest is null
125     */
126    public static void copy(SecondMoment source, SecondMoment dest)
127        throws NullArgumentException {
128        MathUtils.checkNotNull(source);
129        MathUtils.checkNotNull(dest);
130        FirstMoment.copy(source, dest);
131        dest.m2 = source.m2;
132    }
133
134}