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.MathIllegalArgumentException; 27 import org.hipparchus.exception.NullArgumentException; 28 import org.hipparchus.stat.descriptive.AbstractStorelessUnivariateStatistic; 29 import org.hipparchus.util.FastMath; 30 import org.hipparchus.util.MathArrays; 31 import org.hipparchus.util.MathUtils; 32 33 34 /** 35 * Computes the Kurtosis of the available values. 36 * <p> 37 * We use the following (unbiased) formula to define kurtosis: 38 * <p> 39 * kurtosis = { [n(n+1) / (n -1)(n - 2)(n-3)] sum[(x_i - mean)^4] / std^4 } - [3(n-1)^2 / (n-2)(n-3)] 40 * <p> 41 * where n is the number of values, mean is the {@link Mean} and std is the 42 * {@link StandardDeviation}. 43 * <p> 44 * Note that this statistic is undefined for n < 4. <code>Double.Nan</code> 45 * is returned when there is not sufficient data to compute the statistic. 46 * Note that Double.NaN may also be returned if the input includes NaN 47 * and / or infinite values. 48 * <p> 49 * <strong>Note that this implementation is not synchronized.</strong> If 50 * multiple threads access an instance of this class concurrently, and at least 51 * one of the threads invokes the <code>increment()</code> or 52 * <code>clear()</code> method, it must be synchronized externally. 53 */ 54 public class Kurtosis extends AbstractStorelessUnivariateStatistic implements Serializable { 55 56 /** Serializable version identifier */ 57 private static final long serialVersionUID = 20150412L; 58 59 /**Fourth Moment on which this statistic is based */ 60 protected final FourthMoment moment; 61 62 /** 63 * Determines whether or not this statistic can be incremented or cleared. 64 * <p> 65 * Statistics based on (constructed from) external moments cannot 66 * be incremented or cleared. 67 */ 68 protected final boolean incMoment; 69 70 /** 71 * Construct a Kurtosis. 72 */ 73 public Kurtosis() { 74 moment = new FourthMoment(); 75 incMoment = true; 76 } 77 78 /** 79 * Construct a Kurtosis from an external moment. 80 * 81 * @param m4 external Moment 82 */ 83 public Kurtosis(final FourthMoment m4) { 84 this.moment = m4; 85 incMoment = false; 86 } 87 88 /** 89 * Copy constructor, creates a new {@code Kurtosis} identical 90 * to the {@code original}. 91 * 92 * @param original the {@code Kurtosis} instance to copy 93 * @throws NullArgumentException if original is null 94 */ 95 public Kurtosis(Kurtosis original) throws NullArgumentException { 96 MathUtils.checkNotNull(original); 97 this.moment = original.moment.copy(); 98 this.incMoment = original.incMoment; 99 } 100 101 /** 102 * {@inheritDoc} 103 * <p>Note that when {@link #Kurtosis(FourthMoment)} is used to 104 * create a Variance, this method does nothing. In that case, the 105 * FourthMoment should be incremented directly.</p> 106 */ 107 @Override 108 public void increment(final double d) { 109 if (incMoment) { 110 moment.increment(d); 111 } 112 } 113 114 /** {@inheritDoc} */ 115 @Override 116 public double getResult() { 117 double kurtosis = Double.NaN; 118 if (moment.getN() > 3) { 119 double variance = moment.m2 / (moment.n - 1); 120 if (moment.n <= 3 || variance < 10E-20) { 121 kurtosis = 0.0; 122 } else { 123 double n = moment.n; 124 kurtosis = 125 (n * (n + 1) * moment.getResult() - 126 3 * moment.m2 * moment.m2 * (n - 1)) / 127 ((n - 1) * (n -2) * (n -3) * variance * variance); 128 } 129 } 130 return kurtosis; 131 } 132 133 /** {@inheritDoc} */ 134 @Override 135 public void clear() { 136 if (incMoment) { 137 moment.clear(); 138 } 139 } 140 141 /** {@inheritDoc} */ 142 @Override 143 public long getN() { 144 return moment.getN(); 145 } 146 147 /* UnvariateStatistic Approach */ 148 149 /** 150 * Returns the kurtosis of the entries in the specified portion of the 151 * input array. 152 * <p> 153 * See {@link Kurtosis} for details on the computing algorithm.</p> 154 * <p> 155 * Throws <code>IllegalArgumentException</code> if the array is null.</p> 156 * 157 * @param values the input array 158 * @param begin index of the first array element to include 159 * @param length the number of elements to include 160 * @return the kurtosis of the values or Double.NaN if length is less than 4 161 * @throws MathIllegalArgumentException if the input array is null or the array 162 * index parameters are not valid 163 */ 164 @Override 165 public double evaluate(final double[] values, final int begin, final int length) 166 throws MathIllegalArgumentException { 167 168 // Initialize the kurtosis 169 double kurt = Double.NaN; 170 171 if (MathArrays.verifyValues(values, begin, length) && length > 3) { 172 // Compute the mean and standard deviation 173 Variance variance = new Variance(); 174 variance.incrementAll(values, begin, length); 175 double mean = variance.moment.m1; 176 double stdDev = FastMath.sqrt(variance.getResult()); 177 178 // Sum the ^4 of the distance from the mean divided by the 179 // standard deviation 180 double accum3 = 0.0; 181 for (int i = begin; i < begin + length; i++) { 182 accum3 += FastMath.pow(values[i] - mean, 4.0); 183 } 184 accum3 /= FastMath.pow(stdDev, 4.0d); 185 186 // Get N 187 double n0 = length; 188 189 double coefficientOne = 190 (n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3)); 191 double termTwo = 192 (3 * FastMath.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3)); 193 194 // Calculate kurtosis 195 kurt = (coefficientOne * accum3) - termTwo; 196 } 197 return kurt; 198 } 199 200 /** {@inheritDoc} */ 201 @Override 202 public Kurtosis copy() { 203 return new Kurtosis(this); 204 } 205 206 }