Here you can find the source of median(List
Parameter | Description |
---|---|
sortedDataAscendingOrder | a parameter |
public final static Double median(List<Double> sortedDataAscendingOrder)
//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; } }