    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017    package org.apache.commons.math.stat.descriptive.moment;
    018
    019    import java.io.Serializable;
    020
    021    import org.apache.commons.math.MathRuntimeException;
    022    import org.apache.commons.math.exception.util.LocalizedFormats;
    023    import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
    024    import org.apache.commons.math.util.FastMath;
    025
    026
    027    /**
    028     * Computes the Kurtosis of the available values.
    029     * <p>
    030     * We use the following (unbiased) formula to define kurtosis:</p>
    031     *  <p>
    032     *  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)]
    033     *  </p><p>
    034     *  where n is the number of values, mean is the {@link Mean} and std is the
    035     * {@link StandardDeviation}</p>
    036     * <p>
    037     *  Note that this statistic is undefined for n < 4.  <code>Double.Nan</code>
    038     *  is returned when there is not sufficient data to compute the statistic.</p>
    039     * <p>
    040     * <strong>Note that this implementation is not synchronized.</strong> If
    041     * multiple threads access an instance of this class concurrently, and at least
    042     * one of the threads invokes the <code>increment()</code> or
    043     * <code>clear()</code> method, it must be synchronized externally.</p>
    044     *
    045     * @version $Revision: 1006299 $ $Date: 2010-10-10 16:47:17 +0200 (dim. 10 oct. 2010) $
    046     */
    047    public class Kurtosis extends AbstractStorelessUnivariateStatistic  implements Serializable {
    048
    049        /** Serializable version identifier */
    050        private static final long serialVersionUID = 2784465764798260919L;
    051
    052        /**Fourth Moment on which this statistic is based */
    053        protected FourthMoment moment;
    054
    055        /**
    056         * Determines whether or not this statistic can be incremented or cleared.
    057         * <p>
    058         * Statistics based on (constructed from) external moments cannot
    059         * be incremented or cleared.</p>
    060        */
    061        protected boolean incMoment;
    062
    063        /**
    064         * Construct a Kurtosis
    065         */
    066        public Kurtosis() {
    067            incMoment = true;
    068            moment = new FourthMoment();
    069        }
    070
    071        /**
    072         * Construct a Kurtosis from an external moment
    073         *
    074         * @param m4 external Moment
    075         */
    076        public Kurtosis(final FourthMoment m4) {
    077            incMoment = false;
    078            this.moment = m4;
    079        }
    080
    081        /**
    082         * Copy constructor, creates a new {@code Kurtosis} identical
    083         * to the {@code original}
    084         *
    085         * @param original the {@code Kurtosis} instance to copy
    086         */
    087        public Kurtosis(Kurtosis original) {
    088            copy(original, this);
    089        }
    090
    091        /**
    092         * {@inheritDoc}
    093         */
    094        @Override
    095        public void increment(final double d) {
    096            if (incMoment) {
    097                moment.increment(d);
    098            }  else  {
    099                throw MathRuntimeException.createIllegalStateException(
    100                        LocalizedFormats.CANNOT_INCREMENT_STATISTIC_CONSTRUCTED_FROM_EXTERNAL_MOMENTS);
    101            }
    102        }
    103
    104        /**
    105         * {@inheritDoc}
    106         */
    107        @Override
    108        public double getResult() {
    109            double kurtosis = Double.NaN;
    110            if (moment.getN() > 3) {
    111                double variance = moment.m2 / (moment.n - 1);
    112                    if (moment.n <= 3 || variance < 10E-20) {
    113                        kurtosis = 0.0;
    114                    } else {
    115                        double n = moment.n;
    116                        kurtosis =
    117                            (n * (n + 1) * moment.m4 -
    118                                    3 * moment.m2 * moment.m2 * (n - 1)) /
    119                                    ((n - 1) * (n -2) * (n -3) * variance * variance);
    120                    }
    121            }
    122            return kurtosis;
    123        }
    124
    125        /**
    126         * {@inheritDoc}
    127         */
    128        @Override
    129        public void clear() {
    130            if (incMoment) {
    131                moment.clear();
    132            } else  {
    133                throw MathRuntimeException.createIllegalStateException(
    134                        LocalizedFormats.CANNOT_CLEAR_STATISTIC_CONSTRUCTED_FROM_EXTERNAL_MOMENTS);
    135            }
    136        }
    137
    138        /**
    139         * {@inheritDoc}
    140         */
    141        public long getN() {
    142            return moment.getN();
    143        }
    144
    145        /* UnvariateStatistic Approach  */
    146
    147        /**
    148         * Returns the kurtosis of the entries in the specified portion of the
    149         * input array.
    150         * <p>
    151         * See {@link Kurtosis} for details on the computing algorithm.</p>
    152         * <p>
    153         * Throws <code>IllegalArgumentException</code> if the array is null.</p>
    154         *
    155         * @param values the input array
    156         * @param begin index of the first array element to include
    157         * @param length the number of elements to include
    158         * @return the kurtosis of the values or Double.NaN if length is less than
    159         * 4
    160         * @throws IllegalArgumentException if the input array is null or the array
    161         * index parameters are not valid
    162         */
    163        @Override
    164        public double evaluate(final double[] values,final int begin, final int length) {
    165            // Initialize the kurtosis
    166            double kurt = Double.NaN;
    167
    168            if (test(values, begin, length) && length > 3) {
    169
    170                // Compute the mean and standard deviation
    171                Variance variance = new Variance();
    172                variance.incrementAll(values, begin, length);
    173                double mean = variance.moment.m1;
    174                double stdDev = FastMath.sqrt(variance.getResult());
    175
    176                // Sum the ^4 of the distance from the mean divided by the
    177                // standard deviation
    178                double accum3 = 0.0;
    179                for (int i = begin; i < begin + length; i++) {
    180                    accum3 += FastMath.pow(values[i] - mean, 4.0);
    181                }
    182                accum3 /= FastMath.pow(stdDev, 4.0d);
    183
    184                // Get N
    185                double n0 = length;
    186
    187                double coefficientOne =
    188                    (n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3));
    189                double termTwo =
    190                    (3 * FastMath.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3));
    191
    192                // Calculate kurtosis
    193                kurt = (coefficientOne * accum3) - termTwo;
    194            }
    195            return kurt;
    196        }
    197
    198        /**
    199         * {@inheritDoc}
    200         */
    201        @Override
    202        public Kurtosis copy() {
    203            Kurtosis result = new Kurtosis();
    204            copy(this, result);
    205            return result;
    206        }
    207
    208        /**
    209         * Copies source to dest.
    210         * <p>Neither source nor dest can be null.</p>
    211         *
    212         * @param source Kurtosis to copy
    213         * @param dest Kurtosis to copy to
    214         * @throws NullPointerException if either source or dest is null
    215         */
    216        public static void copy(Kurtosis source, Kurtosis dest) {
    217            dest.setData(source.getDataRef());
    218            dest.moment = source.moment.copy();
    219            dest.incMoment = source.incMoment;
    220        }
    221
    222    }