Skip to content

deepbox/stats

Hypothesis Tests & Power

Parametric and rank-based hypothesis tests, variance and normality tests, multiple-comparison corrections, and t-test power analysis.

  • Inference

Overview

  • Tests take tensors, flatten them, and return { statistic, pvalue }. alternative defaults to "two-sided" wherever it exists. anderson, chi2Contingency and fTwoway return larger result objects.
  • ttestInd assumes equal variances by default, like SciPy. Pass { equalVar: false } for Welch's test, which uses Welch-Satterthwaite degrees of freedom.
  • mannwhitneyu and wilcoxon use exact p-values for small samples, like SciPy's method="auto", and the mannwhitneyu statistic is U1. wilcoxon takes zeroMethod. kstest, ks2samp and anderson follow SciPy 1.17.
  • bonferroni, holm, hochberg, sidak, benjaminiHochberg and benjaminiYekutieli take an array of p-values and a level, and return { pvalues, corrected, rejected }.
  • tTestPower({ effectSize, nObs, alpha, power }) solves for whichever one field you leave out. When it solves for nObs, it returns the smallest integer sample size that reaches the power.

Types

15

MultipleComparisonResult

interface

Result of a multiple comparison correction.

  • pvalues: readonly number[]

    Original (uncorrected) p-values

  • corrected: readonly number[]

    Corrected p-values

  • rejected: readonly boolean[]

    Boolean array indicating which hypotheses are rejected at given alpha

PowerAnalysisResult

interface

Result of a power analysis.

  • effectSize: number
  • nObs: number
  • alpha: number
  • power: number

TTestPowerOptions

interface

Options for power analysis of a two-sample t-test.

  • effectSize?: number

    Cohen's d effect size

  • nObs?: number

    Sample size of the first group (per group when ratio is 1)

  • alpha?: number

    Significance level (Type I error rate)

  • power?: number

    Statistical power (1 - Type II error rate)

  • alternative?: 1 | 2

    Number of tails: 1 or 2 (default: 2)

  • ratio?: number

    Ratio of the size of the second group to the first, nObs2 / nObs (default: 1)

AndersonResult

type

Result of anderson.

criticalValues[i] is the critical value of the statistic at significance level significanceLevel[i] (given as a fraction, e.g. 0.05; SciPy reports percentages). Reject normality at that level when statistic exceeds the critical value.

  • statistic: number
  • criticalValues: number[]
  • significanceLevel: number[]
  • critical_values: number[]deprecated

    Same as criticalValues.

  • significance_level: number[]deprecated

    Same as significanceLevel.

  • pvalue: number

    Approximate p-value (D'Agostino and Stephens, 1986).

ContingencyResult

type

Result of a contingency table analysis (chi-square test).

  • statistic: number
  • pvalue: number
  • dof: number
  • expected: number[][]

KsTestOptions

interface

Options of kstest and ks_2samp.

  • alternative?: TestAlternative

    Alternative hypothesis (default: "two-sided").

  • method?: KsMethod

    p-value method.

MedianTestOptions

interface

Options of median_test.

  • correction?: boolean

    Apply Yates' continuity correction when there are two samples (default: true).

  • ties?: "below" | "above" | "ignore"

    Where values equal to the grand median are counted: "below" (default), "above", or "ignore" to drop them.

RankTestOptions

interface

Options shared by mannwhitneyu and wilcoxon.

  • alternative?: TestAlternative

    Alternative hypothesis (default: "two-sided").

  • method?: "auto" | "exact" | "asymptotic"

    p-value method.

  • correction?: boolean

    Apply a continuity correction of 0.5 in the normal approximation.

TestAlternative

type

Alternative hypothesis for tests that support one-sided p-values.

- "two-sided": the parameter differs from the null value (default). - "greater": the parameter (or first sample) is greater than the null value. - "less": the parameter (or first sample) is less than the null value.

type TestAlternative = "two-sided" | "less" | "greater";

TestResult

type

Result of a statistical hypothesis test.

  • statistic: number
  • pvalue: number

TTestIndOptions

interface

Options of ttest_ind.

  • equalVar?: boolean

    Assume equal population variances (default: true); false gives Welch's t-test.

  • alternative?: TestAlternative

    Alternative hypothesis (default: "two-sided").

TwoWayAnovaResult

type

Result of two-way ANOVA test.

  • factorA: TestResult
  • factorB: TestResult
  • interaction: TestResult

VarianceTestCenter

type

Location estimate used to center the groups in Levene's and Fligner-Killeen's tests.

type VarianceTestCenter = "mean" | "median" | "trimmed";

VarianceTestOptions

interface

Options of levene and fligner.

  • center?: VarianceTestCenter

    How each group is centered: "median" (default), "mean" or "trimmed".

  • proportiontocut?: number

    Fraction trimmed from each end for center: "trimmed" (default: 0.05).

WilcoxonOptions

interface

Options of wilcoxon.

  • zeroMethod?: "wilcox" | "pratt" | "zsplit"

    Treatment of zero differences (default: "wilcox").

Functions

30

benjaminiHochberg

function benjaminiHochberg(pvalues: readonly number[], alpha?: number): MultipleComparisonResult;

Benjamini-Hochberg (BH) procedure for controlling the false discovery rate (FDR).

A step-up procedure that controls the expected proportion of false positives among rejected hypotheses. More powerful than FWER-controlling methods when many tests are performed. Valid for independent or positively dependent tests.

Parameters

pvalues
readonly number[]
Array of p-values from individual tests
alphaoptional
number
Significance level / FDR level (default: 0.05)

Returns

MultipleComparisonResultCorrected p-values (q-values) and rejection decisions

Throws

  • InvalidParameterErrorIf pvalues is empty or contains a value outside [0, 1], or alpha is not in (0, 1)
Example
const result = benjaminiHochberg([0.01, 0.04, 0.03, 0.005], 0.05);result.corrected;  // [0.02, 0.04, 0.04, 0.02]

benjaminiYekutieli

function benjaminiYekutieli(pvalues: readonly number[], alpha?: number): MultipleComparisonResult;

Benjamini-Yekutieli (BY) procedure for controlling the false discovery rate (FDR).

Like benjaminiHochberg, but valid under arbitrary dependence between the tests. It divides the BH threshold by 1 + 1/2 + ... + 1/m, so it is more conservative.

Parameters

pvalues
readonly number[]
Array of p-values from individual tests
alphaoptional
number
FDR level (default: 0.05)

Returns

MultipleComparisonResultCorrected p-values (q-values) and rejection decisions

Throws

  • InvalidParameterErrorIf pvalues is empty or contains a value outside [0, 1], or alpha is not in (0, 1)
Example
const result = benjaminiYekutieli([0.01, 0.04, 0.03, 0.005], 0.05);

bonferroni

function bonferroni(pvalues: readonly number[], alpha?: number): MultipleComparisonResult;

Bonferroni correction for multiple comparisons.

The simplest and most conservative method. Multiplies each p-value by the number of tests performed. Controls the family-wise error rate (FWER).

Parameters

pvalues
readonly number[]
Array of p-values from individual tests
alphaoptional
number
Significance level (default: 0.05)

Returns

MultipleComparisonResultCorrected p-values and rejection decisions

Throws

  • InvalidParameterErrorIf pvalues is empty or contains a value outside [0, 1], or alpha is not in (0, 1)
Example
const result = bonferroni([0.01, 0.04, 0.03, 0.005], 0.05);result.corrected;  // [0.04, 0.16, 0.12, 0.02]result.rejected;   // [true, false, false, true]

hochberg

function hochberg(pvalues: readonly number[], alpha?: number): MultipleComparisonResult;

Hochberg step-up correction for multiple comparisons.

Controls the FWER under independence (or positive dependence of the tests) and is more powerful than Holm's method. The adjusted p-value of the i-th smallest p-value is min over j >= i of (m - j + 1) * p_(j).

Parameters

pvalues
readonly number[]
Array of p-values from individual tests
alphaoptional
number
Significance level (default: 0.05)

Returns

MultipleComparisonResultCorrected p-values and rejection decisions

Throws

  • InvalidParameterErrorIf pvalues is empty or contains a value outside [0, 1], or alpha is not in (0, 1)
Example
const result = hochberg([0.01, 0.04, 0.03, 0.005], 0.05);result.corrected;  // [0.03, 0.04, 0.04, 0.02]

holm

function holm(pvalues: readonly number[], alpha?: number): MultipleComparisonResult;

Holm-Bonferroni step-down correction for multiple comparisons.

A sequentially rejective method that is uniformly more powerful than Bonferroni while still controlling the FWER. Sorts p-values and applies decreasing multipliers.

Parameters

pvalues
readonly number[]
Array of p-values from individual tests
alphaoptional
number
Significance level (default: 0.05)

Returns

MultipleComparisonResultCorrected p-values and rejection decisions

Throws

  • InvalidParameterErrorIf pvalues is empty or contains a value outside [0, 1], or alpha is not in (0, 1)
Example
const result = holm([0.01, 0.04, 0.03, 0.005], 0.05);result.corrected;  // [0.03, 0.06, 0.06, 0.02]

sidak

function sidak(pvalues: readonly number[], alpha?: number): MultipleComparisonResult;

Sidak correction for multiple comparisons.

Assumes independence of tests and provides a less conservative correction than Bonferroni. Uses: p_corrected = 1 - (1 - p)^m

Parameters

pvalues
readonly number[]
Array of p-values from individual tests
alphaoptional
number
Significance level (default: 0.05)

Returns

MultipleComparisonResultCorrected p-values and rejection decisions

Throws

  • InvalidParameterErrorIf pvalues is empty or contains a value outside [0, 1], or alpha is not in (0, 1)
Example
const result = sidak([0.01, 0.04, 0.03, 0.005], 0.05);

tTestPower

function tTestPower(options: TTestPowerOptions): PowerAnalysisResult;

Power analysis for independent two-sample t-test.

Details

Exactly one of effectSize, nObs, alpha, or power must be omitted (undefined). The function solves for the missing parameter.

The power is computed from the non-central t distribution with df = nObs * (1 + ratio) - 2 degrees of freedom and non-centrality effectSize * sqrt(nObs * ratio / (1 + ratio)), summing both rejection regions for the two-sided test (the same model as statsmodels.stats.power.TTestIndPower). When solving for nObs the result is the smallest integer sample size that reaches the requested power.

Parameters

options
TTestPowerOptions
Parameters (leave exactly one undefined to solve for it)

Returns

PowerAnalysisResultObject with all four parameters filled in

Throws

  • InvalidParameterErrorIf the number of omitted parameters is not exactly one, a value is outside its valid range, or the requested value cannot be reached
Example
import { tTestPower } from 'deepbox/stats'; // Find required sample size for d=0.5, alpha=0.05, power=0.8const result = tTestPower({ effectSize: 0.5, alpha: 0.05, power: 0.8 });console.log(result.nObs); // 64 // Find power for given n and effect sizeconst result2 = tTestPower({ effectSize: 0.5, nObs: 50, alpha: 0.05 });console.log(result2.power); // 0.6969...

anderson

function anderson(x: Tensor): AndersonResult;

Anderson-Darling test for normality.

The mean and standard deviation (with n - 1 in the denominator) are estimated from the data. Critical values are the asymptotic points for the case of unknown mean and variance (0.561, 0.631, 0.752, 0.873, 1.035), divided by the small-sample factor 1 + 0.75 / n + 2.25 / n^2 (D'Agostino and Stephens, 1986), the same as SciPy's anderson(x, "norm").

Parameters

x
Tensor
Sample values (flattened, at least 2 values, not all equal)

Returns

AndersonResultThe statistic A², critical values at the significance levels 15%, 10%, 5%, 2.5% and 1% (as fractions) and an approximate p-value

Example
import { anderson } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; const res = anderson(tensor([1, 2, 3, 4, 5.5]));// res.statistic is about 0.1436; res.criticalValues[2] (5% level) is 0.606

bartlett

function bartlett(...samples: Tensor[]): TestResult;

Bartlett's test for equality of variances.

Tests whether two or more groups have equal variances. Assumes data is normally distributed; use Levene's test for non-normal data.

Parameters

samples
Tensor[]
Two or more sample tensors to compare (at least 2 values each)

Returns

TestResultThe chi-square statistic and its p-value (NaN if a value is NaN)

Throws

  • InvalidParameterErrorIf fewer than two groups are given, a group has fewer than 2 values, or a group has zero variance
Example
import { bartlett } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; const group1 = tensor([1, 2, 3, 4, 5]);const group2 = tensor([2, 4, 6, 8, 10]);const result = bartlett(group1, group2);console.log(result.pvalue);  // p-value for equal variances

chi2Contingency

function chi2Contingency(observed: readonly (readonly number[])[], correction?: boolean): ContingencyResult;

Chi-squared test of independence for a contingency table.

Tests whether two categorical variables are independent given their observed frequency table.

For 2×2 tables (one degree of freedom) Yates' continuity correction is applied by default, as in SciPy: each observed count is moved by at most 0.5 towards its expected count. Pass correction = false for the plain Pearson statistic.

Parameters

observed
readonly (readonly number[])[]
2D array of observed frequencies (rows × cols)
correctionoptional
boolean
Apply Yates' continuity correction when dof = 1 (default: true)

Returns

ContingencyResultObject with chi2 statistic, p-value, degrees of freedom, and expected frequencies

Throws

  • InvalidParameterErrorIf the table is smaller than 2×2, ragged, contains negative or non-finite counts, or has a row or column that sums to zero
Example
import { chi2Contingency } from 'deepbox/stats';const result = chi2Contingency([[10, 20, 30], [6, 9, 17]]);console.log(result.pvalue);

chisquare

function chisquare(f_obs: Tensor, f_exp?: Tensor, ddof?: number): TestResult;

Chi-square goodness of fit test.

Observed and expected frequencies must be non-negative and sum to the same total (within floating-point tolerance). Without f_exp the categories are assumed to be equally likely.

Parameters

f_obs
Tensor
Observed frequencies (flattened)
f_expoptional
Tensor
Expected frequencies (same number of elements as f_obs); uniform if omitted
ddofoptional
number
"Delta degrees of freedom": the p-value uses k - 1 - ddof degrees of freedom, where k is the number of categories (default: 0)

Returns

TestResultThe chi-square statistic and p-value

Example
import { chisquare } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; const { statistic, pvalue } = chisquare(tensor([16, 18, 16, 14, 12, 12]));

fisherExact

function fisherExact(table: readonly [ readonly [ number, number ], readonly [ number, number ] ], alternative?: "two-sided" | "less" | "greater"): { oddsRatio: number; pvalue: number; };

Fisher's exact test for a 2×2 contingency table.

Details

Computes the exact p-value for the association between two binary variables. Uses the hypergeometric distribution. The two-sided p-value sums the probabilities of all tables that are at most as likely as the observed one (with a relative tolerance of 1e-7, as in R's fisher.test; SciPy 1.17 uses 1e-14, which only matters for tables whose probabilities agree to more than 7 digits without being equal).

Parameters

table
readonly [ readonly [ number, number ], readonly [ number, number ] ]
2×2 array of observed counts [[a, b], [c, d]] (non-negative integers)
alternativeoptional
"two-sided" | "less" | "greater"
'two-sided' (default), 'less', or 'greater'

Returns

{ oddsRatio: number; pvalue: number; }Object with the sample odds ratio a * d / (b * c) (Infinity or NaN when the denominator is zero) and the p-value

Throws

  • InvalidParameterErrorIf a count is negative, not an integer or not finite, the total count exceeds 2^40, or alternative is invalid
Example
import { fisherExact } from 'deepbox/stats';const result = fisherExact([[1, 9], [11, 3]]);console.log(result.pvalue);

fligner

function fligner(...samples: Tensor[]): TestResult;

Parameters

samples
Tensor[]

Returns

TestResult

fOneway

function fOneway(...samples: Tensor[]): TestResult;

One-way analysis of variance.

Tests whether two or more groups have the same population mean.

Parameters

samples
Tensor[]
Two or more groups (flattened)

Returns

TestResultThe F statistic and the p-value. F is Infinity (with p-value 0) when the groups have no within-group variation but different means, and NaN when all values are equal.

Throws

  • InvalidParameterErrorIf fewer than 2 groups are given, a group is empty, or every group has a single value
Example
import { fOneway } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; const { statistic, pvalue } = fOneway(tensor([6, 8, 4, 5, 3, 4]), tensor([8, 12, 9, 11, 6, 8]));

friedmanchisquare

function friedmanchisquare(...samples: Tensor[]): TestResult;

Friedman test (non-parametric repeated measures ANOVA).

Tests whether k related samples (measurements of the same n subjects) have the same distribution. The p-value uses the chi-square approximation with k - 1 degrees of freedom and the tie correction, as scipy.stats.friedmanchisquare does.

Parameters

samples
Tensor[]
Three or more samples of equal length, paired by position

Returns

TestResultThe chi-square statistic and the p-value (NaN if a value is NaN)

Throws

  • InvalidParameterErrorIf fewer than 3 samples are given, the lengths differ, a sample is empty, or all values are identical within every block
Example
import { friedmanchisquare } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; const { statistic, pvalue } = friedmanchisquare(  tensor([7.0, 9.9, 8.5, 5.1, 10.3]),  tensor([5.3, 5.7, 4.7, 3.5, 7.7]),  tensor([4.9, 7.6, 5.5, 2.8, 8.4]));

fTwoway

function fTwoway(data: number[][][]): TwoWayAnovaResult;

Two-way ANOVA for balanced designs.

Tests main effects of two factors and their interaction. Expects data organized as a 3D array: data[a][b][replication].

Parameters

data
number[][][]
3D array indexed by [factorA level][factorB level][replications]

Returns

TwoWayAnovaResultObject with factorA, factorB, and interaction test results

Throws

  • InvalidParameterErrorIf the design is unbalanced, a factor has fewer than 2 levels, cells have a single replication, or a value is not finite

kruskal

function kruskal(...samples: Tensor[]): TestResult;

Kruskal-Wallis H-test (non-parametric version of ANOVA).

Tests whether two or more independent samples come from the same distribution. The p-value uses the chi-square approximation with k - 1 degrees of freedom and the tie correction, as scipy.stats.kruskal does.

Parameters

samples
Tensor[]
Two or more samples (flattened)

Returns

TestResultThe H statistic and the p-value (NaN if a value is NaN)

Throws

  • InvalidParameterErrorIf fewer than 2 samples are given, a sample is empty, or all values are identical
Example
import { kruskal } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; const { statistic, pvalue } = kruskal(tensor([2.9, 3.0, 2.5, 2.6]), tensor([3.8, 2.7, 4.0]));

ks2samp

function ks2samp(x: Tensor, y: Tensor, options?: KsTestOptions): TestResult;

Two-sample Kolmogorov-Smirnov test.

Details

Tests whether two samples are drawn from the same continuous distribution. The two-sided statistic is the maximum absolute difference between the empirical distribution functions of the two samples.

The p-value comes from the exact null distribution when both samples have at most 10000 values (SciPy's method="auto"), and from the asymptotic distribution otherwise. With tied values the exact p-value is conservative.

Parameters

x
Tensor
First sample (any shape; the tensor is flattened)
y
Tensor
Second sample
optionsoptional
KsTestOptions
alternative ("two-sided" by default) and method (see KsTestOptions)

Returns

TestResultThe statistic (D, or D+ / D- for one-sided alternatives) and the p-value. Both are NaN if a sample contains NaN.

Throws

  • InvalidParameterErrorIf a sample is empty or an option is invalid
Example
import { ks2samp } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; const result = ks2samp(tensor([1, 2, 3]), tensor([1.5, 2.5, 3.5]));console.log(result.statistic, result.pvalue);

kstest

function kstest(data: Tensor, cdf: string | ((x: number) => number), options?: KsTestOptions): TestResult;

Kolmogorov-Smirnov test for goodness of fit.

Compares the empirical distribution of data with a fully specified continuous distribution. The p-value comes from the exact distribution of the statistic (Marsaglia-Tsang-Wang, Smirnov and Pelz-Good approximations, as in SciPy's kstwo), or from the Kolmogorov limit distribution with method: "asymptotic".

Parameters

data
Tensor
Sample values (flattened)
cdf
string | ((x: number) => number)
"norm" for the standard normal distribution, or a function returning the cumulative distribution function of the hypothesized distribution at a point
optionsoptional
KsTestOptions
alternative ("two-sided" by default) and method (see KsTestOptions)

Returns

TestResultThe KS statistic (D, D+ or D- depending on alternative) and the p-value. Both are NaN if the data or the distribution function produce NaN.

Throws

  • InvalidParameterErrorIf data is empty, cdf is an unknown name, or an option is invalid
Example
import { kstest } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; kstest(tensor([0.1, -0.3, 1.2, 0.8, -1.5, 0.4, 2.2]), "norm");// { statistic: 0.2541..., pvalue: 0.6692... }

levene

function levene(...samples: Tensor[]): TestResult;

Parameters

samples
Tensor[]

Returns

TestResult

lilliefors

function lilliefors(x: Tensor): TestResult;

Lilliefors test for normality.

A variant of the Kolmogorov-Smirnov test where the mean and standard deviation are estimated from the data (rather than specified). The p-value uses the Dallal-Wilkinson (1986) approximation, refined for large p-values with the polynomial fits of Stephens, as in R's nortest::lillie.test.

Parameters

x
Tensor
Input tensor of numeric values (at least 4)

Returns

TestResultTestResult with the KS statistic and the approximate p-value. A constant sample gives { statistic: 0, pvalue: 1 }.

Example
import { lilliefors } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray';const result = lilliefors(tensor([1.2, 2.3, 1.8, 2.1, 1.5]));

mannwhitneyu

function mannwhitneyu(x: Tensor, y: Tensor, options?: RankTestOptions): TestResult;

Mann-Whitney U test (non-parametric).

Details

Tests whether two independent samples come from the same distribution.

The p-value is exact when one of the samples has at most 8 values and there are no ties (method: "auto", as in SciPy). Otherwise the normal approximation with tie correction and a continuity correction of 0.5 is used.

Like SciPy, the returned statistic is always U1, the statistic of x (U2 = nx * ny - U1 is the statistic of y). The p-value of the two-sided test is computed from the larger of U1 and U2.

Parameters

x
Tensor
First sample
y
Tensor
Second sample
optionsoptional
RankTestOptions
alternative, method and correction (see RankTestOptions); "greater" tests whether x is stochastically greater than y

Returns

TestResultU1 and the p-value (NaN when all values are tied, or when a value is NaN)

Throws

  • InvalidParameterErrorIf a sample is empty, an option is invalid, or method: "exact" is requested for data with ties or for very large samples
Example
import { mannwhitneyu } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; mannwhitneyu(tensor([19, 22, 16, 29, 24]), tensor([20, 11, 17, 12]));// { statistic: 17, pvalue: 0.1111... }

medianTest

function medianTest(...args: (Tensor | MedianTestOptions)[]): TestResult;

Mood's median test.

Tests whether two or more samples have the same median. It builds the 2 by k contingency table of counts above and below the grand median and applies the chi-square test of independence. As in SciPy, Yates' continuity correction is applied by default when there are two samples.

Parameters

args
(Tensor | MedianTestOptions)[]
Two or more samples (flattened), optionally followed by MedianTestOptions

Returns

TestResultThe chi-square statistic and the p-value (NaN if a value is NaN)

Throws

  • InvalidParameterErrorIf fewer than 2 samples are given, a sample is empty, all values fall on one side of the grand median, or ties: "ignore" leaves a sample empty
Example
import { medianTest } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; const result = medianTest(tensor([1, 2, 3, 4, 5]), tensor([3, 5, 7, 8, 9]));console.log(result.statistic, result.pvalue);

normaltest

function normaltest(a: Tensor): TestResult;

D'Agostino-Pearson omnibus test for normality.

Combines the skewness test and the Anscombe-Glynn kurtosis test into K2 = Z1^2 + Z2^2, which follows a chi-square distribution with 2 degrees of freedom under normality. Matches scipy.stats.normaltest.

Parameters

a
Tensor
Sample values (flattened, at least 8 values, not all equal)

Returns

TestResultThe K2 statistic and the p-value (NaN if a value is NaN)

Throws

  • InvalidParameterErrorIf fewer than 8 values are given or the input is constant
Example
import { normaltest } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; const { statistic, pvalue } = normaltest(tensor([2.1, 1.9, 2.4, 2.2, 2.0, 2.6, 1.7, 2.3, 2.5]));

runsTest

function runsTest(x: Tensor): TestResult;

Wald-Wolfowitz runs test for randomness.

Tests whether a sequence of binary (above/below median) values is random by counting the number of "runs" (consecutive sequences of the same type). Values equal to the median count as "above".

Parameters

x
Tensor
Input tensor of numeric values, in observation order

Returns

TestResultTestResult with z-statistic and two-sided p-value (NaN if the data contain NaN)

Example
import { runsTest } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray';const result = runsTest(tensor([1, 2, 1, 2, 1, 2, 1, 2]));

shapiro

function shapiro(x: Tensor): TestResult;

Shapiro-Wilk test for normality.

Uses Royston's algorithm AS R94, the same as scipy.stats.shapiro. Small values of the W statistic indicate departure from normality.

Parameters

x
Tensor
Sample values (flattened, between 3 and 5000 values, not all equal)

Returns

TestResultThe W statistic and the p-value (NaN if a value is NaN)

Throws

  • InvalidParameterErrorIf the number of values is outside 3 to 5000, or all values are identical
Example
import { shapiro } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; const { statistic, pvalue } = shapiro(tensor([2.1, 1.9, 2.4, 2.2, 2.0, 2.6, 1.7]));

ttest1samp

function ttest1samp(a: Tensor, popmean: number, alternative?: TestAlternative): TestResult;

One-sample t-test.

Tests whether the mean of a sample differs from a hypothesized population mean.

Parameters

a
Tensor
Sample values (any shape; the tensor is flattened)
popmean
number
Hypothesized population mean
alternativeoptional
TestAlternative
"two-sided" (default), "less" or "greater"

Returns

TestResultThe t statistic (df = n - 1) and the p-value

Throws

  • InvalidParameterErrorIf fewer than 2 values are given, the input is constant, or alternative is invalid
Example
import { ttest1samp } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; const { statistic, pvalue } = ttest1samp(tensor([2.1, 1.9, 2.4, 2.2, 2.0]), 2.0);

ttestInd

function ttestInd(a: Tensor, b: Tensor, equalVar?: boolean | TTestIndOptions, alternative?: TestAlternative): TestResult;

Independent two-sample t-test.

Tests whether means of two independent samples are equal. By default the samples are assumed to have equal variance (Student's t-test); pass equalVar = false for Welch's t-test with Welch-Satterthwaite degrees of freedom.

Parameters

a
Tensor
First sample (any shape; the tensor is flattened)
b
Tensor
Second sample
equalVaroptional
boolean | TTestIndOptions
Assume equal population variances (default: true), or a TTestIndOptions object
alternativeoptional
TestAlternative
"two-sided" (default), "less" (mean of a is less than mean of b) or "greater"

Returns

TestResultThe t statistic and the p-value (NaN if a value is NaN)

Throws

  • InvalidParameterErrorIf a sample has fewer than 2 values, the pooled variance is zero, or alternative is invalid
Example
import { ttestInd } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; const a = tensor([2.1, 1.9, 2.4, 2.2, 2.0]);const b = tensor([2.8, 3.1, 2.6, 3.0]);ttestInd(a, b, { equalVar: false, alternative: "less" });

ttestRel

function ttestRel(a: Tensor, b: Tensor, alternative?: TestAlternative): TestResult;

Paired-sample t-test.

Tests whether means of two related samples are equal. Tensors are flattened in row-major order and paired element by element.

Parameters

a
Tensor
First sample
b
Tensor
Second sample (same number of elements as a)
alternativeoptional
TestAlternative
"two-sided" (default), "less" (mean of a - b is less than zero) or "greater"

Returns

TestResultThe t statistic (df = n - 1) and the p-value

wilcoxon

function wilcoxon(x: Tensor, y?: Tensor, options?: WilcoxonOptions): TestResult;

Wilcoxon signed-rank test (non-parametric paired test).

Details

With method: "auto" (the SciPy default) the p-value is exact for at most 50 differences without ties or zeros, comes from the exact sign-flip distribution of the tied ranks for at most 13 differences with ties or zeros, and uses the normal approximation with tie correction otherwise. Zero differences are discarded unless zeroMethod says otherwise.

Like SciPy, the returned statistic is min(W+, W-) for the two-sided test and W+, the sum of the ranks of the positive differences, for one-sided tests.

Parameters

x
Tensor
First sample, or the differences when y is omitted
yoptional
Tensor
Second sample (same number of elements as x)
optionsoptional
WilcoxonOptions
alternative, method, correction and zeroMethod (see WilcoxonOptions); "greater" tests whether x - y is stochastically greater than zero

Returns

TestResultThe statistic and the p-value (NaN if the normal approximation has zero variance or a value is NaN)

Throws

  • InvalidParameterErrorIf the lengths differ, all differences are zero, or an option is invalid
Example
import { wilcoxon } from 'deepbox/stats';import { tensor } from 'deepbox/ndarray'; wilcoxon(tensor([1.5, 2.5, 3.1, -0.5, 4.2, 0.7, 2.2]));// { statistic: 1, pvalue: 0.03125 }

Deprecated names

10

These older spellings still work in 1.x. New code should use the names above.

  • chi2_contingencyPrefer chi2Contingency.
  • f_onewayPrefer fOneway.
  • f_twowayPrefer fTwoway.
  • fisher_exactPrefer fisherExact.
  • ks_2sampPrefer ks2samp.
  • median_testPrefer medianTest.
  • runs_testPrefer runsTest.
  • ttest_1sampPrefer ttest1samp.
  • ttest_indPrefer ttestInd.
  • ttest_relPrefer ttestRel.
stats-tests.ts
import {  benjaminiHochberg,  mannwhitneyu,  shapiro,  tTestPower,  ttestInd,} from "deepbox/stats";import { tensor } from "deepbox/ndarray"; const a = tensor([1, 2, 3, 4]);const b = tensor([3, 4, 5, 6, 9]); console.log(ttestInd(a, b));console.log(ttestInd(a, b, { equalVar: false })); // Welch's testconsole.log(mannwhitneyu(a, b));console.log(shapiro(tensor([1.1, 0.9, 1.0, 1.2, 0.95])));console.log(benjaminiHochberg([0.01, 0.03, 0.2, 0.001], 0.05));console.log(tTestPower({ effectSize: 0.5, alpha: 0.05, power: 0.8 }));