Class NormalDistribution

java.lang.Object
cloud.opencode.base.math.distribution.NormalDistribution

public final class NormalDistribution extends Object
Immutable normal (Gaussian) distribution. 不可变正态(高斯)分布

Provides PDF, CDF, inverse CDF (quantile function), and random sampling. Thread-safe: instances are immutable and sampling uses ThreadLocalRandom.

提供概率密度函数、累积分布函数、逆累积分布函数(分位函数)和随机抽样。 线程安全:实例不可变,抽样使用 ThreadLocalRandom

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

    Fields
    Modifier and Type
    Field
    Description
    static final NormalDistribution
    Standard normal distribution N(0, 1). / 标准正态分布 N(0, 1)
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    cdf(double x)
    Computes the cumulative distribution function (CDF) at x.
    double
    inverseCdf(double p)
    Computes the inverse CDF (quantile function / percent-point function).
    double
    Returns the mean of this distribution.
    of(double mean, double stdDev)
    Creates a normal distribution with the specified mean and standard deviation.
    double
    pdf(double x)
    Computes the probability density function (PDF) at x.
    double
    Generates one random sample from this distribution using the Box-Muller transform.
    double[]
    sample(int n)
     
    double
    Returns the standard deviation of this distribution.
     
    double
    Returns the variance of this distribution.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • STANDARD

      public static final NormalDistribution STANDARD
      Standard normal distribution N(0, 1). / 标准正态分布 N(0, 1)
  • Method Details

    • of

      public static NormalDistribution of(double mean, double stdDev)
      Creates a normal distribution with the specified mean and standard deviation. 创建具有指定均值和标准差的正态分布
      Parameters:
      mean - the mean / 均值
      stdDev - the standard deviation, must be > 0 / 标准差,必须 > 0
      Returns:
      a new NormalDistribution instance / 新的正态分布实例
      Throws:
      IllegalArgumentException - if stdDev ≤ 0
    • mean

      public double mean()
      Returns the mean of this distribution. 返回此分布的均值
      Returns:
      the mean / 均值
    • standardDeviation

      public double standardDeviation()
      Returns the standard deviation of this distribution. 返回此分布的标准差
      Returns:
      the standard deviation / 标准差
    • variance

      public double variance()
      Returns the variance of this distribution. 返回此分布的方差
      Returns:
      the variance (stdDev^2) / 方差
    • pdf

      public double pdf(double x)
      Computes the probability density function (PDF) at x. 计算在 x 处的概率密度函数值

      f(x) = (1 / (sigma * sqrt(2*pi))) * exp(-0.5 * ((x - mu) / sigma)^2)

      Parameters:
      x - the point at which to evaluate the PDF / 计算 PDF 的点
      Returns:
      the density at x / x 处的密度值
    • cdf

      public double cdf(double x)
      Computes the cumulative distribution function (CDF) at x. 计算在 x 处的累积分布函数值

      F(x) = 0.5 * (1 + erf((x - mu) / (sigma * sqrt(2))))

      Parameters:
      x - the point at which to evaluate the CDF / 计算 CDF 的点
      Returns:
      the cumulative probability P(X ≤ x) / 累积概率
    • inverseCdf

      public double inverseCdf(double p)
      Computes the inverse CDF (quantile function / percent-point function). 计算逆累积分布函数(分位函数)

      Uses the rational approximation algorithm by Peter Acklam for the standard normal quantile, then scales by mean and standard deviation.

      使用 Peter Acklam 的有理近似算法计算标准正态分位数,然后按均值和标准差缩放。

      Parameters:
      p - the cumulative probability, must be in (0, 1) / 累积概率,必须在 (0, 1) 内
      Returns:
      the quantile x such that P(X ≤ x) = p / 使得 P(X ≤ x) = p 的分位数
      Throws:
      MathException - if p is not in (0, 1)
    • sample

      public double sample()
      Generates one random sample from this distribution using the Box-Muller transform. 使用 Box-Muller 变换从此分布生成一个随机样本
      Returns:
      a random sample / 随机样本
    • sample

      public double[] sample(int n)
    • toString

      public String toString()
      Overrides:
      toString in class Object