001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat.descriptive.moment;
017:
018: import java.io.Serializable;
019:
020: import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
021:
022: /**
023: * Computes the skewness of the available values.
024: * <p>
025: * We use the following (unbiased) formula to define skewness:
026: * <p>
027: * skewness = [n / (n -1) (n - 2)] sum[(x_i - mean)^3] / std^3
028: * <p>
029: * where n is the number of values, mean is the {@link Mean} and std is the
030: * {@link StandardDeviation}
031: * <p>
032: * <strong>Note that this implementation is not synchronized.</strong> If
033: * multiple threads access an instance of this class concurrently, and at least
034: * one of the threads invokes the <code>increment()</code> or
035: * <code>clear()</code> method, it must be synchronized externally.
036: *
037: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
038: */
039: public class Skewness extends AbstractStorelessUnivariateStatistic
040: implements Serializable {
041:
042: /** Serializable version identifier */
043: private static final long serialVersionUID = 7101857578996691352L;
044:
045: /** Third moment on which this statistic is based */
046: protected ThirdMoment moment = null;
047:
048: /**
049: * Determines whether or not this statistic can be incremented or cleared.
050: * <p>
051: * Statistics based on (constructed from) external moments cannot
052: * be incremented or cleared.
053: */
054: protected boolean incMoment;
055:
056: /**
057: * Constructs a Skewness
058: */
059: public Skewness() {
060: incMoment = true;
061: moment = new ThirdMoment();
062: }
063:
064: /**
065: * Constructs a Skewness with an external moment
066: * @param m3 external moment
067: */
068: public Skewness(final ThirdMoment m3) {
069: incMoment = false;
070: this .moment = m3;
071: }
072:
073: /**
074: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
075: */
076: public void increment(final double d) {
077: if (incMoment) {
078: moment.increment(d);
079: }
080: }
081:
082: /**
083: * Returns the value of the statistic based on the values that have been added.
084: * <p>
085: * See {@link Skewness} for the definition used in the computation.
086: *
087: * @return the skewness of the available values.
088: */
089: public double getResult() {
090:
091: if (moment.n < 3) {
092: return Double.NaN;
093: }
094: double variance = moment.m2 / (double) (moment.n - 1);
095: if (variance < 10E-20) {
096: return 0.0d;
097: } else {
098: double n0 = (double) moment.getN();
099: return (n0 * moment.m3)
100: / ((n0 - 1) * (n0 - 2) * Math.sqrt(variance) * variance);
101: }
102: }
103:
104: /**
105: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
106: */
107: public long getN() {
108: return moment.getN();
109: }
110:
111: /**
112: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
113: */
114: public void clear() {
115: if (incMoment) {
116: moment.clear();
117: }
118: }
119:
120: /**
121: * Returns the Skewness of the entries in the specifed portion of the
122: * input array.
123: * <p>
124: * See {@link Skewness} for the definition used in the computation.
125: * <p>
126: * Throws <code>IllegalArgumentException</code> if the array is null.
127: *
128: * @param values the input array
129: * @param begin the index of the first array element to include
130: * @param length the number of elements to include
131: * @return the skewness of the values or Double.NaN if length is less than
132: * 3
133: * @throws IllegalArgumentException if the array is null or the array index
134: * parameters are not valid
135: */
136: public double evaluate(final double[] values, final int begin,
137: final int length) {
138:
139: // Initialize the skewness
140: double skew = Double.NaN;
141:
142: if (test(values, begin, length) && length > 2) {
143: Mean mean = new Mean();
144: // Get the mean and the standard deviation
145: double m = mean.evaluate(values, begin, length);
146:
147: // Calc the std, this is implemented here instead
148: // of using the standardDeviation method eliminate
149: // a duplicate pass to get the mean
150: double accum = 0.0;
151: double accum2 = 0.0;
152: for (int i = begin; i < begin + length; i++) {
153: accum += Math.pow((values[i] - m), 2.0);
154: accum2 += (values[i] - m);
155: }
156: double stdDev = Math
157: .sqrt((accum - (Math.pow(accum2, 2) / ((double) length)))
158: / (double) (length - 1));
159:
160: double accum3 = 0.0;
161: for (int i = begin; i < begin + length; i++) {
162: accum3 += Math.pow(values[i] - m, 3.0d);
163: }
164: accum3 /= Math.pow(stdDev, 3.0d);
165:
166: // Get N
167: double n0 = length;
168:
169: // Calculate skewness
170: skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3;
171: }
172: return skew;
173: }
174: }
|