Adding dep files and dependencies.

This commit is contained in:
Renan DelValle 2018-09-30 18:02:42 -07:00
parent 45f9efa578
commit b341c0a0e4
No known key found for this signature in database
GPG key ID: 3895800E03F17676
539 changed files with 313111 additions and 0 deletions

2
vendor/github.com/montanaflynn/stats/.gitignore generated vendored Normal file
View file

@ -0,0 +1,2 @@
coverage.out
.directory

20
vendor/github.com/montanaflynn/stats/.travis.yml generated vendored Normal file
View file

@ -0,0 +1,20 @@
language: go
go:
- 1.1
- 1.2
- 1.3
- 1.4
- 1.5
- tip
before_install:
- sudo pip install codecov
script:
- go test
after_success:
- codecov
notifications:
email:
recipients:
- montana@montanaflynn.me
on_success: change
on_failure: always

64
vendor/github.com/montanaflynn/stats/CHANGELOG.md generated vendored Normal file
View file

@ -0,0 +1,64 @@
# Change Log
## [0.2.0](https://github.com/montanaflynn/stats/tree/0.2.0)
### Merged pull requests:
- Fixed typographical error, changed accomdate to accommodate in README. [\#5](https://github.com/montanaflynn/stats/pull/5) ([saromanov](https://github.com/orthographic-pedant))
### Package changes:
- Add `Correlation` function
- Add `Covariance` function
- Add `StandardDeviation` function to be the same as `StandardDeviationPopulation`
- Change `Variance` function to be the same as `PopulationVariation`
- Add helper methods to `Float64Data`
- Add `Float64Data` type to use instead of `[]float64`
- Add `Series` type which references to `[]Coordinate`
## [0.1.0](https://github.com/montanaflynn/stats/tree/0.1.0)
Several functions were renamed in this release. They will still function but may be deprecated in the future.
### Package changes:
- Rename `VarP` to `PopulationVariance`
- Rename `VarS` to `SampleVariance`
- Rename `LinReg` to `LinearRegression`
- Rename `ExpReg` to `ExponentialRegression`
- Rename `LogReg` to `LogarithmicRegression`
- Rename `StdDevP` to `StandardDeviationPopulation`
- Rename `StdDevS` to `StandardDeviationSample`
## [0.0.9](https://github.com/montanaflynn/stats/tree/0.0.9)
### Closed issues:
- Functions have unexpected side effects [\#3](https://github.com/montanaflynn/stats/issues/3)
- Percentile is not calculated correctly [\#2](https://github.com/montanaflynn/stats/issues/2)
### Merged pull requests:
- Sample [\#4](https://github.com/montanaflynn/stats/pull/4) ([saromanov](https://github.com/saromanov))
### Package changes:
- Add HarmonicMean func
- Add GeometricMean func
- Add Outliers stuct and QuantileOutliers func
- Add Interquartile Range, Midhinge and Trimean examples
- Add Trimean
- Add Midhinge
- Add Inter Quartile Range
- Add Quantiles struct and Quantile func
- Add Nearest Rank method of calculating percentiles
- Add errors for all functions
- Add sample
- Add Linear, Exponential and Logarithmic Regression
- Add sample and population variance and deviation
- Add Percentile and Float64ToInt
- Add Round
- Add Standard deviation
- Add Sum
- Add Min and Ma- x
- Add Mean, Median and Mode

21
vendor/github.com/montanaflynn/stats/LICENSE generated vendored Normal file
View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014-2015 Montana Flynn (https://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
AUTHORS 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.

29
vendor/github.com/montanaflynn/stats/Makefile generated vendored Normal file
View file

@ -0,0 +1,29 @@
.PHONY: all
doc:
godoc `pwd`
webdoc:
godoc -http=:44444
format:
go fmt
test:
go test -race
check: format test
benchmark:
go test -bench=. -benchmem
coverage:
go test -coverprofile=coverage.out
go tool cover -html="coverage.out"
lint: format
go get github.com/alecthomas/gometalinter
gometalinter --install
gometalinter
default: lint test

103
vendor/github.com/montanaflynn/stats/README.md generated vendored Normal file
View file

@ -0,0 +1,103 @@
# Stats [![][travis-svg]][travis-url] [![][coveralls-svg]][coveralls-url] [![][godoc-svg]][godoc-url] [![][license-svg]][license-url]
A statistics package with many functions missing from the Golang standard library. See the [CHANGELOG.md](https://github.com/montanaflynn/stats/blob/master/CHANGELOG.md) for API changes and tagged releases you can vendor into your projects.
> Statistics are used much like a drunk uses a lamppost: for support, not illumination. **- Vin Scully**
## Installation
```
go get github.com/montanaflynn/stats
```
**Protip:** `go get -u github.com/montanaflynn/stats` updates stats to the latest version.
## Usage
The [entire API documentation](http://godoc.org/github.com/montanaflynn/stats) is available on GoDoc.org
You can view docs offline with the following commands:
```
godoc ./
godoc ./ Median
godoc ./ Float64Data
```
**Protip:** Generate HTML docs with `godoc -http=:4444`
## Example
All the functions can be seen in [examples/main.go](https://github.com/montanaflynn/stats/blob/master/examples/main.go) but here's a little taste:
```go
// start with the some source data to use
var data = []float64{1, 2, 3, 4, 4, 5}
median, _ := stats.Median(data)
fmt.Println(median) // 3.5
roundedMedian, _ := stats.Round(median, 0)
fmt.Println(roundedMedian) // 4
```
**Protip:** You can [call methods](https://github.com/montanaflynn/stats/blob/master/examples/methods.go) on the data if using the Float64Data type:
```
var d stats.Float64Data = data
max, _ := d.Max()
fmt.Println(max) // 5
```
## Contributing
If you have any suggestions, criticism or bug reports please [create an issue](https://github.com/montanaflynn/stats/issues) 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!
### Pull Requests
Pull request are always welcome no matter how big or small. Here's an easy way to do it:
1. Fork it 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 `README.md` to include new public types or functions in the documentation section.
- Update `examples/main.go` with a simple example of the new feature.
- Keep 100% code coverage (you can check with `make coverage`).
- Run [`gometalinter`](https://github.com/alecthomas/gometalinter) and make your code pass.
- Squash needless commits into single units of work with `git rebase -i new-feature`.
#### Makefile
I've included a [Makefile](https://github.com/montanaflynn/stats/blob/master/Makefile) that has a lot of helper targets for common actions such as linting, testing, code coverage reporting and more.
**Protip:** `watch -n 1 make check` will continuously format and test your code.
## MIT License
Copyright (c) 2014-2015 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.
[travis-url]: https://travis-ci.org/montanaflynn/stats
[travis-svg]: https://img.shields.io/travis/montanaflynn/stats.svg
[coveralls-url]: https://coveralls.io/r/montanaflynn/stats?branch=master
[coveralls-svg]: https://img.shields.io/coveralls/montanaflynn/stats.svg
[godoc-url]: https://godoc.org/github.com/montanaflynn/stats
[godoc-svg]: https://godoc.org/github.com/montanaflynn/stats?status.svg
[license-url]: https://github.com/montanaflynn/stats/blob/master/LICENSE
[license-svg]: https://img.shields.io/badge/license-MIT-blue.svg

33
vendor/github.com/montanaflynn/stats/correlation.go generated vendored Normal file
View file

@ -0,0 +1,33 @@
package stats
import "math"
// Correlation describes the degree of relationship between two sets of data
func Correlation(data1, data2 Float64Data) (float64, error) {
l1 := data1.Len()
l2 := data2.Len()
if l1 == 0 || l2 == 0 {
return math.NaN(), EmptyInput
}
if l1 != l2 {
return math.NaN(), SizeErr
}
sdev1, _ := StandardDeviationPopulation(data1)
sdev2, _ := StandardDeviationPopulation(data2)
if sdev1 == 0 || sdev2 == 0 {
return 0, nil
}
covp, _ := CovariancePopulation(data1, data2)
return covp / (sdev1 * sdev2), nil
}
// Pearson calculates the Pearson product-moment correlation coefficient between two variables.
func Pearson(data1, data2 Float64Data) (float64, error) {
return Correlation(data1, data2)
}

140
vendor/github.com/montanaflynn/stats/data.go generated vendored Normal file
View file

@ -0,0 +1,140 @@
package stats
// Float64Data is a named type for []float64 with helper methods
type Float64Data []float64
// Get item in slice
func (f Float64Data) Get(i int) float64 { return f[i] }
// Len returns length of slice
func (f Float64Data) Len() int { return len(f) }
// Less returns if one number is less than another
func (f Float64Data) Less(i, j int) bool { return f[i] < f[j] }
// Swap switches out two numbers in slice
func (f Float64Data) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
// Min returns the minimum number in the data
func (f Float64Data) Min() (float64, error) { return Min(f) }
// Max returns the maximum number in the data
func (f Float64Data) Max() (float64, error) { return Max(f) }
// Sum returns the total of all the numbers in the data
func (f Float64Data) Sum() (float64, error) { return Sum(f) }
// Mean returns the mean of the data
func (f Float64Data) Mean() (float64, error) { return Mean(f) }
// Median returns the median of the data
func (f Float64Data) Median() (float64, error) { return Median(f) }
// Mode returns the mode of the data
func (f Float64Data) Mode() ([]float64, error) { return Mode(f) }
// GeometricMean returns the median of the data
func (f Float64Data) GeometricMean() (float64, error) { return GeometricMean(f) }
// HarmonicMean returns the mode of the data
func (f Float64Data) HarmonicMean() (float64, error) { return HarmonicMean(f) }
// MedianAbsoluteDeviation the median of the absolute deviations from the dataset median
func (f Float64Data) MedianAbsoluteDeviation() (float64, error) {
return MedianAbsoluteDeviation(f)
}
// MedianAbsoluteDeviationPopulation finds the median of the absolute deviations from the population median
func (f Float64Data) MedianAbsoluteDeviationPopulation() (float64, error) {
return MedianAbsoluteDeviationPopulation(f)
}
// StandardDeviation the amount of variation in the dataset
func (f Float64Data) StandardDeviation() (float64, error) {
return StandardDeviation(f)
}
// StandardDeviationPopulation finds the amount of variation from the population
func (f Float64Data) StandardDeviationPopulation() (float64, error) {
return StandardDeviationPopulation(f)
}
// StandardDeviationSample finds the amount of variation from a sample
func (f Float64Data) StandardDeviationSample() (float64, error) {
return StandardDeviationSample(f)
}
// QuartileOutliers finds the mild and extreme outliers
func (f Float64Data) QuartileOutliers() (Outliers, error) {
return QuartileOutliers(f)
}
// Percentile finds the relative standing in a slice of floats
func (f Float64Data) Percentile(p float64) (float64, error) {
return Percentile(f, p)
}
// PercentileNearestRank finds the relative standing using the Nearest Rank method
func (f Float64Data) PercentileNearestRank(p float64) (float64, error) {
return PercentileNearestRank(f, p)
}
// Correlation describes the degree of relationship between two sets of data
func (f Float64Data) Correlation(d Float64Data) (float64, error) {
return Correlation(f, d)
}
// Pearson calculates the Pearson product-moment correlation coefficient between two variables.
func (f Float64Data) Pearson(d Float64Data) (float64, error) {
return Pearson(f, d)
}
// Quartile returns the three quartile points from a slice of data
func (f Float64Data) Quartile(d Float64Data) (Quartiles, error) {
return Quartile(d)
}
// InterQuartileRange finds the range between Q1 and Q3
func (f Float64Data) InterQuartileRange() (float64, error) {
return InterQuartileRange(f)
}
// Midhinge finds the average of the first and third quartiles
func (f Float64Data) Midhinge(d Float64Data) (float64, error) {
return Midhinge(d)
}
// Trimean finds the average of the median and the midhinge
func (f Float64Data) Trimean(d Float64Data) (float64, error) {
return Trimean(d)
}
// Sample returns sample from input with replacement or without
func (f Float64Data) Sample(n int, r bool) ([]float64, error) {
return Sample(f, n, r)
}
// Variance the amount of variation in the dataset
func (f Float64Data) Variance() (float64, error) {
return Variance(f)
}
// PopulationVariance finds the amount of variance within a population
func (f Float64Data) PopulationVariance() (float64, error) {
return PopulationVariance(f)
}
// SampleVariance finds the amount of variance within a sample
func (f Float64Data) SampleVariance() (float64, error) {
return SampleVariance(f)
}
// Covariance is a measure of how much two sets of data change
func (f Float64Data) Covariance(d Float64Data) (float64, error) {
return Covariance(f, d)
}
// CovariancePopulation computes covariance for entire population between two variables.
func (f Float64Data) CovariancePopulation(d Float64Data) (float64, error) {
return CovariancePopulation(f, d)
}

View file

@ -0,0 +1,94 @@
package stats
import (
"math"
)
// Validate data for distance calculation
func validateData(dataPointX, dataPointY []float64) error {
if len(dataPointX) == 0 || len(dataPointY) == 0 {
return EmptyInput
}
if len(dataPointX) != len(dataPointY) {
return SizeErr
}
return nil
}
// Computes Chebyshev distance between two data sets
func ChebyshevDistance(dataPointX, dataPointY []float64) (distance float64, err error) {
err = validateData(dataPointX, dataPointY)
if err != nil {
return math.NaN(), err
}
var tempDistance float64
for i := 0; i < len(dataPointY); i++ {
tempDistance = math.Abs(dataPointX[i] - dataPointY[i])
if distance < tempDistance {
distance = tempDistance
}
}
return distance, nil
}
//
// Computes Euclidean distance between two data sets
//
func EuclideanDistance(dataPointX, dataPointY []float64) (distance float64, err error) {
err = validateData(dataPointX, dataPointY)
if err != nil {
return math.NaN(), err
}
distance = 0
for i := 0; i < len(dataPointX); i++ {
distance = distance + ((dataPointX[i] - dataPointY[i]) * (dataPointX[i] - dataPointY[i]))
}
return math.Sqrt(distance), nil
}
//
// Computes Manhattan distance between two data sets
//
func ManhattanDistance(dataPointX, dataPointY []float64) (distance float64, err error) {
err = validateData(dataPointX, dataPointY)
if err != nil {
return math.NaN(), err
}
distance = 0
for i := 0; i < len(dataPointX); i++ {
distance = distance + math.Abs(dataPointX[i]-dataPointY[i])
}
return distance, nil
}
//
// Computes minkowski distance between two data sets.
//
// Input:
// 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.
// Output:
// Distance or error
//
func MinkowskiDistance(dataPointX, dataPointY []float64, lambda float64) (distance float64, err error) {
err = validateData(dataPointX, dataPointY)
if err != nil {
return math.NaN(), err
}
for i := 0; i < len(dataPointY); i++ {
distance = distance + math.Pow(math.Abs(dataPointX[i]-dataPointY[i]), lambda)
}
distance = math.Pow(distance, float64(1/lambda))
if math.IsInf(distance, 1) == true {
return math.NaN(), InfValue
}
return distance, nil
}

57
vendor/github.com/montanaflynn/stats/deviation.go generated vendored Normal file
View file

@ -0,0 +1,57 @@
package stats
import "math"
// MedianAbsoluteDeviation finds the median of the absolute deviations from the dataset median
func MedianAbsoluteDeviation(input Float64Data) (mad float64, err error) {
return MedianAbsoluteDeviationPopulation(input)
}
// MedianAbsoluteDeviationPopulation finds the median of the absolute deviations from the population median
func MedianAbsoluteDeviationPopulation(input Float64Data) (mad float64, err error) {
if input.Len() == 0 {
return math.NaN(), EmptyInput
}
i := copyslice(input)
m, _ := Median(i)
for key, value := range i {
i[key] = math.Abs(value - m)
}
return Median(i)
}
// StandardDeviation the amount of variation in the dataset
func StandardDeviation(input Float64Data) (sdev float64, err error) {
return StandardDeviationPopulation(input)
}
// StandardDeviationPopulation finds the amount of variation from the population
func StandardDeviationPopulation(input Float64Data) (sdev float64, err error) {
if input.Len() == 0 {
return math.NaN(), EmptyInput
}
// Get the population variance
vp, _ := PopulationVariance(input)
// Return the population standard deviation
return math.Pow(vp, 0.5), nil
}
// StandardDeviationSample finds the amount of variation from a sample
func StandardDeviationSample(input Float64Data) (sdev float64, err error) {
if input.Len() == 0 {
return math.NaN(), EmptyInput
}
// Get the sample variance
vs, _ := SampleVariance(input)
// Return the sample standard deviation
return math.Pow(vs, 0.5), nil
}

22
vendor/github.com/montanaflynn/stats/errors.go generated vendored Normal file
View file

@ -0,0 +1,22 @@
package stats
type statsErr struct {
err string
}
func (s statsErr) Error() string {
return s.err
}
// These are the package-wide error values.
// All error identification should use these values.
var (
EmptyInput = statsErr{"Input must not be empty."}
SampleSize = statsErr{"Samples number must be less than input length."}
NaNErr = statsErr{"Not a number"}
NegativeErr = statsErr{"Slice must not contain negative values."}
ZeroErr = statsErr{"Slice must not contain zero values."}
BoundsErr = statsErr{"Input is outside of range."}
SizeErr = statsErr{"Slices must be the same length."}
InfValue = statsErr{"Value is infinite."}
)

36
vendor/github.com/montanaflynn/stats/legacy.go generated vendored Normal file
View file

@ -0,0 +1,36 @@
package stats
// VarP is a shortcut to PopulationVariance
func VarP(input Float64Data) (sdev float64, err error) {
return PopulationVariance(input)
}
// VarS is a shortcut to SampleVariance
func VarS(input Float64Data) (sdev float64, err error) {
return SampleVariance(input)
}
// StdDevP is a shortcut to StandardDeviationPopulation
func StdDevP(input Float64Data) (sdev float64, err error) {
return StandardDeviationPopulation(input)
}
// StdDevS is a shortcut to StandardDeviationSample
func StdDevS(input Float64Data) (sdev float64, err error) {
return StandardDeviationSample(input)
}
// LinReg is a shortcut to LinearRegression
func LinReg(s []Coordinate) (regressions []Coordinate, err error) {
return LinearRegression(s)
}
// ExpReg is a shortcut to ExponentialRegression
func ExpReg(s []Coordinate) (regressions []Coordinate, err error) {
return ExponentialRegression(s)
}
// LogReg is a shortcut to LogarithmicRegression
func LogReg(s []Coordinate) (regressions []Coordinate, err error) {
return LogarithmicRegression(s)
}

184
vendor/github.com/montanaflynn/stats/load.go generated vendored Normal file
View file

@ -0,0 +1,184 @@
package stats
import (
"strconv"
"time"
)
// LoadRawData parses and converts a slice of mixed data types to floats
func LoadRawData(raw interface{}) (f Float64Data) {
var r []interface{}
var s Float64Data
switch t := raw.(type) {
case []interface{}:
r = t
case []uint:
for _, v := range t {
s = append(s, float64(v))
}
return s
case []uint8:
for _, v := range t {
s = append(s, float64(v))
}
return s
case []uint16:
for _, v := range t {
s = append(s, float64(v))
}
return s
case []uint32:
for _, v := range t {
s = append(s, float64(v))
}
return s
case []uint64:
for _, v := range t {
s = append(s, float64(v))
}
return s
case []bool:
for _, v := range t {
if v == true {
s = append(s, 1.0)
} else {
s = append(s, 0.0)
}
}
return s
case []float64:
return Float64Data(t)
case []int:
for _, v := range t {
s = append(s, float64(v))
}
return s
case []int8:
for _, v := range t {
s = append(s, float64(v))
}
return s
case []int16:
for _, v := range t {
s = append(s, float64(v))
}
return s
case []int32:
for _, v := range t {
s = append(s, float64(v))
}
return s
case []int64:
for _, v := range t {
s = append(s, float64(v))
}
return s
case []string:
for _, v := range t {
r = append(r, v)
}
case []time.Duration:
for _, v := range t {
r = append(r, v)
}
case map[int]int:
for i := 0; i < len(t); i++ {
s = append(s, float64(t[i]))
}
return s
case map[int]int8:
for i := 0; i < len(t); i++ {
s = append(s, float64(t[i]))
}
return s
case map[int]int16:
for i := 0; i < len(t); i++ {
s = append(s, float64(t[i]))
}
return s
case map[int]int32:
for i := 0; i < len(t); i++ {
s = append(s, float64(t[i]))
}
return s
case map[int]int64:
for i := 0; i < len(t); i++ {
s = append(s, float64(t[i]))
}
return s
case map[int]string:
for i := 0; i < len(t); i++ {
r = append(r, t[i])
}
case map[int]uint:
for i := 0; i < len(t); i++ {
s = append(s, float64(t[i]))
}
return s
case map[int]uint8:
for i := 0; i < len(t); i++ {
s = append(s, float64(t[i]))
}
return s
case map[int]uint16:
for i := 0; i < len(t); i++ {
s = append(s, float64(t[i]))
}
return s
case map[int]uint32:
for i := 0; i < len(t); i++ {
s = append(s, float64(t[i]))
}
return s
case map[int]uint64:
for i := 0; i < len(t); i++ {
s = append(s, float64(t[i]))
}
return s
case map[int]bool:
for i := 0; i < len(t); i++ {
if t[i] == true {
s = append(s, 1.0)
} else {
s = append(s, 0.0)
}
}
return s
case map[int]float64:
for i := 0; i < len(t); i++ {
s = append(s, t[i])
}
return s
case map[int]time.Duration:
for i := 0; i < len(t); i++ {
r = append(r, t[i])
}
}
for _, v := range r {
switch t := v.(type) {
case int:
a := float64(t)
f = append(f, a)
case uint:
f = append(f, float64(t))
case float64:
f = append(f, t)
case string:
fl, err := strconv.ParseFloat(t, 64)
if err == nil {
f = append(f, fl)
}
case bool:
if t == true {
f = append(f, 1.0)
} else {
f = append(f, 0.0)
}
case time.Duration:
f = append(f, float64(t))
}
}
return f
}

24
vendor/github.com/montanaflynn/stats/max.go generated vendored Normal file
View file

@ -0,0 +1,24 @@
package stats
import "math"
// Max finds the highest number in a slice
func Max(input Float64Data) (max float64, err error) {
// Return an error if there are no numbers
if input.Len() == 0 {
return math.NaN(), EmptyInput
}
// Get the first value as the starting point
max = input.Get(0)
// Loop and replace higher values
for i := 1; i < input.Len(); i++ {
if input.Get(i) > max {
max = input.Get(i)
}
}
return max, nil
}

60
vendor/github.com/montanaflynn/stats/mean.go generated vendored Normal file
View file

@ -0,0 +1,60 @@
package stats
import "math"
// Mean gets the average of a slice of numbers
func Mean(input Float64Data) (float64, error) {
if input.Len() == 0 {
return math.NaN(), EmptyInput
}
sum, _ := input.Sum()
return sum / float64(input.Len()), nil
}
// GeometricMean gets the geometric mean for a slice of numbers
func GeometricMean(input Float64Data) (float64, error) {
l := input.Len()
if l == 0 {
return math.NaN(), EmptyInput
}
// Get the product of all the numbers
var p float64
for _, n := range input {
if p == 0 {
p = n
} else {
p *= n
}
}
// Calculate the geometric mean
return math.Pow(p, 1/float64(l)), nil
}
// HarmonicMean gets the harmonic mean for a slice of numbers
func HarmonicMean(input Float64Data) (float64, error) {
l := input.Len()
if l == 0 {
return math.NaN(), EmptyInput
}
// Get the sum of all the numbers reciprocals and return an
// error for values that cannot be included in harmonic mean
var p float64
for _, n := range input {
if n < 0 {
return math.NaN(), NegativeErr
} else if n == 0 {
return math.NaN(), ZeroErr
}
p += (1 / n)
}
return float64(l) / p, nil
}

25
vendor/github.com/montanaflynn/stats/median.go generated vendored Normal file
View file

@ -0,0 +1,25 @@
package stats
import "math"
// Median gets the median number in a slice of numbers
func Median(input Float64Data) (median float64, err error) {
// Start by sorting a copy of the slice
c := sortedCopy(input)
// No math is needed if there are no numbers
// For even numbers we add the two middle numbers
// and divide by two using the mean function above
// For odd numbers we just use the middle number
l := len(c)
if l == 0 {
return math.NaN(), EmptyInput
} else if l%2 == 0 {
median, _ = Mean(c[l/2-1 : l/2+1])
} else {
median = float64(c[l/2])
}
return median, nil
}

26
vendor/github.com/montanaflynn/stats/min.go generated vendored Normal file
View file

@ -0,0 +1,26 @@
package stats
import "math"
// Min finds the lowest number in a set of data
func Min(input Float64Data) (min float64, err error) {
// Get the count of numbers in the slice
l := input.Len()
// Return an error if there are no numbers
if l == 0 {
return math.NaN(), EmptyInput
}
// Get the first value as the starting point
min = input.Get(0)
// Iterate until done checking for a lower value
for i := 1; i < l; i++ {
if input.Get(i) < min {
min = input.Get(i)
}
}
return min, nil
}

47
vendor/github.com/montanaflynn/stats/mode.go generated vendored Normal file
View file

@ -0,0 +1,47 @@
package stats
// Mode gets the mode [most frequent value(s)] of a slice of float64s
func Mode(input Float64Data) (mode []float64, err error) {
// Return the input if there's only one number
l := input.Len()
if l == 1 {
return input, nil
} else if l == 0 {
return nil, EmptyInput
}
c := sortedCopyDif(input)
// Traverse sorted array,
// tracking the longest repeating sequence
mode = make([]float64, 5)
cnt, maxCnt := 1, 1
for i := 1; i < l; i++ {
switch {
case c[i] == c[i-1]:
cnt++
case cnt == maxCnt && maxCnt != 1:
mode = append(mode, c[i-1])
cnt = 1
case cnt > maxCnt:
mode = append(mode[:0], c[i-1])
maxCnt, cnt = cnt, 1
default:
cnt = 1
}
}
switch {
case cnt == maxCnt:
mode = append(mode, c[l-1])
case cnt > maxCnt:
mode = append(mode[:0], c[l-1])
maxCnt = cnt
}
// Since length must be greater than 1,
// check for slices of distinct values
if maxCnt == 1 {
return Float64Data{}, nil
}
return mode, nil
}

44
vendor/github.com/montanaflynn/stats/outlier.go generated vendored Normal file
View file

@ -0,0 +1,44 @@
package stats
// Outliers holds mild and extreme outliers found in data
type Outliers struct {
Mild Float64Data
Extreme Float64Data
}
// QuartileOutliers finds the mild and extreme outliers
func QuartileOutliers(input Float64Data) (Outliers, error) {
if input.Len() == 0 {
return Outliers{}, EmptyInput
}
// Start by sorting a copy of the slice
copy := sortedCopy(input)
// Calculate the quartiles and interquartile range
qs, _ := Quartile(copy)
iqr, _ := InterQuartileRange(copy)
// Calculate the lower and upper inner and outer fences
lif := qs.Q1 - (1.5 * iqr)
uif := qs.Q3 + (1.5 * iqr)
lof := qs.Q1 - (3 * iqr)
uof := qs.Q3 + (3 * iqr)
// Find the data points that are outside of the
// inner and upper fences and add them to mild
// and extreme outlier slices
var mild Float64Data
var extreme Float64Data
for _, v := range copy {
if v < lof || v > uof {
extreme = append(extreme, v)
} else if v < lif || v > uif {
mild = append(mild, v)
}
}
// Wrap them into our struct
return Outliers{mild, extreme}, nil
}

80
vendor/github.com/montanaflynn/stats/percentile.go generated vendored Normal file
View file

@ -0,0 +1,80 @@
package stats
import "math"
// Percentile finds the relative standing in a slice of floats
func Percentile(input Float64Data, percent float64) (percentile float64, err error) {
if input.Len() == 0 {
return math.NaN(), EmptyInput
}
if percent <= 0 || percent > 100 {
return math.NaN(), BoundsErr
}
// Start by sorting a copy of the slice
c := sortedCopy(input)
// Multiply percent by length of input
index := (percent / 100) * float64(len(c))
// Check if the index is a whole number
if index == float64(int64(index)) {
// Convert float to int
i := int(index)
// Find the value at the index
percentile = c[i-1]
} else if index > 1 {
// Convert float to int via truncation
i := int(index)
// Find the average of the index and following values
percentile, _ = Mean(Float64Data{c[i-1], c[i]})
} else {
return math.NaN(), BoundsErr
}
return percentile, nil
}
// PercentileNearestRank finds the relative standing in a slice of floats using the Nearest Rank method
func PercentileNearestRank(input Float64Data, percent float64) (percentile float64, err error) {
// Find the length of items in the slice
il := input.Len()
// Return an error for empty slices
if il == 0 {
return math.NaN(), EmptyInput
}
// Return error for less than 0 or greater than 100 percentages
if percent < 0 || percent > 100 {
return math.NaN(), BoundsErr
}
// Start by sorting a copy of the slice
c := sortedCopy(input)
// Return the last item
if percent == 100.0 {
return c[il-1], nil
}
// Find ordinal ranking
or := int(math.Ceil(float64(il) * percent / 100))
// Return the item that is in the place of the ordinal rank
if or == 0 {
return c[0], nil
}
return c[or-1], nil
}

74
vendor/github.com/montanaflynn/stats/quartile.go generated vendored Normal file
View file

@ -0,0 +1,74 @@
package stats
import "math"
// Quartiles holds the three quartile points
type Quartiles struct {
Q1 float64
Q2 float64
Q3 float64
}
// Quartile returns the three quartile points from a slice of data
func Quartile(input Float64Data) (Quartiles, error) {
il := input.Len()
if il == 0 {
return Quartiles{}, EmptyInput
}
// Start by sorting a copy of the slice
copy := sortedCopy(input)
// Find the cutoff places depeding on if
// the input slice length is even or odd
var c1 int
var c2 int
if il%2 == 0 {
c1 = il / 2
c2 = il / 2
} else {
c1 = (il - 1) / 2
c2 = c1 + 1
}
// Find the Medians with the cutoff points
Q1, _ := Median(copy[:c1])
Q2, _ := Median(copy)
Q3, _ := Median(copy[c2:])
return Quartiles{Q1, Q2, Q3}, nil
}
// InterQuartileRange finds the range between Q1 and Q3
func InterQuartileRange(input Float64Data) (float64, error) {
if input.Len() == 0 {
return math.NaN(), EmptyInput
}
qs, _ := Quartile(input)
iqr := qs.Q3 - qs.Q1
return iqr, nil
}
// Midhinge finds the average of the first and third quartiles
func Midhinge(input Float64Data) (float64, error) {
if input.Len() == 0 {
return math.NaN(), EmptyInput
}
qs, _ := Quartile(input)
mh := (qs.Q1 + qs.Q3) / 2
return mh, nil
}
// Trimean finds the average of the median and the midhinge
func Trimean(input Float64Data) (float64, error) {
if input.Len() == 0 {
return math.NaN(), EmptyInput
}
c := sortedCopy(input)
q, _ := Quartile(c)
return (q.Q1 + (q.Q2 * 2) + q.Q3) / 4, nil
}

113
vendor/github.com/montanaflynn/stats/regression.go generated vendored Normal file
View file

@ -0,0 +1,113 @@
package stats
import "math"
// Series is a container for a series of data
type Series []Coordinate
// Coordinate holds the data in a series
type Coordinate struct {
X, Y float64
}
// LinearRegression finds the least squares linear regression on data series
func LinearRegression(s Series) (regressions Series, err error) {
if len(s) == 0 {
return nil, EmptyInput
}
// Placeholder for the math to be done
var sum [5]float64
// Loop over data keeping index in place
i := 0
for ; i < len(s); i++ {
sum[0] += s[i].X
sum[1] += s[i].Y
sum[2] += s[i].X * s[i].X
sum[3] += s[i].X * s[i].Y
sum[4] += s[i].Y * s[i].Y
}
// Find gradient and intercept
f := float64(i)
gradient := (f*sum[3] - sum[0]*sum[1]) / (f*sum[2] - sum[0]*sum[0])
intercept := (sum[1] / f) - (gradient * sum[0] / f)
// Create the new regression series
for j := 0; j < len(s); j++ {
regressions = append(regressions, Coordinate{
X: s[j].X,
Y: s[j].X*gradient + intercept,
})
}
return regressions, nil
}
// ExponentialRegression returns an exponential regression on data series
func ExponentialRegression(s Series) (regressions Series, err error) {
if len(s) == 0 {
return nil, EmptyInput
}
var sum [6]float64
for i := 0; i < len(s); i++ {
sum[0] += s[i].X
sum[1] += s[i].Y
sum[2] += s[i].X * s[i].X * s[i].Y
sum[3] += s[i].Y * math.Log(s[i].Y)
sum[4] += s[i].X * s[i].Y * math.Log(s[i].Y)
sum[5] += s[i].X * s[i].Y
}
denominator := (sum[1]*sum[2] - sum[5]*sum[5])
a := math.Pow(math.E, (sum[2]*sum[3]-sum[5]*sum[4])/denominator)
b := (sum[1]*sum[4] - sum[5]*sum[3]) / denominator
for j := 0; j < len(s); j++ {
regressions = append(regressions, Coordinate{
X: s[j].X,
Y: a * math.Exp(b*s[j].X),
})
}
return regressions, nil
}
// LogarithmicRegression returns an logarithmic regression on data series
func LogarithmicRegression(s Series) (regressions Series, err error) {
if len(s) == 0 {
return nil, EmptyInput
}
var sum [4]float64
i := 0
for ; i < len(s); i++ {
sum[0] += math.Log(s[i].X)
sum[1] += s[i].Y * math.Log(s[i].X)
sum[2] += s[i].Y
sum[3] += math.Pow(math.Log(s[i].X), 2)
}
f := float64(i)
a := (f*sum[1] - sum[2]*sum[0]) / (f*sum[3] - sum[0]*sum[0])
b := (sum[2] - a*sum[0]) / f
for j := 0; j < len(s); j++ {
regressions = append(regressions, Coordinate{
X: s[j].X,
Y: b + a*math.Log(s[j].X),
})
}
return regressions, nil
}

38
vendor/github.com/montanaflynn/stats/round.go generated vendored Normal file
View file

@ -0,0 +1,38 @@
package stats
import "math"
// Round a float to a specific decimal place or precision
func Round(input float64, places int) (rounded float64, err error) {
// If the float is not a number
if math.IsNaN(input) {
return math.NaN(), NaNErr
}
// Find out the actual sign and correct the input for later
sign := 1.0
if input < 0 {
sign = -1
input *= -1
}
// Use the places arg to get the amount of precision wanted
precision := math.Pow(10, float64(places))
// Find the decimal place we are looking to round
digit := input * precision
// Get the actual decimal number as a fraction to be compared
_, decimal := math.Modf(digit)
// If the decimal is less than .5 we round down otherwise up
if decimal >= 0.5 {
rounded = math.Ceil(digit)
} else {
rounded = math.Floor(digit)
}
// Finally we do the math to actually create a rounded number
return rounded / precision * sign, nil
}

44
vendor/github.com/montanaflynn/stats/sample.go generated vendored Normal file
View file

@ -0,0 +1,44 @@
package stats
import "math/rand"
// Sample returns sample from input with replacement or without
func Sample(input Float64Data, takenum int, replacement bool) ([]float64, error) {
if input.Len() == 0 {
return nil, EmptyInput
}
length := input.Len()
if replacement {
result := Float64Data{}
rand.Seed(unixnano())
// In every step, randomly take the num for
for i := 0; i < takenum; i++ {
idx := rand.Intn(length)
result = append(result, input[idx])
}
return result, nil
} else if !replacement && takenum <= length {
rand.Seed(unixnano())
// Get permutation of number of indexies
perm := rand.Perm(length)
result := Float64Data{}
// Get element of input by permutated index
for _, idx := range perm[0:takenum] {
result = append(result, input[idx])
}
return result, nil
}
return nil, BoundsErr
}

18
vendor/github.com/montanaflynn/stats/sum.go generated vendored Normal file
View file

@ -0,0 +1,18 @@
package stats
import "math"
// Sum adds all the numbers of a slice together
func Sum(input Float64Data) (sum float64, err error) {
if input.Len() == 0 {
return math.NaN(), EmptyInput
}
// Add em up
for _, n := range input {
sum += n
}
return sum, nil
}

43
vendor/github.com/montanaflynn/stats/util.go generated vendored Normal file
View file

@ -0,0 +1,43 @@
package stats
import (
"sort"
"time"
)
// float64ToInt rounds a float64 to an int
func float64ToInt(input float64) (output int) {
r, _ := Round(input, 0)
return int(r)
}
// unixnano returns nanoseconds from UTC epoch
func unixnano() int64 {
return time.Now().UTC().UnixNano()
}
// copyslice copies a slice of float64s
func copyslice(input Float64Data) Float64Data {
s := make(Float64Data, input.Len())
copy(s, input)
return s
}
// sortedCopy returns a sorted copy of float64s
func sortedCopy(input Float64Data) (copy Float64Data) {
copy = copyslice(input)
sort.Float64s(copy)
return
}
// sortedCopyDif returns a sorted copy of float64s
// only if the original data isn't sorted.
// Only use this if returned slice won't be manipulated!
func sortedCopyDif(input Float64Data) (copy Float64Data) {
if sort.Float64sAreSorted(input) {
return input
}
copy = copyslice(input)
sort.Float64s(copy)
return
}

105
vendor/github.com/montanaflynn/stats/variance.go generated vendored Normal file
View file

@ -0,0 +1,105 @@
package stats
import "math"
// _variance finds the variance for both population and sample data
func _variance(input Float64Data, sample int) (variance float64, err error) {
if input.Len() == 0 {
return math.NaN(), EmptyInput
}
// Sum the square of the mean subtracted from each number
m, _ := Mean(input)
for _, n := range input {
variance += (float64(n) - m) * (float64(n) - m)
}
// When getting the mean of the squared differences
// "sample" will allow us to know if it's a sample
// or population and wether to subtract by one or not
return variance / float64((input.Len() - (1 * sample))), nil
}
// Variance the amount of variation in the dataset
func Variance(input Float64Data) (sdev float64, err error) {
return PopulationVariance(input)
}
// PopulationVariance finds the amount of variance within a population
func PopulationVariance(input Float64Data) (pvar float64, err error) {
v, err := _variance(input, 0)
if err != nil {
return math.NaN(), err
}
return v, nil
}
// SampleVariance finds the amount of variance within a sample
func SampleVariance(input Float64Data) (svar float64, err error) {
v, err := _variance(input, 1)
if err != nil {
return math.NaN(), err
}
return v, nil
}
// Covariance is a measure of how much two sets of data change
func Covariance(data1, data2 Float64Data) (float64, error) {
l1 := data1.Len()
l2 := data2.Len()
if l1 == 0 || l2 == 0 {
return math.NaN(), EmptyInput
}
if l1 != l2 {
return math.NaN(), SizeErr
}
m1, _ := Mean(data1)
m2, _ := Mean(data2)
// Calculate sum of squares
var ss float64
for i := 0; i < l1; i++ {
delta1 := (data1.Get(i) - m1)
delta2 := (data2.Get(i) - m2)
ss += (delta1*delta2 - ss) / float64(i+1)
}
return ss * float64(l1) / float64(l1-1), nil
}
// CovariancePopulation computes covariance for entire population between two variables.
func CovariancePopulation(data1, data2 Float64Data) (float64, error) {
l1 := data1.Len()
l2 := data2.Len()
if l1 == 0 || l2 == 0 {
return math.NaN(), EmptyInput
}
if l1 != l2 {
return math.NaN(), SizeErr
}
m1, _ := Mean(data1)
m2, _ := Mean(data2)
var s float64
for i := 0; i < l1; i++ {
delta1 := (data1.Get(i) - m1)
delta2 := (data2.Get(i) - m2)
s += delta1 * delta2
}
return s / float64(l1), nil
}