Class Statistics

java.lang.Object
cloud.opencode.base.math.stats.Statistics

public final class Statistics extends Object
Statistical utility methods for descriptive statistics. 描述性统计工具方法集合

All methods are stateless and thread-safe. Input arrays are never modified. Null or empty arrays cause IllegalArgumentException.

所有方法无状态且线程安全。输入数组不会被修改。 空值或空数组将抛出 IllegalArgumentException

Since:
JDK 25, opencode-base-math V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    correlation(double[] x, double[] y)
    Computes the Pearson correlation coefficient between two arrays.
    static double
    covariance(double[] x, double[] y)
    Computes the sample covariance between two arrays.
    static double
    geometricMean(double[] values)
    Computes the geometric mean.
    static double
    harmonicMean(double[] values)
    Computes the harmonic mean.
    static double
    interquartileRange(double[] data)
    Computes the interquartile range (Q3 - Q1).
    static double
    kendallCorrelation(double[] x, double[] y)
    Computes Kendall's tau-b correlation coefficient between two arrays.
    static double
    kurtosis(double[] data)
    Computes the excess kurtosis of the data.
    static double[]
    mode(double[] data)
    Returns all mode values (most frequent values) in the data.
    static double
    percentile(double[] data, double p)
    Computes the p-th percentile using linear interpolation.
    static double
    range(double[] data)
    Computes the range (max - min) of the data.
    static double
    skewness(double[] data)
    Computes the sample skewness (Fisher's definition).
    static double
    spearmanCorrelation(double[] x, double[] y)
    Computes Spearman's rank correlation coefficient between two arrays.
    static double
    weightedMean(double[] values, double[] weights)
    Computes the weighted arithmetic mean.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • percentile

      public static double percentile(double[] data, double p)
      Computes the p-th percentile using linear interpolation. 使用线性插值法计算第 p 百分位数
      Parameters:
      data - the input data array / 输入数据数组
      p - percentile in [0, 100] / 百分位数,范围 [0, 100]
      Returns:
      the p-th percentile value / 第 p 百分位数的值
      Throws:
      IllegalArgumentException - if data is null/empty or p is out of range
    • mode

      public static double[] mode(double[] data)
      Returns all mode values (most frequent values) in the data. 返回数据中的所有众数(出现频率最高的值)
      Parameters:
      data - the input data array / 输入数据数组
      Returns:
      array of mode values / 众数数组
      Throws:
      IllegalArgumentException - if data is null or empty
    • skewness

      public static double skewness(double[] data)
      Computes the sample skewness (Fisher's definition). 计算样本偏度(Fisher 定义)

      Requires at least 3 data points.

      需要至少 3 个数据点。

      Parameters:
      data - the input data array / 输入数据数组
      Returns:
      the sample skewness / 样本偏度
      Throws:
      IllegalArgumentException - if data is null, empty, or has fewer than 3 elements
    • kurtosis

      public static double kurtosis(double[] data)
      Computes the excess kurtosis of the data. 计算数据的超额峰度

      Requires at least 4 data points. Uses the sample excess kurtosis formula.

      需要至少 4 个数据点。使用样本超额峰度公式。

      Parameters:
      data - the input data array / 输入数据数组
      Returns:
      the excess kurtosis / 超额峰度
      Throws:
      IllegalArgumentException - if data is null, empty, or has fewer than 4 elements
    • covariance

      public static double covariance(double[] x, double[] y)
      Computes the sample covariance between two arrays. 计算两个数组的样本协方差
      Parameters:
      x - the first data array / 第一个数据数组
      y - the second data array / 第二个数据数组
      Returns:
      the sample covariance / 样本协方差
      Throws:
      IllegalArgumentException - if arrays are null, empty, different lengths, or have fewer than 2 elements
    • correlation

      public static double correlation(double[] x, double[] y)
      Computes the Pearson correlation coefficient between two arrays. 计算两个数组的皮尔逊相关系数
      Parameters:
      x - the first data array / 第一个数据数组
      y - the second data array / 第二个数据数组
      Returns:
      the Pearson correlation coefficient in [-1, 1] / 皮尔逊相关系数,范围 [-1, 1]
      Throws:
      IllegalArgumentException - if arrays are null, empty, different lengths, or have fewer than 2 elements
    • weightedMean

      public static double weightedMean(double[] values, double[] weights)
      Computes the weighted arithmetic mean. 计算加权算术平均值
      Parameters:
      values - the data values / 数据值
      weights - the weights (must all be non-negative, at least one positive) / 权重(必须非负,至少一个正值)
      Returns:
      the weighted mean / 加权平均值
      Throws:
      IllegalArgumentException - if arrays are null, empty, different lengths, or weights are invalid
    • geometricMean

      public static double geometricMean(double[] values)
      Computes the geometric mean. All values must be positive. 计算几何平均值。所有值必须为正数。
      Parameters:
      values - the data values (all must be > 0) / 数据值(必须全部大于 0)
      Returns:
      the geometric mean / 几何平均值
      Throws:
      IllegalArgumentException - if values is null, empty, or contains non-positive values
    • harmonicMean

      public static double harmonicMean(double[] values)
      Computes the harmonic mean. All values must be positive. 计算调和平均值。所有值必须为正数。
      Parameters:
      values - the data values (all must be > 0) / 数据值(必须全部大于 0)
      Returns:
      the harmonic mean / 调和平均值
      Throws:
      IllegalArgumentException - if values is null, empty, or contains non-positive values
    • range

      public static double range(double[] data)
      Computes the range (max - min) of the data. 计算数据的极差(最大值 - 最小值)
      Parameters:
      data - the input data array / 输入数据数组
      Returns:
      the range / 极差
      Throws:
      IllegalArgumentException - if data is null or empty
    • interquartileRange

      public static double interquartileRange(double[] data)
      Computes the interquartile range (Q3 - Q1). 计算四分位距(Q3 - Q1)
      Parameters:
      data - the input data array / 输入数据数组
      Returns:
      the interquartile range / 四分位距
      Throws:
      IllegalArgumentException - if data is null or empty
    • spearmanCorrelation

      public static double spearmanCorrelation(double[] x, double[] y)
      Computes Spearman's rank correlation coefficient between two arrays. 计算两个数组的斯皮尔曼等级相关系数

      Converts both arrays to fractional ranks (averaging tied ranks), then computes the Pearson correlation on the ranks.

      将两个数组转换为分数秩(平均处理并列秩), 然后对秩计算皮尔逊相关系数。

      Parameters:
      x - the first data array / 第一个数据数组
      y - the second data array / 第二个数据数组
      Returns:
      Spearman's rho in [-1, 1] / 斯皮尔曼 rho,范围 [-1, 1]
      Throws:
      IllegalArgumentException - if arrays are null, empty, different lengths, have fewer than 2 elements, or contain NaN/Infinity
    • kendallCorrelation

      public static double kendallCorrelation(double[] x, double[] y)
      Computes Kendall's tau-b correlation coefficient between two arrays. 计算两个数组的肯德尔 tau-b 相关系数

      Uses a merge-sort based O(n log n) algorithm (Knight 2006) with proper tie correction (tau-b formula).

      使用基于归并排序的 O(n log n) 算法(Knight 2006), 并使用正确的并列校正(tau-b 公式)。

      Parameters:
      x - the first data array / 第一个数据数组
      y - the second data array / 第二个数据数组
      Returns:
      Kendall's tau-b in [-1, 1] / 肯德尔 tau-b,范围 [-1, 1]
      Throws:
      IllegalArgumentException - if arrays are null, empty, different lengths, have fewer than 2 elements, or contain NaN/Infinity