/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math.stat.descriptive.moment; import java.io.Serializable; import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math.util.FastMath; /** * Computes the Kurtosis of the available values. *
* We use the following (unbiased) formula to define kurtosis:
** 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)] *
* where n is the number of values, mean is the {@link Mean} and std is the * {@link StandardDeviation}
*
* Note that this statistic is undefined for n < 4. Double.Nan
* is returned when there is not sufficient data to compute the statistic.
* Note that this implementation is not synchronized. If
* multiple threads access an instance of this class concurrently, and at least
* one of the threads invokes the increment()
or
* clear()
method, it must be synchronized externally.
* Statistics based on (constructed from) external moments cannot * be incremented or cleared.
*/ protected boolean incMoment; /** * Construct a Kurtosis */ public Kurtosis() { incMoment = true; moment = new FourthMoment(); } /** * Construct a Kurtosis from an external moment * * @param m4 external Moment */ public Kurtosis(final FourthMoment m4) { incMoment = false; this.moment = m4; } /** * Copy constructor, creates a new {@code Kurtosis} identical * to the {@code original} * * @param original the {@code Kurtosis} instance to copy */ public Kurtosis(Kurtosis original) { copy(original, this); } /** * {@inheritDoc} */ @Override public void increment(final double d) { if (incMoment) { moment.increment(d); } else { throw MathRuntimeException.createIllegalStateException( LocalizedFormats.CANNOT_INCREMENT_STATISTIC_CONSTRUCTED_FROM_EXTERNAL_MOMENTS); } } /** * {@inheritDoc} */ @Override public double getResult() { double kurtosis = Double.NaN; if (moment.getN() > 3) { double variance = moment.m2 / (moment.n - 1); if (moment.n <= 3 || variance < 10E-20) { kurtosis = 0.0; } else { double n = moment.n; kurtosis = (n * (n + 1) * moment.m4 - 3 * moment.m2 * moment.m2 * (n - 1)) / ((n - 1) * (n -2) * (n -3) * variance * variance); } } return kurtosis; } /** * {@inheritDoc} */ @Override public void clear() { if (incMoment) { moment.clear(); } else { throw MathRuntimeException.createIllegalStateException( LocalizedFormats.CANNOT_CLEAR_STATISTIC_CONSTRUCTED_FROM_EXTERNAL_MOMENTS); } } /** * {@inheritDoc} */ public long getN() { return moment.getN(); } /* UnvariateStatistic Approach */ /** * Returns the kurtosis of the entries in the specified portion of the * input array. ** See {@link Kurtosis} for details on the computing algorithm.
*
* Throws IllegalArgumentException
if the array is null.
Neither source nor dest can be null.
* * @param source Kurtosis to copy * @param dest Kurtosis to copy to * @throws NullPointerException if either source or dest is null */ public static void copy(Kurtosis source, Kurtosis dest) { dest.setData(source.getDataRef()); dest.moment = source.moment.copy(); dest.incMoment = source.incMoment; } }