161 lines
5.0 KiB
Java
161 lines
5.0 KiB
Java
package com.cnbm.common.utils;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigInteger;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import static java.math.BigDecimal.ROUND_HALF_UP;
|
|
|
|
public class CountUtils {
|
|
|
|
// private static final BigDecimal TWO = BigDecimal.valueOf(2);
|
|
//
|
|
// public static BigDecimal Mean(List<BigDecimal> data){
|
|
// BigDecimal sum = new BigDecimal("0");
|
|
// for (BigDecimal datum : data) {
|
|
// sum = sum.add(datum);
|
|
// }
|
|
// BigDecimal divide = sum.divide(BigDecimal.valueOf(data.size()));
|
|
// return divide;
|
|
// }
|
|
//
|
|
// // population variance 总体方差
|
|
// public static BigDecimal POP_Variance(List<BigDecimal> data) {
|
|
// BigDecimal variance = new BigDecimal("0");
|
|
// for (BigDecimal datum : data) {
|
|
// variance = variance.add(datum.subtract(Mean(data)).pow(2));
|
|
// }
|
|
// variance = variance.divide(BigDecimal.valueOf(data.size()));
|
|
// return variance;
|
|
// }
|
|
//
|
|
// // population standard deviation 总体标准差
|
|
// public static BigDecimal POP_STD_dev(List<BigDecimal> data) {
|
|
// BigDecimal sqrt = sqrt(POP_Variance(data), 4);
|
|
// return sqrt;
|
|
// }
|
|
//
|
|
// //sample variance 样本方差
|
|
// public static BigDecimal Sample_Variance(List<BigDecimal> data) {
|
|
// BigDecimal variance = new BigDecimal("0");
|
|
// for (BigDecimal datum : data) {
|
|
// variance = variance.add(datum.subtract(Mean(data)).pow(2));
|
|
// }
|
|
// variance = variance.divide(BigDecimal.valueOf(data.size() - 1),4);
|
|
// return variance;
|
|
// }
|
|
//
|
|
// // sample standard deviation 样本标准差
|
|
// public static BigDecimal Sample_STD_dev(List<BigDecimal> data) {
|
|
// BigDecimal sqrt = sqrt(Sample_Variance(data), 4);
|
|
// return sqrt;
|
|
// }
|
|
//
|
|
// public static BigDecimal sqrt(BigDecimal A, final int SCALE) {
|
|
// BigDecimal x0 = new BigDecimal("0");
|
|
// BigDecimal x1 = new BigDecimal(Math.sqrt(A.doubleValue()));
|
|
// while (!x0.equals(x1)) {
|
|
// x0 = x1;
|
|
// x1 = A.divide(x0, SCALE, ROUND_HALF_UP);
|
|
// x1 = x1.add(x0);
|
|
// x1 = x1.divide(TWO, SCALE, ROUND_HALF_UP);
|
|
//
|
|
// }
|
|
// return x1;
|
|
// }
|
|
|
|
|
|
double Sum(double[] data) {
|
|
double sum = 0;
|
|
for (int i = 0; i < data.length; i++)
|
|
sum = sum + data[i];
|
|
return sum;
|
|
}
|
|
|
|
double Mean(double[] data) {
|
|
double mean = 0;
|
|
mean = Sum(data) / data.length;
|
|
return mean;
|
|
}
|
|
|
|
// population variance 总体方差
|
|
double POP_Variance(double[] data) {
|
|
double variance = 0;
|
|
for (int i = 0; i < data.length; i++) {
|
|
variance = variance + (Math.pow((data[i] - Mean(data)), 2));
|
|
}
|
|
variance = variance / data.length;
|
|
return variance;
|
|
}
|
|
|
|
// population standard deviation 总体标准差
|
|
double POP_STD_dev(double[] data) {
|
|
double std_dev;
|
|
std_dev = Math.sqrt(POP_Variance(data));
|
|
return std_dev;
|
|
}
|
|
|
|
//sample variance 样本方差
|
|
double Sample_Variance(double[] data) {
|
|
double variance = 0;
|
|
for (int i = 0; i < data.length; i++) {
|
|
variance = variance + (Math.pow((data[i] - Mean(data)), 2));
|
|
}
|
|
variance = variance / (data.length-1);
|
|
return variance;
|
|
}
|
|
|
|
// sample standard deviation 样本标准差
|
|
double Sample_STD_dev(double[] data) {
|
|
double std_dev;
|
|
std_dev = Math.sqrt(Sample_Variance(data));
|
|
return std_dev;
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
// List<BigDecimal> data = new ArrayList<>();
|
|
//
|
|
// data.add(new BigDecimal("10.023"));
|
|
// data.add(new BigDecimal("11.02"));
|
|
// data.add(new BigDecimal("9.99"));
|
|
// data.add(new BigDecimal("9.81"));
|
|
// data.add(new BigDecimal("10.12"));
|
|
// data.add(new BigDecimal("10.9"));
|
|
// data.add(new BigDecimal("9.99"));
|
|
// data.add(new BigDecimal("10.03"));
|
|
// data.add(new BigDecimal("9.99"));
|
|
// data.add(new BigDecimal("12.02"));
|
|
// CountUtils countUtils = new CountUtils();
|
|
//
|
|
// BigDecimal mean = countUtils.Mean(data);
|
|
// System.out.println(mean.toString());
|
|
//
|
|
//
|
|
// BigDecimal bigDecimal1 = POP_STD_dev(data);
|
|
// System.out.println(bigDecimal1.toString());
|
|
//
|
|
// System.out.println(Sample_STD_dev(data).toString());
|
|
//
|
|
// BigDecimal bigDecimal = POP_Variance(data);
|
|
// System.out.println(bigDecimal.toString());
|
|
//
|
|
//
|
|
// System.out.println(Sample_Variance(data));
|
|
|
|
double[] data = new double[]{10.023,11.02,9.99,9.81,10.12,10.9,9.99,10.03,9.99,12.02};
|
|
CountUtils countUtils = new CountUtils();
|
|
|
|
System.out.println(countUtils.Mean(data));
|
|
System.out.println(countUtils.POP_STD_dev(data));
|
|
System.out.println(countUtils.Sample_STD_dev(data));
|
|
|
|
System.out.println(countUtils.POP_Variance(data));
|
|
System.out.println(countUtils.Sample_Variance(data));
|
|
|
|
}
|
|
|
|
}
|