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.  *      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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.descriptive.moment;

  22. import java.io.Serializable;

  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.exception.NullArgumentException;
  25. import org.hipparchus.stat.descriptive.AbstractStorelessUnivariateStatistic;
  26. import org.hipparchus.util.FastMath;
  27. import org.hipparchus.util.MathArrays;
  28. import org.hipparchus.util.MathUtils;


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

  50.     /** Serializable version identifier */
  51.     private static final long serialVersionUID = 20150412L;

  52.     /**Fourth Moment on which this statistic is based */
  53.     protected final FourthMoment moment;

  54.     /**
  55.      * Determines whether or not this statistic can be incremented or cleared.
  56.      * <p>
  57.      * Statistics based on (constructed from) external moments cannot
  58.      * be incremented or cleared.
  59.      */
  60.     protected final boolean incMoment;

  61.     /**
  62.      * Construct a Kurtosis.
  63.      */
  64.     public Kurtosis() {
  65.         moment    = new FourthMoment();
  66.         incMoment = true;
  67.     }

  68.     /**
  69.      * Construct a Kurtosis from an external moment.
  70.      *
  71.      * @param m4 external Moment
  72.      */
  73.     public Kurtosis(final FourthMoment m4) {
  74.         this.moment = m4;
  75.         incMoment   = false;
  76.     }

  77.     /**
  78.      * Copy constructor, creates a new {@code Kurtosis} identical
  79.      * to the {@code original}.
  80.      *
  81.      * @param original the {@code Kurtosis} instance to copy
  82.      * @throws NullArgumentException if original is null
  83.      */
  84.     public Kurtosis(Kurtosis original) throws NullArgumentException {
  85.         MathUtils.checkNotNull(original);
  86.         this.moment    = original.moment.copy();
  87.         this.incMoment = original.incMoment;
  88.     }

  89.     /**
  90.      * {@inheritDoc}
  91.      * <p>Note that when {@link #Kurtosis(FourthMoment)} is used to
  92.      * create a Variance, this method does nothing. In that case, the
  93.      * FourthMoment should be incremented directly.</p>
  94.      */
  95.     @Override
  96.     public void increment(final double d) {
  97.         if (incMoment) {
  98.             moment.increment(d);
  99.         }
  100.     }

  101.     /** {@inheritDoc} */
  102.     @Override
  103.     public double getResult() {
  104.         double kurtosis = Double.NaN;
  105.         if (moment.getN() > 3) {
  106.             double variance = moment.m2 / (moment.n - 1);
  107.                 if (moment.n <= 3 || variance < 10E-20) {
  108.                     kurtosis = 0.0;
  109.                 } else {
  110.                     double n = moment.n;
  111.                     kurtosis =
  112.                         (n * (n + 1) * moment.getResult() -
  113.                                 3 * moment.m2 * moment.m2 * (n - 1)) /
  114.                                 ((n - 1) * (n -2) * (n -3) * variance * variance);
  115.                 }
  116.         }
  117.         return kurtosis;
  118.     }

  119.     /** {@inheritDoc} */
  120.     @Override
  121.     public void clear() {
  122.         if (incMoment) {
  123.             moment.clear();
  124.         }
  125.     }

  126.     /** {@inheritDoc} */
  127.     @Override
  128.     public long getN() {
  129.         return moment.getN();
  130.     }

  131.     /* UnvariateStatistic Approach  */

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

  150.         // Initialize the kurtosis
  151.         double kurt = Double.NaN;

  152.         if (MathArrays.verifyValues(values, begin, length) && length > 3) {
  153.             // Compute the mean and standard deviation
  154.             Variance variance = new Variance();
  155.             variance.incrementAll(values, begin, length);
  156.             double mean = variance.moment.m1;
  157.             double stdDev = FastMath.sqrt(variance.getResult());

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

  165.             // Get N
  166.             double n0 = length;

  167.             double coefficientOne =
  168.                 (n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3));
  169.             double termTwo =
  170.                 (3 * FastMath.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3));

  171.             // Calculate kurtosis
  172.             kurt = (coefficientOne * accum3) - termTwo;
  173.         }
  174.         return kurt;
  175.     }

  176.     /** {@inheritDoc} */
  177.     @Override
  178.     public Kurtosis copy() {
  179.         return new Kurtosis(this);
  180.     }

  181. }