stats

package
v0.0.0-...-ffc4fba Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: Apache-2.0, Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package stats provides core statistical functions for numerical analysis. All standard deviation calculations use population stddev (÷n, not ÷(n−1)).

Index

Constants

View Source
const (
	PercentileMedian = 0.5
	PercentileP95    = 0.95
)

Well-known percentile thresholds.

View Source
const PercentMultiplier = 100

PercentMultiplier is the standard multiplier for converting ratios to percentages.

View Source
const ZScoreMaxSentinel = 100.0

ZScoreMaxSentinel is the cap for z-score when stddev is zero but value differs from mean.

Variables

This section is empty.

Functions

func Clamp

func Clamp[T cmp.Ordered](val, lo, hi T) T

Clamp restricts val to the range [lo, hi].

func Distribution

func Distribution[T any](items []T, classify func(T) string) map[string]int

Distribution counts items per label as determined by classify. Returns nil for a nil slice. Returns an empty map for an empty slice.

func ExceedsThreshold

func ExceedsThreshold(observed, predicted, threshold float64) bool

ExceedsThreshold reports whether observed diverges from predicted by more than threshold (as a fraction, e.g. 0.1 = 10%). Returns false when predicted <= 0 (no meaningful baseline).

func Max

func Max[T cmp.Ordered](values []T) T

Max returns the largest element in values. Returns the zero value of T for an empty slice.

func Mean

func Mean(values []float64) float64

Mean returns the arithmetic mean of values. Returns 0 for an empty slice.

func MeanStdDev

func MeanStdDev(values []float64) (mean, stddev float64)

MeanStdDev returns the arithmetic mean and population standard deviation. Returns (0, 0) for an empty slice.

func Median

func Median(values []float64) float64

Median returns the 50th percentile of values. Returns 0 for an empty slice.

func Min

func Min[T cmp.Ordered](values []T) T

Min returns the smallest element in values. Returns the zero value of T for an empty slice.

func Percentile

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

Percentile returns the p-th percentile of values using linear interpolation. p must be in [0, 1]. The input slice is not modified (a copy is sorted internally). Returns 0 for an empty slice.

func Sum

func Sum[T cmp.Ordered](values []T) T

Sum returns the sum of all elements in values. Returns the zero value of T for an empty slice.

func ToPercent

func ToPercent(ratio float64) float64

ToPercent converts a ratio (0.0–1.0) to a percentage (0–100).

Types

type EMA

type EMA struct {
	// contains filtered or unexported fields
}

EMA computes an exponential moving average with a fixed smoothing factor.

func NewEMA

func NewEMA(alpha float64) *EMA

NewEMA creates an EMA with the given smoothing factor alpha in (0, 1].

func (*EMA) Initialized

func (e *EMA) Initialized() bool

Initialized reports whether Update has been called at least once.

func (*EMA) Update

func (e *EMA) Update(v float64) float64

Update feeds a new observation and returns the updated average. The first call initializes the EMA to the observation value.

func (*EMA) Value

func (e *EMA) Value() float64

Value returns the current EMA value (0 before any Update).

Jump to

Keyboard shortcuts

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