1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22 package org.hipparchus.stat.descriptive.moment;
23
24 import java.io.Serializable;
25
26 import org.hipparchus.exception.NullArgumentException;
27 import org.hipparchus.stat.descriptive.AggregatableStatistic;
28
29 /**
30 * Computes a statistic related to the Second Central Moment. Specifically,
31 * what is computed is the sum of squared deviations from the sample mean.
32 * <p>
33 * The following recursive updating formula is used:
34 * <p>
35 * Let <ul>
36 * <li> dev = (current obs - previous mean) </li>
37 * <li> n = number of observations (including current obs) </li>
38 * </ul>
39 * Then
40 * <p>
41 * new value = old value + dev^2 * (n - 1) / n.
42 * <p>
43 * Returns <code>Double.NaN</code> if no data values have been added and
44 * returns <code>0</code> if there is just one value in the data set.
45 * Note that Double.NaN may also be returned if the input includes NaN
46 * and / or infinite values.
47 * <p>
48 * <strong>Note that this implementation is not synchronized.</strong> If
49 * multiple threads access an instance of this class concurrently, and at least
50 * one of the threads invokes the <code>increment()</code> or
51 * <code>clear()</code> method, it must be synchronized externally.
52 */
53 public class SecondMoment extends FirstMoment
54 implements AggregatableStatistic<SecondMoment>, Serializable {
55
56 /** Serializable version identifier */
57 private static final long serialVersionUID = 20150412L;
58
59 /** Second moment of values that have been added */
60 protected double m2;
61
62 /**
63 * Create a SecondMoment instance.
64 */
65 public SecondMoment() {
66 m2 = Double.NaN;
67 }
68
69 /**
70 * Copy constructor, creates a new {@code SecondMoment} identical
71 * to the {@code original}.
72 *
73 * @param original the {@code SecondMoment} instance to copy
74 * @throws NullArgumentException if original is null
75 */
76 public SecondMoment(SecondMoment original) throws NullArgumentException {
77 super(original);
78 this.m2 = original.m2;
79 }
80
81 /** {@inheritDoc} */
82 @Override
83 public void increment(final double d) {
84 if (n < 1) {
85 m1 = m2 = 0.0;
86 }
87 super.increment(d);
88 m2 += ((double) n - 1) * dev * nDev;
89 }
90
91 /** {@inheritDoc} */
92 @Override
93 public void clear() {
94 super.clear();
95 m2 = Double.NaN;
96 }
97
98 /** {@inheritDoc} */
99 @Override
100 public double getResult() {
101 return m2;
102 }
103
104 /** {@inheritDoc} */
105 @Override
106 public void aggregate(SecondMoment other) {
107 if (other.n > 0) {
108 final double oldN = n;
109 super.aggregate(other);
110 if (oldN == 0) {
111 m2 = other.m2;
112 } else {
113 m2 += other.m2 + (other.n * oldN) / n * dev * dev;
114 }
115 }
116 }
117
118 /** {@inheritDoc} */
119 @Override
120 public SecondMoment copy() {
121 return new SecondMoment(this);
122 }
123
124 }