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 */
017 package org.apache.commons.math.stat.descriptive.moment;
018
019 import java.io.Serializable;
020
021 /**
022 * Computes a statistic related to the Second Central Moment. Specifically,
023 * what is computed is the sum of squared deviations from the sample mean.
024 * <p>
025 * The following recursive updating formula is used:</p>
026 * <p>
027 * Let <ul>
028 * <li> dev = (current obs - previous mean) </li>
029 * <li> n = number of observations (including current obs) </li>
030 * </ul>
031 * Then</p>
032 * <p>
033 * new value = old value + dev^2 * (n -1) / n.</p>
034 * <p>
035 * Returns <code>Double.NaN</code> if no data values have been added and
036 * returns <code>0</code> if there is just one value in the data set.</p>
037 * <p>
038 * <strong>Note that this implementation is not synchronized.</strong> If
039 * multiple threads access an instance of this class concurrently, and at least
040 * one of the threads invokes the <code>increment()</code> or
041 * <code>clear()</code> method, it must be synchronized externally.</p>
042 *
043 * @version $Revision: 811685 $ $Date: 2009-09-05 13:36:48 -0400 (Sat, 05 Sep 2009) $
044 */
045 public class SecondMoment extends FirstMoment implements Serializable {
046
047 /** Serializable version identifier */
048 private static final long serialVersionUID = 3942403127395076445L;
049
050 /** second moment of values that have been added */
051 protected double m2;
052
053 /**
054 * Create a SecondMoment instance
055 */
056 public SecondMoment() {
057 super();
058 m2 = Double.NaN;
059 }
060
061 /**
062 * Copy constructor, creates a new {@code SecondMoment} identical
063 * to the {@code original}
064 *
065 * @param original the {@code SecondMoment} instance to copy
066 */
067 public SecondMoment(SecondMoment original) {
068 super(original);
069 this.m2 = original.m2;
070 }
071
072 /**
073 * {@inheritDoc}
074 */
075 @Override
076 public void increment(final double d) {
077 if (n < 1) {
078 m1 = m2 = 0.0;
079 }
080 super.increment(d);
081 m2 += ((double) n - 1) * dev * nDev;
082 }
083
084 /**
085 * {@inheritDoc}
086 */
087 @Override
088 public void clear() {
089 super.clear();
090 m2 = Double.NaN;
091 }
092
093 /**
094 * {@inheritDoc}
095 */
096 @Override
097 public double getResult() {
098 return m2;
099 }
100
101 /**
102 * {@inheritDoc}
103 */
104 @Override
105 public SecondMoment copy() {
106 SecondMoment result = new SecondMoment();
107 copy(this, result);
108 return result;
109 }
110
111 /**
112 * Copies source to dest.
113 * <p>Neither source nor dest can be null.</p>
114 *
115 * @param source SecondMoment to copy
116 * @param dest SecondMoment to copy to
117 * @throws NullPointerException if either source or dest is null
118 */
119 public static void copy(SecondMoment source, SecondMoment dest) {
120 FirstMoment.copy(source, dest);
121 dest.m2 = source.m2;
122 }
123
124 }