Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
SecondMoment |
|
| 1.25;1.25 |
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$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $ |
|
43 | */ |
|
44 | public class SecondMoment extends FirstMoment implements Serializable { |
|
45 | ||
46 | /** Serializable version identifier */ |
|
47 | 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 | 10418 | super(); |
57 | 10418 | m2 = Double.NaN; |
58 | 10418 | } |
59 | ||
60 | /** |
|
61 | * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double) |
|
62 | */ |
|
63 | public void increment(final double d) { |
|
64 | 133374 | if (n < 1) { |
65 | 4660 | m1 = m2 = 0.0; |
66 | } |
|
67 | 133374 | super.increment(d); |
68 | 133374 | m2 += ((double) n - 1) * dev * nDev; |
69 | 133374 | } |
70 | ||
71 | /** |
|
72 | * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear() |
|
73 | */ |
|
74 | public void clear() { |
|
75 | 452 | super.clear(); |
76 | 452 | m2 = Double.NaN; |
77 | 452 | } |
78 | ||
79 | /** |
|
80 | * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult() |
|
81 | */ |
|
82 | public double getResult() { |
|
83 | 108 | return m2; |
84 | } |
|
85 | ||
86 | } |