Java stddev std(float[][] arr)

Here you can find the source of std(float[][] arr)

Description

Calculate the standard deviation of a 2D array.

License

Open Source License

Parameter

Parameter Description
arr a parameter

Return

the standard deviation

Declaration

public static float std(float[][] arr) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w w  w .  j av  a2s  .  c om
     * Calculate the standard deviation of a 2D array. Calls
     * {@link FloatArrayStatsUtils#var(float[][])} and does a Math.sqrt.
     * 
     * @param arr
     * @return the standard deviation
     */
    public static float std(float[][] arr) {
        return (float) Math.sqrt(var(arr));
    }

    /**
     * Calculate the standard deviation of a 1D array. Calls
     * {@link FloatArrayStatsUtils#var(float[])} and does a Math.sqrt.
     * 
     * @param arr
     * @return the standard deviation
     */
    public static float std(float[] arr) {
        return (float) Math.sqrt(var(arr));
    }

    /**
     * Calculate the variance of a one dimensional float array. If the length of
     * the array is less than 2, variance is 0.
     * 
     * @param arr
     * @return the variance
     */
    public static float var(float[] arr) {
        if (arr.length < 2) {
            return 0;
        }

        int count = 1;
        float oldMean = arr[0];
        float newMean = arr[0];
        float var = 0;

        for (int i = 1; i < arr.length; i++) {
            count++;
            final float x = arr[i];
            newMean = oldMean + (x - oldMean) / count;
            var = var + (x - oldMean) * (x - newMean);
            oldMean = newMean;
        }

        return var / (count - 1);
    }

    /**
     * Calculate the variance of a one dimensional float array. If the length of
     * the array is less than 2, variance is 0.
     * 
     * @param arr
     * @return the variance
     */
    public static float var(float[][] arr) {
        if (arr.length == 0) {
            return 0;
        }

        int firstRowIndex = 0;
        while (arr[firstRowIndex].length == 0)
            firstRowIndex++;
        int firstColIndex = 1;

        int count = 1;
        float oldMean = arr[firstRowIndex][0];
        float newMean = arr[firstRowIndex][0];
        float var = 0;

        for (int i = firstRowIndex; i < arr.length; i++) {
            for (int j = firstColIndex; j < arr[i].length; j++) {
                count++;
                final float x = arr[i][j];
                newMean = oldMean + (x - oldMean) / count;
                var = var + (x - oldMean) * (x - newMean);
                oldMean = newMean;
            }
            firstColIndex = 0;
        }

        return count > 1 ? var / (count - 1) : 0;
    }
}

Related

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