Class StreamingStatistics

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

public final class StreamingStatistics extends Object
Mutable accumulator for computing streaming (online) statistics using Welford's algorithm. 使用 Welford 算法计算流式(在线)统计的可变累加器

Computes count, mean, variance, standard deviation, min, max, and sum in a single pass. Uses Welford's numerically stable online algorithm for variance computation.

在单次遍历中计算计数、均值、方差、标准差、最小值、最大值和总和。 使用 Welford 的数值稳定在线算法计算方差。

NOT thread-safe. For parallel processing, create separate instances and combine them using merge(StreamingStatistics).

非线程安全。对于并行处理,请创建单独的实例并使用 merge(StreamingStatistics) 合并。

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

    • create

      public static StreamingStatistics create()
      Creates a new empty streaming statistics accumulator. 创建新的空流式统计累加器
      Returns:
      a new empty instance / 新的空实例
    • add

      public void add(double value)
      Adds a value to the accumulator using Welford's online algorithm. 使用 Welford 在线算法向累加器添加一个值
      Parameters:
      value - the value to add (must be finite) / 要添加的值(必须是有限数)
      Throws:
      MathException - if value is NaN or Infinity / 如果值为 NaN 或无穷大
    • count

      public long count()
      Returns the number of values added. 返回已添加的值的数量
      Returns:
      the count / 计数
    • mean

      public double mean()
      Returns the arithmetic mean of all added values. 返回所有已添加值的算术平均值
      Returns:
      the mean / 平均值
      Throws:
      MathException - if no values have been added / 如果没有添加任何值
    • variance

      public double variance()
      Returns the population variance (biased, divided by n). 返回总体方差(有偏,除以 n)
      Returns:
      the population variance / 总体方差
      Throws:
      MathException - if no values have been added / 如果没有添加任何值
    • sampleVariance

      public double sampleVariance()
      Returns the sample variance (unbiased, divided by n-1). 返回样本方差(无偏,除以 n-1)
      Returns:
      the sample variance / 样本方差
      Throws:
      MathException - if fewer than 2 values have been added / 如果添加的值少于 2 个
    • stdDev

      public double stdDev()
      Returns the population standard deviation (sqrt of population variance). 返回总体标准差(总体方差的平方根)
      Returns:
      the population standard deviation / 总体标准差
      Throws:
      MathException - if no values have been added / 如果没有添加任何值
    • sampleStdDev

      public double sampleStdDev()
      Returns the sample standard deviation (sqrt of sample variance). 返回样本标准差(样本方差的平方根)
      Returns:
      the sample standard deviation / 样本标准差
      Throws:
      MathException - if fewer than 2 values have been added / 如果添加的值少于 2 个
    • min

      public double min()
      Returns the minimum value added. 返回已添加的最小值
      Returns:
      the minimum value / 最小值
      Throws:
      MathException - if no values have been added / 如果没有添加任何值
    • max

      public double max()
      Returns the maximum value added. 返回已添加的最大值
      Returns:
      the maximum value / 最大值
      Throws:
      MathException - if no values have been added / 如果没有添加任何值
    • sum

      public double sum()
      Returns the sum of all added values. 返回所有已添加值的总和
      Returns:
      the sum / 总和
      Throws:
      MathException - if no values have been added / 如果没有添加任何值
    • merge

      Merges this accumulator with another, returning a NEW instance. Both input instances remain unchanged. 将此累加器与另一个合并,返回新实例。两个输入实例保持不变。

      Uses the parallel Welford merge formula for numerical stability.

      使用并行 Welford 合并公式以保证数值稳定性。

      Parameters:
      other - the other accumulator to merge / 要合并的另一个累加器
      Returns:
      a new merged instance / 新的合并实例
      Throws:
      MathException - if other is null / 如果 other 为 null
    • reset

      public void reset()
      Resets all accumulated state to empty. 重置所有累积状态为空
    • collector

      Returns a Collector for use with Stream.collect(Collector). 返回用于 Stream.collect(Collector)Collector

      Example usage: stream.collect(StreamingStatistics.collector())

      用法示例:stream.collect(StreamingStatistics.collector())

      Returns:
      a collector that produces a StreamingStatistics / 生成 StreamingStatistics 的收集器
    • toString

      public String toString()
      Overrides:
      toString in class Object