statistical

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: MIT Imports: 4 Imported by: 0

README

Statistical

Overview

Statistical is a Go package that provides a collection of statistical operations for slices of Numbers.

Features

  • Generics: Supports any value type, thanks to Go generics.
  • Rich Functional API: Provides a collection of functional methods for easy manipulation of map elements.

Table for the Stastitical Operations

Method Description Input Output
Frequency Calculates the frequency of elements in a slice and returns a map with the counts of each element. items []T map[T]int
Median Calculates the median value of a slice of numbers. s []T T, error
Range Calculates the range of a slice of numbers (maximum value minus the minimum value). s []T T, T, error
Mean Calculates the mean (average) value of a slice of numbers. s []float64 float64
Variance Calculates the variance of a slice of numbers. s []float64 float64, error
StandardDeviation Calculates the standard deviation of a slice of numbers. s []float64 float64, error
Percentile Calculates the percentile value of a slice of numbers for a given percentage (between 0 and 100). s []T, p float64 T, error

Installation

Use go get to add the statistical package to your project:

go get github.com/thalesfsp/go-common-types/statistical

Usage

Example:

package main

import (
	"fmt"
	"github.com/thalesfsp/go-common-types/statistical"
)

func main() {
	items := []string{"a", "b", "b", "c", "c", "c", "d"}
	
	freq := statistical.Frequency(items)

	fmt.Println(freq) // map[a:1 b:2 c:3 d:1]
}

License

See LICENSE file for more details.

Contributing

Feel free to open issues or submit pull requests with improvements or bug fixes. Please ensure that your code follows the coding standards

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Frequency

func Frequency[T comparable](items []T) map[T]int

Frequency calculates the frequency of elements in a slice and returns a map with the counts of each element.

Example
items := []string{"a", "b", "b", "c", "c", "c", "d"}
freq := Frequency(items)
fmt.Printf("Frequency: %v\n", freq)
Output:

Frequency: map[a:1 b:2 c:3 d:1]

func Mean

func Mean(s []float64) float64

Mean calculates the mean (average) value of a slice of numbers.

Example
s := []float64{1, 2, 3, 4, 5}
mean := Mean(s)
fmt.Printf("Mean: %v\n", mean)
Output:

Mean: 3

func Median

func Median[T Numbers](s []T) (T, error)

Median calculates the median value of a slice of numbers.

Example
s := []float64{1, 2, 3, 4, 5}
median, _ := Median(s)
fmt.Printf("Median: %v\n", median)
Output:

Median: 3

func Percentile

func Percentile[T Numbers](s []T, p float64) (T, error)

Percentile calculates the percentile value of a slice of numbers for a given percentage (between 0 and 100).

Example
s := []int{1, 2, 3, 4, 5}
percentile, _ := Percentile(s, 0.5)
fmt.Printf("Percentile: %v\n", percentile)
Output:

Percentile: 3

func Range

func Range[T Numbers](s []T) (T, T, error)

Range calculates the range of a slice of numbers (maximum value minus the minimum value).

Example
s := []float64{1, 2, 3, 4, 5}
min, max, _ := Range(s)
fmt.Printf("Range: (%v, %v)\n", min, max)
Output:

Range: (1, 5)

func StandardDeviation

func StandardDeviation(s []float64) (float64, error)

StandardDeviation calculates the standard deviation of a slice of numbers.

Example
s := []float64{1, 2, 3, 4, 5}
stdDev, _ := StandardDeviation(s)
fmt.Printf("Standard Deviation: %v\n", stdDev)
Output:

Standard Deviation: 1.5811388300841898

func Variance

func Variance(s []float64) (float64, error)

Variance calculates the variance of a slice of numbers.

Example
s := []float64{1, 2, 3, 4, 5}
variance, _ := Variance(s)
fmt.Printf("Variance: %v\n", variance)
Output:

Variance: 2.5

Types

type Numbers

type Numbers interface {
	constraints.Signed | constraints.Float
}

Numbers is a constraint that permits any numeric type.

Jump to

Keyboard shortcuts

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