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.statistics.descriptive;
018
019/**
020 * Computes the standard deviation of the available values. The default implementations uses
021 * the following definition of the <em>sample standard deviation</em>:
022 *
023 * <p>\[ \sqrt{ \tfrac{1}{n-1} \sum_{i=1}^n (x_i-\overline{x})^2 } \]
024 *
025 * <p>where \( \overline{x} \) is the sample mean, and \( n \) is the number of samples.
026 *
027 * <ul>
028 *   <li>The result is {@code NaN} if no values are added.
029 *   <li>The result is {@code NaN} if any of the values is {@code NaN} or infinite.
030 *   <li>The result is {@code NaN} if the sum of the squared deviations from the mean is infinite.
031 *   <li>The result is zero if there is one finite value in the data set.
032 * </ul>
033 *
034 * <p>The use of the term \( n − 1 \) is called Bessel's correction. Omitting the square root,
035 * this provides an unbiased estimator of the variance of a hypothetical infinite population. If the
036 * {@link #setBiased(boolean) biased} option is enabled the normalisation factor is
037 * changed to \( \frac{1}{n} \) for a biased estimator of the <em>sample variance</em>.
038 * Note however that square root is a concave function and thus introduces negative bias
039 * (by Jensen's inequality), which depends on the distribution, and thus the corrected sample
040 * standard deviation (using Bessel's correction) is less biased, but still biased.
041 *
042 * <p>The {@link #accept(double)} method uses a recursive updating algorithm based on West's
043 * algorithm (see Chan and Lewis (1979)).
044 *
045 * <p>The {@link #of(double...)} method uses the corrected two-pass algorithm from
046 * Chan <i>et al</i>, (1983).
047 *
048 * <p>Note that adding values using {@link #accept(double) accept} and then executing
049 * {@link #getAsDouble() getAsDouble} will
050 * sometimes give a different, less accurate, result than executing
051 * {@link #of(double...) of} with the full array of values. The former approach
052 * should only be used when the full array of values is not available.
053 *
054 * <p>Supports up to 2<sup>63</sup> (exclusive) observations.
055 * This implementation does not check for overflow of the count.
056 *
057 * <p>This class is designed to work with (though does not require)
058 * {@linkplain java.util.stream streams}.
059 *
060 * <p><strong>Note that this instance is not synchronized.</strong> If
061 * multiple threads access an instance of this class concurrently, and at least
062 * one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or
063 * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
064 *
065 * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept}
066 * and {@link StatisticAccumulator#combine(StatisticResult) combine}
067 * as {@code accumulator} and {@code combiner} functions of
068 * {@link java.util.stream.Collector Collector} on a parallel stream,
069 * because the parallel instance of {@link java.util.stream.Stream#collect Stream.collect()}
070 * provides the necessary partitioning, isolation, and merging of results for
071 * safe and efficient parallel execution.
072 *
073 * <p>References:
074 * <ul>
075 *   <li>Chan and Lewis (1979)
076 *       Computing standard deviations: accuracy.
077 *       Communications of the ACM, 22, 526-531.
078 *       <a href="http://doi.acm.org/10.1145/359146.359152">doi: 10.1145/359146.359152</a>
079 *   <li>Chan, Golub and Levesque (1983)
080 *       Algorithms for Computing the Sample Variance: Analysis and Recommendations.
081 *       American Statistician, 37, 242-247.
082 *       <a href="https://doi.org/10.2307/2683386">doi: 10.2307/2683386</a>
083 * </ul>
084 *
085 * @see <a href="https://en.wikipedia.org/wiki/Standard_deviation">Standard deviation (Wikipedia)</a>
086 * @see <a href="https://en.wikipedia.org/wiki/Bessel%27s_correction">Bessel&#39;s correction</a>
087 * @see <a href="https://en.wikipedia.org/wiki/Jensen%27s_inequality">Jensen&#39;s inequality</a>
088 * @see Variance
089 * @since 1.1
090 */
091public final class StandardDeviation implements DoubleStatistic, StatisticAccumulator<StandardDeviation> {
092
093    /**
094     * An instance of {@link SumOfSquaredDeviations}, which is used to
095     * compute the standard deviation.
096     */
097    private final SumOfSquaredDeviations ss;
098
099    /** Flag to control if the statistic is biased, or should use a bias correction. */
100    private boolean biased;
101
102    /**
103     * Create an instance.
104     */
105    private StandardDeviation() {
106        this(new SumOfSquaredDeviations());
107    }
108
109    /**
110     * Creates an instance with the sum of squared deviations from the mean.
111     *
112     * @param ss Sum of squared deviations.
113     */
114    StandardDeviation(SumOfSquaredDeviations ss) {
115        this.ss = ss;
116    }
117
118    /**
119     * Creates an instance.
120     *
121     * <p>The initial result is {@code NaN}.
122     *
123     * @return {@code StandardDeviation} instance.
124     */
125    public static StandardDeviation create() {
126        return new StandardDeviation();
127    }
128
129    /**
130     * Returns an instance populated using the input {@code values}.
131     *
132     * <p>Note: {@code StandardDeviation} computed using {@link #accept(double) accept} may be
133     * different from this standard deviation.
134     *
135     * <p>See {@link StandardDeviation} for details on the computing algorithm.
136     *
137     * @param values Values.
138     * @return {@code StandardDeviation} instance.
139     */
140    public static StandardDeviation of(double... values) {
141        return new StandardDeviation(SumOfSquaredDeviations.of(values));
142    }
143
144    /**
145     * Updates the state of the statistic to reflect the addition of {@code value}.
146     *
147     * @param value Value.
148     */
149    @Override
150    public void accept(double value) {
151        ss.accept(value);
152    }
153
154    /**
155     * Gets the standard deviation of all input values.
156     *
157     * <p>When no values have been added, the result is {@code NaN}.
158     *
159     * @return standard deviation of all values.
160     */
161    @Override
162    public double getAsDouble() {
163        // This method checks the sum of squared is finite
164        // to provide a consistent NaN when the computation is not possible.
165        // Note: The SS checks for n=0 and returns NaN.
166        final double m2 = ss.getSumOfSquaredDeviations();
167        if (!Double.isFinite(m2)) {
168            return Double.NaN;
169        }
170        final long n = ss.n;
171        // Avoid a divide by zero
172        if (n == 1) {
173            return 0;
174        }
175        return biased ? Math.sqrt(m2 / n) : Math.sqrt(m2 / (n - 1));
176    }
177
178    @Override
179    public StandardDeviation combine(StandardDeviation other) {
180        ss.combine(other.ss);
181        return this;
182    }
183
184    /**
185     * Sets the value of the biased flag. The default value is {@code false}. The bias
186     * term refers to the computation of the variance; the standard deviation is returned
187     * as the square root of the biased or unbiased <em>sample variance</em>. For further
188     * details see {@link Variance#setBiased(boolean) Variance.setBiased}.
189     *
190     * <p>This flag only controls the final computation of the statistic. The value of
191     * this flag will not affect compatibility between instances during a
192     * {@link #combine(StandardDeviation) combine} operation.
193     *
194     * @param v Value.
195     * @return {@code this} instance
196     * @see Variance#setBiased(boolean)
197     */
198    public StandardDeviation setBiased(boolean v) {
199        biased = v;
200        return this;
201    }
202}