stats.ts

Statistical analysis utilities. Pure functions with zero dependencies - can be used standalone for any data analysis.

view source

Declarations
#

27 declarations

stats_confidence_interval
#

stats.ts view source

(values: readonly number[], options?: StatsConfidenceIntervalOptions | undefined): [number, number] import {stats_confidence_interval} from '@fuzdev/fuz_util/stats.js';

Calculate a confidence interval for the mean, from the sample standard deviation and, by default, the 95% Student's t critical value.

values

type readonly number[]

options?

optional

returns

[number, number]

[lower_bound, upper_bound], both NaN for fewer than two values

stats_confidence_interval_from_summary
#

stats.ts view source

(mean: number, std_dev: number, sample_size: number, options?: StatsConfidenceIntervalOptions | undefined): [number, number] import {stats_confidence_interval_from_summary} from '@fuzdev/fuz_util/stats.js';

Calculate a confidence interval for the mean from summary statistics. Useful when raw data is not available.

mean

the sample mean

type number

std_dev

the sample standard deviation (stats_std_dev_sample)

type number

sample_size

how many values the summary covers

type number

options?

optional

returns

[number, number]

[lower_bound, upper_bound], both NaN when sample_size is below 2

stats_cv
#

stats.ts view source

(mean: number, std_dev: number): number import {stats_cv} from '@fuzdev/fuz_util/stats.js';

Calculate the coefficient of variation (CV). CV = standard deviation / mean, expressed as a ratio. Useful for comparing relative variability between datasets.

mean

type number

std_dev

type number

returns

number

stats_incomplete_beta
#

stats.ts view source

(x: number, a: number, b: number): number import {stats_incomplete_beta} from '@fuzdev/fuz_util/stats.js';

Regularized incomplete beta function I_x(a, b), by the continued fraction of Numerical Recipes' betai/betacf (modified Lentz), using the symmetry I_x(a, b) = 1 - I_(1-x)(b, a) where that converges faster. Accurate to about 1e-8 for the t p-value (b = 0.5). For large a and b both, precision falls with √max(a, b), and the value after the iteration cap is returned as is.

x

the upper limit of integration, in [0, 1]

type number

a

the first shape parameter, positive

type number

b

the second shape parameter, positive

type number

returns

number

I_x(a, b), in [0, 1]

stats_ln_gamma
#

stats.ts view source

(z: number): number import {stats_ln_gamma} from '@fuzdev/fuz_util/stats.js';

Log gamma function approximation (Lanczos approximation).

z

type number

returns

number

stats_mean
#

stats.ts view source

(values: readonly number[]): number import {stats_mean} from '@fuzdev/fuz_util/stats.js';

Calculate the mean (average) of an array of numbers.

values

type readonly number[]

returns

number

stats_median
#

stats.ts view source

(values: readonly number[]): number import {stats_median} from '@fuzdev/fuz_util/stats.js';

Calculate the median of an array of numbers. NaN values are filtered out before computing.

values

type readonly number[]

returns

number

stats_min_max
#

stats.ts view source

(values: readonly number[]): { min: number; max: number; } import {stats_min_max} from '@fuzdev/fuz_util/stats.js';

Calculate min and max values. NaN values are ignored.

values

type readonly number[]

returns

{ min: number; max: number; }

stats_normal_cdf
#

stats.ts view source

(x: number): number import {stats_normal_cdf} from '@fuzdev/fuz_util/stats.js';

Standard normal CDF approximation (Abramowitz and Stegun formula 7.1.26).

x

type number

returns

number

stats_outliers_iqr
#

stats.ts view source

(values: number[], options?: StatsOutliersIqrOptions | undefined): StatsOutlierResult import {stats_outliers_iqr} from '@fuzdev/fuz_util/stats.js';

Detect outliers using the IQR (Interquartile Range) method. Values outside [Q1 - multiplier*IQR, Q3 + multiplier*IQR] are considered outliers.

values

type number[]

options?

optional

returns

StatsOutlierResult

stats_outliers_mad
#

stats.ts view source

(values: number[], options?: StatsOutliersMadOptions | undefined): StatsOutlierResult import {stats_outliers_mad} from '@fuzdev/fuz_util/stats.js';

Detect outliers using the MAD (Median Absolute Deviation) method. More robust than IQR for skewed distributions. Uses modified Z-score: |0.6745 * (x - median) / MAD| Values with modified Z-score > threshold are considered outliers.

values

type number[]

options?

optional

returns

StatsOutlierResult

stats_pairwise_deviation
#

stats.ts view source

(groups: readonly (readonly number[])[]): StatsPairwiseDeviation | null import {stats_pairwise_deviation} from '@fuzdev/fuz_util/stats.js';

Calculate how much values that should agree disagree, as an A/A noise figure. Each group holds repeated measurements of one thing, and every pair within a group contributes the stats_ratio_deviation of its ratio. Groups are never compared with each other. A value that is not a positive finite number is skipped, and so is a pair whose ratio overflows.

groups

each group's repeated measurements

type readonly (readonly number[])[]

returns

StatsPairwiseDeviation | null

the figures over every pair, or null when there is no usable pair

stats_percentile
#

stats.ts view source

(values: readonly number[], p: number): number import {stats_percentile} from '@fuzdev/fuz_util/stats.js';

Calculate a percentile of an array of numbers using linear interpolation. Uses the "R-7" method (default in R, NumPy, Excel) which interpolates between data points for more accurate percentile estimates, especially with smaller samples.

values

type readonly number[]

p

percentile (0-1, e.g., 0.95 for 95th percentile)

type number

returns

number

stats_ratio_deviation
#

stats.ts view source

(ratio: number): number import {stats_ratio_deviation} from '@fuzdev/fuz_util/stats.js';

Calculate how far a ratio is from 1, the same in either direction: max(ratio, 1 / ratio) - 1, so 1.1 and 1 / 1.1 both give 0.1.

ratio

type number

returns

number

the deviation, or NaN when ratio is not positive

stats_spread
#

stats.ts view source

(values: readonly number[]): number import {stats_spread} from '@fuzdev/fuz_util/stats.js';

Calculate the spread of an array of numbers: the largest over the smallest, so 1 when all agree. NaN values are ignored, as stats_min_max does.

values

type readonly number[]

returns

number

the ratio of max to min, or NaN when there is no value or the smallest is not positive

stats_std_dev
#

stats.ts view source

(values: readonly number[], mean?: number | undefined): number import {stats_std_dev} from '@fuzdev/fuz_util/stats.js';

Calculate the population standard deviation of an array of numbers (divides by n). For a sample standing in for a larger population, as with timings, use stats_std_dev_sample.

values

type readonly number[]

mean?

type number
optional

returns

number

stats_std_dev_sample
#

stats.ts view source

(values: readonly number[], mean?: number | undefined): number import {stats_std_dev_sample} from '@fuzdev/fuz_util/stats.js';

Calculate the sample standard deviation of an array of numbers, with Bessel's correction (divides by n - 1). This is the estimator a t-based confidence interval and Welch's t-test expect.

values

the sample

type readonly number[]

mean?

the sample's mean, when already computed

type number
optional

returns

number

the sample standard deviation, or NaN for fewer than two values

stats_t_critical_95
#

stats.ts view source

(df: number): number import {stats_t_critical_95} from '@fuzdev/fuz_util/stats.js';

Two-sided 95% critical value of Student's t distribution: the multiple of the standard error a 95% confidence interval spans on each side of the mean. A table gives whole df up to 30 and 40, 60, and 120, each rounded up to three decimals. Between table points, and for fractional df such as Welch's, it interpolates linearly in 1 / df. t is convex in 1 / df, so the interpolation lands above the true value, well above it under df 3. The value is never below the true one, so an interval is never narrower than it should be.

df

degrees of freedom, at least 1

type number

returns

number

the critical value, 1.96 for infinite df, or NaN when df is below 1 or NaN

stats_t_distribution_p_value
#

stats.ts view source

(t: number, df: number): number import {stats_t_distribution_p_value} from '@fuzdev/fuz_util/stats.js';

Two-tailed p-value of Student's t distribution, through the regularized incomplete beta function. Above df 1e7 it is the normal limit: df / (df + t²) loses precision as df grows, and from there t and the normal differ by less than the normal approximation's own error, so the switch moves the value by less than 1e-6.

t

absolute value of t-statistic

type number

df

degrees of freedom

type number

returns

number

two-tailed p-value, or NaN when df is not positive

stats_variance
#

stats.ts view source

(values: readonly number[], mean?: number | undefined): number import {stats_variance} from '@fuzdev/fuz_util/stats.js';

Calculate the variance of an array of numbers.

values

type readonly number[]

mean?

type number
optional

returns

number

stats_welch_t_test
#

stats.ts view source

(mean1: number, std1: number, n1: number, mean2: number, std2: number, n2: number): StatsWelchTTestResult import {stats_welch_t_test} from '@fuzdev/fuz_util/stats.js';

Calculate Welch's t-test statistic and degrees of freedom. Welch's t-test is more robust than Student's t-test when variances are unequal.

Params suffixed 1 describe the first sample, 2 the second.

mean1

type number

std1

type number

n1

type number

mean2

type number

std2

type number

n2

type number

returns

StatsWelchTTestResult

StatsConfidenceIntervalOptions
#

stats.ts view source

StatsConfidenceIntervalOptions import type {StatsConfidenceIntervalOptions} from '@fuzdev/fuz_util/stats.js';

Configuration options for confidence interval calculation.

critical?

The multiple of the standard error on each side of the mean. Defaults to stats_t_critical_95 at n - 1 degrees of freedom.

type number

StatsOutlierResult
#

stats.ts view source

StatsOutlierResult import type {StatsOutlierResult} from '@fuzdev/fuz_util/stats.js';

Result from outlier detection.

cleaned

Values after removing outliers

type number[]

outliers

Detected outlier values

type number[]

StatsOutliersIqrOptions
#

stats.ts view source

StatsOutliersIqrOptions import type {StatsOutliersIqrOptions} from '@fuzdev/fuz_util/stats.js';

Configuration options for IQR outlier detection.

iqr_multiplier?

Multiplier for IQR bounds (default: 1.5)

type number

min_sample_size?

Minimum sample size to perform outlier detection (default: 3)

type number

StatsOutliersMadOptions
#

stats.ts view source

StatsOutliersMadOptions import type {StatsOutliersMadOptions} from '@fuzdev/fuz_util/stats.js';

Configuration options for MAD outlier detection.

z_score_threshold?

Modified Z-score threshold for outlier detection (default: 3.5)

type number

z_score_extreme?

Extreme Z-score threshold when too many outliers detected (default: 5.0)

type number

mad_constant?

MAD constant for normal distribution (default: 0.6745)

type number

outlier_ratio_high?

Ratio threshold to switch to extreme mode (default: 0.3)

type number

outlier_ratio_extreme?

Ratio threshold to switch to keep-closest mode (default: 0.4)

type number

outlier_keep_ratio?

Ratio of values to keep in keep-closest mode (default: 0.8)

type number

min_sample_size?

Minimum sample size to perform outlier detection (default: 3)

type number

iqr_options?

Options to pass to IQR fallback when MAD is zero

type StatsOutliersIqrOptions

StatsPairwiseDeviation
#

stats.ts view source

StatsPairwiseDeviation import type {StatsPairwiseDeviation} from '@fuzdev/fuz_util/stats.js';

Deviation figures over pairs of values that measure the same thing.

pairs

How many pairs the figures are taken over.

type number

median

The median deviation.

type number

p95

The 95th percentile deviation.

type number

max

The largest deviation.

type number

StatsWelchTTestResult
#

stats.ts view source

StatsWelchTTestResult import type {StatsWelchTTestResult} from '@fuzdev/fuz_util/stats.js';

Result from Welch's t-test calculation.

t_statistic

The t-statistic

type number

degrees_of_freedom

Welch-Satterthwaite degrees of freedom

type number

Imported by
#