📄 statistics.java
字号:
package com.biolab.node.nexTest.Jama;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* A utility class that provides some simple statistical functions.
*/
public abstract class Statistics {
/**
* Returns the mean of an array of numbers.
*
* @param values the values (<code>null</code> permitted, returns
* <code>Double.NaN</code>).
*
* @return The mean.
*/
public static double calculateMean(Number[] values) {
double result = Double.NaN;
if (values != null && values.length > 0) {
double sum = 0.0;
int counter = 0;
for (; counter < values.length; counter++) {
sum = sum + values[counter].doubleValue();
}
result = (sum / counter);
}
return result;
}
/**
* Returns the mean of an array of numbers.
*
* @param values the values
* @return The mean.
zhangweian 2006/03/08
*/
public static double calculateMean(double[] values) {
double result = Double.NaN;
if (values != null && values.length > 0) {
double sum = 0.0;
int counter = 0;
for (; counter < values.length; counter++) {
sum = sum + values[counter];
}
result = (sum / counter);
}
return result;
}
/**
* Returns the mean of a collection of <code>Number</code> objects.
*
* @param values the values (<code>null</code> permitted, returns
* <code>Double.NaN</code>).
*
* @return The mean.
*/
public static double calculateMean(Collection values) {
double result = Double.NaN;
int count = 0;
double total = 0.0;
Iterator iterator = values.iterator();
while (iterator.hasNext()) {
Object object = iterator.next();
if (object != null && object instanceof Number) {
Number number = (Number) object;
total = total + number.doubleValue();
count = count + 1;
}
}
if (count > 0) {
result = total / count;
}
return result;
}
/**
* Calculates the median for a list of values (<code>Number</code> objects).
* The list of values will be sorted first.
*
* @param values the values.
*
* @return The median.
*/
public static double calculateMedian(List values) {
return calculateMedian(values, true);
}
/**
* Calculates the median for a list of values (<code>Number</code> objects)
* that are assumed to be in ascending order.
*
* @param values the values.
* @param copyAndSort a flag that controls whether the list of values is
* copied and sorted.
*
* @return The median.
*/
public static double calculateMedian(List values, boolean copyAndSort) {
double result = Double.NaN;
if (values != null) {
if (copyAndSort) {
int itemCount = values.size();
List copy = new ArrayList(itemCount);
for (int i = 0; i < itemCount; i++) {
copy.add(i, values.get(i));
}
Collections.sort(copy);
values = copy;
}
int count = values.size();
if (count > 0) {
if (count % 2 == 1) {
if (count > 1) {
Number value = (Number) values.get((count - 1) / 2);
result = value.doubleValue();
}
else {
Number value = (Number) values.get(0);
result = value.doubleValue();
}
}
else {
Number value1 = (Number) values.get(count / 2 - 1);
Number value2 = (Number) values.get(count / 2);
result = (value1.doubleValue() + value2.doubleValue())
/ 2.0;
}
}
}
return result;
}
/**
* Calculates the median for a sublist within a list of values
* (<code>Number</code> objects).
*
* @param values the values (in any order).
* @param start the start index.
* @param end the end index.
*
* @return The median.
*/
public static double calculateMedian(List values, int start, int end) {
return calculateMedian(values, start, end, true);
}
/**
* Calculates the median for a sublist within a list of values
* (<code>Number</code> objects). The entire list will be sorted if the
* <code>ascending</code< argument is <code>false</code>.
*
* @param values the values.
* @param start the start index.
* @param end the end index.
* @param copyAndSort a flag that that controls whether the list of values
* is copied and sorted.
*
* @return The median.
*/
public static double calculateMedian(List values, int start, int end,
boolean copyAndSort) {
double result = Double.NaN;
if (copyAndSort) {
List working = new ArrayList(end - start + 1);
for (int i = start; i <= end; i++) {
working.add(values.get(i));
}
Collections.sort(working);
result = calculateMedian(working, false);
}
else {
int count = end - start + 1;
if (count > 0) {
if (count % 2 == 1) {
if (count > 1) {
Number value
= (Number) values.get(start + (count - 1) / 2);
result = value.doubleValue();
}
else {
Number value = (Number) values.get(start);
result = value.doubleValue();
}
}
else {
Number value1 = (Number) values.get(start + count / 2 - 1);
Number value2 = (Number) values.get(start + count / 2);
result
= (value1.doubleValue() + value2.doubleValue()) / 2.0;
}
}
}
return result;
}
/**
* Returns the standard deviation of a set of numbers.
*
* @param data the data.
*
* @return The standard deviation of a set of numbers.
*/
public static double getStdDev(Number[] data) {
double avg = calculateMean(data);
double sum = 0.0;
for (int counter = 0; counter < data.length; counter++) {
double diff = data[counter].doubleValue() - avg;
sum = sum + diff * diff;
}
return Math.sqrt(sum / (data.length - 1));
}
/**
* Returns the standard deviation of a set of numbers.
*
* @param data the data.
*
* @return The standard deviation of a set of numbers.
zhangweian 2006/03/08
*/
public static double getStdDev(double[] data) {
double avg = calculateMean(data);
double sum = 0.0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -