Java stddev std(Collection dist, boolean populationStd)

Here you can find the source of std(Collection dist, boolean populationStd)

Description

std

License

Open Source License

Parameter

Parameter Description
dist a parameter
populationStd a parameter

Declaration

public static Double std(Collection<Double> dist, boolean populationStd) 

Method Source Code

//package com.java2s;
import java.util.Collection;

import java.util.List;

public class Main {
    /**//from   w  ww . ja v  a  2  s  .  co m
     *
     * @param stdSum
     * @param count
     * @param populationStd
     * @return
     */
    public static double std(double stdSum, int count, boolean populationStd) {
        return populationStd ? count > 0 ? Math.sqrt(stdSum / count) : null
                : count - 1 > 0 ? Math.sqrt(stdSum / (count - 1)) : null;
    }

    /**
     *
     * @param dist
     * @param populationStd
     * @return
     */
    public static Double std(Collection<Double> dist, boolean populationStd) {
        return std(mean(dist), dist, populationStd);
    }

    /**
     *
     * @param mean
     * @param dist
     * @param populationStd
     * @return
     */
    public static Double std(Double mean, Collection<Double> dist,
            boolean populationStd) {
        if (dist != null && !dist.isEmpty()) {
            mean = mean == null ? mean(dist) : mean; //First compute the mean if it's null
            double sum = 0d;
            int count = 0;
            if (mean != null) {
                for (Double val : dist) {
                    if (val != null) {
                        sum += Math.pow(val - mean, 2);
                        count++;
                    }
                }
                return populationStd ? count > 0 ? Math.sqrt(sum / count)
                        : null : count - 1 > 0 ? Math.sqrt(sum
                        / (count - 1)) : null;
            }
        }
        return null;
    }

    /**
     * @param sums
     * @param count
     */
    public static void mean(List<Double> sums, int count) {
        if (sums != null && !sums.isEmpty()) {
            for (int i = 0; i < sums.size(); i++) {
                sums.set(i, sums.get(i) / count);
            }
        }
    }

    /**
     *
     * @param sum
     * @param count
     * @return
     */
    public static double mean(double sum, int count) {
        return count > 0 ? sum / count : null;
    }

    /**
     *
     * @param dist
     * @return
     */
    public static Double mean(Collection<Double> dist) {
        if (dist != null && !dist.isEmpty()) {
            double sum = 0d;
            int count = 0;
            for (Double val : dist) {
                if (val != null) {
                    sum += val;
                    count++;
                }
            }
            return count > 0 ? sum / count : null;
        }
        return null;
    }
}

Related

  1. getStandardDeviation(double meanValue, ArrayList values)
  2. standardDeviation(double[] data)
  3. standardDeviation(double[] data, int opt)
  4. std(double a[])
  5. std(double[] a)
  6. std(double[] arr)
  7. std(double[] array)