Skip to content

thorsphere/lpstats

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

lpstats

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.

Go Report Card CodeFactor OSS Lifecycle

PkgGoDev GitHub go.mod Go version Libraries.io dependency status for GitHub repo

GitHub release (latest by date) GitHub last commit GitHub commit activity GitHub code size in bytes GitHub Top Language GitHub

Features

  • 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

Installation

Install the package using go get:

go get github.com/thorsphere/lpstats

Import in your Go code:

import "github.com/thorsphere/lpstats"

Requirements

  • Go 1.26 or higher (uses generic types)

Type Constraints

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

API Reference

Supported Functions

  • 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

Quick Start

Example: Calculate Mean and Variance

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)
}

Known Limitations

  • 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

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

Security

For security issues, please see SECURITY.md.

License

This project is licensed under the GNU Affero General Public License v3.0. See LICENSE for details.

Resources

About

Go package for simple statistics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Languages