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 }.alternativedefaults to"two-sided"wherever it exists.anderson,chi2ContingencyandfTwowayreturn larger result objects. ttestIndassumes equal variances by default, like SciPy. Pass{ equalVar: false }for Welch's test, which uses Welch-Satterthwaite degrees of freedom.mannwhitneyuandwilcoxonuse exact p-values for small samples, like SciPy'smethod="auto", and themannwhitneyustatistic is U1.wilcoxontakeszeroMethod.kstest,ks2sampandandersonfollow SciPy 1.17.bonferroni,holm,hochberg,sidak,benjaminiHochbergandbenjaminiYekutielitake 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 fornObs, it returns the smallest integer sample size that reaches the power.
MultipleComparisonResult
interfaceResult 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
interfaceResult of a power analysis.
effectSize: numbernObs: numberalpha: numberpower: number
TTestPowerOptions
interfaceOptions for power analysis of a two-sample t-test.
effectSize?: numberCohen's d effect size
nObs?: numberSample size of the first group (per group when
ratiois 1)alpha?: numberSignificance level (Type I error rate)
power?: numberStatistical power (1 - Type II error rate)
alternative?: 1 | 2Number of tails: 1 or 2 (default: 2)
ratio?: numberRatio of the size of the second group to the first,
nObs2 / nObs(default: 1)
AndersonResult
typeResult 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: numbercriticalValues: number[]significanceLevel: number[]critical_values: number[]deprecatedSame as
criticalValues.significance_level: number[]deprecatedSame as
significanceLevel.pvalue: numberApproximate p-value (D'Agostino and Stephens, 1986).
ContingencyResult
typeResult of a contingency table analysis (chi-square test).
statistic: numberpvalue: numberdof: numberexpected: number[][]
KsTestOptions
interfaceOptions of kstest and ks_2samp.
alternative?: TestAlternativeAlternative hypothesis (default:
"two-sided").method?: KsMethodp-value method.
MedianTestOptions
interfaceOptions of median_test.
correction?: booleanApply 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
interfaceOptions shared by mannwhitneyu and wilcoxon.
alternative?: TestAlternativeAlternative hypothesis (default:
"two-sided").method?: "auto" | "exact" | "asymptotic"p-value method.
correction?: booleanApply a continuity correction of 0.5 in the normal approximation.
TestAlternative
typeAlternative 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
typeResult of a statistical hypothesis test.
statistic: numberpvalue: number
TTestIndOptions
interfaceOptions of ttest_ind.
equalVar?: booleanAssume equal population variances (default:
true);falsegives Welch's t-test.alternative?: TestAlternativeAlternative hypothesis (default:
"two-sided").
TwoWayAnovaResult
typeResult of two-way ANOVA test.
factorA: TestResultfactorB: TestResultinteraction: TestResult
VarianceTestCenter
typeLocation estimate used to center the groups in Levene's and Fligner-Killeen's tests.
type VarianceTestCenter = "mean" | "median" | "trimmed";VarianceTestOptions
interfaceOptions of levene and fligner.
center?: VarianceTestCenterHow each group is centered:
"median"(default),"mean"or"trimmed".proportiontocut?: numberFraction trimmed from each end for
center: "trimmed"(default: 0.05).
WilcoxonOptions
interfaceOptions of wilcoxon.
zeroMethod?: "wilcox" | "pratt" | "zsplit"Treatment of zero differences (default:
"wilcox").
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
pvaluesreadonly number[]- Array of p-values from individual tests
alphaoptionalnumber- 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)
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
pvaluesreadonly number[]- Array of p-values from individual tests
alphaoptionalnumber- 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)
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
pvaluesreadonly number[]- Array of p-values from individual tests
alphaoptionalnumber- 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)
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
pvaluesreadonly number[]- Array of p-values from individual tests
alphaoptionalnumber- 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)
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
pvaluesreadonly number[]- Array of p-values from individual tests
alphaoptionalnumber- 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)
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
pvaluesreadonly number[]- Array of p-values from individual tests
alphaoptionalnumber- 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)
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
optionsTTestPowerOptions- 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
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
xTensor- 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
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.606bartlett
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
samplesTensor[]- 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
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 varianceschi2Contingency
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
observedreadonly (readonly number[])[]- 2D array of observed frequencies (rows × cols)
correctionoptionalboolean- 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
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_obsTensor- Observed frequencies (flattened)
f_expoptionalTensor- Expected frequencies (same number of elements as
f_obs); uniform if omitted ddofoptionalnumber- "Delta degrees of freedom": the p-value uses
k - 1 - ddofdegrees of freedom, wherekis the number of categories (default: 0)
Returns
TestResultThe chi-square statistic and p-value
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
tablereadonly [ 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, oralternativeis invalid
import { fisherExact } from 'deepbox/stats';const result = fisherExact([[1, 9], [11, 3]]);console.log(result.pvalue);fligner
function fligner(...samples: Tensor[]): TestResult;Parameters
samplesTensor[]
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
samplesTensor[]- 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
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
samplesTensor[]- 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
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
datanumber[][][]- 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
samplesTensor[]- 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
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
xTensor- First sample (any shape; the tensor is flattened)
yTensor- Second sample
optionsoptionalKsTestOptionsalternative("two-sided"by default) andmethod(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
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
dataTensor- Sample values (flattened)
cdfstring | ((x: number) => number)"norm"for the standard normal distribution, or a function returning the cumulative distribution function of the hypothesized distribution at a pointoptionsoptionalKsTestOptionsalternative("two-sided"by default) andmethod(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
InvalidParameterErrorIfdatais empty,cdfis an unknown name, or an option is invalid
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
samplesTensor[]
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
xTensor- 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 }.
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
xTensor- First sample
yTensor- Second sample
optionsoptionalRankTestOptionsalternative,methodandcorrection(see RankTestOptions);"greater"tests whetherxis stochastically greater thany
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, ormethod: "exact"is requested for data with ties or for very large samples
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, orties: "ignore"leaves a sample empty
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
aTensor- 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
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
xTensor- Input tensor of numeric values, in observation order
Returns
TestResultTestResult with z-statistic and two-sided p-value (NaN if the data contain NaN)
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
xTensor- 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
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
aTensor- Sample values (any shape; the tensor is flattened)
popmeannumber- Hypothesized population mean
alternativeoptionalTestAlternative"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, oralternativeis invalid
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
aTensor- First sample (any shape; the tensor is flattened)
bTensor- Second sample
equalVaroptionalboolean | TTestIndOptions- Assume equal population variances (default:
true), or a TTestIndOptions object alternativeoptionalTestAlternative"two-sided"(default),"less"(mean ofais less than mean ofb) 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, oralternativeis invalid
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
aTensor- First sample
bTensor- Second sample (same number of elements as
a) alternativeoptionalTestAlternative"two-sided"(default),"less"(mean ofa - bis 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
xTensor- First sample, or the differences when
yis omitted yoptionalTensor- Second sample (same number of elements as
x) optionsoptionalWilcoxonOptionsalternative,method,correctionandzeroMethod(see WilcoxonOptions);"greater"tests whetherx - yis 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
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 }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.
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 }));Found a mistake? Open an issue.