Use Commons
Math’s
StatUtils
to calculate simple univariate
statistics. The following example uses StatUtils
to calculate simple statistics for a double[]
:
import org.apache.commons.math.stat.StatUtils; double[] values = new double[] { 2.3, 5.4, 6.2, 7.3, 23.3 }; System.out.println( "min: " + StatUtils.min( values ) ); System.out.println( "max: " + StatUtils.max( values ) ); System.out.println( "mean: " + StatUtils.mean( values ) ); System.out.println( "product: " + StatUtils.product( values ) ); System.out.println( "sum: " + StatUtils.sum( values ) ); System.out.println( "variance: " + StatUtils.variance( values ) );
This code executes and prints a few simple statistics to the console, as follows:
min: 2.3 max: 23.3 mean: 8.9 product: 13097.61036 sum: 44.5 variance: 68.25500000000001
StatUtils
delegates these calculations to functors
in the
org.apache.commons.math.stat.univariate.moment
,
org.apache.commons.math.stat.univariate.rank
, and
org.apache.commons.math.stat.univariate.summary
packages. The following example uses the individual classes from
these packages to recreate the previous example, and it adds some
measures not available in StatUtil
:
import org.apache.commons.math.stat.univariate.moment.*; import org.apache.commons.math.stat.univariate.rank.*; import org.apache.commons.math.stat.univariate.summary.*; // Measures from previous example Min min = new Min( ); Max max = new Max( ); Mean mean = new Mean( ); Product product = new Product( ); Sum sum = new Sum( ); Variance variance = new Variance( ); System.out.println( "min: " + min.evaluate( values ) ); System.out.println( "max: " + max.evaluate( values ) ); System.out.println( "mean: " + mean.evaluate( values ) ); System.out.println( "product: " + product.evaluate( values ) ); System.out.println( "sum: " + sum.evaluate( values ) ); System.out.println( "variance: " + variance.evaluate( values ) ); // New measures Percentile percentile = new Percentile( ); GeometricMean geoMean = new GeometricMean( ); Skewness skewness = new Skewness( ); Kurtosis kurtosis = new Kurtosis( ); System.out.println( "80 percentile value: " + percentile.evaluate( values, 80.0 ) ); System.out.println( "geometric mean: " + geoMean.evaluate( values ) ); System.out.println( "skewness: " + skewness.evaluate( values ) ); System.out.println( "kurtosis: " + kurtosis.evaluate( values ) );
The previous example adds percentile, geometric mean, standard deviation, skewness, and kurtosis to the available univariate statistics. The previous example produces the following output:
min: 2.3 max: 23.3 mean: 8.9 product: 13097.61036 sum: 44.5 variance: 68.25500000000001 80 percentile value: 20.099999999999998 geometric mean: 6.659450778469037 standard dev: 8.261658429153314 skewness: 1.9446683453691376 kurtosis: 4.102348153299074
If you need a formal definition of a specific moment, rank, or summary, see MathWorld (http://mathworld.wolfram.com), an invaluable mathematical reference site from Wolfram, the makers of Mathematica.
18.221.197.95