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 */
017package org.apache.commons.math4.legacy.stat.descriptive.moment;
018
019import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
020import org.apache.commons.math4.legacy.exception.NullArgumentException;
021import org.apache.commons.math4.legacy.stat.descriptive.AbstractStorelessUnivariateStatistic;
022import org.apache.commons.math4.core.jdkmath.JdkMath;
023import org.apache.commons.math4.legacy.core.MathArrays;
024
025/**
026 * Computes the skewness of the available values.
027 * <p>
028 * We use the following (unbiased) formula to define skewness:</p>
029 * <p>
030 * skewness = [n / (n -1) (n - 2)] sum[(x_i - mean)^3] / std^3 </p>
031 * <p>
032 * where n is the number of values, mean is the {@link Mean} and std is the
033 * {@link StandardDeviation} </p>
034 * <p>
035 * Note that this statistic is undefined for {@code n < 3}.  <code>Double.Nan</code>
036 * is returned when there is not sufficient data to compute the statistic.
037 * Double.NaN may also be returned if the input includes NaN and / or
038 * infinite values.</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 */
045public class Skewness extends AbstractStorelessUnivariateStatistic {
046    /** The value below which the variance is considered zero and thus skewness is zero. */
047    private static final double ZERO_VARIANCE_THRESHOLD = 10E-20;
048
049    /** Third moment on which this statistic is based. */
050    protected ThirdMoment moment;
051
052     /**
053     * Determines whether or not this statistic can be incremented or cleared.
054     * <p>
055     * Statistics based on (constructed from) external moments cannot
056     * be incremented or cleared.</p>
057    */
058    protected boolean incMoment;
059
060    /**
061     * Constructs a Skewness.
062     */
063    public Skewness() {
064        incMoment = true;
065        moment = new ThirdMoment();
066    }
067
068    /**
069     * Constructs a Skewness with an external moment.
070     * @param m3 external moment
071     */
072    public Skewness(final ThirdMoment m3) {
073        incMoment = false;
074        this.moment = m3;
075    }
076
077    /**
078     * Copy constructor, creates a new {@code Skewness} identical
079     * to the {@code original}.
080     *
081     * @param original the {@code Skewness} instance to copy
082     * @throws NullArgumentException if original is null
083     */
084    public Skewness(Skewness original) throws NullArgumentException {
085        copy(original, this);
086    }
087
088    /**
089     * {@inheritDoc}
090     * <p>Note that when {@link #Skewness(ThirdMoment)} is used to
091     * create a Skewness, this method does nothing. In that case, the
092     * ThirdMoment should be incremented directly.</p>
093     */
094    @Override
095    public void increment(final double d) {
096        if (incMoment) {
097            moment.increment(d);
098        }
099    }
100
101    /**
102     * Returns the value of the statistic based on the values that have been added.
103     * <p>
104     * See {@link Skewness} for the definition used in the computation.</p>
105     *
106     * @return the skewness of the available values.
107     */
108    @Override
109    public double getResult() {
110
111        if (moment.n < 3) {
112            return Double.NaN;
113        }
114        double variance = moment.m2 / (moment.n - 1);
115        if (variance < ZERO_VARIANCE_THRESHOLD) {
116            return 0.0d;
117        } else {
118            double n0 = moment.getN();
119            return  (n0 * moment.m3) /
120            ((n0 - 1) * (n0 -2) * JdkMath.sqrt(variance) * variance);
121        }
122    }
123
124    /**
125     * {@inheritDoc}
126     */
127    @Override
128    public long getN() {
129        return moment.getN();
130    }
131
132    /**
133     * {@inheritDoc}
134     */
135    @Override
136    public void clear() {
137        if (incMoment) {
138            moment.clear();
139        }
140    }
141
142    /**
143     * Returns the Skewness of the entries in the specified portion of the
144     * input array.
145     * <p>
146     * See {@link Skewness} for the definition used in the computation.</p>
147     * <p>
148     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
149     *
150     * @param values the input array
151     * @param begin the index of the first array element to include
152     * @param length the number of elements to include
153     * @return the skewness of the values or Double.NaN if length is less than 3
154     * @throws MathIllegalArgumentException if the array is null or the array index
155     *  parameters are not valid
156     */
157    @Override
158    public double evaluate(final double[] values,final int begin, final int length)
159        throws MathIllegalArgumentException {
160
161        // Initialize the skewness
162        double skew = Double.NaN;
163
164        if (MathArrays.verifyValues(values, begin, length) && length > 2 ) {
165            Mean mean = new Mean();
166            // Get the mean and the standard deviation
167            double m = mean.evaluate(values, begin, length);
168
169            // Calc the std, this is implemented here instead
170            // of using the standardDeviation method eliminate
171            // a duplicate pass to get the mean
172            double accum = 0.0;
173            double accum2 = 0.0;
174            for (int i = begin; i < begin + length; i++) {
175                final double d = values[i] - m;
176                accum  += d * d;
177                accum2 += d;
178            }
179            final double variance = (accum - (accum2 * accum2 / length)) / (length - 1);
180            if (variance < ZERO_VARIANCE_THRESHOLD) {
181                skew = 0.0d;
182            } else {
183                double accum3 = 0.0;
184                for (int i = begin; i < begin + length; i++) {
185                    final double d = values[i] - m;
186                    accum3 += d * d * d;
187                }
188                accum3 /= variance * JdkMath.sqrt(variance);
189
190                // Get N
191                double n0 = length;
192
193                // Calculate skewness
194                skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3;
195            }
196        }
197        return skew;
198    }
199
200    /**
201     * {@inheritDoc}
202     */
203    @Override
204    public Skewness copy() {
205        Skewness result = new Skewness();
206        // No try-catch or advertised exception because args are guaranteed non-null
207        copy(this, result);
208        return result;
209    }
210
211    /**
212     * Copies source to dest.
213     * <p>Neither source nor dest can be null.</p>
214     *
215     * @param source Skewness to copy
216     * @param dest Skewness to copy to
217     * @throws NullArgumentException if either source or dest is null
218     */
219    public static void copy(Skewness source, Skewness dest)
220        throws NullArgumentException {
221        NullArgumentException.check(source);
222        NullArgumentException.check(dest);
223        dest.moment = new ThirdMoment(source.moment.copy());
224        dest.incMoment = source.incMoment;
225    }
226}