Class MathUtils

java.lang.Object
com.quantfinlib.util.MathUtils

public final class MathUtils extends Object
Numerical primitives shared across the library. All methods are allocation-light and operate on primitive arrays.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[][]
    cholesky(double[][] a)
    Cholesky decomposition: returns lower-triangular L with A = L * L'.
    static double
    clamp(double v, double lo, double hi)
     
    static double
    correlation(double[] a, double[] b)
     
    static double
    covariance(double[] a, double[] b)
    Sample covariance of two equally-sized series (n >= 2).
    static double
    decayFactor(long dtNanos, long halfLifeNanos)
    Exponential decay factor for a half-life over an elapsed interval: exp(-dt·ln2/halfLife); 1.0 for non-positive dt.
    static double
    dot(double[] a, double[] b)
     
    static double[][]
    inverse(double[][] a)
    Matrix inverse by Gauss-Jordan elimination with partial pivoting.
    static double
    kurtosis(double[] v)
    Population kurtosis: m4 / m2^2 (3 for a normal distribution, not excess).
    static double
    logGamma(double x)
    Natural log of the gamma function (Lanczos, |relative error| < 2e-10).
    static double[]
    matVec(double[][] m, double[] v)
     
    static double
    mean(double[] v)
     
    static double
    mean(double[] v, int from, int to)
     
    static double[]
    nanArray(int n)
     
    static double
    normCdf(double x)
    Standard normal CDF (Abramowitz & Stegun 26.2.17, |error| < 7.5e-8).
    static double
    normInv(double p)
    Inverse standard normal CDF (Acklam's approximation, |error| invalid input: '<' 1.15e-9).
    static double
    normPdf(double x)
    Standard normal density.
    static void
    pairSort(double[] keys, int[] values)
    Sorts keys ascending while permuting values identically — the primitive replacement for boxing an Integer[] index array through a comparator sort (no allocation beyond the caller's arrays, no boxed compares).
    static double
    percentile(double[] values, double p)
    Linear-interpolated percentile, p in [0, 1].
    static double
    percentileSorted(double[] sorted, double p)
    Percentile on an already-sorted array (no copy).
    static double
    quadraticForm(double[] w, double[][] m)
    w' * M * w (quadratic form).
    static double
    regularizedIncompleteBeta(double a, double b, double x)
    Regularized incomplete beta function I_x(a, b) via the continued fraction (modified Lentz), switching to the symmetry I_x(a,b) = 1 − I_{1−x}(b,a) where the fraction converges fastest.
    static double
    skewness(double[] v)
    Population skewness: m3 / m2^1.5.
    static double[]
    solveLinear(double[][] a, double[] b)
    Solves A x = b by Gaussian elimination with partial pivoting.
    static double
    stdDev(double[] v)
    Sample standard deviation.
    static double
    stdDevP(double[] v, int from, int to)
    Population standard deviation over [from, to).
    static double
    stdDevSample(double[] v, int from, int to)
    Sample standard deviation over [from, to).
    static double
    tCdf(double t, double df)
    Student-t CDF with df degrees of freedom — exact via the regularized incomplete beta (P(T ≤ t) = 1 − ½·I_{ν/(ν+t²)}(ν/2, ½) for t ≥ 0), no normal approximation: the tails are precisely where a t distribution and its moment-matched normal disagree most.
    static double
    variance(double[] v)
    Sample variance (n - 1 denominator).

    Methods inherited from class java.lang.Object

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

    • mean

      public static double mean(double[] v)
    • mean

      public static double mean(double[] v, int from, int to)
    • variance

      public static double variance(double[] v)
      Sample variance (n - 1 denominator).
    • stdDev

      public static double stdDev(double[] v)
      Sample standard deviation.
    • stdDevP

      public static double stdDevP(double[] v, int from, int to)
      Population standard deviation over [from, to).
    • stdDevSample

      public static double stdDevSample(double[] v, int from, int to)
      Sample standard deviation over [from, to).
    • percentile

      public static double percentile(double[] values, double p)
      Linear-interpolated percentile, p in [0, 1]. Copies and sorts the input.
    • percentileSorted

      public static double percentileSorted(double[] sorted, double p)
      Percentile on an already-sorted array (no copy).
    • dot

      public static double dot(double[] a, double[] b)
    • matVec

      public static double[] matVec(double[][] m, double[] v)
    • quadraticForm

      public static double quadraticForm(double[] w, double[][] m)
      w' * M * w (quadratic form).
    • covariance

      public static double covariance(double[] a, double[] b)
      Sample covariance of two equally-sized series (n >= 2).
    • correlation

      public static double correlation(double[] a, double[] b)
    • cholesky

      public static double[][] cholesky(double[][] a)
      Cholesky decomposition: returns lower-triangular L with A = L * L'. Adds tiny diagonal jitter if the matrix is BORDERLINE non-PSD (rank-deficient factor models produce pivots a hair below zero), but a pivot grossly negative relative to the diagonal scale means the input is genuinely indefinite — a typo'd correlation > 1, or inconsistent pairwise estimates — and simulating a silently clamped, DIFFERENT dependence structure would misstate risk, so that fails loudly instead.
    • normInv

      public static double normInv(double p)
      Inverse standard normal CDF (Acklam's approximation, |error| invalid input: '<' 1.15e-9).
    • skewness

      public static double skewness(double[] v)
      Population skewness: m3 / m2^1.5.
    • kurtosis

      public static double kurtosis(double[] v)
      Population kurtosis: m4 / m2^2 (3 for a normal distribution, not excess).
    • pairSort

      public static void pairSort(double[] keys, int[] values)
      Sorts keys ascending while permuting values identically — the primitive replacement for boxing an Integer[] index array through a comparator sort (no allocation beyond the caller's arrays, no boxed compares). Quicksort with median-of-three pivots and an insertion cutoff; NaN keys are not supported (callers filter NaN before ranking/selecting).
    • normPdf

      public static double normPdf(double x)
      Standard normal density.
    • normCdf

      public static double normCdf(double x)
      Standard normal CDF (Abramowitz & Stegun 26.2.17, |error| < 7.5e-8).
    • solveLinear

      public static double[] solveLinear(double[][] a, double[] b)
      Solves A x = b by Gaussian elimination with partial pivoting. Inputs are not modified.
    • inverse

      public static double[][] inverse(double[][] a)
      Matrix inverse by Gauss-Jordan elimination with partial pivoting.
    • clamp

      public static double clamp(double v, double lo, double hi)
    • nanArray

      public static double[] nanArray(int n)
    • decayFactor

      public static double decayFactor(long dtNanos, long halfLifeNanos)
      Exponential decay factor for a half-life over an elapsed interval: exp(-dt·ln2/halfLife); 1.0 for non-positive dt. The single home for the half-life→decay conversion the streaming estimators use — the ln2 factor is exactly the constant that goes missing when this is re-spelled per class.
    • logGamma

      public static double logGamma(double x)
      Natural log of the gamma function (Lanczos, |relative error| < 2e-10).
    • regularizedIncompleteBeta

      public static double regularizedIncompleteBeta(double a, double b, double x)
      Regularized incomplete beta function I_x(a, b) via the continued fraction (modified Lentz), switching to the symmetry I_x(a,b) = 1 − I_{1−x}(b,a) where the fraction converges fastest. Accurate to ~1e-13 across (0, 1).
    • tCdf

      public static double tCdf(double t, double df)
      Student-t CDF with df degrees of freedom — exact via the regularized incomplete beta (P(T ≤ t) = 1 − ½·I_{ν/(ν+t²)}(ν/2, ½) for t ≥ 0), no normal approximation: the tails are precisely where a t distribution and its moment-matched normal disagree most.