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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.stat.descriptive.moment;
19
import java.io.Serializable;
21
import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
24
* Computes the skewness of the available values.
26
* We use the following (unbiased) formula to define skewness:</p>
28
* skewness = [n / (n -1) (n - 2)] sum[(x_i - mean)^3] / std^3 </p>
30
* where n is the number of values, mean is the {@link Mean} and std is the
31
* {@link StandardDeviation} </p>
33
* <strong>Note that this implementation is not synchronized.</strong> If
34
* multiple threads access an instance of this class concurrently, and at least
35
* one of the threads invokes the <code>increment()</code> or
36
* <code>clear()</code> method, it must be synchronized externally. </p>
38
* @version $Revision: 764209 $ $Date: 2009-04-11 11:32:18 -0400 (Sat, 11 Apr 2009) $
40
public class Skewness extends AbstractStorelessUnivariateStatistic implements Serializable {
42
/** Serializable version identifier */
43
private static final long serialVersionUID = 7101857578996691352L;
45
/** Third moment on which this statistic is based */
46
protected ThirdMoment moment = null;
49
* Determines whether or not this statistic can be incremented or cleared.
51
* Statistics based on (constructed from) external moments cannot
52
* be incremented or cleared.</p>
54
protected boolean incMoment;
57
* Constructs a Skewness
61
moment = new ThirdMoment();
65
* Constructs a Skewness with an external moment
66
* @param m3 external moment
68
public Skewness(final ThirdMoment m3) {
74
* Copy constructor, creates a new {@code Skewness} identical
75
* to the {@code original}
77
* @param original the {@code Skewness} instance to copy
79
public Skewness(Skewness original) {
87
public void increment(final double d) {
94
* Returns the value of the statistic based on the values that have been added.
96
* See {@link Skewness} for the definition used in the computation.</p>
98
* @return the skewness of the available values.
101
public double getResult() {
106
double variance = moment.m2 / (moment.n - 1);
107
if (variance < 10E-20) {
110
double n0 = moment.getN();
111
return (n0 * moment.m3) /
112
((n0 - 1) * (n0 -2) * Math.sqrt(variance) * variance);
120
return moment.getN();
127
public void clear() {
134
* Returns the Skewness of the entries in the specifed portion of the
137
* See {@link Skewness} for the definition used in the computation.</p>
139
* Throws <code>IllegalArgumentException</code> if the array is null.</p>
141
* @param values the input array
142
* @param begin the index of the first array element to include
143
* @param length the number of elements to include
144
* @return the skewness of the values or Double.NaN if length is less than
146
* @throws IllegalArgumentException if the array is null or the array index
147
* parameters are not valid
150
public double evaluate(final double[] values,final int begin,
153
// Initialize the skewness
154
double skew = Double.NaN;
156
if (test(values, begin, length) && length > 2 ){
157
Mean mean = new Mean();
158
// Get the mean and the standard deviation
159
double m = mean.evaluate(values, begin, length);
161
// Calc the std, this is implemented here instead
162
// of using the standardDeviation method eliminate
163
// a duplicate pass to get the mean
166
for (int i = begin; i < begin + length; i++) {
167
accum += Math.pow((values[i] - m), 2.0);
168
accum2 += (values[i] - m);
170
double stdDev = Math.sqrt((accum - (Math.pow(accum2, 2) / length)) /
174
for (int i = begin; i < begin + length; i++) {
175
accum3 += Math.pow(values[i] - m, 3.0d);
177
accum3 /= Math.pow(stdDev, 3.0d);
182
// Calculate skewness
183
skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3;
192
public Skewness copy() {
193
Skewness result = new Skewness();
199
* Copies source to dest.
200
* <p>Neither source nor dest can be null.</p>
202
* @param source Skewness to copy
203
* @param dest Skewness to copy to
204
* @throws NullPointerException if either source or dest is null
206
public static void copy(Skewness source, Skewness dest) {
207
dest.moment = new ThirdMoment(source.moment.copy());
208
dest.incMoment = source.incMoment;