Skip to content

HMath.Statistics.Advanced

Module index · Numerical conventions

Description

Finite-data checked statistics. Quantiles use Hyndman--Fan type 7; sample variance divides by n-1; skewness and excess kurtosis use population central moments. Weights are reliability weights normalized by their positive sum. NaN/infinity are rejected; masked selects True entries before validation. Hypothesis tests report a statistic, a two-sided p value (unless intrinsically upper-tailed), and degrees of freedom. Rank tests use tie-corrected normal/ chi-square asymptotics. KS uses the asymptotic continuous-null law; normality tests require a fully specified or fitted normal as stated on each function.

On this page

API Purpose
arithmeticMean Mean of finite nonempty data.
weightedMean Reliability-weighted mean of observations and nonnegative finite weights; weights must have finite positive total.
geometricMean exp(mean(log x)) for strictly positive finite observations.
harmonicMean Reciprocal of the mean reciprocal, for strictly positive finite observations.
rms Root-mean-square with max scaling to avoid squaring overflow.
trimmedMean Discard floor(fraction*n) samples from each sorted tail, with fraction in [0,1/2), then average.
winsorizedMean Replace floor(fraction*n) values at each tail with the nearest retained value, then average.
median Type-7 50th percentile; averages the central pair for an even sample count.
modes All values of maximum multiplicity, sorted ascending; exact floating equality defines ties.
quantile Hyndman--Fan type-7 quantile with linear interpolation and probability p in [0,1].
percentile Type-7 quantile with percentage in [0,100].
interquartileRange Type-7 75th percentile minus 25th percentile.
medianAbsoluteDeviation Median absolute deviation from the median, without the normal consistency multiplier.
dataRange Maximum minus minimum; arithmetic overflow is an explicit error.
populationVariance Second population central moment, dividing by n.
sampleVariance Unbiased sample variance, dividing by n-1; requires at least two observations.
populationSD Square root of population variance.
sampleSD Square root of unbiased sample variance.
coefficientOfVariation Population standard deviation divided by absolute mean; rejects zero mean.
centralMoment Population central moment of integer order 0..32; nonfinite intermediate/final arithmetic is rejected.
skewness Uncorrected population third standardized central moment; requires nonconstant observations.
kurtosis Uncorrected population excess kurtosis, subtracting three; requires nonconstant observations.
covariance Covariance of two matching finite data vectors; True selects the n-1 sample correction.
correlation Pearson correlation of matching nonconstant data vectors.
covarianceMatrix Covariance matrix for variables supplied as columns (each list is one variable).
spearman Pearson correlation of average tied ranks; matching nonconstant rank vectors required.
kendall Kendall tau-b with tied-pair correction; quadratic reference implementation.
partialCorrelation Partial correlation of variable indices i,j conditional on every other supplied column, through the inverse sample covariance.
autocorrelation Pearson correlation between overlapping lagged segments; zero<=lag<n-1.
crossCorrelation Pearson correlation of overlapping x_t and y_(t+lag), using their common finite length.
movingAverage Valid-window moving means; window must lie in [1,n], with no endpoint padding.
exponentialMovingAverage EMA with alpha in (0,1], initialized at the first observation.
movingVariance Valid-window population variances with no endpoint padding.
Streaming Opaque Welford accumulator storing an unbounded Integer count, mean and sum of squared deviations.
emptyStreaming Zero-count Welford accumulator.
updateStreaming Add one finite observation by Welford recurrence; nonfinite arithmetic is rejected.
combineStreaming Chan parallel merge of two Welford accumulators; empty accumulators are identities.
streamingResult Return count, mean and variance; True uses n-1 correction and requires at least two samples.
histogramND Histogram edges must increase strictly; intervals are [a,b), with the last interval closed on its right.
entropy Shannon entropy in nats for normalized nonnegative probabilities, with 0*log(0)=0.
mutualInformation Mutual information in nats from a rectangular normalized joint-probability table.
klDivergence KL(P||Q) in nats, including positive infinity when Q is zero on positive P mass.
jsDivergence Equal-mixture Jensen--Shannon divergence in nats, bounded by log 2.
zScores Standardize with population standard deviation; constant samples are rejected.
standardError Unbiased sample standard deviation divided by sqrt n, requiring n>=2.
confidenceInterval Two-sided Student-t confidence interval for a normal population mean with unknown variance; confidence level in (0,1).
bootstrap Seeded nonparametric resampling with replacement.
jackknife Leave-one-out bias-corrected statistic and jackknife standard error; requires n>=2.
TestResult Test statistic, p value clamped to [0,1] after numerical checks, and applicable degrees of freedom.
TestResult Declaration and type contract below.
testStatistic Computed statistic for the selected hypothesis test.
pValue Reported tail probability, clamped to [0,1]; interpret using the test's documented alternative hypothesis.
degreesOfFreedom Distribution degrees-of-freedom parameters in test-specific order (for example numerator then denominator for F tests).
oneSampleT Two-sided one-sample Student t test against a finite null mean; assumes independent normal observations.
twoSampleT Two-sided pooled t test when True, Welch unequal-variance test when False; each group requires n>=2 and positive combined variation.
pairedT Two-sided one-sample t test on paired differences; matching group lengths required.
zTest Two-sided one-sample normal z test with known strictly positive population standard deviation.
chiSquareTest Pearson goodness-of-fit chi-square statistic for observed and positive expected counts; first argument is number of fitted nuisance parameters.
varianceFTest Two-sided normal-population variance-ratio F test, requiring positive sample variances.
anova One-way fixed-effects F test on independent normal groups with common variance; each group requires n>=2.
welchAnova Welch one-way heteroscedastic F test with Satterthwaite denominator degrees of freedom.
kruskalWallis Tie-corrected Kruskal--Wallis H statistic with chi-square asymptotic calibration.
mannWhitney Mann--Whitney U for the first sample; two-sided tie-corrected normal approximation with continuity correction.
wilcoxon Wilcoxon signed-rank test for differences, dropping zeros and using average absolute tied ranks and continuity-corrected normal calibration.
kolmogorovSmirnov Two-sided KS statistic and asymptotic p value against a fully specified continuous distribution.
shapiroWilk Royston's AS R94 coefficients and normal-tail approximation, n=3..5000, uncensored samples.
andersonDarling Anderson--Darling normality test after fitting the sample mean and unbiased sample standard deviation.
levene One-way ANOVA on absolute deviations; True chooses group medians (Brown--Forsythe), False chooses means.
bartlett Bartlett homogeneity-of-variance test, appropriate for normal samples; positive group variances required.
fisherExact Two-sided Fisher exact test on nonnegative 2-by-2 counts (a,b;c,d).
permutationTest Seeded two-sided random-label permutation p value for a caller statistic, with the (b+1)/(B+1) correction.
bonferroni Bonferroni-adjusted p values in original order, capped at one.
benjaminiHochberg Monotone Benjamini--Hochberg FDR-adjusted p values in original order.
cohensD Difference of sample means divided by pooled unbiased standard deviation.
etaSquared One-way ANOVA explained sum of squares divided by total sum of squares.
normalTestPower Exact power under a known-variance two-sided one-sample normal z-test.
normalSampleSize Smallest integer n whose exact known-variance two-sided normal-test power meets the target; hard budget n<=100000000.
tukeyOutliers Zero-based indices outside [Q1-kIQR,Q3+kIQR], with positive multiplier k and type-7 quartiles.
grubbs Two-sided single-outlier Grubbs test, returning candidate index, G statistic and rejection decision at alpha; normality assumption required.
masked Keep True-mask observations, then validate retained values.
huberLocation Huber M location by iteratively reweighted means, scaled by 1.4826*MAD; zero MAD returns the median.

Documentation

arithmeticMean

arithmeticMean :: [Double] -> Either MathError Double Source #

Mean of finite nonempty data. Dividing each observation before summation avoids preventable same-sign sum overflow.

weightedMean

weightedMean :: [Double] -> [Double] -> Either MathError Double Source #

Reliability-weighted mean of observations and nonnegative finite weights; weights must have finite positive total.

geometricMean

geometricMean :: [Double] -> Either MathError Double Source #

exp(mean(log x)) for strictly positive finite observations.

harmonicMean

harmonicMean :: [Double] -> Either MathError Double Source #

Reciprocal of the mean reciprocal, for strictly positive finite observations.

rms

rms :: [Double] -> Either MathError Double Source #

Root-mean-square with max scaling to avoid squaring overflow.

trimmedMean

trimmedMean :: Double -> [Double] -> Either MathError Double Source #

Discard floor(fraction*n) samples from each sorted tail, with fraction in [0,1/2), then average.

winsorizedMean

winsorizedMean :: Double -> [Double] -> Either MathError Double Source #

Replace floor(fraction*n) values at each tail with the nearest retained value, then average.

median

median :: [Double] -> Either MathError Double Source #

Type-7 50th percentile; averages the central pair for an even sample count.

modes

modes :: [Double] -> Either MathError [Double] Source #

All values of maximum multiplicity, sorted ascending; exact floating equality defines ties.

quantile

quantile :: Double -> [Double] -> Either MathError Double Source #

Hyndman--Fan type-7 quantile with linear interpolation and probability p in [0,1].

percentile

percentile :: Double -> [Double] -> Either MathError Double Source #

Type-7 quantile with percentage in [0,100].

interquartileRange

interquartileRange :: [Double] -> Either MathError Double Source #

Type-7 75th percentile minus 25th percentile.

medianAbsoluteDeviation

medianAbsoluteDeviation :: [Double] -> Either MathError Double Source #

Median absolute deviation from the median, without the normal consistency multiplier.

dataRange

dataRange :: [Double] -> Either MathError Double Source #

Maximum minus minimum; arithmetic overflow is an explicit error.

populationVariance

populationVariance :: [Double] -> Either MathError Double Source #

Second population central moment, dividing by n.

sampleVariance

sampleVariance :: [Double] -> Either MathError Double Source #

Unbiased sample variance, dividing by n-1; requires at least two observations.

populationSD

populationSD :: [Double] -> Either MathError Double Source #

Square root of population variance.

sampleSD

sampleSD :: [Double] -> Either MathError Double Source #

Square root of unbiased sample variance.

coefficientOfVariation

coefficientOfVariation :: [Double] -> Either MathError Double Source #

Population standard deviation divided by absolute mean; rejects zero mean.

centralMoment

centralMoment :: Int -> [Double] -> Either MathError Double Source #

Population central moment of integer order 0..32; nonfinite intermediate/final arithmetic is rejected.

skewness

skewness :: [Double] -> Either MathError Double Source #

Uncorrected population third standardized central moment; requires nonconstant observations.

kurtosis

kurtosis :: [Double] -> Either MathError Double Source #

Uncorrected population excess kurtosis, subtracting three; requires nonconstant observations.

covariance

covariance :: Bool -> [Double] -> [Double] -> Either MathError Double Source #

Covariance of two matching finite data vectors; True selects the n-1 sample correction.

correlation

correlation :: [Double] -> [Double] -> Either MathError Double Source #

Pearson correlation of matching nonconstant data vectors.

covarianceMatrix

covarianceMatrix :: Bool -> [[Double]] -> Either MathError [[Double]] Source #

Covariance matrix for variables supplied as columns (each list is one variable). Boolean selects sample correction.

spearman

spearman :: [Double] -> [Double] -> Either MathError Double Source #

Pearson correlation of average tied ranks; matching nonconstant rank vectors required.

kendall

kendall :: [Double] -> [Double] -> Either MathError Double Source #

Kendall tau-b with tied-pair correction; quadratic reference implementation.

partialCorrelation

partialCorrelation :: [[Double]] -> Int -> Int -> Either MathError Double Source #

Partial correlation of variable indices i,j conditional on every other supplied column, through the inverse sample covariance.

autocorrelation

autocorrelation :: Int -> [Double] -> Either MathError Double Source #

Pearson correlation between overlapping lagged segments; zero<=lag<n-1.

crossCorrelation

crossCorrelation :: Int -> [Double] -> [Double] -> Either MathError Double Source #

Pearson correlation of overlapping x_t and y_(t+lag), using their common finite length.

movingAverage

movingAverage :: Int -> [Double] -> Either MathError [Double] Source #

Valid-window moving means; window must lie in [1,n], with no endpoint padding.

exponentialMovingAverage

exponentialMovingAverage :: Double -> [Double] -> Either MathError [Double] Source #

EMA with alpha in (0,1], initialized at the first observation.

movingVariance

movingVariance :: Int -> [Double] -> Either MathError [Double] Source #

Valid-window population variances with no endpoint padding.

Streaming

data Streaming Source #

Opaque Welford accumulator storing an unbounded Integer count, mean and sum of squared deviations.

Instances

Instances details
Show Streaming Source # 
Instance details

Defined in HMath.Statistics.Advanced

Eq Streaming Source # 
Instance details

Defined in HMath.Statistics.Advanced

emptyStreaming

emptyStreaming :: Streaming Source #

Zero-count Welford accumulator.

updateStreaming

updateStreaming :: Streaming -> Double -> Either MathError Streaming Source #

Add one finite observation by Welford recurrence; nonfinite arithmetic is rejected.

combineStreaming

combineStreaming :: Streaming -> Streaming -> Either MathError Streaming Source #

Chan parallel merge of two Welford accumulators; empty accumulators are identities.

streamingResult

streamingResult :: Bool -> Streaming -> Either MathError (Integer, Double, Double) Source #

Return count, mean and variance; True uses n-1 correction and requires at least two samples.

histogramND

histogramND :: [[Double]] -> [[Double]] -> [Double] -> Either MathError [Double] Source #

Histogram edges must increase strictly; intervals are [a,b), with the last interval closed on its right. Points outside any axis are excluded. Flattening is row-major, the last axis varies fastest. Weights need not sum to one.

entropy

entropy :: [Double] -> Either MathError Double Source #

Shannon entropy in nats for normalized nonnegative probabilities, with 0*log(0)=0.

mutualInformation

mutualInformation :: [[Double]] -> Either MathError Double Source #

Mutual information in nats from a rectangular normalized joint-probability table.

klDivergence

klDivergence :: [Double] -> [Double] -> Either MathError Double Source #

KL(P||Q) in nats, including positive infinity when Q is zero on positive P mass.

jsDivergence

jsDivergence :: [Double] -> [Double] -> Either MathError Double Source #

Equal-mixture Jensen--Shannon divergence in nats, bounded by log 2.

zScores

zScores :: [Double] -> Either MathError [Double] Source #

Standardize with population standard deviation; constant samples are rejected.

standardError

standardError :: [Double] -> Either MathError Double Source #

Unbiased sample standard deviation divided by sqrt n, requiring n>=2.

confidenceInterval

confidenceInterval :: Double -> [Double] -> Either MathError (Double, Double) Source #

Two-sided Student-t confidence interval for a normal population mean with unknown variance; confidence level in (0,1).

bootstrap

bootstrap :: Int -> ([Double] -> Either MathError Double) -> [Double] -> RNG -> Either MathError ([Double], RNG) Source #

Seeded nonparametric resampling with replacement. Returns caller statistic for each replicate and final RNG state.

jackknife

jackknife :: ([Double] -> Either MathError Double) -> [Double] -> Either MathError (Double, Double) Source #

Leave-one-out bias-corrected statistic and jackknife standard error; requires n>=2.

TestResult

data TestResult Source #

Test statistic, p value clamped to [0,1] after numerical checks, and applicable degrees of freedom.

Constructors

TestResult 

Fields

  • testStatistic :: Double

    Computed statistic for the selected hypothesis test.

  • pValue :: Double

    Reported tail probability, clamped to [0,1]; interpret using the test's documented alternative hypothesis.

  • degreesOfFreedom :: [Double]

    Distribution degrees-of-freedom parameters in test-specific order (for example numerator then denominator for F tests).

Instances

Instances details
Show TestResult Source # 
Instance details

Defined in HMath.Statistics.Advanced

Eq TestResult Source # 
Instance details

Defined in HMath.Statistics.Advanced

oneSampleT

oneSampleT :: Double -> [Double] -> Either MathError TestResult Source #

Two-sided one-sample Student t test against a finite null mean; assumes independent normal observations.

twoSampleT

twoSampleT :: Bool -> [Double] -> [Double] -> Either MathError TestResult Source #

Two-sided pooled t test when True, Welch unequal-variance test when False; each group requires n>=2 and positive combined variation.

pairedT

pairedT :: [Double] -> [Double] -> Either MathError TestResult Source #

Two-sided one-sample t test on paired differences; matching group lengths required.

zTest

zTest :: Double -> Double -> [Double] -> Either MathError TestResult Source #

Two-sided one-sample normal z test with known strictly positive population standard deviation.

chiSquareTest

chiSquareTest :: Int -> [Double] -> [Double] -> Either MathError TestResult Source #

Pearson goodness-of-fit chi-square statistic for observed and positive expected counts; first argument is number of fitted nuisance parameters.

varianceFTest

varianceFTest :: [Double] -> [Double] -> Either MathError TestResult Source #

Two-sided normal-population variance-ratio F test, requiring positive sample variances.

anova

anova :: [[Double]] -> Either MathError TestResult Source #

One-way fixed-effects F test on independent normal groups with common variance; each group requires n>=2.

welchAnova

welchAnova :: [[Double]] -> Either MathError TestResult Source #

Welch one-way heteroscedastic F test with Satterthwaite denominator degrees of freedom.

kruskalWallis

kruskalWallis :: [[Double]] -> Either MathError TestResult Source #

Tie-corrected Kruskal--Wallis H statistic with chi-square asymptotic calibration.

mannWhitney

mannWhitney :: [Double] -> [Double] -> Either MathError TestResult Source #

Mann--Whitney U for the first sample; two-sided tie-corrected normal approximation with continuity correction.

wilcoxon

wilcoxon :: [Double] -> Either MathError TestResult Source #

Wilcoxon signed-rank test for differences, dropping zeros and using average absolute tied ranks and continuity-corrected normal calibration.

kolmogorovSmirnov

kolmogorovSmirnov :: Distribution -> [Double] -> Either MathError TestResult Source #

Two-sided KS statistic and asymptotic p value against a fully specified continuous distribution. A Jacobi theta transformation handles very small statistics. Fitting parameters from these observations invalidates this null calibration.

shapiroWilk

shapiroWilk :: [Double] -> Either MathError TestResult Source #

Royston's AS R94 coefficients and normal-tail approximation, n=3..5000, uncensored samples. Numerical constants are published statistical data. Reference: https://doi.org/10.2307/2986146

andersonDarling

andersonDarling :: [Double] -> Either MathError TestResult Source #

Anderson--Darling normality test after fitting the sample mean and unbiased sample standard deviation. The returned statistic is raw A²; Stephens' p-value approximation uses A²*(1+0.75n+2.25n²). For adjusted A²>13, the published approximation is outside its calibrated range and the extreme tail is reported as zero instead of extrapolating its quadratic back upward.

levene

levene :: Bool -> [[Double]] -> Either MathError TestResult Source #

One-way ANOVA on absolute deviations; True chooses group medians (Brown--Forsythe), False chooses means.

bartlett

bartlett :: [[Double]] -> Either MathError TestResult Source #

Bartlett homogeneity-of-variance test, appropriate for normal samples; positive group variances required.

fisherExact

fisherExact :: Int -> Int -> Int -> Int -> Either MathError TestResult Source #

Two-sided Fisher exact test on nonnegative 2-by-2 counts (a,b;c,d). Enumerates tables with mass no larger than the observed table.

permutationTest

permutationTest :: Int -> ([Double] -> [Double] -> Either MathError Double) -> [Double] -> [Double] -> RNG -> Either MathError (TestResult, RNG) Source #

Seeded two-sided random-label permutation p value for a caller statistic, with the (b+1)/(B+1) correction.

bonferroni

bonferroni :: [Double] -> Either MathError [Double] Source #

Bonferroni-adjusted p values in original order, capped at one.

benjaminiHochberg

benjaminiHochberg :: [Double] -> Either MathError [Double] Source #

Monotone Benjamini--Hochberg FDR-adjusted p values in original order.

cohensD

cohensD :: [Double] -> [Double] -> Either MathError Double Source #

Difference of sample means divided by pooled unbiased standard deviation.

etaSquared

etaSquared :: [[Double]] -> Either MathError Double Source #

One-way ANOVA explained sum of squares divided by total sum of squares.

normalTestPower

normalTestPower :: Double -> Double -> Int -> Either MathError Double Source #

Exact power under a known-variance two-sided one-sample normal z-test.

normalSampleSize

normalSampleSize :: Double -> Double -> Double -> Either MathError Int Source #

Smallest integer n whose exact known-variance two-sided normal-test power meets the target; hard budget n<=100000000.

tukeyOutliers

tukeyOutliers :: Double -> [Double] -> Either MathError [Int] Source #

Zero-based indices outside [Q1-k*IQR,Q3+k*IQR], with positive multiplier k and type-7 quartiles.

grubbs

grubbs :: Double -> [Double] -> Either MathError (Int, Double, Bool) Source #

Two-sided single-outlier Grubbs test, returning candidate index, G statistic and rejection decision at alpha; normality assumption required.

masked

masked :: [Bool] -> [Double] -> Either MathError [Double] Source #

Keep True-mask observations, then validate retained values. Excluded NaN/infinity do not contaminate results.

huberLocation

huberLocation :: Double -> [Double] -> Either MathError Double Source #

Huber M location by iteratively reweighted means, scaled by 1.4826*MAD; zero MAD returns the median. Positive tuning and finite data required.