A lightweight Go package for simple mathematical and statistical calculations. Provides generic functions for computing mean, variance, and other common statistical operations with support for both integer and floating-point types.
- Simple API: No configuration required, just function calls
- Type Flexible: Support for integer and floating-point types via Go generics
- Tested: Comprehensive unit test coverage
- Minimal Dependencies: Only depends on the Go Standard Library and tserr for error handling
Install the package using go get:
go get github.com/thorsphere/lpstatsImport in your Go code:
import "github.com/thorsphere/lpstats"- Go 1.26 or higher (uses generic types)
Most functions use Go generics with type constraints defined in constraints.go. The main constraints are:
-
Number: All number types (int, uint, float, etc.)
Example:func Square[T Number](x T) float64 -
Signed: Signed number types (int, float)
Example:func Sign[T Signed](a T) T -
Sinteger: Signed integer types (int, int8, int16, etc.)
Example:func EqualS[T Sinteger](x, y T) error -
Uinteger: Unsigned integer types (uint, uint8, uint16, etc.)
Example:func VarianceN[T Uinteger](n T) float64
- Absolute value:
Abs()- returns the absolute value of a number - Arithmetic mean:
ArithmeticMean()- calculates the mean of a discrete value array - Equal for slices:
EqualS()- equality check for slices of signed integers - Near equal:
NearEqual()- approximate equality for float types - Sign:
Sign()- returns the sign of a number - Square:
Square()- returns the square of a number - Sum:
Sum()- calculates the sum of values - Mean:
Mean()- calculates the mean of a discrete value array - Variance:
Variance()- calculates the variance of a discrete value array - Expected value:
VarianceN()- calculates variance for a uniform distribution - Format float pointer:
FmtFloatPtr()- formats a pointer to a number as a string with one decimal place - Equal for float pointers:
NearEqualFloatPtr()- approximate equality check for pointers to float values
package main
import (
"fmt"
"github.com/thorsphere/lpstats"
)
func main() {
// Work with different numeric types
intSlice := []int{1, 2, 3, 4, 5, 6}
floatSlice := []float64{1.1, 2.1, 3.1, 4.1, 5.1, 6.1}
dieSize := uint(6)
// Calculate arithmetic mean
intMean, _ := lpstats.ArithmeticMean(intSlice)
floatMean, _ := lpstats.ArithmeticMean(floatSlice)
// Calculate variance
intVariance, _ := lpstats.Variance(intSlice)
floatVariance, _ := lpstats.Variance(floatSlice)
fmt.Printf("Integer slice - Mean: %.2f, Variance: %.2f\n", intMean, intVariance)
fmt.Printf("Float slice - Mean: %.2f, Variance: %.2f\n", floatMean, floatVariance)
// Variance for uniform distribution (e.g., dice)
diceVariance := lpstats.VarianceN(dieSize)
fmt.Printf("Variance of a %d-sided die: %.4f\n", dieSize, diceVariance)
}- Calculations use floating-point arithmetic and are not suitable for arbitrary precision fixed-point decimal arithmetic
- For very large datasets, consider memory and performance implications
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
For security issues, please see SECURITY.md.
This project is licensed under the GNU Affero General Public License v3.0. See LICENSE for details.