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.  *      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.math4.legacy.stat.descriptive.moment;

  18. import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
  19. import org.apache.commons.math4.legacy.exception.NullArgumentException;
  20. import org.apache.commons.math4.legacy.stat.descriptive.AbstractStorelessUnivariateStatistic;
  21. import org.apache.commons.math4.core.jdkmath.JdkMath;

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

  40.     /**
  41.      * Constructs a StandardDeviation.  Sets the underlying {@link Variance}
  42.      * instance's <code>isBiasCorrected</code> property to true.
  43.      */
  44.     public StandardDeviation() {
  45.         variance = new Variance();
  46.     }

  47.     /**
  48.      * Constructs a StandardDeviation from an external second moment.
  49.      *
  50.      * @param m2 the external moment
  51.      */
  52.     public StandardDeviation(final SecondMoment m2) {
  53.         variance = new Variance(m2);
  54.     }

  55.     /**
  56.      * Copy constructor, creates a new {@code StandardDeviation} identical
  57.      * to the {@code original}.
  58.      *
  59.      * @param original the {@code StandardDeviation} instance to copy
  60.      * @throws NullArgumentException if original is null
  61.      */
  62.     public StandardDeviation(StandardDeviation original) throws NullArgumentException {
  63.         copy(original, this);
  64.     }

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

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

  92.     /**
  93.      * {@inheritDoc}
  94.      */
  95.     @Override
  96.     public void increment(final double d) {
  97.         variance.increment(d);
  98.     }

  99.     /**
  100.      * {@inheritDoc}
  101.      */
  102.     @Override
  103.     public long getN() {
  104.         return variance.getN();
  105.     }

  106.     /**
  107.      * {@inheritDoc}
  108.      */
  109.     @Override
  110.     public double getResult() {
  111.         return JdkMath.sqrt(variance.getResult());
  112.     }

  113.     /**
  114.      * {@inheritDoc}
  115.      */
  116.     @Override
  117.     public void clear() {
  118.         variance.clear();
  119.     }

  120.     /**
  121.      * Returns the Standard Deviation of the entries in the input array, or
  122.      * <code>Double.NaN</code> if the array is empty.
  123.      * <p>
  124.      * Returns 0 for a single-value (i.e. length = 1) sample.</p>
  125.      * <p>
  126.      * Throws <code>MathIllegalArgumentException</code> if the array is null.</p>
  127.      * <p>
  128.      * Does not change the internal state of the statistic.</p>
  129.      *
  130.      * @param values the input array
  131.      * @return the standard deviation of the values or Double.NaN if length = 0
  132.      * @throws MathIllegalArgumentException if the array is null
  133.      */
  134.     @Override
  135.     public double evaluate(final double[] values) throws MathIllegalArgumentException  {
  136.         return JdkMath.sqrt(variance.evaluate(values));
  137.     }

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

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

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

  214.     /**
  215.      * @return Returns the isBiasCorrected.
  216.      */
  217.     public boolean isBiasCorrected() {
  218.         return variance.isBiasCorrected();
  219.     }

  220.     /**
  221.      * @param isBiasCorrected The isBiasCorrected to set.
  222.      */
  223.     public void setBiasCorrected(boolean isBiasCorrected) {
  224.         variance.setBiasCorrected(isBiasCorrected);
  225.     }

  226.     /**
  227.      * {@inheritDoc}
  228.      */
  229.     @Override
  230.     public StandardDeviation copy() {
  231.         StandardDeviation result = new StandardDeviation();
  232.         // No try-catch or advertised exception because args are guaranteed non-null
  233.         copy(this, result);
  234.         return result;
  235.     }

  236.     /**
  237.      * Copies source to dest.
  238.      * <p>Neither source nor dest can be null.</p>
  239.      *
  240.      * @param source StandardDeviation to copy
  241.      * @param dest StandardDeviation to copy to
  242.      * @throws NullArgumentException if either source or dest is null
  243.      */
  244.     public static void copy(StandardDeviation source, StandardDeviation dest) throws NullArgumentException {
  245.         NullArgumentException.check(source);
  246.         NullArgumentException.check(dest);
  247.         dest.variance = source.variance.copy();
  248.     }
  249. }