Raymond | dee0849 | 2015-04-02 10:43:13 -0700 | [diff] [blame] | 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.math.stat.descriptive.moment; |
| 18 | |
| 19 | import java.io.Serializable; |
| 20 | |
| 21 | /** |
| 22 | * Computes a statistic related to the Fourth Central Moment. Specifically, |
| 23 | * what is computed is the sum of |
| 24 | * <p> |
| 25 | * (x_i - xbar) ^ 4, </p> |
| 26 | * <p> |
| 27 | * where the x_i are the |
| 28 | * sample observations and xbar is the sample mean. </p> |
| 29 | * <p> |
| 30 | * The following recursive updating formula is used: </p> |
| 31 | * <p> |
| 32 | * Let <ul> |
| 33 | * <li> dev = (current obs - previous mean) </li> |
| 34 | * <li> m2 = previous value of {@link SecondMoment} </li> |
| 35 | * <li> m2 = previous value of {@link ThirdMoment} </li> |
| 36 | * <li> n = number of observations (including current obs) </li> |
| 37 | * </ul> |
| 38 | * Then </p> |
| 39 | * <p> |
| 40 | * new value = old value - 4 * (dev/n) * m3 + 6 * (dev/n)^2 * m2 + <br> |
| 41 | * [n^2 - 3 * (n-1)] * dev^4 * (n-1) / n^3 </p> |
| 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. </p> |
| 45 | * <p> |
| 46 | * <strong>Note that this implementation is not synchronized.</strong> If |
| 47 | * multiple threads access an instance of this class concurrently, and at least |
| 48 | * one of the threads invokes the <code>increment()</code> or |
| 49 | * <code>clear()</code> method, it must be synchronized externally. </p> |
| 50 | * |
| 51 | * @version $Revision: 811685 $ $Date: 2009-09-05 19:36:48 +0200 (sam. 05 sept. 2009) $ |
| 52 | */ |
| 53 | public class FourthMoment extends ThirdMoment implements Serializable{ |
| 54 | |
| 55 | /** Serializable version identifier */ |
| 56 | private static final long serialVersionUID = 4763990447117157611L; |
| 57 | |
| 58 | /** fourth moment of values that have been added */ |
| 59 | protected double m4; |
| 60 | |
| 61 | /** |
| 62 | * Create a FourthMoment instance |
| 63 | */ |
| 64 | public FourthMoment() { |
| 65 | super(); |
| 66 | m4 = Double.NaN; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Copy constructor, creates a new {@code FourthMoment} identical |
| 71 | * to the {@code original} |
| 72 | * |
| 73 | * @param original the {@code FourthMoment} instance to copy |
| 74 | */ |
| 75 | public FourthMoment(FourthMoment original) { |
| 76 | super(); |
| 77 | copy(original, this); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * {@inheritDoc} |
| 82 | */ |
| 83 | @Override |
| 84 | public void increment(final double d) { |
| 85 | if (n < 1) { |
| 86 | m4 = 0.0; |
| 87 | m3 = 0.0; |
| 88 | m2 = 0.0; |
| 89 | m1 = 0.0; |
| 90 | } |
| 91 | |
| 92 | double prevM3 = m3; |
| 93 | double prevM2 = m2; |
| 94 | |
| 95 | super.increment(d); |
| 96 | |
| 97 | double n0 = n; |
| 98 | |
| 99 | m4 = m4 - 4.0 * nDev * prevM3 + 6.0 * nDevSq * prevM2 + |
| 100 | ((n0 * n0) - 3 * (n0 -1)) * (nDevSq * nDevSq * (n0 - 1) * n0); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * {@inheritDoc} |
| 105 | */ |
| 106 | @Override |
| 107 | public double getResult() { |
| 108 | return m4; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * {@inheritDoc} |
| 113 | */ |
| 114 | @Override |
| 115 | public void clear() { |
| 116 | super.clear(); |
| 117 | m4 = Double.NaN; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * {@inheritDoc} |
| 122 | */ |
| 123 | @Override |
| 124 | public FourthMoment copy() { |
| 125 | FourthMoment result = new FourthMoment(); |
| 126 | copy(this, result); |
| 127 | return result; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Copies source to dest. |
| 132 | * <p>Neither source nor dest can be null.</p> |
| 133 | * |
| 134 | * @param source FourthMoment to copy |
| 135 | * @param dest FourthMoment to copy to |
| 136 | * @throws NullPointerException if either source or dest is null |
| 137 | */ |
| 138 | public static void copy(FourthMoment source, FourthMoment dest) { |
| 139 | ThirdMoment.copy(source, dest); |
| 140 | dest.m4 = source.m4; |
| 141 | } |
| 142 | } |