v2026.11.2
All Bundles
Bundle

Classical statistics for small analyses: descriptive statistics, the Student t, normal, chi-square and F distributions, t-tests with confidence intervals and effect sizes, Pearson correlation with a p-value, and an ordinary least squares fit with the full summary table (standard errors, t statistics, p values, confidence intervals, F test, R-squared, AIC and BIC). Compile with -lib stats,gen_collect; it needs nothing from System.ML.

No call here ends the program on bad input. A function that returns a Float returns NaN, and a function that returns a result object returns one whose IsValid() is false and whose GetError() says why. Design and verification notes: docs/AI.md, "Statistics".

TTest

t-tests. Every test takes an optional confidence level (default 0.95) for the interval on the mean difference. All return a TTestResult; none can end the program.

Failures (IsValid() false): Nil input, a sample too short (one-sample and paired need 2 values, two-sample needs 2 per group), unequal lengths for the paired test, NaN or infinity in the data, a level not strictly between 0 and 1, and zero variance (a standard error of 0 makes t undefined).

Example

a := [4.1, 5.2, 6.3, 5.8, 4.9, 5.5];
b := [6.0, 7.1, 6.8, 7.4, 6.5, 7.0];
r := TTest->Welch(a, b);
r->ToString()->PrintLine();
if(r->IsValid() & r->GetP() < 0.05) {
  "means differ, d = {$r->GetCohenD()}"->PrintLine();
};

Operations

OneSample # function

One-sample t-test of the mean of x against a hypothesised mean, at the 95% level.

function : OneSample(x:Float[], mu:Float) ~ TTestResult

Parameters

NameTypeDescription
xFloatsample, at least 2 values
muFloathypothesised mean

Return

TypeDescription
TTestResultresult; the difference is mean(x) - mu

OneSample # function

One-sample t-test of the mean of x against a hypothesised mean.

function : OneSample(x:Float[], mu:Float, level:Float) ~ TTestResult

Parameters

NameTypeDescription
xFloatsample, at least 2 values
muFloathypothesised mean
levelFloatconfidence level of the interval, strictly between 0 and 1

Return

TypeDescription
TTestResultresult; the difference is mean(x) - mu

Paired # function

Paired t-test, at the 95% level.

function : Paired(x:Float[], y:Float[]) ~ TTestResult

Parameters

NameTypeDescription
xFloatfirst measurements
yFloatsecond measurements, same length as x, at least 2

Return

TypeDescription
TTestResultresult; the difference is mean(x - y)

Paired # function

Paired t-test: a one-sample test of the differences x - y against 0. Cohen's d is d_z, the mean difference over the standard deviation of the differences.

function : Paired(x:Float[], y:Float[], level:Float) ~ TTestResult

Parameters

NameTypeDescription
xFloatfirst measurements
yFloatsecond measurements, same length as x, at least 2
levelFloatconfidence level of the interval, strictly between 0 and 1

Return

TypeDescription
TTestResultresult; the difference is mean(x - y)

TwoSample # function

Student's two-sample t-test (equal variances assumed), at the 95% level.

function : TwoSample(x:Float[], y:Float[]) ~ TTestResult

Parameters

NameTypeDescription
xFloatfirst sample, at least 2 values
yFloatsecond sample, at least 2 values

Return

TypeDescription
TTestResultresult; the difference is mean(x) - mean(y)

TwoSample # function

Student's two-sample t-test (equal variances assumed, pooled variance, df = n1 + n2 - 2).

function : TwoSample(x:Float[], y:Float[], level:Float) ~ TTestResult

Parameters

NameTypeDescription
xFloatfirst sample, at least 2 values
yFloatsecond sample, at least 2 values
levelFloatconfidence level of the interval, strictly between 0 and 1

Return

TypeDescription
TTestResultresult; the difference is mean(x) - mean(y)

Welch # function

Welch's t-test (unequal variances, Welch-Satterthwaite degrees of freedom), at the 95% level.

function : Welch(x:Float[], y:Float[]) ~ TTestResult

Parameters

NameTypeDescription
xFloatfirst sample, at least 2 values
yFloatsecond sample, at least 2 values

Return

TypeDescription
TTestResultresult; the difference is mean(x) - mean(y)

Welch # function

Welch's t-test (unequal variances). Cohen's d and Hedges' g use the pooled standard deviation, as for the Student test.

function : Welch(x:Float[], y:Float[], level:Float) ~ TTestResult

Parameters

NameTypeDescription
xFloatfirst sample, at least 2 values
yFloatsecond sample, at least 2 values
levelFloatconfidence level of the interval, strictly between 0 and 1

Return

TypeDescription
TTestResultresult; the difference is mean(x) - mean(y)