Java mean meanAndStandardDeviation(final double[] inp, final int startIndex, final int pastEnd)

Here you can find the source of meanAndStandardDeviation(final double[] inp, final int startIndex, final int pastEnd)

Description

Calculates the mean and standard deviation of an array of double

License

Open Source License

Parameter

Parameter Description
inp floating point input array
startIndex the first index to average
pastEnd the index just past the last one to average

Return

{mean, standardDeviation}

Declaration

private static double[] meanAndStandardDeviation(final double[] inp,
        final int startIndex, final int pastEnd) 

Method Source Code

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

public class Main {
    /**/*from  w w  w.j ava 2  s. c  om*/
     * Calculates the mean and standard deviation of an array of double
     *
     * @param inp        floating point input array
     * @param startIndex the first index to average
     * @param pastEnd    the index just past the last one to average
     * @return {mean, standardDeviation}
     */
    private static double[] meanAndStandardDeviation(final double[] inp,
            final int startIndex, final int pastEnd) {
        if (startIndex < 0)
            throw new IllegalArgumentException("startIndex must be >= 0");
        if (pastEnd > inp.length)
            throw new IllegalArgumentException(
                    "pastEnd must be <= inp.length");
        if (startIndex >= pastEnd)
            throw new IllegalArgumentException(
                    "pastEnd must be > startIndex");

        double sum_y = 0.0;
        double sum_ymean = 0.0;
        double total = pastEnd - startIndex;

        for (int i = startIndex; i < pastEnd; ++i)
            sum_y += inp[i];
        double local_mean = sum_y / total;
        for (int i = startIndex; i < pastEnd; ++i) {
            double y = inp[i] - local_mean;
            sum_ymean += (y * y);
        }

        double std_dev = Math.sqrt(sum_ymean / total);
        return new double[] { local_mean, std_dev };
    }
}

Related

  1. mean(long[] values)
  2. mean(Number[] array)
  3. Mean(Object in)
  4. mean_Integer(List values)
  5. meanAbortedExecutionTime( double totalExecutionTime, int totalOps, double granuleAbortProb)
  6. meanAndVariance(double[] a, boolean useUnbiasedEstimate)
  7. meanArithmetic(LinkedList a)
  8. meanArray(double[] arr)
  9. meandiff(double[] v1, double[] v2)