Java stddev std(long[] array)

Here you can find the source of std(long[] array)

Description

Returns the standard deviation of an array using the Welford's method.

License

Open Source License

Parameter

Parameter Description
array Input array

Return

Standard deviation

Declaration

public static double std(long[] array) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors://from w  ww  .  java  2 s  .c  o m
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

public class Main {
    /**
     * Returns the standard deviation of an array using the Welford's method.
     *
     * @param array Input array
     * @return Standard deviation
     * 
     */
    public static double std(long[] array) {
        if (array.length == 0)
            return 0;

        double M = 0.0;
        double S = 0.0;
        int k = 1;
        for (long value : array) {
            double tmpM = M;
            M += (value - tmpM) / k;
            S += (value - tmpM) * (value - M);
            k++;
        }

        return Math.sqrt(S / (k - 1));
    }
}

Related

  1. std(double[] arr)
  2. std(double[] array)
  3. std(final double[] vec)
  4. std(final double[] x, final int begin, final int end)
  5. std(float[][] arr)
  6. stdarr(int[] a, double avg)
  7. stddev(Collection a)
  8. stdDev(Collection coll)
  9. stddev(double sum, double sqSum, int numberValues)