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.</p>
040 * <p>
041 * <strong>Note that this implementation is not synchronized.</strong> If
042 * multiple threads access an instance of this class concurrently, and at least
043 * one of the threads invokes the <code>increment()</code> or
044 * <code>clear()</code> method, it must be synchronized externally.</p>
045 *
046 */
047public class SecondMoment extends FirstMoment implements Serializable {
048
049    /** Serializable version identifier */
050    private static final long serialVersionUID = 3942403127395076445L;
051
052    /** second moment of values that have been added */
053    protected double m2;
054
055    /**
056     * Create a SecondMoment instance
057     */
058    public SecondMoment() {
059        super();
060        m2 = Double.NaN;
061    }
062
063    /**
064     * Copy constructor, creates a new {@code SecondMoment} identical
065     * to the {@code original}
066     *
067     * @param original the {@code SecondMoment} instance to copy
068     * @throws NullArgumentException if original is null
069     */
070    public SecondMoment(SecondMoment original)
071    throws NullArgumentException {
072        super(original);
073        this.m2 = original.m2;
074    }
075
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public void increment(final double d) {
081        if (n < 1) {
082            m1 = m2 = 0.0;
083        }
084        super.increment(d);
085        m2 += ((double) n - 1) * dev * nDev;
086    }
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public void clear() {
093        super.clear();
094        m2 = Double.NaN;
095    }
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    public double getResult() {
102        return m2;
103    }
104
105    /**
106     * {@inheritDoc}
107     */
108    @Override
109    public SecondMoment copy() {
110        SecondMoment result = new SecondMoment();
111        // no try-catch or advertised NAE because args are guaranteed non-null
112        copy(this, result);
113        return result;
114    }
115
116    /**
117     * Copies source to dest.
118     * <p>Neither source nor dest can be null.</p>
119     *
120     * @param source SecondMoment to copy
121     * @param dest SecondMoment to copy to
122     * @throws NullArgumentException if either source or dest is null
123     */
124    public static void copy(SecondMoment source, SecondMoment dest)
125        throws NullArgumentException {
126        MathUtils.checkNotNull(source);
127        MathUtils.checkNotNull(dest);
128        FirstMoment.copy(source, dest);
129        dest.m2 = source.m2;
130    }
131
132}