View Javadoc

1   /*
2    * Copyright 2003-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.math.stat.descriptive.moment;
17  
18  import java.io.Serializable;
19  
20  /**
21   * Computes a statistic related to the Second Central Moment.  Specifically,
22   * what is computed is the sum of squared deviations from the sample mean.
23   * <p>
24   * The following recursive updating formula is used:
25   * <p>
26   * Let <ul>
27   * <li> dev = (current obs - previous mean) </li>
28   * <li> n = number of observations (including current obs) </li>
29   * </ul>
30   * Then
31   * <p>
32   * new value = old value + dev^2 * (n -1) / n.
33   * <p>
34   * Returns <code>Double.NaN</code> if no data values have been added and
35   * returns <code>0</code> if there is just one value in the data set.
36   * <p>
37   * <strong>Note that this implementation is not synchronized.</strong> If 
38   * multiple threads access an instance of this class concurrently, and at least
39   * one of the threads invokes the <code>increment()</code> or 
40   * <code>clear()</code> method, it must be synchronized externally.
41   * 
42   * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
43   */
44  public class SecondMoment extends FirstMoment implements Serializable {
45  
46      /** Serializable version identifier */
47      private static final long serialVersionUID = 3942403127395076445L;  
48        
49      /** second moment of values that have been added */
50      protected double m2;
51  
52      /**
53       * Create a SecondMoment instance
54       */
55      public SecondMoment() {
56          super();
57          m2 = Double.NaN;
58      }
59      
60      /**
61       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
62       */
63      public void increment(final double d) {
64          if (n < 1) {
65              m1 = m2 = 0.0;
66          }
67          super.increment(d);
68          m2 += ((double) n - 1) * dev * nDev;
69      }
70  
71      /**
72       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
73       */
74      public void clear() {
75          super.clear();
76          m2 = Double.NaN;
77      }
78  
79      /**
80       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
81       */
82      public double getResult() {
83          return m2;
84      }
85  
86  }