1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.stat.descriptive.moment;
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.io.Serializable;
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.MathRuntimeException;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.LocalizedFormats;
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.FastMath;
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Computes the Kurtosis of the available values.
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * We use the following (unbiased) formula to define kurtosis:</p>
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *  <p>
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *  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)]
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *  </p><p>
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *  where n is the number of values, mean is the {@link Mean} and std is the
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * {@link StandardDeviation}</p>
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *  Note that this statistic is undefined for n < 4.  <code>Double.Nan</code>
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *  is returned when there is not sufficient data to compute the statistic.</p>
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <strong>Note that this implementation is not synchronized.</strong> If
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * multiple threads access an instance of this class concurrently, and at least
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * one of the threads invokes the <code>increment()</code> or
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <code>clear()</code> method, it must be synchronized externally.</p>
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1006299 $ $Date: 2010-10-10 16:47:17 +0200 (dim. 10 oct. 2010) $
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class Kurtosis extends AbstractStorelessUnivariateStatistic  implements Serializable {
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Serializable version identifier */
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final long serialVersionUID = 2784465764798260919L;
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**Fourth Moment on which this statistic is based */
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    protected FourthMoment moment;
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Determines whether or not this statistic can be incremented or cleared.
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Statistics based on (constructed from) external moments cannot
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * be incremented or cleared.</p>
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    */
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    protected boolean incMoment;
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Construct a Kurtosis
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Kurtosis() {
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        incMoment = true;
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        moment = new FourthMoment();
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Construct a Kurtosis from an external moment
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param m4 external Moment
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Kurtosis(final FourthMoment m4) {
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        incMoment = false;
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.moment = m4;
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Copy constructor, creates a new {@code Kurtosis} identical
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * to the {@code original}
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param original the {@code Kurtosis} instance to copy
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Kurtosis(Kurtosis original) {
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        copy(original, this);
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public void increment(final double d) {
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (incMoment) {
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            moment.increment(d);
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }  else  {
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalStateException(
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    LocalizedFormats.CANNOT_INCREMENT_STATISTIC_CONSTRUCTED_FROM_EXTERNAL_MOMENTS);
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double getResult() {
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double kurtosis = Double.NaN;
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (moment.getN() > 3) {
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double variance = moment.m2 / (moment.n - 1);
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                if (moment.n <= 3 || variance < 10E-20) {
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    kurtosis = 0.0;
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                } else {
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    double n = moment.n;
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    kurtosis =
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                        (n * (n + 1) * moment.m4 -
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                3 * moment.m2 * moment.m2 * (n - 1)) /
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                ((n - 1) * (n -2) * (n -3) * variance * variance);
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                }
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return kurtosis;
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public void clear() {
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (incMoment) {
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            moment.clear();
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        } else  {
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalStateException(
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                    LocalizedFormats.CANNOT_CLEAR_STATISTIC_CONSTRUCTED_FROM_EXTERNAL_MOMENTS);
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public long getN() {
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return moment.getN();
143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /* UnvariateStatistic Approach  */
146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the kurtosis of the entries in the specified portion of the
149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * input array.
150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link Kurtosis} for details on the computing algorithm.</p>
152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the kurtosis of the values or Double.NaN if length is less than
159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * 4
160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the input array is null or the array
161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * index parameters are not valid
162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public double evaluate(final double[] values,final int begin, final int length) {
165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // Initialize the kurtosis
166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double kurt = Double.NaN;
167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (test(values, begin, length) && length > 3) {
169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // Compute the mean and standard deviation
171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            Variance variance = new Variance();
172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            variance.incrementAll(values, begin, length);
173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double mean = variance.moment.m1;
174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double stdDev = FastMath.sqrt(variance.getResult());
175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // Sum the ^4 of the distance from the mean divided by the
177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // standard deviation
178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double accum3 = 0.0;
179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            for (int i = begin; i < begin + length; i++) {
180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                accum3 += FastMath.pow(values[i] - mean, 4.0);
181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            accum3 /= FastMath.pow(stdDev, 4.0d);
183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
184dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // Get N
185dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double n0 = length;
186dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
187dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double coefficientOne =
188dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                (n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3));
189dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double termTwo =
190dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                (3 * FastMath.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3));
191dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
192dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // Calculate kurtosis
193dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            kurt = (coefficientOne * accum3) - termTwo;
194dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
195dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return kurt;
196dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
197dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
198dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
199dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * {@inheritDoc}
200dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
201dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    @Override
202dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public Kurtosis copy() {
203dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        Kurtosis result = new Kurtosis();
204dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        copy(this, result);
205dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
206dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
207dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
208dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
209dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Copies source to dest.
210dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>Neither source nor dest can be null.</p>
211dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
212dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param source Kurtosis to copy
213dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param dest Kurtosis to copy to
214dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws NullPointerException if either source or dest is null
215dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
216dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static void copy(Kurtosis source, Kurtosis dest) {
217dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.setData(source.getDataRef());
218dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.moment = source.moment.copy();
219dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        dest.incMoment = source.incMoment;
220dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
221dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
222dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
223