StandardDeviation.java

  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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.descriptive.moment;

  22. import java.io.Serializable;

  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.exception.NullArgumentException;
  25. import org.hipparchus.stat.descriptive.AbstractStorelessUnivariateStatistic;
  26. import org.hipparchus.util.FastMath;
  27. import org.hipparchus.util.MathUtils;

  28. /**
  29.  * Computes the sample standard deviation.
  30.  * <p>
  31.  * The standard deviation is the positive square root of the variance.
  32.  * This implementation wraps a {@link Variance} instance.
  33.  * <p>
  34.  * The <code>isBiasCorrected</code> property of the wrapped Variance
  35.  * instance is exposed, so that this class can be used to compute both
  36.  * the "sample standard deviation" (the square root of the bias-corrected
  37.  * "sample variance") or the "population standard deviation" (the square
  38.  * root of the non-bias-corrected "population variance").
  39.  * See {@link Variance} for more information.
  40.  * <p>
  41.  * <strong>Note that this implementation is not synchronized.</strong> If
  42.  * multiple threads access an instance of this class concurrently, and at least
  43.  * one of the threads invokes the <code>increment()</code> or
  44.  * <code>clear()</code> method, it must be synchronized externally.
  45.  */
  46. public class StandardDeviation extends AbstractStorelessUnivariateStatistic
  47.     implements Serializable {

  48.     /** Serializable version identifier */
  49.     private static final long serialVersionUID = 20150412L;

  50.     /** Wrapped Variance instance */
  51.     private final Variance variance;

  52.     /**
  53.      * Constructs a StandardDeviation.  Sets the underlying {@link Variance}
  54.      * instance's <code>isBiasCorrected</code> property to true.
  55.      */
  56.     public StandardDeviation() {
  57.         this(new Variance());
  58.     }

  59.     /**
  60.      * Constructs a StandardDeviation from an external second moment.
  61.      *
  62.      * @param m2 the external moment
  63.      */
  64.     public StandardDeviation(final SecondMoment m2) {
  65.         this(new Variance(m2));
  66.     }

  67.     /**
  68.      * Constructs a StandardDeviation with the specified value for the
  69.      * <code>isBiasCorrected</code> property.  If this property is set to
  70.      * <code>true</code>, the {@link Variance} used in computing results will
  71.      * use the bias-corrected, or "sample" formula.  See {@link Variance} for
  72.      * details.
  73.      *
  74.      * @param isBiasCorrected  whether or not the variance computation will use
  75.      * the bias-corrected formula
  76.      */
  77.     public StandardDeviation(boolean isBiasCorrected) {
  78.         this(new Variance(isBiasCorrected));
  79.     }

  80.     /**
  81.      * Constructs a StandardDeviation with the specified value for the
  82.      * <code>isBiasCorrected</code> property and the supplied external moment.
  83.      * If <code>isBiasCorrected</code> is set to <code>true</code>, the
  84.      * {@link Variance} used in computing results will use the bias-corrected,
  85.      * or "sample" formula. See {@link Variance} for details.
  86.      *
  87.      * @param isBiasCorrected  whether or not the variance computation will use
  88.      * the bias-corrected formula
  89.      * @param m2 the external moment
  90.      */
  91.     public StandardDeviation(boolean isBiasCorrected, SecondMoment m2) {
  92.         this(new Variance(isBiasCorrected, m2));
  93.     }

  94.     /**
  95.      * Create a new instance with the given variance.
  96.      * @param variance the variance to use
  97.      */
  98.     private StandardDeviation(Variance variance) {
  99.         this.variance = variance;
  100.     }

  101.     /**
  102.      * Copy constructor, creates a new {@code StandardDeviation} identical
  103.      * to the {@code original}.
  104.      *
  105.      * @param original the {@code StandardDeviation} instance to copy
  106.      * @throws NullArgumentException if original is null
  107.      */
  108.     public StandardDeviation(StandardDeviation original) throws NullArgumentException {
  109.         MathUtils.checkNotNull(original);
  110.         this.variance = original.variance.copy();
  111.     }

  112.     /** {@inheritDoc} */
  113.     @Override
  114.     public void increment(final double d) {
  115.         variance.increment(d);
  116.     }

  117.     /** {@inheritDoc} */
  118.     @Override
  119.     public long getN() {
  120.         return variance.getN();
  121.     }

  122.     /** {@inheritDoc} */
  123.     @Override
  124.     public double getResult() {
  125.         return FastMath.sqrt(variance.getResult());
  126.     }

  127.     /** {@inheritDoc} */
  128.     @Override
  129.     public void clear() {
  130.         variance.clear();
  131.     }

  132.     /**
  133.      * Returns the Standard Deviation of the entries in the specified portion of
  134.      * the input array, or <code>Double.NaN</code> if the designated subarray
  135.      * is empty.
  136.      * <p>
  137.      * Returns 0 for a single-value (i.e. length = 1) sample.
  138.      * <p>
  139.      * Does not change the internal state of the statistic.
  140.      *
  141.      * @param values the input array
  142.      * @param begin index of the first array element to include
  143.      * @param length the number of elements to include
  144.      * @return the standard deviation of the values or Double.NaN if length = 0
  145.      * @throws MathIllegalArgumentException if the array is null or the array index
  146.      *  parameters are not valid
  147.      */
  148.     @Override
  149.     public double evaluate(final double[] values, final int begin, final int length)
  150.         throws MathIllegalArgumentException  {
  151.         return FastMath.sqrt(variance.evaluate(values, begin, length));
  152.     }

  153.     /**
  154.      * Returns the Standard Deviation of the entries in the specified portion of
  155.      * the input array, using the precomputed mean value.  Returns
  156.      * <code>Double.NaN</code> if the designated subarray is empty.
  157.      * <p>
  158.      * Returns 0 for a single-value (i.e. length = 1) sample.
  159.      * <p>
  160.      * The formula used assumes that the supplied mean value is the arithmetic
  161.      * mean of the sample data, not a known population parameter.  This method
  162.      * is supplied only to save computation when the mean has already been
  163.      * computed.
  164.      * <p>
  165.      * Does not change the internal state of the statistic.
  166.      *
  167.      * @param values the input array
  168.      * @param mean the precomputed mean value
  169.      * @param begin index of the first array element to include
  170.      * @param length the number of elements to include
  171.      * @return the standard deviation of the values or Double.NaN if length = 0
  172.      * @throws MathIllegalArgumentException if the array is null or the array index
  173.      *  parameters are not valid
  174.      */
  175.     public double evaluate(final double[] values, final double mean,
  176.                            final int begin, final int length)
  177.         throws MathIllegalArgumentException  {
  178.         return FastMath.sqrt(variance.evaluate(values, mean, begin, length));
  179.     }

  180.     /**
  181.      * Returns the Standard Deviation of the entries in the input array, using
  182.      * the precomputed mean value.  Returns
  183.      * <code>Double.NaN</code> if the designated subarray is empty.
  184.      * <p>
  185.      * Returns 0 for a single-value (i.e. length = 1) sample.
  186.      * <p>
  187.      * The formula used assumes that the supplied mean value is the arithmetic
  188.      * mean of the sample data, not a known population parameter.  This method
  189.      * is supplied only to save computation when the mean has already been
  190.      * computed.
  191.      * <p>
  192.      * Does not change the internal state of the statistic.
  193.      *
  194.      * @param values the input array
  195.      * @param mean the precomputed mean value
  196.      * @return the standard deviation of the values or Double.NaN if length = 0
  197.      * @throws MathIllegalArgumentException if the array is null
  198.      */
  199.     public double evaluate(final double[] values, final double mean)
  200.         throws MathIllegalArgumentException  {
  201.         return FastMath.sqrt(variance.evaluate(values, mean));
  202.     }

  203.     /** Check if bias is corrected.
  204.      * @return Returns the isBiasCorrected.
  205.      */
  206.     public boolean isBiasCorrected() {
  207.         return variance.isBiasCorrected();
  208.     }

  209.     /**
  210.      * Returns a new copy of this standard deviation with the given
  211.      * bias correction setting.
  212.      *
  213.      * @param biasCorrection The bias correction flag to set.
  214.      * @return a copy of this instance with the given bias correction setting
  215.      */
  216.     public StandardDeviation withBiasCorrection(boolean biasCorrection) {
  217.         return new StandardDeviation(variance.withBiasCorrection(biasCorrection));
  218.     }

  219.     /** {@inheritDoc} */
  220.     @Override
  221.     public StandardDeviation copy() {
  222.         return new StandardDeviation(this);
  223.     }

  224. }