Apache Commons Math 본문

Programming/Java

Apache Commons Math

halatha 2011. 5. 4. 04:29
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
import org.apache.commons.math.stat.descriptive.rank.Percentile;

//	$ javac -cp .:./commons-math-2.2/commons-math-2.2.jar TestCommonsMath.java
public class TestCommonsMath	{
	public static void main(final String[] args)	{
		double[]	inputArray;
		if ( 1 == args.length )
		{
			List<Double>	list	=	new ArrayList<Double>();
			try	{
				String	str;
				BufferedReader	reader	=	new BufferedReader(new
						FileReader(args[0]));
				while ( null != ( str = reader.readLine() ) )
				{
					list.add(Double.parseDouble(str));
				}
			} catch (IOException e) {}
			inputArray	=	new double[list.size()];
			int	index	=	0;
			for ( Double d : list )	inputArray[index++]	=	d;
		}
		else
		{
			inputArray	=	new double[]
			{ 1, 2, 2, 4, 9, 2, 200, 4, 4, 2, 1, 2, 2, 4, 9, 2, 200, 4, 4, 2 };
		}

		long	time0	=	System.nanoTime();
		//	http://commons.apache.org/math/userguide/stat.html
		// Get a DescriptiveStatistics instance
		DescriptiveStatistics stats = new DescriptiveStatistics(inputArray);

		// Add the data from the array
		//for( int i = 0; i < inputArray.length; i++) {
		//	stats.addValue(inputArray[i]);
		//}

		// Compute some statistics
		double	sum	=	stats.getSum();
		double	min	=	stats.getMin();
		double	max	=	stats.getMax();
		double	q1	=	stats.getPercentile(25);
		double	q2	=	stats.getPercentile(50);
		double	q3	=	stats.getPercentile(75);
		double	mean	=	stats.getMean();
		double	var	=	stats.getVariance();
		double	std	=	stats.getStandardDeviation();
		long	time1	=	System.nanoTime();

		System.out.println("count:\t" + inputArray.length + "\nsum:\t" + sum +
				"\nmin:\t" + min + "\nmax:\t" + max + "\ncount_unique:\tN/A" +
				"\nq1:\t" + q1 + "\nmedian:\t" + q2 + "\nq3:\t" + q3 +
				"\niqr:\t" + (q3 - q1) + "\nmean:\t" + mean + "\nvariance:\t" +
				var + "\nstd:\t" + std);
		System.out.println("took " + (time1 - time0) / 1000 / 1000 + " ms");

		long	time2	=	System.nanoTime();
		Percentile	p	=	new Percentile();
		q1	=	p.evaluate(inputArray, 25.0);
		q2	=	p.evaluate(inputArray, 50.0);
		q3	=	p.evaluate(inputArray, 75.0);

		SummaryStatistics	sStats	=	new SummaryStatistics();
		for( int i = 0; i < inputArray.length; i++) {
			sStats.addValue(inputArray[i]);
		}
		sum	=	sStats.getSum();
		min	=	sStats.getMin();
		max	=	sStats.getMax();
		mean	=	sStats.getMean();
		var	=	sStats.getVariance();
		std	=	sStats.getStandardDeviation();
		long	time3	=	System.nanoTime();

		System.out.println("count:\t" + inputArray.length + "\nsum:\t" + sum +
				"\nmin:\t" + min + "\nmax:\t" + max + "\ncount_unique:\tN/A" +
				"\nmean:\t" + mean + "\nvariance:\t" + var + "\nstd:\t" + std);
		System.out.println("p1:\t" + q1);
		System.out.println("p2:\t" + q2);
		System.out.println("p3:\t" + q3);
		System.out.println("took " + (time3 - time2) / 1000 / 1000 + " ms");
	}
}

* test 결과 Descriptive쪽보다 Summary + Percentile쪽이 약간 더 빠름
* http://commons.apache.org/math/userguide/stat.html
Comments