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.math4.stat.descriptive.moment;
018
019import java.io.Serializable;
020
021import org.apache.commons.math4.exception.NullArgumentException;
022import org.apache.commons.math4.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
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 */
048public class SecondMoment extends FirstMoment implements Serializable {
049
050    /** Serializable version identifier */
051    private static final long serialVersionUID = 20150412L;
052
053    /** second moment of values that have been added */
054    protected double m2;
055
056    /**
057     * Create a SecondMoment instance.
058     */
059    public SecondMoment() {
060        super();
061        m2 = Double.NaN;
062    }
063
064    /**
065     * Copy constructor, creates a new {@code SecondMoment} identical
066     * to the {@code original}.
067     *
068     * @param original the {@code SecondMoment} instance to copy
069     * @throws NullArgumentException if original is null
070     */
071    public SecondMoment(SecondMoment original) 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}