Java Array Scale scaleMAD(double[] data)

Here you can find the source of scaleMAD(double[] data)

Description

Scale MAD in order to use it as a consistent estimator for the estimation of the standard deviation

License

Apache License

Parameter

Parameter Description
data a parameter

Return

sd (related to MAD)

Declaration

public static double scaleMAD(double[] data) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {
    /**/*from   w  w w. jav a2s  .  c  o  m*/
     * Scale MAD in order to use it as a consistent estimator for the estimation
     * of the standard deviation
     *
     * @param data
     * @return sd (related to MAD)
     */
    public static double scaleMAD(double[] data) {
        //scale factor for asymptotically normal consistency
        final double constant = 1.4826;
        return constant * computeMAD(data);
    }

    /**
     * Compute Median Absolute Deviation (MAD) of an array of double
     *
     * @param data
     * @return MAD
     */
    private static double computeMAD(double[] data) {
        double[] deviations = new double[data.length];
        double median = computeMedian(data);
        for (int i = 0; i < data.length; i++) {
            deviations[i] = Math.abs(data[i] - median);
        }
        return computeMedian(deviations);
    }

    /**
     * Compute median value of an array of double
     *
     * @param data
     * @return median
     */
    public static double computeMedian(double[] data) {
        // sort the input data, i.e. arrange the data points in ascending order
        Arrays.sort(data);
        //make a distinction between odd and even dataset sizes
        // odd size: return the data point in the middle position
        if (data.length % 2 == 1) {
            return data[(data.length + 1) / 2 - 1];
        } else {
            // even size
            double lower = data[(data.length / 2 - 1)];
            double upper = data[(data.length / 2)];
            return (lower + upper) / 2;
        }
    }

    /**
     * Compute median value of a list of double
     *
     * @param data
     * @return median
     */
    public static Double computeMedian(List<Double> data) {
        // sort the input data, i.e. arrange the data points in ascending order
        Collections.sort(data);
        //make a distinction between odd and even dataset sizes
        // odd size: return the data point in the middle position
        if (data.size() % 2 == 1) {
            return data.get((data.size() + 1) / 2 - 1);
        } else {
            // even size
            Double lower = data.get((data.size() / 2 - 1));
            Double upper = data.get((data.size() / 2));
            return (lower + upper) / 2;
        }
    }
}

Related

  1. arrayScale(final Double[] first, final double scale)
  2. expandToLength16(byte scaledValue[], final boolean isNegative)
  3. minMaxScale(final double[] x)
  4. scale(double[][] as)
  5. scale(final double[] a, double scale)
  6. scalePoints(double[] points, int height)