A well tested and comprehensive Golang statistics library package with no dependencies.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Add Entropy documentation, example and benchmarks Jan 28, 2019
.gitignore Re-wrote mode functionality for better performance. Added tests, incl… Oct 21, 2015
.travis.yml Change travis tip alias to master Sep 11, 2018
CHANGELOG.md Release version 0.2.0 Oct 14, 2015
LICENSE Add a standard MIT license file Jul 9, 2015
Makefile Switch default and check targets Oct 15, 2015
README.md Improve documentation section with command comments Jan 28, 2019
correlation.go Fix AutoCorrelation name in comments and remove unneeded Sprintf Jan 28, 2019
correlation_test.go Fix AutoCorrelation name in comments and remove unneeded Sprintf Jan 28, 2019
cumulative_sum.go Add CumulativeSum (#40) Sep 11, 2018
cumulative_sum_test.go Add CumulativeSum (#40) Sep 11, 2018
data.go Fix AutoCorrelation name in comments and remove unneeded Sprintf Jan 28, 2019
data_test.go Add Sigmoid, SoftMax and Entropy methods and tests Jan 28, 2019
deviation.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
deviation_test.go Consolidated all error values. Dec 4, 2015
distances.go Update Distance functions signatures to use Float64Data Jan 28, 2019
distances_test.go Update Distance functions signatures to use Float64Data Jan 28, 2019
doc.go Add doc.go file to show description and usage on godoc.org Jan 28, 2019
entropy.go Add Entropy function (#44) Jan 28, 2019
entropy_test.go Add Entropy documentation, example and benchmarks Jan 28, 2019
errors.go Use a single var block for exported errors Jan 28, 2019
errors_test.go Update error names with backward compatibility Jan 17, 2019
examples_test.go Add Examples to test files Jan 14, 2019
go.mod Add go.mod file for module support Jan 28, 2019
legacy.go Add comments to new error and legacy error variables Jan 28, 2019
legacy_test.go Add Float64Data and Series types Aug 21, 2015
load.go Fix a few lint warnings Sep 11, 2018
load_test.go Add more tests and rename distance files Sep 11, 2018
max.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
max_test.go Add Examples to test files Jan 14, 2019
mean.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
mean_test.go Fix a few lint warnings Sep 11, 2018
median.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
median_test.go Improve error log messaging Nov 18, 2015
min.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
min_test.go Add Examples to test files Jan 14, 2019
mode.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
mode_test.go Mode calculation fix and tests Dec 17, 2015
nist_test.go Remove boolean comparison to get rid of gometalinter warning Jan 28, 2019
outlier.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
outlier_test.go Split up stats.go into separate files Oct 15, 2015
percentile.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
percentile_test.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
quartile.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
quartile_test.go Split up stats.go into separate files Oct 15, 2015
regression.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
regression_test.go Add Y coordinate error for ExponentialRegression Jan 14, 2019
round.go Consolidated all error values. Dec 4, 2015
round_test.go Add ExampleRound function to tests Jan 28, 2019
sample.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
sample_test.go Split up stats.go into separate files Oct 15, 2015
sigmoid.go Add Sigmoid and Softmax functions Jan 16, 2019
sigmoid_test.go Update Sigmoid examples Jan 17, 2019
softmax.go Add Sigmoid and Softmax functions Jan 16, 2019
softmax_test.go Add Sigmoid and Softmax functions Jan 16, 2019
sum.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
sum_test.go Add Examples to test files Jan 14, 2019
util.go Re-wrote mode functionality for better performance. Added tests, incl… Oct 21, 2015
util_test.go Re-wrote mode functionality for better performance. Added tests, incl… Oct 21, 2015
variance.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019
variance_test.go Return EmptyInputErr instead of EmptyInput Jan 14, 2019

README.md

Stats

A well tested and comprehensive Golang statistics library package 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

You can 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 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 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 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) {}

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. Commit changes (git commit -am 'Did something')
  6. Push branch (git push origin some-thing)
  7. 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

MIT License

Copyright (c) 2014-2019 Montana Flynn http://anonfunction.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.