-
간단한 자바 통계 프로그램.JAVA 2013. 5. 7. 11:10
간단한 통계기능이 필요한데 R같은 라이브러리는 무겁고해서 만든 자바 프로그램입니다.
분산, 공분산, 표준편차, 결정계수(R-Squared) 계산이 가능합니다.
뒤져봐도 결정계수 계산하는 소스는 잘 안나와서 직접 만들었습니다.
import java.util.ArrayList; import java.util.List; public class Statistics { public static double getSum(List
Values){ double SumOfData = 0; for(int i = 0 ; i < Values.size() ; i++){ SumOfData += Values.get(i).doubleValue(); } return SumOfData; } public static double getRSquared(List originData, List targetData) { double R2 = 0; R2 = Math.pow(getCovariance(originData, targetData) / (getStandardDeviation(originData)* getStandardDeviation(targetData)), 2); return R2; } public static double getAvg(List datas) { double AvgOfData = 0; AvgOfData = getSum(datas)/datas.size(); return AvgOfData; } public static double getVariance(List datas) { double Variance = 0; double avg = 0; avg = getAvg(datas); double SumOfDeviation = 0; for(int i = 0; i < datas.size(); i++) { SumOfDeviation += Math.pow((datas.get(i).doubleValue() - avg), 2); } Variance = SumOfDeviation / datas.size(); return Variance; } public static double getStandardDeviation(List datas) { double StandardDeviation = 0; StandardDeviation = Math.sqrt(getVariance(datas)); return StandardDeviation; } public static double getCovariance(List datas, List targetData) { double a_avg = getAvg(datas); double b_avg = getAvg(targetData); List AB = new ArrayList(); for(int i = 0; i < datas.size(); i++){ AB.add(new Double(datas.get(i).doubleValue() * targetData.get(i).doubleValue())); } double ab_avg = getAvg(AB); return ab_avg - (a_avg * b_avg); } public static double getArSum(double[] arDatas) { double SumOfData = 0; for(int i = 0 ; i < arDatas.length ; i++){ SumOfData += arDatas[i]; } return SumOfData; } public static double getArAvg(double[] datas) { return getArSum(datas)/datas.length; } public static double getArVariance(double[] arDatas) { double avg = 0; avg = getArAvg(arDatas); double SumOfDeviation = 0; for(int i = 0; i < arDatas.length; i++) { SumOfDeviation += Math.pow((arDatas[i] - avg), 2); } return SumOfDeviation / arDatas.length; } public static double getArStandardDeviation(double[] arDatas) { return Math.sqrt(getArVariance(arDatas)); } public static double getArCovariance(double[] arDatas, double[] arTargetDatas) { double a_avg = getArAvg(arDatas); double b_avg = getArAvg(arTargetDatas); double AB[] = new double[arDatas.length]; for(int i = 0; i < arDatas.length; i++){ AB[i] = arDatas[i] * arTargetDatas[i]; } double ab_avg = getArAvg(AB); return ab_avg - (a_avg * b_avg); } public static double getArRSquared(double[] arDatas, double[] arTargetDatas) { double R2 = 0; R2 = Math.pow(getArCovariance(arDatas, arTargetDatas) / (getArStandardDeviation(arDatas)* getArStandardDeviation(arTargetDatas)), 2); if(Double.isNaN(R2)){R2 = 0;} return R2; } } 'JAVA' 카테고리의 다른 글
인텔리J 에서 스프링 MVC, 하이버네이트, JSON 사용하기 (0) 2014.05.12 JAVA JSON 라이브러리 Jackson 사용법 (0) 2013.05.22 댓글