stats

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package stats is a standard-library-only Go port of the core of the npm package simple-statistics (https://simplestatistics.org), providing the descriptive-statistics helpers that data-facing Express/Node services reach for: measures of central tendency (Mean, Median, Mode and the geometric and harmonic means), dispersion (Variance, StandardDeviation and their sample variants, Range, InterquartileRange, MedianAbsoluteDeviation), quantiles (Quantile, Percentile), bivariate measures (Covariance, Correlation, LinearRegression) and a few combinatorial functions (Factorial, Combinations, Permutations).

All sequence functions operate on []float64 and treat the slice as an unordered sample; those that need order (Median, Quantile and friends) sort a private copy, so the caller's slice is never modified. Quantile uses linear interpolation of the empirical CDF (index = p*(n-1)), the same method as NumPy's default and Percentile is its 0–100 wrapper. Variance and StandardDeviation are population statistics (divide by n); the Sample* variants use Bessel's correction (divide by n-1). Covariance and Correlation are population statistics over paired slices of equal length.

Functions return NaN for inputs that leave the statistic undefined — an empty slice for Mean, fewer than two elements for a sample variance, or mismatched lengths for the bivariate helpers — mirroring simple-statistics returning undefined, but in a form Go callers can test with math.IsNaN. Everything is deterministic and depends only on the math and sort packages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Combinations

func Combinations(n, k int) float64

Combinations returns the binomial coefficient C(n, k) as a float64, or NaN when either argument is negative. It is 0 when k > n.

func Correlation

func Correlation(xs, ys []float64) float64

Correlation returns the Pearson correlation coefficient of xs and ys, a value in [-1,1], or NaN when the slices are empty, of unequal length, or either has zero variance.

func Covariance

func Covariance(xs, ys []float64) float64

Covariance returns the population covariance of the paired slices xs and ys, or NaN when they are empty or of unequal length.

func CumulativeSum

func CumulativeSum(xs []float64) []float64

CumulativeSum returns a slice whose i-th element is the sum of xs[0..i]. The result has the same length as xs; an empty input yields an empty slice.

func Factorial

func Factorial(n int) float64

Factorial returns n! as a float64, or NaN for negative n. Factorial(0) is 1.

func Frequencies

func Frequencies(xs []float64) map[float64]int

Frequencies returns a map from each distinct value in xs to the number of times it occurs.

func GeometricMean

func GeometricMean(xs []float64) float64

GeometricMean returns the geometric mean of xs, or NaN when xs is empty or contains a non-positive value.

func HarmonicMean

func HarmonicMean(xs []float64) float64

HarmonicMean returns the harmonic mean of xs, or NaN when xs is empty or contains a non-positive value.

func InterquartileRange

func InterquartileRange(xs []float64) float64

InterquartileRange returns the difference between the 75th and 25th percentiles of xs, or NaN when xs is empty.

func LinearRegression

func LinearRegression(xs, ys []float64) (slope, intercept float64)

LinearRegression fits the ordinary least-squares line y = slope*x + intercept to the paired slices and returns its slope and intercept. It returns NaN values when the slices are empty, of unequal length, or x has zero variance.

func Max

func Max(xs []float64) float64

Max returns the largest value in xs, or NaN when xs is empty.

func Mean

func Mean(xs []float64) float64

Mean returns the arithmetic mean of xs, or NaN when xs is empty.

func Median

func Median(xs []float64) float64

Median returns the median of xs (the average of the two middle values for an even count), or NaN when xs is empty.

func MedianAbsoluteDeviation

func MedianAbsoluteDeviation(xs []float64) float64

MedianAbsoluteDeviation returns the median of the absolute deviations of xs from their median, a robust measure of spread. It returns NaN for an empty slice.

func Min

func Min(xs []float64) float64

Min returns the smallest value in xs, or NaN when xs is empty.

func Mode

func Mode(xs []float64) []float64

Mode returns the value or values that occur most frequently in xs, sorted ascending. It returns an empty slice when xs is empty.

func Percentile

func Percentile(xs []float64, p float64) float64

Percentile returns the p-th percentile of xs (p in [0,100]), equivalent to Quantile(xs, p/100).

func Permutations

func Permutations(n, k int) float64

Permutations returns the number of ordered k-arrangements of n items, n!/(n-k)!, as a float64, or NaN when either argument is negative. It is 0 when k > n.

func Product

func Product(xs []float64) float64

Product returns the product of xs. The product of an empty slice is 1.

func Quantile

func Quantile(xs []float64, p float64) float64

Quantile returns the p-quantile of xs (p in [0,1]) using linear interpolation of the empirical CDF. Quantile(xs, 0.5) equals Median(xs). It returns NaN for an empty slice or a p outside [0,1].

func Range

func Range(xs []float64) float64

Range returns the difference between the largest and smallest values in xs, or NaN when xs is empty.

func RootMeanSquare

func RootMeanSquare(xs []float64) float64

RootMeanSquare returns the quadratic mean (root mean square) of xs, or NaN when xs is empty.

func SampleStandardDeviation

func SampleStandardDeviation(xs []float64) float64

SampleStandardDeviation returns the sample standard deviation of xs, or NaN when xs has fewer than two elements.

func SampleVariance

func SampleVariance(xs []float64) float64

SampleVariance returns the sample variance of xs (Bessel-corrected, dividing by n-1), or NaN when xs has fewer than two elements.

func StandardDeviation

func StandardDeviation(xs []float64) float64

StandardDeviation returns the population standard deviation of xs, or NaN when xs is empty.

func Sum

func Sum(xs []float64) float64

Sum returns the sum of xs. The sum of an empty slice is 0.

func Variance

func Variance(xs []float64) float64

Variance returns the population variance of xs (dividing by n), or NaN when xs is empty.

func ZScore

func ZScore(x, mean, standardDeviation float64) float64

ZScore returns the number of standard deviations x lies from a distribution with the given mean and standard deviation.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL