Kurtosis.java

  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. import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
  19. import org.apache.commons.math4.legacy.exception.NullArgumentException;
  20. import org.apache.commons.math4.legacy.stat.descriptive.AbstractStorelessUnivariateStatistic;
  21. import org.apache.commons.math4.core.jdkmath.JdkMath;
  22. import org.apache.commons.math4.legacy.core.MathArrays;


  23. /**
  24.  * Computes the Kurtosis of the available values.
  25.  * <p>
  26.  * We use the following (unbiased) formula to define kurtosis:</p>
  27.  * <p>
  28.  * 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)]
  29.  * </p><p>
  30.  * where n is the number of values, mean is the {@link Mean} and std is the
  31.  * {@link StandardDeviation}</p>
  32.  * <p>
  33.  * Note that this statistic is undefined for {@code n < 4}.  <code>Double.Nan</code>
  34.  * is returned when there is not sufficient data to compute the statistic.
  35.  * Note that Double.NaN may also be returned if the input includes NaN
  36.  * and / or infinite values.</p>
  37.  * <p>
  38.  * <strong>Note that this implementation is not synchronized.</strong> If
  39.  * multiple threads access an instance of this class concurrently, and at least
  40.  * one of the threads invokes the <code>increment()</code> or
  41.  * <code>clear()</code> method, it must be synchronized externally.</p>
  42.  */
  43. public class Kurtosis extends AbstractStorelessUnivariateStatistic {
  44.     /**Fourth Moment on which this statistic is based. */
  45.     protected FourthMoment moment;

  46.     /**
  47.      * Determines whether or not this statistic can be incremented or cleared.
  48.      * <p>
  49.      * Statistics based on (constructed from) external moments cannot
  50.      * be incremented or cleared.</p>
  51.      */
  52.     protected boolean incMoment;

  53.     /**
  54.      * Construct a Kurtosis.
  55.      */
  56.     public Kurtosis() {
  57.         incMoment = true;
  58.         moment = new FourthMoment();
  59.     }

  60.     /**
  61.      * Construct a Kurtosis from an external moment.
  62.      *
  63.      * @param m4 external Moment
  64.      */
  65.     public Kurtosis(final FourthMoment m4) {
  66.         incMoment = false;
  67.         this.moment = m4;
  68.     }

  69.     /**
  70.      * Copy constructor, creates a new {@code Kurtosis} identical
  71.      * to the {@code original}.
  72.      *
  73.      * @param original the {@code Kurtosis} instance to copy
  74.      * @throws NullArgumentException if original is null
  75.      */
  76.     public Kurtosis(Kurtosis original) throws NullArgumentException {
  77.         copy(original, this);
  78.     }

  79.     /**
  80.      * {@inheritDoc}
  81.      * <p>Note that when {@link #Kurtosis(FourthMoment)} is used to
  82.      * create a Variance, this method does nothing. In that case, the
  83.      * FourthMoment should be incremented directly.</p>
  84.      */
  85.     @Override
  86.     public void increment(final double d) {
  87.         if (incMoment) {
  88.             moment.increment(d);
  89.         }
  90.     }

  91.     /**
  92.      * {@inheritDoc}
  93.      */
  94.     @Override
  95.     public double getResult() {
  96.         double kurtosis = Double.NaN;
  97.         if (moment.getN() > 3) {
  98.             double variance = moment.m2 / (moment.n - 1);
  99.                 if (moment.n <= 3 || variance < 10E-20) {
  100.                     kurtosis = 0.0;
  101.                 } else {
  102.                     double n = moment.n;
  103.                     kurtosis =
  104.                         (n * (n + 1) * moment.getResult() -
  105.                                 3 * moment.m2 * moment.m2 * (n - 1)) /
  106.                                 ((n - 1) * (n -2) * (n -3) * variance * variance);
  107.                 }
  108.         }
  109.         return kurtosis;
  110.     }

  111.     /**
  112.      * {@inheritDoc}
  113.      */
  114.     @Override
  115.     public void clear() {
  116.         if (incMoment) {
  117.             moment.clear();
  118.         }
  119.     }

  120.     /**
  121.      * {@inheritDoc}
  122.      */
  123.     @Override
  124.     public long getN() {
  125.         return moment.getN();
  126.     }

  127.     /* UnivariateStatistic Approach  */

  128.     /**
  129.      * Returns the kurtosis of the entries in the specified portion of the
  130.      * input array.
  131.      * <p>
  132.      * See {@link Kurtosis} for details on the computing algorithm.</p>
  133.      * <p>
  134.      * Throws <code>IllegalArgumentException</code> if the array is null.</p>
  135.      *
  136.      * @param values the input array
  137.      * @param begin index of the first array element to include
  138.      * @param length the number of elements to include
  139.      * @return the kurtosis of the values or Double.NaN if length is less than 4
  140.      * @throws MathIllegalArgumentException if the input array is null or the array
  141.      * index parameters are not valid
  142.      */
  143.     @Override
  144.     public double evaluate(final double[] values, final int begin, final int length)
  145.         throws MathIllegalArgumentException {

  146.         // Initialize the kurtosis
  147.         double kurt = Double.NaN;

  148.         if (MathArrays.verifyValues(values, begin, length) && length > 3) {
  149.             // Compute the mean and standard deviation
  150.             Variance variance = new Variance();
  151.             variance.incrementAll(values, begin, length);
  152.             double mean = variance.moment.m1;
  153.             double stdDev = JdkMath.sqrt(variance.getResult());

  154.             // Sum the ^4 of the distance from the mean divided by the
  155.             // standard deviation
  156.             double accum3 = 0.0;
  157.             for (int i = begin; i < begin + length; i++) {
  158.                 accum3 += JdkMath.pow(values[i] - mean, 4.0);
  159.             }
  160.             accum3 /= JdkMath.pow(stdDev, 4.0d);

  161.             // Get N
  162.             double n0 = length;

  163.             double coefficientOne =
  164.                 (n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3));
  165.             double termTwo =
  166.                 (3 * JdkMath.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3));

  167.             // Calculate kurtosis
  168.             kurt = (coefficientOne * accum3) - termTwo;
  169.         }
  170.         return kurt;
  171.     }

  172.     /**
  173.      * {@inheritDoc}
  174.      */
  175.     @Override
  176.     public Kurtosis copy() {
  177.         Kurtosis result = new Kurtosis();
  178.         // No try-catch because args are guaranteed non-null
  179.         copy(this, result);
  180.         return result;
  181.     }

  182.     /**
  183.      * Copies source to dest.
  184.      * <p>Neither source nor dest can be null.</p>
  185.      *
  186.      * @param source Kurtosis to copy
  187.      * @param dest Kurtosis to copy to
  188.      * @throws NullArgumentException if either source or dest is null
  189.      */
  190.     public static void copy(Kurtosis source, Kurtosis dest)
  191.         throws NullArgumentException {
  192.         NullArgumentException.check(source);
  193.         NullArgumentException.check(dest);
  194.         dest.moment = source.moment.copy();
  195.         dest.incMoment = source.incMoment;
  196.     }
  197. }