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 * http://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 package org.apache.commons.math4.legacy.stat.descriptive.moment; 18 19 import org.apache.commons.math4.legacy.exception.NullArgumentException; 20 21 /** 22 * Computes a statistic related to the Second Central Moment. Specifically, 23 * what is computed is the sum of squared deviations from the sample mean. 24 * <p> 25 * The following recursive updating formula is used:</p> 26 * <p> 27 * Let <ul> 28 * <li> dev = (current obs - previous mean) </li> 29 * <li> n = number of observations (including current obs) </li> 30 * </ul> 31 * Then 32 * <p> 33 * new value = old value + dev^2 * (n -1) / n.</p> 34 * <p> 35 * Returns <code>Double.NaN</code> if no data values have been added and 36 * returns <code>0</code> if there is just one value in the data set. 37 * Note that Double.NaN may also be returned if the input includes NaN 38 * and / or infinite values.</p> 39 * <p> 40 * <strong>Note that this implementation is not synchronized.</strong> If 41 * multiple threads access an instance of this class concurrently, and at least 42 * one of the threads invokes the <code>increment()</code> or 43 * <code>clear()</code> method, it must be synchronized externally.</p> 44 */ 45 public class SecondMoment extends FirstMoment { 46 /** second moment of values that have been added. */ 47 protected double m2; 48 49 /** 50 * Create a SecondMoment instance. 51 */ 52 public SecondMoment() { 53 super(); 54 m2 = Double.NaN; 55 } 56 57 /** 58 * Copy constructor, creates a new {@code SecondMoment} identical 59 * to the {@code original}. 60 * 61 * @param original the {@code SecondMoment} instance to copy 62 * @throws NullArgumentException if original is null 63 */ 64 public SecondMoment(SecondMoment original) throws NullArgumentException { 65 super(original); 66 this.m2 = original.m2; 67 } 68 69 /** 70 * {@inheritDoc} 71 */ 72 @Override 73 public void increment(final double d) { 74 if (n < 1) { 75 m1 = m2 = 0.0; 76 } 77 super.increment(d); 78 m2 += ((double) n - 1) * dev * nDev; 79 } 80 81 /** 82 * {@inheritDoc} 83 */ 84 @Override 85 public void clear() { 86 super.clear(); 87 m2 = Double.NaN; 88 } 89 90 /** 91 * {@inheritDoc} 92 */ 93 @Override 94 public double getResult() { 95 return m2; 96 } 97 98 /** 99 * {@inheritDoc} 100 */ 101 @Override 102 public SecondMoment copy() { 103 SecondMoment result = new SecondMoment(); 104 // no try-catch or advertised NAE because args are guaranteed non-null 105 copy(this, result); 106 return result; 107 } 108 109 /** 110 * Copies source to dest. 111 * <p>Neither source nor dest can be null.</p> 112 * 113 * @param source SecondMoment to copy 114 * @param dest SecondMoment to copy to 115 * @throws NullArgumentException if either source or dest is null 116 */ 117 public static void copy(SecondMoment source, SecondMoment dest) 118 throws NullArgumentException { 119 NullArgumentException.check(source); 120 NullArgumentException.check(dest); 121 FirstMoment.copy(source, dest); 122 dest.m2 = source.m2; 123 } 124 }