Java stddev std(final double[] vec)

Here you can find the source of std(final double[] vec)

Description

Computes the standard deviation of the given values, that is the square root of the sample variance.

License

Open Source License

Parameter

Parameter Description
vec the vector to compute the standard deviation of

Return

the standard deviation of vec

Declaration

public static double std(final double[] vec) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from w  w  w . j  a v a2  s  .c o m*/
     * Computes the standard deviation of the given values, that is the square
     * root of the sample variance.
     * @param vec   the vector to compute the standard deviation of
     * @return      the standard deviation of <code>vec</code>
     */
    public static double std(final double[] vec) {
        assert vec != null;

        return Math.sqrt(var(vec));
    }

    /**
     * Computes the sample variance of the given values, that is the quadratic
     * deviation from mean.
     * @param vec   the vector to compute the variance of
     * @return      the variance of <code>vec</code>
     */
    public static double var(final double[] vec) {
        assert vec != null;

        if (vec.length == 1) {
            return 0.;
        }
        final double mean = mean(vec);
        double var = 0;
        for (double d : vec) {
            var += (d - mean) * (d - mean);
        }
        var /= (vec.length - 1);
        return var;
    }

    /**
    * Computes the mean of the elements of a vector.
    * @param vec   the vector.
    * @return       the mean of the elements of the vector.
    */
    public static double mean(final double[] vec) {
        assert vec != null;

        return sum(vec) / vec.length;
    }

    /**
    * Computes the sum of the elements of a vector.
    * @param array  the array, to be summed.
    * @return       the sum of the elements of the vector.
    */
    public static double sum(final double[] vec) {
        assert vec != null;

        double s = 0;
        for (double i : vec) {
            s += i;
        }
        return s;
    }
}

Related

  1. std(Collection dist, boolean populationStd)
  2. std(double a[])
  3. std(double[] a)
  4. std(double[] arr)
  5. std(double[] array)
  6. std(final double[] x, final int begin, final int end)
  7. std(float[][] arr)
  8. std(long[] array)
  9. stdarr(int[] a, double avg)