apache commons Math是一組偏向科學計算爲主的函數,主要是針對線性代數,數學分析,機率和統計等方面。apache
packagetest.ffm83.commons.math; importorg.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.LUDecomposition; importorg.apache.commons.math3.linear.RealMatrix; importorg.apache.commons.math3.stat.descriptive.moment.GeometricMean; importorg.apache.commons.math3.stat.descriptive.moment.Kurtosis; importorg.apache.commons.math3.stat.descriptive.moment.Mean; importorg.apache.commons.math3.stat.descriptive.moment.Skewness; importorg.apache.commons.math3.stat.descriptive.moment.StandardDeviation; importorg.apache.commons.math3.stat.descriptive.moment.Variance; import org.apache.commons.math3.stat.descriptive.rank.Max; importorg.apache.commons.math3.stat.descriptive.rank.Min; importorg.apache.commons.math3.stat.descriptive.rank.Percentile; importorg.apache.commons.math3.stat.descriptive.summary.Product; importorg.apache.commons.math3.stat.descriptive.summary.Sum; importorg.apache.commons.math3.stat.descriptive.summary.SumOfSquares; /** * 簡單使用commons Math方法 * @author 範芳銘 */ public class MathUsage { public static void main(String[] args) { double[] values = new double[] { 0.33, 1.33,0.27333, 0.3, 0.501, 0.444, 0.44, 0.34496, 0.33,0.3, 0.292, 0.667 }; 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)); Percentile percentile = newPercentile(); // 百分位數 GeometricMean geoMean = newGeometricMean(); // 幾何平均數,n個正數的連乘積的n次算術根叫作這n個數的幾何平均數 Skewness skewness = new Skewness(); //Skewness(); Kurtosis kurtosis = new Kurtosis(); //Kurtosis,峯度 SumOfSquares sumOfSquares = newSumOfSquares(); // 平方和 StandardDeviation StandardDeviation =new StandardDeviation();//標準差 System.out.println("80 percentilevalue: " + percentile.evaluate(values,80.0)); System.out.println("geometricmean: " + geoMean.evaluate(values)); System.out.println("skewness:" + skewness.evaluate(values)); System.out.println("kurtosis:" + kurtosis.evaluate(values)); System.out.println("sumOfSquares:" + sumOfSquares.evaluate(values)); System.out.println("StandardDeviation: " +StandardDeviation.evaluate(values)); System.out.println("-------------------------------------"); // Create a real matrix with two rowsand three columns double[][] matrixData = { {1d,2d,3d},{2d,5d,3d}}; RealMatrix m = newArray2DRowRealMatrix(matrixData); System.out.println(m); // One more with three rows, twocolumns double[][] matrixData2 = { {1d,2d},{2d,5d}, {1d, 7d}}; RealMatrix n = newArray2DRowRealMatrix(matrixData2); // Note: The constructor copies the input double[][] array. // Now multiply m by n RealMatrix p = m.multiply(n); System.out.println("p:"+p); System.out.println(p.getRowDimension()); // 2 System.out.println(p.getColumnDimension()); // 2 // Invert p, using LUdecomposition RealMatrix pInverse = newLUDecomposition(p).getSolver().getInverse(); System.out.println(pInverse); } } 運行結果以下: min: 0.27333 max: 1.33 mean: 0.46269083333333333 product: 2.3429343978460972E-5 sum: 5.552289999999999 variance: 0.08757300031742428 80 percentile value: 0.5674000000000001 geometric mean: 0.4112886050879374 skewness: 2.670095445623868 kurtosis: 7.718241303328169 sumOfSquares: 3.5322966905000004 StandardDeviation: 0.2959273564870681 ------------------------------------- Array2DRowRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0}} p:Array2DRowRealMatrix{{8.0,33.0},{15.0,50.0}} 2 2 Array2DRowRealMatrix{{-0.5263157895,0.3473684211},{0.1578947368,-0.0842105263}}
package com; import org.apache.commons.lang.math.Range; import org.apache.commons.lang3.StringUtils; import org.apache.commons.math3.stat.StatUtils; import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation; import org.apache.commons.math3.stat.descriptive.rank.Median; /* * @description:簡單的數據統計分析 * */ public class MathYsf { public static void main(String[] args){ double[] values = new double[] { 0.33, 1.33,0.27333, 0.3, 0.501, 0.444, 0.44, 0.34496, 0.33,0.3, 0.292, 0.667 }; double[] values2 = new double[] { 0.89, 1.51,0.37999, 0.4, 0.701, 0.484, 0.54, 0.56496, 0.43,0.3, 0.392, 0.567 }; //計數 System.out.println("計算樣本個數爲:" +values.length); //mean--算數平均數 System.out.println("平均數:" + StatUtils.mean(values)); //sum--和 System.out.println("全部數據相加結果爲:" + StatUtils.sum(values)); //max--最小值 System.out.println("最小值:" + StatUtils.min(values)); //max--最大值 System.out.println("最大值:" + StatUtils.max(values)); //範圍 System.out.println("範圍是:" + (StatUtils.max(values)-StatUtils.min(values))); //標準差 StandardDeviation standardDeviation =new StandardDeviation(); System.out.println("一組數據的標準差爲:" + standardDeviation.evaluate(values)); //variance--方差 System.out.println("一組數據的方差爲:" + StatUtils.variance(values)); //median--中位數 Median median= new Median(); System.out.println("中位數:" + median.evaluate(values)); //mode--衆數 double[] res = StatUtils.mode(values); System.out.println("衆數:" + res[0]+","+res[1]); for(int i = 0;i<res.length;i++){ System.out.println("第"+(i+1)+"個衆數爲:"+res[i]); } //geometricMean--幾何平均數 System.out.println("幾何平均數爲:" +StatUtils.geometricMean(values)); //meanDifference-- 平均差,平均機率誤差 System.out.println("平均差爲:"+StatUtils.meanDifference(values, values2)); //normalize--標準化 double[] norm = StatUtils.normalize(values2); for(int i = 0;i<res.length;i++){ System.out.println("第"+(i+1)+"個數據標準化結果爲:" + norm[i]); } //percentile--百分位數 System.out.println("從小到大排序後位於80%位置的數:" + StatUtils.percentile(values, 70.0)); //populationVariance--整體方差 System.out.println("整體方差爲:" + StatUtils.populationVariance(values)); //product--乘積 System.out.println("全部數據相乘結果爲:" + StatUtils.product(values)); //sumDifference--和差 System.out.println("兩樣本數據的和差爲:" + StatUtils.sumDifference(values,values2)); //sumLog--對數求和 System.out.println("一組數據的對數求和爲:" + StatUtils.sumLog(values)); //sumSq--計算一組數值的平方和 System.out.println("一組數據的平方和:" + StatUtils.sumSq(values)); //varianceDifference --方差差別性。 System.out.println("一組數據的方差差別性爲:" + StatUtils.varianceDifference(values,values2,StatUtils.meanDifference(values, values2))); } }