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 super();
67 m2 = Double.NaN;
68 }
69
70 /**
71 * Copy constructor, creates a new {@code SecondMoment} identical
72 * to the {@code original}.
73 *
74 * @param original the {@code SecondMoment} instance to copy
75 * @throws NullArgumentException if original is null
76 */
77 public SecondMoment(SecondMoment original) throws NullArgumentException {
78 super(original);
79 this.m2 = original.m2;
80 }
81
82 /** {@inheritDoc} */
83 @Override
84 public void increment(final double d) {
85 if (n < 1) {
86 m1 = m2 = 0.0;
87 }
88 super.increment(d);
89 m2 += ((double) n - 1) * dev * nDev;
90 }
91
92 /** {@inheritDoc} */
93 @Override
94 public void clear() {
95 super.clear();
96 m2 = Double.NaN;
97 }
98
99 /** {@inheritDoc} */
100 @Override
101 public double getResult() {
102 return m2;
103 }
104
105 /** {@inheritDoc} */
106 @Override
107 public void aggregate(SecondMoment other) {
108 if (other.n > 0) {
109 final double oldN = n;
110 super.aggregate(other);
111 if (oldN == 0) {
112 m2 = other.m2;
113 } else {
114 m2 += other.m2 + (other.n * oldN) / n * dev * dev;
115 }
116 }
117 }
118
119 /** {@inheritDoc} */
120 @Override
121 public SecondMoment copy() {
122 return new SecondMoment(this);
123 }
124
125 }