chestats

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2025 License: MIT Imports: 4 Imported by: 0

README

chestats - Statistical Functions

Common statistical calculations for numeric data with zero dependencies.

Features

  • Mean, Median, Mode
  • Variance and Standard Deviation
  • Percentiles and Quartiles
  • Min, Max, Range, Sum
  • Correlation
  • Generic support for all numeric types

Quick Start

data := []float64{1, 2, 3, 4, 5}

mean := chestats.Mean(data)         // 3.0
median := chestats.Median(data)     // 3.0
stdDev := chestats.StdDev(data)     // ~1.41

q1, q2, q3 := chestats.Quartiles(data)
p95 := chestats.Percentile(data, 95)

API Reference

  • Mean[T Number](values []T) float64
  • Median[T Number](values []T) float64
  • Mode[T Number](values []T) T
  • Variance[T Number](values []T) float64
  • SampleVariance[T Number](values []T) float64
  • StdDev[T Number](values []T) float64
  • SampleStdDev[T Number](values []T) float64
  • Min[T Number](values []T) T
  • Max[T Number](values []T) T
  • Sum[T Number](values []T) T
  • Percentile[T Number](values []T, p float64) float64
  • Quartiles[T Number](values []T) (q1, q2, q3 float64)
  • IQR[T Number](values []T) float64
  • Range[T Number](values []T) T
  • Correlation[T Number](x, y []T) float64

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Correlation

func Correlation[T Number](x, y []T) float64

Correlation calculates the Pearson correlation coefficient between two slices. Returns 0 if slices are empty or of different lengths. Returns value between -1 and 1, where:

-1 indicates perfect negative correlation
 0 indicates no correlation
 1 indicates perfect positive correlation

func IQR

func IQR[T Number](values []T) float64

IQR calculates the Interquartile Range (Q3 - Q1). Returns 0 for empty slice.

func Max

func Max[T Number](values []T) T

Max returns the maximum value from a slice of numbers. Panics if the slice is empty.

func Mean

func Mean[T Number](values []T) float64

Mean calculates the arithmetic mean (average) of a slice of numbers. Returns 0 for empty slice.

func Median

func Median[T Number](values []T) float64

Median calculates the median (middle value) of a slice of numbers. Returns 0 for empty slice.

func Min

func Min[T Number](values []T) T

Min returns the minimum value from a slice of numbers. Panics if the slice is empty.

func Mode

func Mode[T Number](values []T) T

Mode calculates the mode (most frequent value) of a slice of numbers. Returns 0 if the slice is empty or if there's no unique mode.

func Percentile

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

Percentile calculates the nth percentile of a slice of numbers. p should be between 0 and 100. Returns 0 for empty slice.

func Quartiles

func Quartiles[T Number](values []T) (q1, q2, q3 float64)

Quartiles calculates the first (Q1), second (Q2/median), and third (Q3) quartiles. Returns (0, 0, 0) for empty slice.

func Range

func Range[T Number](values []T) T

Range calculates the range (max - min) of a slice of numbers. Panics if the slice is empty.

func SampleStdDev

func SampleStdDev[T Number](values []T) float64

SampleStdDev calculates the sample standard deviation of a slice of numbers. Returns 0 for slices with less than 2 elements.

func SampleVariance

func SampleVariance[T Number](values []T) float64

SampleVariance calculates the sample variance of a slice of numbers. Returns 0 for slices with less than 2 elements.

func StdDev

func StdDev[T Number](values []T) float64

StdDev calculates the population standard deviation of a slice of numbers. Returns 0 for empty slice.

func Sum

func Sum[T Number](values []T) T

Sum calculates the sum of a slice of numbers.

func Variance

func Variance[T Number](values []T) float64

Variance calculates the population variance of a slice of numbers. Returns 0 for empty slice.

Types

type Number

type Number interface {
	constraints.Integer | constraints.Float
}

Number represents numeric types that can be used with statistical functions.

Jump to

Keyboard shortcuts

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