首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Apache Commons Math 統計與數學工具實戰:描述性統計、隨機數與線性代數

     編輯:Java 相關知識

簡介

Apache Commons Math 是一個輕量級、自包含的數學與統計庫,提供描述性統計、隨機數生成、線性代數、優化、幾何等功能。官網:https://commons.apache.org/math/。本文展示核心統計類與線性方程求解的使用範例。

描述性統計:一站式計算

庫提供了豐富的統計量實現類,均實現 UnivariateStatistic 接口,調用 evaluate(double[]) 即可計算。

package demo;

import org.apache.commons.math.stat.descriptive.moment.*;
import org.apache.commons.math.stat.descriptive.rank.*;
import org.apache.commons.math.stat.descriptive.summary.*;

public class TestMathUsage {
    public static void main(String[] args) {
        double[] values = {0.33, 1.33, 0.27333, 0.3, 0.501, 0.444, 0.44, 0.34496, 0.33, 0.3, 0.292, 0.667};

        // 基礎統計量
        System.out.println("min: " + new Min().evaluate(values));
        System.out.println("max: " + new Max().evaluate(values));
        System.out.println("mean (算術平均): " + new Mean().evaluate(values));
        System.out.println("product (乘積): " + new Product().evaluate(values));
        System.out.println("sum (求和): " + new Sum().evaluate(values));
        System.out.println("variance (方差): " + new Variance().evaluate(values));

        // 進階統計量
        Percentile percentile = new Percentile();
        GeometricMean geoMean = new GeometricMean();
        Skewness skewness = new Skewness();
        Kurtosis kurtosis = new Kurtosis();
        SumOfSquares sumSq = new SumOfSquares();
        StandardDeviation stdDev = new StandardDeviation();

        System.out.println("80th percentile: " + 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));
        System.out.println("sum of squares: " + sumSq.evaluate(values));
        System.out.println("standard deviation (標準差): " + stdDev.evaluate(values));
    }
}

功能類速覽

包路徑主要類功能
org.apache.commons.math.stat.descriptive.momentMean, Variance, StandardDeviation, Skewness, Kurtosis, GeometricMean矩相關統計量
org.apache.commons.math.stat.descriptive.rankMin, Max, Median, Percentile順位統計量
org.apache.commons.math.stat.descriptive.summarySum, Product, SumOfSquares摘要統計量
org.apache.commons.math.randomRandomData, RandomDataImpl隨機數生成
org.apache.commons.math.linearRealMatrix, RealMatrixImpl矩陣運算、線性方程求解

隨機數生成:RandomData

import org.apache.commons.math.random.RandomData;
import org.apache.commons.math.random.RandomDataImpl;

public class MathDemo {
    public static void main(String[] args) {
        RandomData randomData = new RandomDataImpl();
        // 同一實例連續生成
        for (int i = 0; i < 10; i++) {
            System.out.println(randomData.nextLong(1, 100));
        }
        System.out.println("==========");
        // 每次新實例(種子可能相同導致序列相同)
        for (int i = 0; i < 10; i++) {
            RandomData rd = new RandomDataImpl();
            System.out.println(rd.nextLong(1, 100));
        }
    }
}

提示:生產環境建議復用 RandomDataImpl 實例,避免種子碰撞導致隨機性下降。

線性代數:求解線性方程組

解方程組:

2x + 3y - 2z = 1
-x + 7y + 6z = -2
4x - 3y - 5z = 1
package demo;

import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.linear.RealMatrixImpl;

public class MathDemo {
    public static void main(String[] args) {
        double[][] coeffData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}};
        RealMatrix coefficients = new RealMatrixImpl(coeffData);
        double[] constants = {1, -2, 1};
        double[] solution = coefficients.solve(constants); // 返回 [x, y, z]

        System.out.println("x = " + solution[0]);
        System.out.println("y = " + solution[1]);
        System.out.println("z = " + solution[2]);
    }
}

總結

Commons Math 以清晰的類結構封裝了常用數學統計功能,無需依賴重型庫即可在項目中快速引入科學計算能力。統計類採用策略模式設計,擴展自定義統計量極為方便。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved