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 ¶
- func Combinations(n, k int) float64
- func Correlation(xs, ys []float64) float64
- func Covariance(xs, ys []float64) float64
- func CumulativeSum(xs []float64) []float64
- func Factorial(n int) float64
- func Frequencies(xs []float64) map[float64]int
- func GeometricMean(xs []float64) float64
- func HarmonicMean(xs []float64) float64
- func InterquartileRange(xs []float64) float64
- func LinearRegression(xs, ys []float64) (slope, intercept float64)
- func Max(xs []float64) float64
- func Mean(xs []float64) float64
- func Median(xs []float64) float64
- func MedianAbsoluteDeviation(xs []float64) float64
- func Min(xs []float64) float64
- func Mode(xs []float64) []float64
- func Percentile(xs []float64, p float64) float64
- func Permutations(n, k int) float64
- func Product(xs []float64) float64
- func Quantile(xs []float64, p float64) float64
- func Range(xs []float64) float64
- func RootMeanSquare(xs []float64) float64
- func SampleStandardDeviation(xs []float64) float64
- func SampleVariance(xs []float64) float64
- func StandardDeviation(xs []float64) float64
- func Sum(xs []float64) float64
- func Variance(xs []float64) float64
- func ZScore(x, mean, standardDeviation float64) float64
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Combinations ¶
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 ¶
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 ¶
Covariance returns the population covariance of the paired slices xs and ys, or NaN when they are empty or of unequal length.
func CumulativeSum ¶
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 Frequencies ¶
Frequencies returns a map from each distinct value in xs to the number of times it occurs.
func GeometricMean ¶
GeometricMean returns the geometric mean of xs, or NaN when xs is empty or contains a non-positive value.
func HarmonicMean ¶
HarmonicMean returns the harmonic mean of xs, or NaN when xs is empty or contains a non-positive value.
func InterquartileRange ¶
InterquartileRange returns the difference between the 75th and 25th percentiles of xs, or NaN when xs is empty.
func LinearRegression ¶
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 Median ¶
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 ¶
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 Mode ¶
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 ¶
Percentile returns the p-th percentile of xs (p in [0,100]), equivalent to Quantile(xs, p/100).
func Permutations ¶
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 Quantile ¶
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 ¶
Range returns the difference between the largest and smallest values in xs, or NaN when xs is empty.
func RootMeanSquare ¶
RootMeanSquare returns the quadratic mean (root mean square) of xs, or NaN when xs is empty.
func SampleStandardDeviation ¶
SampleStandardDeviation returns the sample standard deviation of xs, or NaN when xs has fewer than two elements.
func SampleVariance ¶
SampleVariance returns the sample variance of xs (Bessel-corrected, dividing by n-1), or NaN when xs has fewer than two elements.
func StandardDeviation ¶
StandardDeviation returns the population standard deviation of xs, or NaN when xs is empty.
Types ¶
This section is empty.