Java mean meanAndVariance(double[] a, boolean useUnbiasedEstimate)

Here you can find the source of meanAndVariance(double[] a, boolean useUnbiasedEstimate)

Description

Computes the mean and variance of the input array and returns the result as a two-element double array: {mean, variance}}, using a numerically stable algorithm described by <a href="http://www.jstor.org/stable/1266577">Welford</a>.

License

Apache License

Parameter

Parameter Description
a input array
useUnbiasedEstimate set to true to return unbiased estimate of variance

Return

two-element array whose first value is the mean of the input array and second value is the variance

Declaration

public static final double[] meanAndVariance(double[] a, boolean useUnbiasedEstimate) 

Method Source Code

//package com.java2s;
/*// ww w.j av  a2  s .  c om
 * Copyright 2014 Jon N. Marsh.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Computes the mean and variance of the input array and returns the result
     * as a two-element double array: {@code {mean, variance}}, using a
     * numerically stable algorithm described by
     * <a href="http://www.jstor.org/stable/1266577">Welford</a>.
     *
     * @param a                     input array
     * @param useUnbiasedEstimate set to true to return unbiased estimate of
     *                            variance
     * @return two-element array whose first value is the mean of the input
     *         array and second value is the variance
     */
    public static final double[] meanAndVariance(double[] a, boolean useUnbiasedEstimate) {
        return meanAndVariance(a, useUnbiasedEstimate, 0, a.length);
    }

    /**
     * Computes the mean and variance of the specified range of an array and
     * returns the result as a two-element double array:
     * {@code {mean, variance}}, using a numerically stable algorithm described
     * by <a href="http://www.jstor.org/stable/1266577">Welford</a>. No error
     * checking is performed on range limits; if the values are negative or
     * outside the range of the array, unexpected results may occur or a runtime
     * exception may be thrown.
     *
     * @param a                     input array
     * @param useUnbiasedEstimate set to true to return unbiased estimate of
     *                            variance
     * @param from                initial index of the range to compute the mean
     *                            and variance, inclusive
     * @param to                  final index of the range to compute the mean
     *                            and variance, exclusive
     * @return two-element array whose first value is the mean within the
     *         specified range of input array and second value is the variance
     *         for the specified range
     */
    public static final double[] meanAndVariance(double[] a, boolean useUnbiasedEstimate, int from, int to) {
        long n = 0;
        double mean = 0.0;
        double m2 = 0.0;

        for (int i = from; i < to; i++) {
            n++;
            double x = a[i];
            double delta = x - mean;
            mean += delta / n;
            m2 += delta * (x - mean);
        }

        double norm = useUnbiasedEstimate ? 1.0 / (n - 1) : 1.0 / n;
        return new double[] { mean, m2 * norm };
    }

    /**
     * Computes the mean and variance of the input array and returns the result
     * as a two-element double array: {@code {mean, variance}}, using a
     * numerically stable algorithm described by
     * <a href="http://www.jstor.org/stable/1266577">Welford</a>.
     *
     * @param a                     input array
     * @param useUnbiasedEstimate set to true to return unbiased estimate of
     *                            variance
     * @return two-element array whose first value is the mean of the input
     *         array and second value is the variance
     */
    public static final double[] meanAndVariance(float[] a, boolean useUnbiasedEstimate) {
        return meanAndVariance(a, useUnbiasedEstimate, 0, a.length);
    }

    /**
     * Computes the mean and variance of the specified range of an array and
     * returns the result as a two-element double array:
     * {@code {mean, variance}}, using a numerically stable algorithm described
     * by <a href="http://www.jstor.org/stable/1266577">Welford</a>. No error
     * checking is performed on range limits; if the values are negative or
     * outside the range of the array, unexpected results may occur or a runtime
     * exception may be thrown.
     *
     * @param a                     input array
     * @param useUnbiasedEstimate normalize by {@code n-1} instead of {@code n}
     * @param from                initial index of the range to compute the mean
     *                            and variance, inclusive
     * @param to                  final index of the range to compute the mean
     *                            and variance, exclusive
     * @return two-element array whose first value is the mean within the
     *         specified range of input array and second value is the variance
     *         for the specified range
     */
    public static final double[] meanAndVariance(float[] a, boolean useUnbiasedEstimate, int from, int to) {
        long n = 0;
        double mean = 0.0;
        double m2 = 0.0;

        for (int i = from; i < to; i++) {
            n++;
            double x = a[i];
            double delta = x - mean;
            mean += delta / n;
            m2 += delta * (x - mean);
        }

        double norm = useUnbiasedEstimate ? 1.0 / (n - 1) : 1.0 / n;
        return new double[] { mean, m2 * norm };
    }
}

Related

  1. mean(Number[] array)
  2. Mean(Object in)
  3. mean_Integer(List values)
  4. meanAbortedExecutionTime( double totalExecutionTime, int totalOps, double granuleAbortProb)
  5. meanAndStandardDeviation(final double[] inp, final int startIndex, final int pastEnd)
  6. meanArithmetic(LinkedList a)
  7. meanArray(double[] arr)
  8. meandiff(double[] v1, double[] v2)
  9. meanEnt(double[] nums)