Java List Median median(List sortedDataAscendingOrder)

Here you can find the source of median(List sortedDataAscendingOrder)

Description

Get the median value thru linear interpolations

License

LGPL

Parameter

Parameter Description
sortedDataAscendingOrder a parameter

Return

Double

Declaration

public final static Double median(List<Double> sortedDataAscendingOrder) 

Method Source Code


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

import java.util.List;

public class Main {
    /**// ww w .j ava 2s  .  c o  m
     * Get the median value thru linear interpolations
     * 
     * @param sortedDataAscendingOrder
     * @return Double
     */
    public final static Double median(List<Double> sortedDataAscendingOrder) {
        return interpolateLinearlyQuantile(sortedDataAscendingOrder, 0.50);
    }

    /**
     * Interpolate linearly an quantile
     * 
     * Inspired on: http://msenux.redwoods.edu/math/R/boxplot.php
     * 
     * @param sortedDataAscendingOrder sorted data in ascending order (NOT NULL) 
     * @param p percentage
     * @return Double interpolated linearly quantile
     */
    private final static Double interpolateLinearlyQuantile(List<Double> sortedDataAscendingOrder, Double p) {
        int n = sortedDataAscendingOrder.size();
        double position = (1 + (p * (n - 1)));

        int leftIndex = (int) Math.floor(position) - 1;
        int rightIndex = (int) Math.ceil(position) - 1;
        Double quantile;

        if (leftIndex == rightIndex) {
            quantile = sortedDataAscendingOrder.get(leftIndex);
        } else {
            Double leftIndexValue = sortedDataAscendingOrder.get(leftIndex);
            Double rightIndexValue = sortedDataAscendingOrder.get(rightIndex);

            quantile = leftIndexValue + 0.5 * (rightIndexValue - leftIndexValue);
        }

        return quantile;
    }
}

Related

  1. median(final List array)
  2. median(List data)
  3. median(List list)
  4. median(List list)
  5. median(List p)
  6. median(List list)
  7. median(List data)
  8. median(List data)
  9. medianAbsVal(List vals)