1 /*
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17 package org.apache.commons.statistics.descriptive;
18
19 /**
20 * Computes the skewness of the available values. The skewness is defined as:
21 *
22 * <p>\[ \gamma_1 = \operatorname{E}\left[ \left(\frac{X-\mu}{\sigma}\right)^3 \right] = \frac{\mu_3}{\sigma^3} \]
23 *
24 * <p>where \( \mu \) is the mean of \( X \), \( \sigma \) is the standard deviation of \( X \),
25 * \( \operatorname{E} \) represents the expectation operator, and \( \mu_3 \) is the third
26 * central moment.
27 *
28 * <p>The default implementation uses the following definition of the <em>sample skewness</em>:
29 *
30 * <p>\[ G_1 = \frac{k_3}{k_2^{3/2}} = \frac{\sqrt{n(n-1)}}{n-2}\; g_1 = \frac{n^2}{(n-1)(n-2)}\;
31 * \frac{\tfrac{1}{n} \sum_{i=1}^n (x_i-\overline{x})^3}
32 * {\left[\tfrac{1}{n-1} \sum_{i=1}^n (x_i-\overline{x})^2 \right]^{3/2}} \]
33 *
34 * <p>where \( k_3 \) is the unique symmetric unbiased estimator of the third cumulant,
35 * \( k_2 \) is the symmetric unbiased estimator of the second cumulant (i.e. the <em>sample variance</em>),
36 * \( g_1 \) is a method of moments estimator (see below), \( \overline{x} \) is the sample mean,
37 * and \( n \) is the number of samples.
38 *
39 * <ul>
40 * <li>The result is {@code NaN} if less than 3 values are added.
41 * <li>The result is {@code NaN} if any of the values is {@code NaN} or infinite.
42 * <li>The result is {@code NaN} if the sum of the cubed deviations from the mean is infinite.
43 * </ul>
44 *
45 * <p>The default computation is for the adjusted Fisher–Pearson standardized moment coefficient
46 * \( G_1 \). If the {@link #setBiased(boolean) biased} option is enabled the following equation
47 * applies:
48 *
49 * <p>\[ g_1 = \frac{m_3}{m_2^{3/2}} = \frac{\tfrac{1}{n} \sum_{i=1}^n (x_i-\overline{x})^3}
50 * {\left[\tfrac{1}{n} \sum_{i=1}^n (x_i-\overline{x})^2 \right]^{3/2}} \]
51 *
52 * <p>where \( g_2 \) is a method of moments estimator,
53 * \( m_3 \) is the (biased) sample third central moment and \( m_2^{3/2} \) is the
54 * (biased) sample second central moment.
55 * <p>In this case the computation only requires 2 values are added (i.e. the result is
56 * {@code NaN} if less than 2 values are added).
57 *
58 * <p>Note that the computation requires division by the second central moment \( m_2 \).
59 * If this is effectively zero then the result is {@code NaN}. This occurs when the value
60 * \( m_2 \) approaches the machine precision of the mean: \( m_2 \le (m_1 \times 10^{-15})^2 \).
61 *
62 * <p>The {@link #accept(double)} method uses a recursive updating algorithm.
63 *
64 * <p>The {@link #of(double...)} method uses a two-pass algorithm, starting with computation
65 * of the mean, and then computing the sum of deviations in a second pass.
66 *
67 * <p>Note that adding values using {@link #accept(double) accept} and then executing
68 * {@link #getAsDouble() getAsDouble} will
69 * sometimes give a different result than executing
70 * {@link #of(double...) of} with the full array of values. The former approach
71 * should only be used when the full array of values is not available.
72 *
73 * <p>Supports up to 2<sup>63</sup> (exclusive) observations.
74 * This implementation does not check for overflow of the count.
75 *
76 * <p>This class is designed to work with (though does not require)
77 * {@linkplain java.util.stream streams}.
78 *
79 * <p><strong>Note that this instance is not synchronized.</strong> If
80 * multiple threads access an instance of this class concurrently, and at least
81 * one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or
82 * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
83 *
84 * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept}
85 * and {@link StatisticAccumulator#combine(StatisticResult) combine}
86 * as {@code accumulator} and {@code combiner} functions of
87 * {@link java.util.stream.Collector Collector} on a parallel stream,
88 * because the parallel instance of {@link java.util.stream.Stream#collect Stream.collect()}
89 * provides the necessary partitioning, isolation, and merging of results for
90 * safe and efficient parallel execution.
91 *
92 * @see <a href="https://en.wikipedia.org/wiki/Skewness">Skewness (Wikipedia)</a>
93 * @since 1.1
94 */
95 public final class Skewness implements DoubleStatistic, StatisticAccumulator<Skewness> {
96 /** 2, the length limit where the biased skewness is undefined.
97 * This limit effectively imposes the result m3 / m2^1.5 = 0 / 0 = NaN when 1 value
98 * has been added. Note that when more samples are added and the variance
99 * approaches zero the result is also returned as NaN. */
100 private static final int LENGTH_TWO = 2;
101 /** 3, the length limit where the unbiased skewness is undefined. */
102 private static final int LENGTH_THREE = 3;
103
104 /**
105 * An instance of {@link SumOfCubedDeviations}, which is used to
106 * compute the skewness.
107 */
108 private final SumOfCubedDeviations sc;
109
110 /** Flag to control if the statistic is biased, or should use a bias correction. */
111 private boolean biased;
112
113 /**
114 * Create an instance.
115 */
116 private Skewness() {
117 this(new SumOfCubedDeviations());
118 }
119
120 /**
121 * Creates an instance with the sum of cubed deviations from the mean.
122 *
123 * @param sc Sum of cubed deviations.
124 */
125 Skewness(SumOfCubedDeviations sc) {
126 this.sc = sc;
127 }
128
129 /**
130 * Creates an instance.
131 *
132 * <p>The initial result is {@code NaN}.
133 *
134 * @return {@code Skewness} instance.
135 */
136 public static Skewness create() {
137 return new Skewness();
138 }
139
140 /**
141 * Returns an instance populated using the input {@code values}.
142 *
143 * <p>Note: {@code Skewness} computed using {@link #accept(double) accept} may be
144 * different from this instance.
145 *
146 * @param values Values.
147 * @return {@code Skewness} instance.
148 */
149 public static Skewness of(double... values) {
150 return new Skewness(SumOfCubedDeviations.of(values));
151 }
152
153 /**
154 * Returns an instance populated using the specified range of {@code values}.
155 *
156 * <p>Note: {@code Skewness} computed using {@link #accept(double) accept} may be
157 * different from this instance.
158 *
159 * @param values Values.
160 * @param from Inclusive start of the range.
161 * @param to Exclusive end of the range.
162 * @return {@code Skewness} instance.
163 * @throws IndexOutOfBoundsException if the sub-range is out of bounds
164 * @since 1.2
165 */
166 public static Skewness ofRange(double[] values, int from, int to) {
167 Statistics.checkFromToIndex(from, to, values.length);
168 return new Skewness(SumOfCubedDeviations.ofRange(values, from, to));
169 }
170
171 /**
172 * Returns an instance populated using the input {@code values}.
173 *
174 * <p>Note: {@code Skewness} computed using {@link #accept(double) accept} may be
175 * different from this instance.
176 *
177 * @param values Values.
178 * @return {@code Skewness} instance.
179 */
180 public static Skewness of(int... values) {
181 return new Skewness(SumOfCubedDeviations.of(values));
182 }
183
184 /**
185 * Returns an instance populated using the specified range of {@code values}.
186 *
187 * <p>Note: {@code Skewness} computed using {@link #accept(double) accept} may be
188 * different from this instance.
189 *
190 * @param values Values.
191 * @param from Inclusive start of the range.
192 * @param to Exclusive end of the range.
193 * @return {@code Skewness} instance.
194 * @throws IndexOutOfBoundsException if the sub-range is out of bounds
195 * @since 1.2
196 */
197 public static Skewness ofRange(int[] values, int from, int to) {
198 Statistics.checkFromToIndex(from, to, values.length);
199 return new Skewness(SumOfCubedDeviations.ofRange(values, from, to));
200 }
201
202 /**
203 * Returns an instance populated using the input {@code values}.
204 *
205 * <p>Note: {@code Skewness} computed using {@link #accept(double) accept} may be
206 * different from this instance.
207 *
208 * @param values Values.
209 * @return {@code Skewness} instance.
210 */
211 public static Skewness of(long... values) {
212 return new Skewness(SumOfCubedDeviations.of(values));
213 }
214
215 /**
216 * Returns an instance populated using the specified range of {@code values}.
217 *
218 * <p>Note: {@code Skewness} computed using {@link #accept(double) accept} may be
219 * different from this instance.
220 *
221 * @param values Values.
222 * @param from Inclusive start of the range.
223 * @param to Exclusive end of the range.
224 * @return {@code Skewness} instance.
225 * @throws IndexOutOfBoundsException if the sub-range is out of bounds
226 * @since 1.2
227 */
228 public static Skewness ofRange(long[] values, int from, int to) {
229 Statistics.checkFromToIndex(from, to, values.length);
230 return new Skewness(SumOfCubedDeviations.ofRange(values, from, to));
231 }
232
233 /**
234 * Updates the state of the statistic to reflect the addition of {@code value}.
235 *
236 * @param value Value.
237 */
238 @Override
239 public void accept(double value) {
240 sc.accept(value);
241 }
242
243 /**
244 * Gets the skewness of all input values.
245 *
246 * <p>When fewer than 3 values have been added, the result is {@code NaN}.
247 *
248 * @return skewness of all values.
249 */
250 @Override
251 public double getAsDouble() {
252 // This method checks the sum of squared or cubed deviations is finite
253 // and the value of the biased variance
254 // to provide a consistent result when the computation is not possible.
255
256 if (sc.n < (biased ? LENGTH_TWO : LENGTH_THREE)) {
257 return Double.NaN;
258 }
259 final double x2 = sc.getSumOfSquaredDeviations();
260 if (!Double.isFinite(x2)) {
261 return Double.NaN;
262 }
263 final double x3 = sc.getSumOfCubedDeviations();
264 if (!Double.isFinite(x3)) {
265 return Double.NaN;
266 }
267 // Avoid a divide by zero; for a negligible variance return NaN.
268 // Note: Commons Math returns zero if variance is < 1e-19.
269 final double m2 = x2 / sc.n;
270 if (Statistics.zeroVariance(sc.getFirstMoment(), m2)) {
271 return Double.NaN;
272 }
273 // denom = pow(m2, 1.5)
274 final double denom = Math.sqrt(m2) * m2;
275 final double m3 = x3 / sc.n;
276 double g1 = m3 / denom;
277 if (!biased) {
278 final double n = sc.n;
279 g1 *= Math.sqrt(n * (n - 1)) / (n - 2);
280 }
281 return g1;
282 }
283
284 @Override
285 public Skewness combine(Skewness other) {
286 sc.combine(other.sc);
287 return this;
288 }
289
290 /**
291 * Sets the value of the biased flag. The default value is {@code false}.
292 * See {@link Skewness} for details on the computing algorithm.
293 *
294 * <p>This flag only controls the final computation of the statistic. The value of this flag
295 * will not affect compatibility between instances during a {@link #combine(Skewness) combine}
296 * operation.
297 *
298 * @param v Value.
299 * @return {@code this} instance
300 */
301 public Skewness setBiased(boolean v) {
302 biased = v;
303 return this;
304 }
305 }