math3 commons java statistics

java - math3 - apache commons math 3.4 api download



Cálculos estadísticos (5)

¿Qué versión de Apache Commmons Math estás usando? Hay una clase mediana al menos en 2.1 en adelante (la versión anterior no estoy seguro). Puedes usarlo como:

Median median = new Median(); median.evaluate(values);

¿Hay alguna biblioteca incorporada que pueda usar para calcular la mediana en Java?

Estaba trabajando con apache.commons.math para otras funciones estadísticas, pero la mediana no se encontraba por ningún lado.

Gracias,


Coloque todos los números en una lista, clasifique la lista y tome el valor medio (o el promedio de los dos valores medios para el tamaño par). Sin cálculos necesarios


Desde el departamento de "demasiado tiempo en mis manos": aquí hay una pequeña clase de MedianGenerator:

/** * Methods to calculate the median value of a supplied {@link List}. */ public final class MedianGenerator{ private MedianGenerator(){ } /** * Calculate the median of a supplied list. * <ol> * <li>A copy will be generated</li> * <li>this copy will be sorted with the supplied comparator</li> * <li>the median will be calculated, using the supplied averageCalculator * for collections with an even number of items</li> * </ol> * * @param data * @param comparator * @param averageCalculator * @return the median */ public static <T> T calculateMedian(final List<T> data, final Comparator<? super T> comparator, final AverageCalculator<T> averageCalculator){ final List<T> copy = new ArrayList<T>(data); Collections.sort(copy, comparator); return doCalculateMedian(data, averageCalculator); } /** * Calculate the median of a supplied list. * <ol> * <li>A copy will be generated</li> * <li>this copy will be sorted with the supplied comparator</li> * <li>the median will be calculated, using the {@link #ALWAYS_FIRST} {@link AverageCalculator} * for collections with an even number of items</li> * </ol> * * @param data * @param comparator * @return the median */ @SuppressWarnings("unchecked") public static <T> T calculateMedian(final List<T> data, final Comparator<? super T> comparator){ return calculateMedian(data, comparator, (AverageCalculator<T>) ALWAYS_FIRST); } /** * Calculate the median of a supplied list. * <ol> * <li>A copy will be generated</li> * <li>this copy will be sorted using natural ordering</li> * <li>the median will be calculated, using the {@link #ALWAYS_FIRST} {@link AverageCalculator} * for collections with an even number of items</li> * </ol> * * @param data * @return the median */ @SuppressWarnings("unchecked") public static <T extends Comparable<? super T>> T calculateMedian(final List<T> data){ return calculateMedian(data, (AverageCalculator<T>) ALWAYS_FIRST); } /** * Calculate the median of a supplied list. * <ol> * <li>A copy will be generated</li> * <li>this copy will be sorted using natural ordering</li> * <li>the median will be calculated, using the supplied averageCalculator * for collections with an even number of items</li> * </ol> * * @param data * @param averageCalculator * @return the median */ public static <T extends Comparable<? super T>> T calculateMedian(final List<T> data, final AverageCalculator<T> averageCalculator){ final List<T> copy = new ArrayList<T>(data); Collections.sort(copy); return doCalculateMedian(copy, averageCalculator); } private static <T> T doCalculateMedian(final List<T> sortedData, final AverageCalculator<T> averageCalculator){ T result; if(sortedData.isEmpty()){ result = null; } else{ final int size = sortedData.size(); if(size % 2 == 0){ result = averageCalculator.getAverage(sortedData.get(size / 2 - 1), sortedData.get(size / 2)); } else{ result = sortedData.get(size / 2 - 1); } } return result; } /** * Generic accessor method for {@link #ALWAYS_FIRST}. */ @SuppressWarnings("unchecked") public static <T> AverageCalculator<T> alwaysFirst(){ return ALWAYS_FIRST; } /** * {@link AverageCalculator} implementation that always returns the lower * bound unchanged. */ @SuppressWarnings("rawtypes") public static final AverageCalculator ALWAYS_FIRST = new AverageCalculator(){ @Override public Object getAverage(final Object first, final Object second){ return first; } }; /** * When there is an even number of items, this interface is used to generate * the average between the two middle items. */ public static interface AverageCalculator<E> { E getAverage(E first, E second); } }


Obtenga los valores en un objeto List. Supongamos que los valores son enteros y la lista se llama "valores". Entonces

List<Integer> values; ... populate values ... Collections.sort(values); int median; int midpoint=values.size()/2; if (values.size()%2==1) median=values.get(midpoint+1).intValue(); else median=(values.get(midpoint).intValue()+values.get(midpoint+1).intValue())/2;

Si el número de valores es grande, como cientos o más, jugar con el mod-2 puede ser técnicamente correcto pero superfluo.

Tal vez haya una manera más eficiente de hacerlo, la clasificación es bastante lenta en una lista grande, pero esto funcionaría.

Ah, y realmente deberías buscar una lista con cero entradas. Tal vez estoy perdiendo otras condiciones de contorno.