Class MathUtils
java.lang.Object
com.quantfinlib.util.MathUtils
Numerical primitives shared across the library. All methods are allocation-light
and operate on primitive arrays.
-
Method Summary
Modifier and TypeMethodDescriptionstatic double[][]cholesky(double[][] a) Cholesky decomposition: returns lower-triangular L with A = L * L'.static doubleclamp(double v, double lo, double hi) static doublecorrelation(double[] a, double[] b) static doublecovariance(double[] a, double[] b) Sample covariance of two equally-sized series (n >= 2).static doubledecayFactor(long dtNanos, long halfLifeNanos) Exponential decay factor for a half-life over an elapsed interval:exp(-dt·ln2/halfLife); 1.0 for non-positivedt.static doubledot(double[] a, double[] b) static double[][]inverse(double[][] a) Matrix inverse by Gauss-Jordan elimination with partial pivoting.static doublekurtosis(double[] v) Population kurtosis: m4 / m2^2 (3 for a normal distribution, not excess).static doublelogGamma(double x) Natural log of the gamma function (Lanczos, |relative error| < 2e-10).static double[]matVec(double[][] m, double[] v) static doublemean(double[] v) static doublemean(double[] v, int from, int to) static double[]nanArray(int n) static doublenormCdf(double x) Standard normal CDF (Abramowitz & Stegun 26.2.17, |error| < 7.5e-8).static doublenormInv(double p) Inverse standard normal CDF (Acklam's approximation, |error| invalid input: '<' 1.15e-9).static doublenormPdf(double x) Standard normal density.static voidpairSort(double[] keys, int[] values) Sortskeysascending while permutingvaluesidentically — the primitive replacement for boxing anInteger[]index array through a comparator sort (no allocation beyond the caller's arrays, no boxed compares).static doublepercentile(double[] values, double p) Linear-interpolated percentile, p in [0, 1].static doublepercentileSorted(double[] sorted, double p) Percentile on an already-sorted array (no copy).static doublequadraticForm(double[] w, double[][] m) w' * M * w (quadratic form).static doubleregularizedIncompleteBeta(double a, double b, double x) Regularized incomplete beta function I_x(a, b) via the continued fraction (modified Lentz), switching to the symmetryI_x(a,b) = 1 − I_{1−x}(b,a)where the fraction converges fastest.static doubleskewness(double[] v) Population skewness: m3 / m2^1.5.static double[]solveLinear(double[][] a, double[] b) SolvesA x = bby Gaussian elimination with partial pivoting.static doublestdDev(double[] v) Sample standard deviation.static doublestdDevP(double[] v, int from, int to) Population standard deviation over [from, to).static doublestdDevSample(double[] v, int from, int to) Sample standard deviation over [from, to).static doubletCdf(double t, double df) Student-t CDF withdfdegrees 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 doublevariance(double[] v) Sample variance (n - 1 denominator).
-
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) Sortskeysascending while permutingvaluesidentically — the primitive replacement for boxing anInteger[]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) SolvesA x = bby 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-positivedt. 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 symmetryI_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 withdfdegrees 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.
-