All Downloads are FREE. Search and download functionalities are using the official Maven repository.

refdiff.core.util.Statistics Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package refdiff.core.util;

import java.util.List;

public class Statistics {
	
	public static double median(List values) {
		return median(values, 0, values.size());
	}
	
	public static double q1(List values) {
		int length = values.size();
		if (length == 0) {
			throw new IllegalArgumentException("There are no values");
		} else {
			return median(values, 0, length / 2);
		}
	}
	
	public static double q3(List values) {
		int length = values.size();
		if (length == 0) {
			throw new IllegalArgumentException("There are no values");
		} else if (length % 2 == 0) {
			return median(values, length / 2, length);
		} else {
			return median(values, (length / 2) + 1, length);
		}
	}
	
	private static double median(List values, int from, int to) {
		int length = to - from;
		if (length <= 0) {
			throw new IllegalArgumentException("There are no values");
		} else if (length % 2 == 0) {
			int i1 = from + (length / 2) - 1;
			int i2 = from + (length / 2);
			return (values.get(i1).doubleValue() + values.get(i2).doubleValue()) / 2;
		} else {
			int i = from + length / 2;
			return values.get(i).doubleValue();
		}
	}
	
	public static double min(List values) {
		int length = values.size();
		if (length == 0) {
			throw new IllegalArgumentException("There are no values");
		} else {
			return values.get(0).doubleValue();
		}
	}
	
	public static double max(List values) {
		int length = values.size();
		if (length == 0) {
			throw new IllegalArgumentException("There are no values");
		} else {
			return values.get(values.size() - 1).doubleValue();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy