stats

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: MIT Imports: 9 Imported by: 1,171

README

Stats - Golang Statistics Package

A well tested and comprehensive Golang statistics library / package / module with no dependencies.

If you have any suggestions, problems or bug reports please create an issue and I'll do my best to accommodate you. In addition simply starring the repo would show your support for the project and be very much appreciated!

Installation

go get github.com/montanaflynn/stats

Example Usage

All the functions can be seen in examples/main.go but here's a little taste:

// start with some source data to use
data := []float64{1.0, 2.1, 3.2, 4.823, 4.1, 5.8}

// you could also use different types like this
// data := stats.LoadRawData([]int{1, 2, 3, 4, 5})
// data := stats.LoadRawData([]interface{}{1.1, "2", 3})
// etc...

median, _ := stats.Median(data)
fmt.Println(median) // 3.65

roundedMedian, _ := stats.Round(median, 0)
fmt.Println(roundedMedian) // 4

Documentation

The entire API documentation is available on GoDoc.org or pkg.go.dev.

You can also view docs offline with the following commands:

# Command line
godoc .              # show all exported apis
godoc . Median       # show a single function
godoc -ex . Round    # show function with example
godoc . Float64Data  # show the type and methods

# Local website
godoc -http=:4444    # start the godoc server on port 4444
open http://localhost:4444/pkg/github.com/montanaflynn/stats/

The exported API is as follows:

var (
    ErrEmptyInput = statsError{"Input must not be empty."}
    ErrNaN        = statsError{"Not a number."}
    ErrNegative   = statsError{"Must not contain negative values."}
    ErrZero       = statsError{"Must not contain zero values."}
    ErrBounds     = statsError{"Input is outside of range."}
    ErrSize       = statsError{"Must be the same length."}
    ErrInfValue   = statsError{"Value is infinite."}
    ErrYCoord     = statsError{"Y Value must be greater than zero."}
)

func Round(input float64, places int) (rounded float64, err error) {}

type Float64Data []float64

func LoadRawData(raw interface{}) (f Float64Data) {}

func AutoCorrelation(data Float64Data, lags int) (float64, error) {}
func ChebyshevDistance(dataPointX, dataPointY Float64Data) (distance float64, err error) {}
func Correlation(data1, data2 Float64Data) (float64, error) {}
func Covariance(data1, data2 Float64Data) (float64, error) {}
func CovariancePopulation(data1, data2 Float64Data) (float64, error) {}
func CumulativeSum(input Float64Data) ([]float64, error) {}
func Describe(input Float64Data, allowNaN bool, percentiles *[]float64) (*Description, error) {}
func DescribePercentileFunc(input Float64Data, allowNaN bool, percentiles *[]float64, percentileFunc func(Float64Data, float64) (float64, error)) (*Description, error) {}
func Entropy(input Float64Data) (float64, error) {}
func EuclideanDistance(dataPointX, dataPointY Float64Data) (distance float64, err error) {}
func GeometricMean(input Float64Data) (float64, error) {}
func HarmonicMean(input Float64Data) (float64, error) {}
func InterQuartileRange(input Float64Data) (float64, error) {}
func ManhattanDistance(dataPointX, dataPointY Float64Data) (distance float64, err error) {}
func Max(input Float64Data) (max float64, err error) {}
func Mean(input Float64Data) (float64, error) {}
func Median(input Float64Data) (median float64, err error) {}
func MedianAbsoluteDeviation(input Float64Data) (mad float64, err error) {}
func MedianAbsoluteDeviationPopulation(input Float64Data) (mad float64, err error) {}
func Midhinge(input Float64Data) (float64, error) {}
func Min(input Float64Data) (min float64, err error) {}
func MinkowskiDistance(dataPointX, dataPointY Float64Data, lambda float64) (distance float64, err error) {}
func Mode(input Float64Data) (mode []float64, err error) {}
func NormBoxMullerRvs(loc float64, scale float64, size int) []float64 {}
func NormCdf(x float64, loc float64, scale float64) float64 {}
func NormEntropy(loc float64, scale float64) float64 {}
func NormFit(data []float64) [2]float64{}
func NormInterval(alpha float64, loc float64,  scale float64 ) [2]float64 {}
func NormIsf(p float64, loc float64, scale float64) (x float64) {}
func NormLogCdf(x float64, loc float64, scale float64) float64 {}
func NormLogPdf(x float64, loc float64, scale float64) float64 {}
func NormLogSf(x float64, loc float64, scale float64) float64 {}
func NormMean(loc float64, scale float64) float64 {}
func NormMedian(loc float64, scale float64) float64 {}
func NormMoment(n int, loc float64, scale float64) float64 {}
func NormPdf(x float64, loc float64, scale float64) float64 {}
func NormPpf(p float64, loc float64, scale float64) (x float64) {}
func NormPpfRvs(loc float64, scale float64, size int) []float64 {}
func NormSf(x float64, loc float64, scale float64) float64 {}
func NormStats(loc float64, scale float64, moments string) []float64 {}
func NormStd(loc float64, scale float64) float64 {}
func NormVar(loc float64, scale float64) float64 {}
func Pearson(data1, data2 Float64Data) (float64, error) {}
func Percentile(input Float64Data, percent float64) (percentile float64, err error) {}
func PercentileNearestRank(input Float64Data, percent float64) (percentile float64, err error) {}
func PopulationVariance(input Float64Data) (pvar float64, err error) {}
func Sample(input Float64Data, takenum int, replacement bool) ([]float64, error) {}
func SampleVariance(input Float64Data) (svar float64, err error) {}
func Sigmoid(input Float64Data) ([]float64, error) {}
func SoftMax(input Float64Data) ([]float64, error) {}
func StableSample(input Float64Data, takenum int) ([]float64, error) {}
func StandardDeviation(input Float64Data) (sdev float64, err error) {}
func StandardDeviationPopulation(input Float64Data) (sdev float64, err error) {}
func StandardDeviationSample(input Float64Data) (sdev float64, err error) {}
func StdDevP(input Float64Data) (sdev float64, err error) {}
func StdDevS(input Float64Data) (sdev float64, err error) {}
func Sum(input Float64Data) (sum float64, err error) {}
func Trimean(input Float64Data) (float64, error) {}
func VarP(input Float64Data) (sdev float64, err error) {}
func VarS(input Float64Data) (sdev float64, err error) {}
func Variance(input Float64Data) (sdev float64, err error) {}
func ProbGeom(a int, b int, p float64) (prob float64, err error) {}
func ExpGeom(p float64) (exp float64, err error) {}
func VarGeom(p float64) (exp float64, err error) {}

type Coordinate struct {
    X, Y float64
}

type Series []Coordinate

func ExponentialRegression(s Series) (regressions Series, err error) {}
func LinearRegression(s Series) (regressions Series, err error) {}
func LogarithmicRegression(s Series) (regressions Series, err error) {}

type Outliers struct {
    Mild    Float64Data
    Extreme Float64Data
}

type Quartiles struct {
    Q1 float64
    Q2 float64
    Q3 float64
}

func Quartile(input Float64Data) (Quartiles, error) {}
func QuartileOutliers(input Float64Data) (Outliers, error) {}

Contributing

Pull request are always welcome no matter how big or small. I've included a Makefile that has a lot of helper targets for common actions such as linting, testing, code coverage reporting and more.

  1. Fork the repo and clone your fork
  2. Create new branch (git checkout -b some-thing)
  3. Make the desired changes
  4. Ensure tests pass (go test -cover or make test)
  5. Run lint and fix problems (go vet . or make lint)
  6. Commit changes (git commit -am 'Did something')
  7. Push branch (git push origin some-thing)
  8. Submit pull request

To make things as seamless as possible please also consider the following steps:

  • Update examples/main.go with a simple example of the new feature
  • Update README.md documentation section with any new exported API
  • Keep 100% code coverage (you can check with make coverage)
  • Squash commits into single units of work with git rebase -i new-feature

Releasing

This is not required by contributors and mostly here as a reminder to myself as the maintainer of this repo. To release a new version we should update the CHANGELOG.md and DOCUMENTATION.md.

First install the tools used to generate the markdown files and release:

go install github.com/davecheney/godoc2md@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
brew tap git-chglog/git-chglog
brew install gnu-sed hub git-chglog

Then you can run these make directives:

# Generate DOCUMENTATION.md
make docs

Then we can create a CHANGELOG.md a new git tag and a github release:

make release TAG=v0.x.x

To authenticate hub for the release you will need to create a personal access token and use it as the password when it's requested.

MIT License

Copyright (c) 2014-2023 Montana Flynn (https://montanaflynn.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORpublicS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package stats is a well tested and comprehensive statistics library package with no dependencies.

Example Usage:

// start with some source data to use
data := []float64{1.0, 2.1, 3.2, 4.823, 4.1, 5.8}

// you could also use different types like this
// data := stats.LoadRawData([]int{1, 2, 3, 4, 5})
// data := stats.LoadRawData([]interface{}{1.1, "2", 3})
// etc...

median, _ := stats.Median(data)
fmt.Println(median) // 3.65

roundedMedian, _ := stats.Round(median, 0)
fmt.Println(roundedMedian) // 4

MIT License Copyright (c) 2014-2020 Montana Flynn (https://montanaflynn.com)

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyInput Input must not be empty
	ErrEmptyInput = statsError{"Input must not be empty."}
	// ErrNaN Not a number
	ErrNaN = statsError{"Not a number."}
	// ErrNegative Must not contain negative values
	ErrNegative = statsError{"Must not contain negative values."}
	// ErrZero Must not contain zero values
	ErrZero = statsError{"Must not contain zero values."}
	// ErrBounds Input is outside of range
	ErrBounds = statsError{"Input is outside of range."}
	// ErrSize Must be the same length
	ErrSize = statsError{"Must be the same length."}
	// ErrInfValue Value is infinite
	ErrInfValue = statsError{"Value is infinite."}
	// ErrYCoord Y Value must be greater than zero
	ErrYCoord = statsError{"Y Value must be greater than zero."}
)

These are the package-wide error values. All error identification should use these values. https://github.com/golang/go/wiki/Errors#naming

View Source
var (
	EmptyInputErr = ErrEmptyInput
	NaNErr        = ErrNaN
	NegativeErr   = ErrNegative
	ZeroErr       = ErrZero
	BoundsErr     = ErrBounds
	SizeErr       = ErrSize
	InfValue      = ErrInfValue
	YCoordErr     = ErrYCoord
	EmptyInput    = ErrEmptyInput
)

Legacy error names that didn't start with Err

Functions

func AutoCorrelation

func AutoCorrelation(data Float64Data, lags int) (float64, error)

AutoCorrelation is the correlation of a signal with a delayed copy of itself as a function of delay

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	s1 := []float64{1, 2, 3, 4, 5}
	a, _ := stats.AutoCorrelation(s1, 1)
	fmt.Println(a)
}
Output:

0.4

func ChebyshevDistance

func ChebyshevDistance(dataPointX, dataPointY Float64Data) (distance float64, err error)

ChebyshevDistance computes the Chebyshev distance between two data sets

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	d1 := []float64{2, 3, 4, 5, 6, 7, 8}
	d2 := []float64{8, 7, 6, 5, 4, 3, 2}
	cd, _ := stats.ChebyshevDistance(d1, d2)
	fmt.Println(cd)
}
Output:

6

func Correlation

func Correlation(data1, data2 Float64Data) (float64, error)

Correlation describes the degree of relationship between two sets of data

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	s1 := []float64{1, 2, 3, 4, 5}
	s2 := []float64{1, 2, 3, 5, 6}
	a, _ := stats.Correlation(s1, s2)
	rounded, _ := stats.Round(a, 5)
	fmt.Println(rounded)
}
Output:

0.99124

func Covariance

func Covariance(data1, data2 Float64Data) (float64, error)

Covariance is a measure of how much two sets of data change

func CovariancePopulation

func CovariancePopulation(data1, data2 Float64Data) (float64, error)

CovariancePopulation computes covariance for entire population between two variables.

func CumulativeSum

func CumulativeSum(input Float64Data) ([]float64, error)

CumulativeSum calculates the cumulative sum of the input slice

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	data := []float64{1.0, 2.1, 3.2, 4.823, 4.1, 5.8}
	csum, _ := stats.CumulativeSum(data)
	fmt.Println(csum)
}
Output:

[1 3.1 6.300000000000001 11.123000000000001 15.223 21.023]

func Entropy added in v0.6.0

func Entropy(input Float64Data) (float64, error)

Entropy provides calculation of the entropy

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	d := []float64{1.1, 2.2, 3.3}
	e, _ := stats.Entropy(d)
	fmt.Println(e)
}
Output:

1.0114042647073518

func EuclideanDistance

func EuclideanDistance(dataPointX, dataPointY Float64Data) (distance float64, err error)

EuclideanDistance computes the Euclidean distance between two data sets

func ExpGeom added in v0.7.0

func ExpGeom(p float64) (exp float64, err error)

ProbGeom generates the expectation or average number of trials for a geometric random variable with parameter p

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	p := 0.5
	exp, _ := stats.ExpGeom(p)
	fmt.Println(exp)
}
Output:

2

func GeometricMean

func GeometricMean(input Float64Data) (float64, error)

GeometricMean gets the geometric mean for a slice of numbers

func HarmonicMean

func HarmonicMean(input Float64Data) (float64, error)

HarmonicMean gets the harmonic mean for a slice of numbers

func InterQuartileRange

func InterQuartileRange(input Float64Data) (float64, error)

InterQuartileRange finds the range between Q1 and Q3

func ManhattanDistance

func ManhattanDistance(dataPointX, dataPointY Float64Data) (distance float64, err error)

ManhattanDistance computes the Manhattan distance between two data sets

func Max

func Max(input Float64Data) (max float64, err error)

Max finds the highest number in a slice

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	d := []float64{1.1, 2.3, 3.2, 4.0, 4.01, 5.09}
	a, _ := stats.Max(d)
	fmt.Println(a)
}
Output:

5.09

func Mean

func Mean(input Float64Data) (float64, error)

Mean gets the average of a slice of numbers

func Median

func Median(input Float64Data) (median float64, err error)

Median gets the median number in a slice of numbers

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	data := []float64{1.0, 2.1, 3.2, 4.823, 4.1, 5.8}
	median, _ := stats.Median(data)
	fmt.Println(median)
}
Output:

3.65

func MedianAbsoluteDeviation

func MedianAbsoluteDeviation(input Float64Data) (mad float64, err error)

MedianAbsoluteDeviation finds the median of the absolute deviations from the dataset median

func MedianAbsoluteDeviationPopulation

func MedianAbsoluteDeviationPopulation(input Float64Data) (mad float64, err error)

MedianAbsoluteDeviationPopulation finds the median of the absolute deviations from the population median

func Midhinge

func Midhinge(input Float64Data) (float64, error)

Midhinge finds the average of the first and third quartiles

func Min

func Min(input Float64Data) (min float64, err error)

Min finds the lowest number in a set of data

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	d := stats.LoadRawData([]interface{}{1.1, "2", 3.0, 4, "5"})
	a, _ := stats.Min(d)
	fmt.Println(a)
}
Output:

1.1

func MinkowskiDistance

func MinkowskiDistance(dataPointX, dataPointY Float64Data, lambda float64) (distance float64, err error)

MinkowskiDistance computes the Minkowski distance between two data sets

Arguments:

dataPointX: First set of data points
dataPointY: Second set of data points. Length of both data
            sets must be equal.
lambda:     aka p or city blocks; With lambda = 1
            returned distance is manhattan distance and
            lambda = 2; it is euclidean distance. Lambda
            reaching to infinite - distance would be chebysev
            distance.

Return:

Distance or error

func Mode

func Mode(input Float64Data) (mode []float64, err error)

Mode gets the mode [most frequent value(s)] of a slice of float64s

func Ncr added in v0.6.0

func Ncr(n, r int) int

Ncr is an N choose R algorithm. Aaron Cannon's algorithm.

func NormBoxMullerRvs added in v0.6.0

func NormBoxMullerRvs(loc float64, scale float64, size int) []float64

NormBoxMullerRvs generates random variates using the Box–Muller transform. For more information please visit: http://mathworld.wolfram.com/Box-MullerTransformation.html

func NormCdf added in v0.6.0

func NormCdf(x float64, loc float64, scale float64) float64

NormCdf is the cumulative distribution function.

func NormEntropy added in v0.6.0

func NormEntropy(loc float64, scale float64) float64

NormEntropy is the differential entropy of the RV.

func NormFit added in v0.6.0

func NormFit(data []float64) [2]float64

NormFit returns the maximum likelihood estimators for the Normal Distribution. Takes array of float64 values. Returns array of Mean followed by Standard Deviation.

func NormInterval added in v0.6.0

func NormInterval(alpha float64, loc float64, scale float64) [2]float64

NormInterval finds endpoints of the range that contains alpha percent of the distribution.

func NormIsf added in v0.6.0

func NormIsf(p float64, loc float64, scale float64) (x float64)

NormIsf is the inverse survival function (inverse of sf).

func NormLogCdf added in v0.6.0

func NormLogCdf(x float64, loc float64, scale float64) float64

NormLogCdf is the log of the cumulative distribution function.

func NormLogPdf added in v0.6.0

func NormLogPdf(x float64, loc float64, scale float64) float64

NormLogPdf is the log of the probability density function.

func NormLogSf added in v0.6.0

func NormLogSf(x float64, loc float64, scale float64) float64

NormLogSf is the log of the survival function.

func NormMean added in v0.6.0

func NormMean(loc float64, scale float64) float64

NormMean is the mean/expected value of the distribution.

func NormMedian added in v0.6.0

func NormMedian(loc float64, scale float64) float64

NormMedian is the median of the distribution.

func NormMoment added in v0.6.0

func NormMoment(n int, loc float64, scale float64) float64

NormMoment approximates the non-central (raw) moment of order n. For more information please visit: https://math.stackexchange.com/questions/1945448/methods-for-finding-raw-moments-of-the-normal-distribution

func NormPdf added in v0.6.0

func NormPdf(x float64, loc float64, scale float64) float64

NormPdf is the probability density function.

func NormPpf added in v0.6.0

func NormPpf(p float64, loc float64, scale float64) (x float64)

NormPpf is the point percentile function. This is based on Peter John Acklam's inverse normal CDF. algorithm: http://home.online.no/~pjacklam/notes/invnorm/ (no longer visible). For more information please visit: https://stackedboxes.org/2017/05/01/acklams-normal-quantile-function/

func NormPpfRvs added in v0.6.0

func NormPpfRvs(loc float64, scale float64, size int) []float64

NormPpfRvs generates random variates using the Point Percentile Function. For more information please visit: https://demonstrations.wolfram.com/TheMethodOfInverseTransforms/

func NormSf added in v0.6.0

func NormSf(x float64, loc float64, scale float64) float64

NormSf is the survival function (also defined as 1 - cdf, but sf is sometimes more accurate).

func NormStats added in v0.6.0

func NormStats(loc float64, scale float64, moments string) []float64

NormStats returns the mean, variance, skew, and/or kurtosis. Mean(‘m’), variance(‘v’), skew(‘s’), and/or kurtosis(‘k’). Takes string containing any of 'mvsk'. Returns array of m v s k in that order.

func NormStd added in v0.6.0

func NormStd(loc float64, scale float64) float64

NormStd is the standard deviation of the distribution.

func NormVar added in v0.6.0

func NormVar(loc float64, scale float64) float64

NormVar is the variance of the distribution.

func Pearson

func Pearson(data1, data2 Float64Data) (float64, error)

Pearson calculates the Pearson product-moment correlation coefficient between two variables

func Percentile

func Percentile(input Float64Data, percent float64) (percentile float64, err error)

Percentile finds the relative standing in a slice of floats

func PercentileNearestRank

func PercentileNearestRank(input Float64Data, percent float64) (percentile float64, err error)

PercentileNearestRank finds the relative standing in a slice of floats using the Nearest Rank method

func PopulationVariance

func PopulationVariance(input Float64Data) (pvar float64, err error)

PopulationVariance finds the amount of variance within a population

func ProbGeom added in v0.7.0

func ProbGeom(a int, b int, p float64) (prob float64, err error)

ProbGeom generates the probability for a geometric random variable with parameter p to achieve success in the interval of [a, b] trials See https://en.wikipedia.org/wiki/Geometric_distribution for more information

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	p := 0.5
	a := 1
	b := 2
	chance, _ := stats.ProbGeom(a, b, p)
	fmt.Println(chance)
}
Output:

0.25

func Round

func Round(input float64, places int) (rounded float64, err error)

Round a float to a specific decimal place or precision

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	rounded, _ := stats.Round(1.534424, 1)
	fmt.Println(rounded)
}
Output:

1.5

func Sample

func Sample(input Float64Data, takenum int, replacement bool) ([]float64, error)

Sample returns sample from input with replacement or without

func SampleVariance

func SampleVariance(input Float64Data) (svar float64, err error)

SampleVariance finds the amount of variance within a sample

func Sigmoid added in v0.5.0

func Sigmoid(input Float64Data) ([]float64, error)

Sigmoid returns the input values in the range of -1 to 1 along the sigmoid or s-shaped curve, commonly used in machine learning while training neural networks as an activation function.

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	s, _ := stats.Sigmoid([]float64{3.0, 1.0, 2.1})
	fmt.Println(s)
}
Output:

[0.9525741268224334 0.7310585786300049 0.8909031788043871]

func SoftMax added in v0.5.0

func SoftMax(input Float64Data) ([]float64, error)

SoftMax returns the input values in the range of 0 to 1 with sum of all the probabilities being equal to one. It is commonly used in machine learning neural networks.

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	sm, _ := stats.SoftMax([]float64{3.0, 1.0, 0.2})
	fmt.Println(sm)
}
Output:

[0.8360188027814407 0.11314284146556013 0.05083835575299916]

func StableSample added in v0.6.0

func StableSample(input Float64Data, takenum int) ([]float64, error)

StableSample like stable sort, it returns samples from input while keeps the order of original data.

func StandardDeviation

func StandardDeviation(input Float64Data) (sdev float64, err error)

StandardDeviation the amount of variation in the dataset

func StandardDeviationPopulation

func StandardDeviationPopulation(input Float64Data) (sdev float64, err error)

StandardDeviationPopulation finds the amount of variation from the population

func StandardDeviationSample

func StandardDeviationSample(input Float64Data) (sdev float64, err error)

StandardDeviationSample finds the amount of variation from a sample

func StdDevP

func StdDevP(input Float64Data) (sdev float64, err error)

StdDevP is a shortcut to StandardDeviationPopulation

func StdDevS

func StdDevS(input Float64Data) (sdev float64, err error)

StdDevS is a shortcut to StandardDeviationSample

func Sum

func Sum(input Float64Data) (sum float64, err error)

Sum adds all the numbers of a slice together

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	d := []float64{1.1, 2.2, 3.3}
	a, _ := stats.Sum(d)
	fmt.Println(a)
}
Output:

6.6

func Trimean

func Trimean(input Float64Data) (float64, error)

Trimean finds the average of the median and the midhinge

func VarGeom added in v0.7.0

func VarGeom(p float64) (exp float64, err error)

ProbGeom generates the variance for number for a geometric random variable with parameter p

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	p := 0.5
	vari, _ := stats.VarGeom(p)
	fmt.Println(vari)
}
Output:

2

func VarP

func VarP(input Float64Data) (sdev float64, err error)

VarP is a shortcut to PopulationVariance

func VarS

func VarS(input Float64Data) (sdev float64, err error)

VarS is a shortcut to SampleVariance

func Variance

func Variance(input Float64Data) (sdev float64, err error)

Variance the amount of variation in the dataset

Types

type Coordinate

type Coordinate struct {
	X, Y float64
}

Coordinate holds the data in a series

func ExpReg

func ExpReg(s []Coordinate) (regressions []Coordinate, err error)

ExpReg is a shortcut to ExponentialRegression

func LinReg

func LinReg(s []Coordinate) (regressions []Coordinate, err error)

LinReg is a shortcut to LinearRegression

func LogReg

func LogReg(s []Coordinate) (regressions []Coordinate, err error)

LogReg is a shortcut to LogarithmicRegression

type Description added in v0.7.1

type Description struct {
	Count                  int
	Mean                   float64
	Std                    float64
	Max                    float64
	Min                    float64
	DescriptionPercentiles []descriptionPercentile
	AllowedNaN             bool
}

Holds information about the dataset provided to Describe

func Describe added in v0.7.1

func Describe(input Float64Data, allowNaN bool, percentiles *[]float64) (*Description, error)

Describe generates descriptive statistics about a provided dataset, similar to python's pandas.describe()

func DescribePercentileFunc added in v0.7.1

func DescribePercentileFunc(input Float64Data, allowNaN bool, percentiles *[]float64, percentileFunc func(Float64Data, float64) (float64, error)) (*Description, error)

Describe generates descriptive statistics about a provided dataset, similar to python's pandas.describe() Takes in a function to use for percentile calculation

func (*Description) String added in v0.7.1

func (d *Description) String(decimals int) string

Represents the Description instance in a string format with specified number of decimals

count   3
mean    2.00
std     0.82
max     3.00
min     1.00
25.00%  NaN
50.00%  1.50
75.00%  2.50
NaN OK  true

type Float64Data

type Float64Data []float64

Float64Data is a named type for []float64 with helper methods

func LoadRawData

func LoadRawData(raw interface{}) (f Float64Data)

LoadRawData parses and converts a slice of mixed data types to floats

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	data := stats.LoadRawData([]interface{}{1.1, "2", 3})
	fmt.Println(data)
}
Output:

[1.1 2 3]

func (Float64Data) AutoCorrelation

func (f Float64Data) AutoCorrelation(lags int) (float64, error)

AutoCorrelation is the correlation of a signal with a delayed copy of itself as a function of delay

func (Float64Data) Correlation

func (f Float64Data) Correlation(d Float64Data) (float64, error)

Correlation describes the degree of relationship between two sets of data

func (Float64Data) Covariance

func (f Float64Data) Covariance(d Float64Data) (float64, error)

Covariance is a measure of how much two sets of data change

func (Float64Data) CovariancePopulation

func (f Float64Data) CovariancePopulation(d Float64Data) (float64, error)

CovariancePopulation computes covariance for entire population between two variables

func (Float64Data) CumulativeSum

func (f Float64Data) CumulativeSum() ([]float64, error)

CumulativeSum returns the cumulative sum of the data

func (Float64Data) Entropy added in v0.6.0

func (f Float64Data) Entropy() (float64, error)

Entropy provides calculation of the entropy

func (Float64Data) GeometricMean

func (f Float64Data) GeometricMean() (float64, error)

GeometricMean returns the median of the data

func (Float64Data) Get

func (f Float64Data) Get(i int) float64

Get item in slice

func (Float64Data) HarmonicMean

func (f Float64Data) HarmonicMean() (float64, error)

HarmonicMean returns the mode of the data

func (Float64Data) InterQuartileRange

func (f Float64Data) InterQuartileRange() (float64, error)

InterQuartileRange finds the range between Q1 and Q3

func (Float64Data) Len

func (f Float64Data) Len() int

Len returns length of slice

func (Float64Data) Less

func (f Float64Data) Less(i, j int) bool

Less returns if one number is less than another

func (Float64Data) Max

func (f Float64Data) Max() (float64, error)

Max returns the maximum number in the data

func (Float64Data) Mean

func (f Float64Data) Mean() (float64, error)

Mean returns the mean of the data

func (Float64Data) Median

func (f Float64Data) Median() (float64, error)

Median returns the median of the data

func (Float64Data) MedianAbsoluteDeviation

func (f Float64Data) MedianAbsoluteDeviation() (float64, error)

MedianAbsoluteDeviation the median of the absolute deviations from the dataset median

func (Float64Data) MedianAbsoluteDeviationPopulation

func (f Float64Data) MedianAbsoluteDeviationPopulation() (float64, error)

MedianAbsoluteDeviationPopulation finds the median of the absolute deviations from the population median

func (Float64Data) Midhinge

func (f Float64Data) Midhinge(d Float64Data) (float64, error)

Midhinge finds the average of the first and third quartiles

func (Float64Data) Min

func (f Float64Data) Min() (float64, error)

Min returns the minimum number in the data

func (Float64Data) Mode

func (f Float64Data) Mode() ([]float64, error)

Mode returns the mode of the data

func (Float64Data) Pearson

func (f Float64Data) Pearson(d Float64Data) (float64, error)

Pearson calculates the Pearson product-moment correlation coefficient between two variables.

func (Float64Data) Percentile

func (f Float64Data) Percentile(p float64) (float64, error)

Percentile finds the relative standing in a slice of floats

func (Float64Data) PercentileNearestRank

func (f Float64Data) PercentileNearestRank(p float64) (float64, error)

PercentileNearestRank finds the relative standing using the Nearest Rank method

func (Float64Data) PopulationVariance

func (f Float64Data) PopulationVariance() (float64, error)

PopulationVariance finds the amount of variance within a population

func (Float64Data) Quartile

func (f Float64Data) Quartile(d Float64Data) (Quartiles, error)

Quartile returns the three quartile points from a slice of data

func (Float64Data) QuartileOutliers

func (f Float64Data) QuartileOutliers() (Outliers, error)

QuartileOutliers finds the mild and extreme outliers

func (Float64Data) Quartiles added in v0.6.5

func (f Float64Data) Quartiles() (Quartiles, error)

Quartiles returns the three quartile points from instance of Float64Data

func (Float64Data) Sample

func (f Float64Data) Sample(n int, r bool) ([]float64, error)

Sample returns sample from input with replacement or without

func (Float64Data) SampleVariance

func (f Float64Data) SampleVariance() (float64, error)

SampleVariance finds the amount of variance within a sample

func (Float64Data) Sigmoid added in v0.6.0

func (f Float64Data) Sigmoid() ([]float64, error)

Sigmoid returns the input values along the sigmoid or s-shaped curve

func (Float64Data) SoftMax added in v0.6.0

func (f Float64Data) SoftMax() ([]float64, error)

SoftMax returns the input values in the range of 0 to 1 with sum of all the probabilities being equal to one.

func (Float64Data) StandardDeviation

func (f Float64Data) StandardDeviation() (float64, error)

StandardDeviation the amount of variation in the dataset

func (Float64Data) StandardDeviationPopulation

func (f Float64Data) StandardDeviationPopulation() (float64, error)

StandardDeviationPopulation finds the amount of variation from the population

func (Float64Data) StandardDeviationSample

func (f Float64Data) StandardDeviationSample() (float64, error)

StandardDeviationSample finds the amount of variation from a sample

func (Float64Data) Sum

func (f Float64Data) Sum() (float64, error)

Sum returns the total of all the numbers in the data

func (Float64Data) Swap

func (f Float64Data) Swap(i, j int)

Swap switches out two numbers in slice

func (Float64Data) Trimean

func (f Float64Data) Trimean(d Float64Data) (float64, error)

Trimean finds the average of the median and the midhinge

func (Float64Data) Variance

func (f Float64Data) Variance() (float64, error)

Variance the amount of variation in the dataset

type Outliers

type Outliers struct {
	Mild    Float64Data
	Extreme Float64Data
}

Outliers holds mild and extreme outliers found in data

func QuartileOutliers

func QuartileOutliers(input Float64Data) (Outliers, error)

QuartileOutliers finds the mild and extreme outliers

type Quartiles

type Quartiles struct {
	Q1 float64
	Q2 float64
	Q3 float64
}

Quartiles holds the three quartile points

func Quartile

func Quartile(input Float64Data) (Quartiles, error)

Quartile returns the three quartile points from a slice of data

type Series

type Series []Coordinate

Series is a container for a series of data

func ExponentialRegression

func ExponentialRegression(s Series) (regressions Series, err error)

ExponentialRegression returns an exponential regression on data series

func LinearRegression

func LinearRegression(s Series) (regressions Series, err error)

LinearRegression finds the least squares linear regression on data series

Example
package main

import (
	"fmt"

	"github.com/montanaflynn/stats"
)

func main() {
	data := []stats.Coordinate{
		{1, 2.3},
		{2, 3.3},
		{3, 3.7},
	}

	r, _ := stats.LinearRegression(data)
	fmt.Println(r)
}
Output:

[{1 2.400000000000001} {2 3.1} {3 3.7999999999999994}]

func LogarithmicRegression

func LogarithmicRegression(s Series) (regressions Series, err error)

LogarithmicRegression returns an logarithmic regression on data series

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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