mathutil

package
v0.0.0-...-bc49051 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy[T any](input []T) []T

Copy copies an array of float64s.

func CopySort

func CopySort[T cmp.Ordered](input []T) []T

CopySort copies and sorts a slice ascending.

func Correlation

func Correlation[T Operatable](actual, prediction []T) float64

Correlation computes the r^2 correlation from a given set of actual values to a given set of prediction values.

An r^2 of +1.0 means perfectly correlated, and 0 means completely uncorrelated.

Because the r^2 is "squared" you will detect inverse and positive correlation with the same output value.

func Mean

func Mean[T Operatable](input []T) (mean T)

Mean gets the average of a slice of numbers

func MeanDuration

func MeanDuration(durations []time.Duration) time.Duration

MeanDuration returns the mean or average of a list of durations.

The underlying math is done with the `big` package so it may be slightly slower than anticipated, but this prevents overflows.

func Median

func Median[T Operatable](values []T) T

Median returns the middle value from a list of _unsorted_ values.

func MedianSorted

func MedianSorted[T Operatable](values []T) (output T)

MedianSorted returns the middle value from a list of _sorted_ values.

func MinMax

func MinMax[T cmp.Ordered](values []T) (min, max T)

MinMax returns both the min and max in one pass.

func Normalize

func Normalize[T Operatable](values []T) []float64

Normalize takes a list of values and maps them onto the interval [0, 1.0].

It is important to take from the above that the output will always be on a positive interval regardless of the sign of the inputs.

func PercentChange

func PercentChange(x1, x2 float64) float64

PercentChange returns the percent change of two floats.

func Percentile

func Percentile[T Operatable](input []T, percent float64) (output T)

Percentile finds the relative standing in a slice of floats. `percent` should be given on the interval [0,100.0).

func PercentileSorted

func PercentileSorted[T Operatable](sortedInput []T, percent float64) (percentile T)

PercentileSorted finds the relative standing in a sorted slice of floats. `percent` should be given on the interval [0,100.0).

func RoundPlaces

func RoundPlaces(input float64, places int) float64

RoundPlaces a float to a specific decimal place or precision

func Sum

func Sum[T Operatable](input []T) (total T)

Sum adds all the elements of a slice together

func Var

func Var[T Operatable](input []T, sample int) (variance T)

Var finds the variance for both population and sample data

func VarP

func VarP[T Operatable](input []T) T

VarP finds the amount of variance within a population

func VarS

func VarS[T Operatable](input []T) T

VarS finds the amount of variance within a sample

Types

type Operatable

type Operatable interface {
	~int | ~int64 | ~float64
}

Operatable are types that can be mathed.

Strictly it is the `Ordered` types less string.

Jump to

Keyboard shortcuts

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