expgolang.org/x/exp/stats Index | Files

package stats

import "golang.org/x/exp/stats"

Package stats provides basic descriptive statistics.

This is intended not as a comprehensive statistics package, but to provide common, everyday statistical functions.

As a rule of thumb, a statistical function belongs in this package if it would be explained in a typical high school.

These functions aim to balance performance and accuracy, but some amount of error is inevitable in floating-point computations. The underlying implementations may change, resulting in small changes in their results from version to version. If the caller needs particular guarantees on accuracy and overflow behavior or version stability, they should use a more specialized implementation.

Index

Functions

func Mean

func Mean[F ~float64](values []F) F

Mean returns the arithmetic mean of the values in values.

Mean does not modify the array.

Mean panics if values is an empty slice.

If values contains NaN or both Inf and -Inf, it returns NaN. If values contains Inf, it returns Inf. If values contains -Inf, it returns -Inf.

func MeanAndStdDev

func MeanAndStdDev[F ~float64](values []F) (F, F)

MeanAndStdDev returns the arithmetic mean and sample standard deviation of values; the standard deviation is only defined for len(values) > 1.

MeanAndStdDev does not modify the array.

MeanAndStdDev panics if values is an empty slice.

If values contains NaN, it returns NaN, NaN. If values contains both Inf and -Inf, it returns NaN, Inf. If values contains Inf, it returns Inf, Inf. If values contains -Inf, it returns -Inf, Inf.

func Median

func Median[F ~float64](values []F) F

Median returns the median of the values in values.

Median does not modify the array.

Median may perform asymptotically faster and allocate asymptotically less if the slice is already sorted.

If values is an empty slice, it panics. If values contains NaN, it returns NaN. -Inf is treated as smaller than all other values, Inf is treated as larger than all other values, and -0.0 is treated as smaller than 0.0.

func Quantiles

func Quantiles[F ~float64](values []F, quantiles ...F) []F

Quantiles returns a sequence of quantiles of values.

The returned slice has the same length as the quantiles slice, and the elements are one-to-one with the input quantiles. A quantile of 0 corresponds to the minimum value in values and a quantile of 1 corresponds to the maximum value in values. A quantile of 0.5 is the same as the value returned by Median.

Quantiles does not modify the array.

Quantiles may perform asymptotically faster and allocate asymptotically less if the slice is already sorted.

Quantiles panics if values is an empty slice or any quantile is not contained in the interval [0, 1].

If values contains NaN, it returns [NaN, ..., NaN]. -Inf is treated as smaller than all other values, Inf is treated as larger than all other values, and -0.0 is treated as smaller than 0.0.

Source Files

stats.go

Version
v0.0.0-20250718183923-645b1fa84792 (latest)
Published
Jul 18, 2025
Platform
linux/amd64
Imports
2 packages
Last checked
3 hours ago

Tools for package owners.