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 sample standard deviation. The standard deviation
25
* is the positive square root of the variance. This implementation wraps a
26
* {@link Variance} instance. The <code>isBiasCorrected</code> property of the
27
* wrapped Variance instance is exposed, so that this class can be used to
28
* compute both the "sample standard deviation" (the square root of the
29
* bias-corrected "sample variance") or the "population standard deviation"
30
* (the square root of the non-bias-corrected "population variance"). See
31
* {@link Variance} for more information.
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: 762116 $ $Date: 2009-04-05 12:48:53 -0400 (Sun, 05 Apr 2009) $
40
public class StandardDeviation extends AbstractStorelessUnivariateStatistic
41
implements Serializable {
43
/** Serializable version identifier */
44
private static final long serialVersionUID = 5728716329662425188L;
46
/** Wrapped Variance instance */
47
private Variance variance = null;
50
* Constructs a StandardDeviation. Sets the underlying {@link Variance}
51
* instance's <code>isBiasCorrected</code> property to true.
53
public StandardDeviation() {
54
variance = new Variance();
58
* Constructs a StandardDeviation from an external second moment.
60
* @param m2 the external moment
62
public StandardDeviation(final SecondMoment m2) {
63
variance = new Variance(m2);
67
* Copy constructor, creates a new {@code StandardDeviation} identical
68
* to the {@code original}
70
* @param original the {@code StandardDeviation} instance to copy
72
public StandardDeviation(StandardDeviation original) {
77
* Contructs a StandardDeviation with the specified value for the
78
* <code>isBiasCorrected</code> property. If this property is set to
79
* <code>true</code>, the {@link Variance} used in computing results will
80
* use the bias-corrected, or "sample" formula. See {@link Variance} for
83
* @param isBiasCorrected whether or not the variance computation will use
84
* the bias-corrected formula
86
public StandardDeviation(boolean isBiasCorrected) {
87
variance = new Variance(isBiasCorrected);
91
* Contructs a StandardDeviation with the specified value for the
92
* <code>isBiasCorrected</code> property and the supplied external moment.
93
* If <code>isBiasCorrected</code> is set to <code>true</code>, the
94
* {@link Variance} used in computing results will use the bias-corrected,
95
* or "sample" formula. See {@link Variance} for details.
97
* @param isBiasCorrected whether or not the variance computation will use
98
* the bias-corrected formula
99
* @param m2 the external moment
101
public StandardDeviation(boolean isBiasCorrected, SecondMoment m2) {
102
variance = new Variance(isBiasCorrected, m2);
109
public void increment(final double d) {
110
variance.increment(d);
117
return variance.getN();
124
public double getResult() {
125
return Math.sqrt(variance.getResult());
132
public void clear() {
137
* Returns the Standard Deviation of the entries in the input array, or
138
* <code>Double.NaN</code> if the array is empty.
140
* Returns 0 for a single-value (i.e. length = 1) sample.</p>
142
* Throws <code>IllegalArgumentException</code> if the array is null.</p>
144
* Does not change the internal state of the statistic.</p>
146
* @param values the input array
147
* @return the standard deviation of the values or Double.NaN if length = 0
148
* @throws IllegalArgumentException if the array is null
151
public double evaluate(final double[] values) {
152
return Math.sqrt(variance.evaluate(values));
156
* Returns the Standard Deviation of the entries in the specified portion of
157
* the input array, or <code>Double.NaN</code> if the designated subarray
160
* Returns 0 for a single-value (i.e. length = 1) sample. </p>
162
* Throws <code>IllegalArgumentException</code> if the array is null.</p>
164
* Does not change the internal state of the statistic.</p>
166
* @param values the input array
167
* @param begin index of the first array element to include
168
* @param length the number of elements to include
169
* @return the standard deviation of the values or Double.NaN if length = 0
170
* @throws IllegalArgumentException if the array is null or the array index
171
* parameters are not valid
174
public double evaluate(final double[] values, final int begin, final int length) {
175
return Math.sqrt(variance.evaluate(values, begin, length));
179
* Returns the Standard Deviation of the entries in the specified portion of
180
* the input array, using the precomputed mean value. Returns
181
* <code>Double.NaN</code> if the designated subarray is empty.
183
* Returns 0 for a single-value (i.e. length = 1) sample.</p>
185
* The formula used assumes that the supplied mean value is the arithmetic
186
* mean of the sample data, not a known population parameter. This method
187
* is supplied only to save computation when the mean has already been
190
* Throws <code>IllegalArgumentException</code> if the array is null.</p>
192
* Does not change the internal state of the statistic.</p>
194
* @param values the input array
195
* @param mean the precomputed mean value
196
* @param begin index of the first array element to include
197
* @param length the number of elements to include
198
* @return the standard deviation of the values or Double.NaN if length = 0
199
* @throws IllegalArgumentException if the array is null or the array index
200
* parameters are not valid
202
public double evaluate(final double[] values, final double mean,
203
final int begin, final int length) {
204
return Math.sqrt(variance.evaluate(values, mean, begin, length));
208
* Returns the Standard Deviation of the entries in the input array, using
209
* the precomputed mean value. Returns
210
* <code>Double.NaN</code> if the designated subarray is empty.
212
* Returns 0 for a single-value (i.e. length = 1) sample.</p>
214
* The formula used assumes that the supplied mean value is the arithmetic
215
* mean of the sample data, not a known population parameter. This method
216
* is supplied only to save computation when the mean has already been
219
* Throws <code>IllegalArgumentException</code> if the array is null.</p>
221
* Does not change the internal state of the statistic.</p>
223
* @param values the input array
224
* @param mean the precomputed mean value
225
* @return the standard deviation of the values or Double.NaN if length = 0
226
* @throws IllegalArgumentException if the array is null
228
public double evaluate(final double[] values, final double mean) {
229
return Math.sqrt(variance.evaluate(values, mean));
233
* @return Returns the isBiasCorrected.
235
public boolean isBiasCorrected() {
236
return variance.isBiasCorrected();
240
* @param isBiasCorrected The isBiasCorrected to set.
242
public void setBiasCorrected(boolean isBiasCorrected) {
243
variance.setBiasCorrected(isBiasCorrected);
250
public StandardDeviation copy() {
251
StandardDeviation result = new StandardDeviation();
258
* Copies source to dest.
259
* <p>Neither source nor dest can be null.</p>
261
* @param source StandardDeviation to copy
262
* @param dest StandardDeviation to copy to
263
* @throws NullPointerException if either source or dest is null
265
public static void copy(StandardDeviation source, StandardDeviation dest) {
266
dest.variance = source.variance.copy();