stats.ts

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

view source

Declarations
#

23 declarations

stats_confidence_interval
#

stats.ts view source

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

Calculate confidence interval for the mean.

values

type number[]

options?

type StatsConfidenceIntervalOptions | undefined
optional

returns

[number, number]

[lower_bound, upper_bound]

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 confidence interval from summary statistics (mean, std_dev, sample_size). Useful when raw data is not available.

mean

type number

std_dev

type number

sample_size

type number

options?

type StatsConfidenceIntervalOptions | undefined
optional

returns

[number, number]

[lower_bound, upper_bound]

stats_confidence_level_to_z_score
#

stats.ts view source

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

Convert a confidence level (0-1) to a z-score. Uses a lookup table for common values, approximates others.

level

type number

returns

number

throws

  • Error - if `level` is not in the open interval (0, 1)

examples

stats_confidence_level_to_z_score(0.95); // 1.96 stats_confidence_level_to_z_score(0.99); // 2.576

STATS_CONFIDENCE_Z_SCORES
#

stats.ts view source

Record<number, number> import {STATS_CONFIDENCE_Z_SCORES} from '@fuzdev/fuz_util/stats.js';

Common z-scores for confidence intervals.

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';

Approximate regularized incomplete beta function for p-value calculation. Uses continued fraction expansion for reasonable accuracy.

x

type number

a

type number

b

type number

returns

number

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: number[]): number import {stats_mean} from '@fuzdev/fuz_util/stats.js';

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

values

type number[]

returns

number

stats_median
#

stats.ts view source

(values: 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 number[]

returns

number

stats_min_max
#

stats.ts view source

(values: 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 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?

type StatsOutliersIqrOptions | undefined
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?

type StatsOutliersMadOptions | undefined
optional

returns

StatsOutlierResult

stats_percentile
#

stats.ts view source

(values: 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 number[]

p

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

type number

returns

number

stats_std_dev
#

stats.ts view source

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

Calculate the standard deviation of an array of numbers. Uses population standard deviation (divides by n, not n-1). For benchmarks with many samples, this is typically appropriate.

values

type number[]

mean?

type number | undefined
optional

returns

number

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';

Approximate two-tailed p-value from t-distribution. For large df (>100), uses normal approximation. For smaller df, uses incomplete beta function.

t

absolute value of t-statistic

type number

df

degrees of freedom

type number

returns

number

two-tailed p-value

stats_variance
#

stats.ts view source

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

Calculate the variance of an array of numbers.

values

type number[]

mean?

type number | undefined
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.

z_score?

Z-score for confidence level (default: 1.96 for 95% CI)

type number

confidence_level?

Confidence level (0-1), alternative to z_score. If both provided, z_score takes precedence.

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 Array<number>

outliers

Detected outlier values

type Array<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

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
#