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-2026 Montana Flynn (https://montanaflynn.com)
Index ¶
- Variables
- func ArgMax(input Float64Data) (int, error)
- func ArgMin(input Float64Data) (int, error)
- func AutoCorrelation(data Float64Data, lags int) (float64, error)
- func ChebyshevDistance(dataPointX, dataPointY Float64Data) (distance float64, err error)
- func Clip(input Float64Data, min, max float64) ([]float64, error)
- func CoefficientOfVariation(input Float64Data) (float64, error)
- func Correlation(data1, data2 Float64Data) (float64, error)
- func Covariance(data1, data2 Float64Data) (float64, error)
- func CovariancePopulation(data1, data2 Float64Data) (float64, error)
- func CumulativeMax(input Float64Data) ([]float64, error)
- func CumulativeMin(input Float64Data) ([]float64, error)
- func CumulativeProduct(input Float64Data) ([]float64, error)
- func CumulativeSum(input Float64Data) ([]float64, error)
- func Diff(input Float64Data) ([]float64, error)
- func EWMA(input Float64Data, alpha float64) ([]float64, error)
- func Entropy(input Float64Data) (float64, error)
- func EuclideanDistance(dataPointX, dataPointY Float64Data) (distance float64, err error)
- func ExpGeom(p float64) (exp float64, err error)
- func GeometricMean(input Float64Data) (float64, error)
- func HarmonicMean(input Float64Data) (float64, error)
- func Histogram(input Float64Data, bins int) ([]int, []float64, error)
- func InterQuartileRange(input Float64Data) (float64, error)
- func Interp(x, xp, fp Float64Data) ([]float64, error)
- func KendallTau(data1, data2 Float64Data) (float64, error)
- func Kurtosis(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 MovingAverage(input Float64Data, window int) ([]float64, error)
- func MovingMax(input Float64Data, window int) ([]float64, error)
- func MovingMedian(input Float64Data, window int) ([]float64, error)
- func MovingMin(input Float64Data, window int) ([]float64, error)
- func MovingStdDev(input Float64Data, window int) ([]float64, error)
- func MovingSum(input Float64Data, window int) ([]float64, error)
- func Ncr(n, r int) int
- 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 NormSample(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 PercentChange(input Float64Data) ([]float64, error)
- func Percentile(input Float64Data, percent float64) (percentile float64, err error)
- func PercentileNearestRank(input Float64Data, percent float64) (percentile float64, err error)
- func PercentileOfScore(input Float64Data, score float64) (float64, error)
- func PercentileWeighted(data, weights Float64Data, percent float64) (percentile float64, err error)
- func PopulationKurtosis(input Float64Data) (float64, error)
- func PopulationSkewness(input Float64Data) (float64, error)
- func PopulationVariance(input Float64Data) (pvar float64, err error)
- func ProbGeom(a int, b int, p float64) (prob float64, err error)
- func Product(input Float64Data) (float64, error)
- func RMS(input Float64Data) (float64, error)
- func Range(input Float64Data) (float64, error)
- func Rank(input Float64Data) ([]float64, error)
- func Rescale(input Float64Data) ([]float64, error)
- func Round(input float64, places int) (rounded float64, err error)
- func SEM(input Float64Data) (float64, error)
- func Sample(input Float64Data, takenum int, replacement bool) ([]float64, error)
- func SampleKurtosis(input Float64Data) (float64, error)
- func SampleSkewness(input Float64Data) (float64, error)
- func SampleVariance(input Float64Data) (svar float64, err error)
- func Sigmoid(input Float64Data) ([]float64, error)
- func Skewness(input Float64Data) (float64, error)
- func SoftMax(input Float64Data) ([]float64, error)
- func Spearman(data1, data2 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 TTest(data1, data2 Float64Data, populationMean float64) (t float64, pvalue float64, err error)
- func Trimean(input Float64Data) (float64, error)
- func TrimmedMean(input Float64Data, percent float64) (float64, error)
- func VarGeom(p float64) (exp float64, err 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 WeightedMean(data, weights Float64Data) (float64, error)
- func Winsorize(input Float64Data, percent float64) ([]float64, error)
- func ZScore(input Float64Data) ([]float64, error)
- func ZTest(data1, data2 Float64Data, populationMean, populationStdDev float64) (z float64, pvalue float64, err error)
- type Coordinate
- type Description
- type Float64Data
- func (f Float64Data) ArgMax() (int, error)
- func (f Float64Data) ArgMin() (int, error)
- func (f Float64Data) AutoCorrelation(lags int) (float64, error)
- func (f Float64Data) Clip(min, max float64) ([]float64, error)
- func (f Float64Data) CoefficientOfVariation() (float64, error)
- func (f Float64Data) Correlation(d Float64Data) (float64, error)
- func (f Float64Data) Covariance(d Float64Data) (float64, error)
- func (f Float64Data) CovariancePopulation(d Float64Data) (float64, error)
- func (f Float64Data) CumulativeMax() ([]float64, error)
- func (f Float64Data) CumulativeMin() ([]float64, error)
- func (f Float64Data) CumulativeProduct() ([]float64, error)
- func (f Float64Data) CumulativeSum() ([]float64, error)
- func (f Float64Data) Diff() ([]float64, error)
- func (f Float64Data) EWMA(alpha float64) ([]float64, error)
- func (f Float64Data) Entropy() (float64, error)
- func (f Float64Data) GeometricMean() (float64, error)
- func (f Float64Data) Get(i int) float64
- func (f Float64Data) HarmonicMean() (float64, error)
- func (f Float64Data) Histogram(bins int) ([]int, []float64, error)
- func (f Float64Data) InterQuartileRange() (float64, error)
- func (f Float64Data) KendallTau(d Float64Data) (float64, error)
- func (f Float64Data) Kurtosis() (float64, error)
- func (f Float64Data) Len() int
- func (f Float64Data) Less(i, j int) bool
- func (f Float64Data) Max() (float64, error)
- func (f Float64Data) Mean() (float64, error)
- func (f Float64Data) Median() (float64, error)
- func (f Float64Data) MedianAbsoluteDeviation() (float64, error)
- func (f Float64Data) MedianAbsoluteDeviationPopulation() (float64, error)
- func (f Float64Data) Midhinge(d Float64Data) (float64, error)
- func (f Float64Data) Min() (float64, error)
- func (f Float64Data) Mode() ([]float64, error)
- func (f Float64Data) MovingAverage(window int) ([]float64, error)
- func (f Float64Data) MovingMax(window int) ([]float64, error)
- func (f Float64Data) MovingMedian(window int) ([]float64, error)
- func (f Float64Data) MovingMin(window int) ([]float64, error)
- func (f Float64Data) MovingStdDev(window int) ([]float64, error)
- func (f Float64Data) MovingSum(window int) ([]float64, error)
- func (f Float64Data) Pearson(d Float64Data) (float64, error)
- func (f Float64Data) PercentChange() ([]float64, error)
- func (f Float64Data) Percentile(p float64) (float64, error)
- func (f Float64Data) PercentileNearestRank(p float64) (float64, error)
- func (f Float64Data) PercentileOfScore(score float64) (float64, error)
- func (f Float64Data) PopulationKurtosis() (float64, error)
- func (f Float64Data) PopulationVariance() (float64, error)
- func (f Float64Data) Product() (float64, error)
- func (f Float64Data) Quartile(d Float64Data) (Quartiles, error)
- func (f Float64Data) QuartileOutliers() (Outliers, error)
- func (f Float64Data) Quartiles() (Quartiles, error)
- func (f Float64Data) RMS() (float64, error)
- func (f Float64Data) Range() (float64, error)
- func (f Float64Data) Rank() ([]float64, error)
- func (f Float64Data) Rescale() ([]float64, error)
- func (f Float64Data) SEM() (float64, error)
- func (f Float64Data) Sample(n int, r bool) ([]float64, error)
- func (f Float64Data) SampleKurtosis() (float64, error)
- func (f Float64Data) SampleVariance() (float64, error)
- func (f Float64Data) Sigmoid() ([]float64, error)
- func (f Float64Data) SoftMax() ([]float64, error)
- func (f Float64Data) Spearman(d Float64Data) (float64, error)
- func (f Float64Data) StandardDeviation() (float64, error)
- func (f Float64Data) StandardDeviationPopulation() (float64, error)
- func (f Float64Data) StandardDeviationSample() (float64, error)
- func (f Float64Data) Sum() (float64, error)
- func (f Float64Data) Swap(i, j int)
- func (f Float64Data) Trimean(d Float64Data) (float64, error)
- func (f Float64Data) TrimmedMean(percent float64) (float64, error)
- func (f Float64Data) Variance() (float64, error)
- func (f Float64Data) WeightedMean(weights Float64Data) (float64, error)
- func (f Float64Data) Winsorize(percent float64) ([]float64, error)
- func (f Float64Data) ZScore() ([]float64, error)
- type Outliers
- type Quartiles
- type Series
Examples ¶
- ArgMax
- ArgMin
- AutoCorrelation
- ChebyshevDistance
- Clip
- Correlation
- CumulativeMax
- CumulativeMin
- CumulativeProduct
- CumulativeSum
- Diff
- EWMA
- Entropy
- ExpGeom
- Histogram
- Interp
- KendallTau
- Kurtosis
- LinearRegression
- LoadRawData
- Max
- Median
- Min
- MovingAverage
- MovingMax
- MovingMedian
- MovingMin
- MovingStdDev
- MovingSum
- PercentChange
- PercentileOfScore
- ProbGeom
- Product
- RMS
- Range
- Rank
- Rescale
- Round
- SEM
- SampleKurtosis
- Sigmoid
- SoftMax
- Spearman
- Sum
- TrimmedMean
- VarGeom
- Winsorize
- ZScore
Constants ¶
This section is empty.
Variables ¶
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
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 ArgMax ¶ added in v0.10.0
func ArgMax(input Float64Data) (int, error)
ArgMax finds the index of the highest number in a slice, returning the first occurrence in the case of ties
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.ArgMax(d)
fmt.Println(a)
}
Output: 5
func ArgMin ¶ added in v0.10.0
func ArgMin(input Float64Data) (int, error)
ArgMin finds the index of the lowest number in a slice, returning the first occurrence in the case of ties
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.ArgMin(d)
fmt.Println(a)
}
Output: 0
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 Clip ¶ added in v0.11.0
func Clip(input Float64Data, min, max float64) ([]float64, error)
Clip clamps each value in the input slice into the inclusive range between min and max.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
c, _ := stats.Clip([]float64{1, 5, 10}, 2, 8)
fmt.Println(c)
}
Output: [2 5 8]
func CoefficientOfVariation ¶ added in v0.10.0
func CoefficientOfVariation(input Float64Data) (float64, error)
CoefficientOfVariation finds the coefficient of variation of a slice of floats, defined as the sample standard deviation divided by the mean. This matches the behavior of Python's scipy.stats.variation with ddof=1.
The input must not be empty and its mean must not be zero.
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 CumulativeMax ¶ added in v0.10.0
func CumulativeMax(input Float64Data) ([]float64, error)
CumulativeMax calculates the cumulative maximum of the input slice
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
data := []float64{3.0, 1.0, 4.0, 1.0, 5.0}
cmax, _ := stats.CumulativeMax(data)
fmt.Println(cmax)
}
Output: [3 3 4 4 5]
func CumulativeMin ¶ added in v0.10.0
func CumulativeMin(input Float64Data) ([]float64, error)
CumulativeMin calculates the cumulative minimum of the input slice
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
data := []float64{3.0, 1.0, 4.0, 1.0, 5.0}
cmin, _ := stats.CumulativeMin(data)
fmt.Println(cmin)
}
Output: [3 1 1 1 1]
func CumulativeProduct ¶ added in v0.10.0
func CumulativeProduct(input Float64Data) ([]float64, error)
CumulativeProduct calculates the cumulative product of the input slice
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
data := []float64{1.0, 2.0, 3.0, 4.0}
cprod, _ := stats.CumulativeProduct(data)
fmt.Println(cprod)
}
Output: [1 2 6 24]
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 Diff ¶ added in v0.10.0
func Diff(input Float64Data) ([]float64, error)
Diff calculates the successive differences of the input slice, returning input[i] - input[i-1] for each i in 1..len(input)-1. The output has length len(input) - 1; a single-element input returns an empty slice.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
data := []float64{1, 4, 9, 16}
diff, _ := stats.Diff(data)
fmt.Println(diff)
}
Output: [3 5 7]
func EWMA ¶ added in v0.11.0
func EWMA(input Float64Data, alpha float64) ([]float64, error)
EWMA calculates the exponentially weighted moving average of the input with smoothing factor alpha. The first output equals the first input and each subsequent entry is alpha*input[i] + (1-alpha)*output[i-1], so the result has the same length as the input. The alpha must satisfy 0 < alpha <= 1 or ErrBounds is returned. An empty input returns ErrEmptyInput.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
data := []float64{1, 2, 3}
ewma, _ := stats.EWMA(data, 0.5)
fmt.Println(ewma)
}
Output: [1 1.5 2.25]
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
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 Histogram ¶ added in v0.11.0
func Histogram(input Float64Data, bins int) ([]int, []float64, error)
Histogram calculates the histogram of a slice using the given number of equal-width bins over [min, max], returning the count of values in each bin along with the bins+1 bin edges. Each bin is half-open [edges[i], edges[i+1]) except the last, which also includes the maximum value.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
counts, edges, _ := stats.Histogram([]float64{1, 2, 1, 3, 4}, 3)
fmt.Println(counts, edges)
}
Output: [2 1 2] [1 2 3 4]
func InterQuartileRange ¶
func InterQuartileRange(input Float64Data) (float64, error)
InterQuartileRange finds the range between Q1 and Q3
func Interp ¶ added in v0.11.0
func Interp(x, xp, fp Float64Data) ([]float64, error)
Interp calculates the one-dimensional piecewise-linear interpolant to a function with given discrete data points (xp, fp), evaluated at each x. Values of x below xp[0] return fp[0] and values above xp[len(xp)-1] return fp[len(xp)-1], so no extrapolation is performed. Unlike numpy's interp, which silently returns nonsense for unsorted coordinates, xp must be strictly increasing or ErrBounds is returned. An empty x or xp returns ErrEmptyInput and xp and fp of different lengths return ErrSize.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
out, _ := stats.Interp([]float64{1.5}, []float64{1, 2}, []float64{10, 20})
fmt.Println(out)
}
Output: [15]
func KendallTau ¶ added in v0.11.0
func KendallTau(data1, data2 Float64Data) (float64, error)
KendallTau calculates Kendall's tau-b rank correlation coefficient between two variables. Tau-b corrects for ties, matching the values produced by SciPy's kendalltau and pandas' corr(method="kendall"). Pairs are compared with a simple O(n^2) loop for clarity.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
s1 := []float64{1, 2, 2, 3}
s2 := []float64{1, 2, 3, 3}
a, _ := stats.KendallTau(s1, s2)
rounded, _ := stats.Round(a, 5)
fmt.Println(rounded)
}
Output: 0.8
func Kurtosis ¶ added in v0.11.0
func Kurtosis(input Float64Data) (float64, error)
Kurtosis computes the population excess kurtosis of the dataset
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
k, _ := stats.Kurtosis([]float64{1, 2, 3, 4, 5})
fmt.Println(k)
}
Output: -1.3
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 MovingAverage ¶ added in v0.10.0
func MovingAverage(input Float64Data, window int) ([]float64, error)
MovingAverage calculates the rolling mean of the input over a trailing window. Only fully-populated windows produce output, so the result has len(input)-window+1 entries and entry i is the mean of input[i : i+window]. The window must satisfy 1 <= window <= len(input) or ErrBounds is returned. An empty input returns ErrEmptyInput.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
data := []float64{1, 2, 3, 4, 5}
avg, _ := stats.MovingAverage(data, 3)
fmt.Println(avg)
}
Output: [2 3 4]
func MovingMax ¶ added in v0.11.0
func MovingMax(input Float64Data, window int) ([]float64, error)
MovingMax calculates the rolling maximum of the input over a trailing window. Only fully-populated windows produce output, so the result has len(input)-window+1 entries and entry i is the maximum of input[i : i+window]. The window must satisfy 1 <= window <= len(input) or ErrBounds is returned. An empty input returns ErrEmptyInput.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
data := []float64{5, 1, 4, 2, 3}
max, _ := stats.MovingMax(data, 3)
fmt.Println(max)
}
Output: [5 4 4]
func MovingMedian ¶ added in v0.11.0
func MovingMedian(input Float64Data, window int) ([]float64, error)
MovingMedian calculates the rolling median of the input over a trailing window. Only fully-populated windows produce output, so the result has len(input)-window+1 entries and entry i is the median of input[i : i+window]. The window must satisfy 1 <= window <= len(input) or ErrBounds is returned. An empty input returns ErrEmptyInput.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
data := []float64{5, 1, 4, 2, 3}
median, _ := stats.MovingMedian(data, 3)
fmt.Println(median)
}
Output: [4 2 3]
func MovingMin ¶ added in v0.11.0
func MovingMin(input Float64Data, window int) ([]float64, error)
MovingMin calculates the rolling minimum of the input over a trailing window. Only fully-populated windows produce output, so the result has len(input)-window+1 entries and entry i is the minimum of input[i : i+window]. The window must satisfy 1 <= window <= len(input) or ErrBounds is returned. An empty input returns ErrEmptyInput.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
data := []float64{5, 1, 4, 2, 3}
min, _ := stats.MovingMin(data, 3)
fmt.Println(min)
}
Output: [1 1 2]
func MovingStdDev ¶ added in v0.10.0
func MovingStdDev(input Float64Data, window int) ([]float64, error)
MovingStdDev calculates the rolling sample standard deviation of the input over a trailing window. Only fully-populated windows produce output, so the result has len(input)-window+1 entries and entry i is the sample standard deviation of input[i : i+window]. The window must satisfy 2 <= window <= len(input) or ErrBounds is returned, since the sample standard deviation of a single value is undefined. An empty input returns ErrEmptyInput.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
data := []float64{1, 2, 3, 4}
sdev, _ := stats.MovingStdDev(data, 4)
fmt.Println(sdev)
}
Output: [1.2909944487358056]
func MovingSum ¶ added in v0.11.0
func MovingSum(input Float64Data, window int) ([]float64, error)
MovingSum calculates the rolling sum of the input over a trailing window. Only fully-populated windows produce output, so the result has len(input)-window+1 entries and entry i is the sum of input[i : i+window]. The window must satisfy 1 <= window <= len(input) or ErrBounds is returned. An empty input returns ErrEmptyInput.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
data := []float64{1, 2, 3, 4, 5}
sum, _ := stats.MovingSum(data, 3)
fmt.Println(sum)
}
Output: [6 9 12]
func NormBoxMullerRvs ¶ added in v0.6.0
NormBoxMullerRvs generates random variates using the Box–Muller transform. For more information please visit: http://mathworld.wolfram.com/Box-MullerTransformation.html
func NormEntropy ¶ added in v0.6.0
NormEntropy is the differential entropy of the RV.
func NormFit ¶ added in v0.6.0
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
NormInterval finds endpoints of the range that contains alpha percent of the distribution.
func NormLogCdf ¶ added in v0.6.0
NormLogCdf is the log of the cumulative distribution function.
func NormLogPdf ¶ added in v0.6.0
NormLogPdf is the log of the probability density function.
func NormMedian ¶ added in v0.6.0
NormMedian is the median of the distribution.
func NormMoment ¶ added in v0.6.0
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 NormPpf ¶ added in v0.6.0
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
NormPpfRvs generates random variates using the Point Percentile Function. For more information please visit: https://demonstrations.wolfram.com/TheMethodOfInverseTransforms/
func NormSample ¶ added in v0.10.0
NormSample generates random samples from a normal distribution with the given mean (loc) and standard deviation (scale).
func NormSf ¶ added in v0.6.0
NormSf is the survival function (also defined as 1 - cdf, but sf is sometimes more accurate).
func NormStats ¶ added in v0.6.0
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 Pearson ¶
func Pearson(data1, data2 Float64Data) (float64, error)
Pearson calculates the Pearson product-moment correlation coefficient between two variables
func PercentChange ¶ added in v0.10.0
func PercentChange(input Float64Data) ([]float64, error)
PercentChange calculates the fractional change between successive elements of the input slice, returning (input[i] - input[i-1]) / input[i-1] for each i in 1..len(input)-1. The output has length len(input) - 1; a single-element input returns an empty slice. A zero denominator follows IEEE 754 semantics, yielding +Inf, -Inf, or NaN (for 0/0), matching the behavior of pandas pct_change.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
data := []float64{100, 110, 99}
change, _ := stats.PercentChange(data)
fmt.Println(change)
}
Output: [0.1 -0.1]
func Percentile ¶
func Percentile(input Float64Data, percent float64) (percentile float64, err error)
Percentile finds the relative standing in a slice of floats.
The function uses the Linear Interpolation Between Closest Ranks method as recommended by NIST [1] and used by Excel (PERCENTILE), Google Sheets, NumPy (default), and other standard tools.
Algorithm (for percent p and sorted data of length n):
- Compute the rank: rank = (p / 100) * (n - 1)
- Split into integer part k and fractional part f
- Result = data[k] + f * (data[k+1] - data[k])
[1] https://www.itl.nist.gov/div898/handbook/prc/section2/prc262.htm
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 PercentileOfScore ¶ added in v0.11.0
func PercentileOfScore(input Float64Data, score float64) (float64, error)
PercentileOfScore calculates the percentile rank of a score relative to a slice of floats, defined as the percentage of values strictly below the score plus half the percentage of values equal to the score. The result is between 0 and 100. This matches the behavior of Python's scipy.stats.percentileofscore with kind="rank".
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
rank, _ := stats.PercentileOfScore([]float64{1, 2, 3, 4}, 3)
fmt.Println(rank)
}
Output: 62.5
func PercentileWeighted ¶ added in v0.10.0
func PercentileWeighted(data, weights Float64Data, percent float64) (percentile float64, err error)
PercentileWeighted finds the weighted percentile of a slice of floats using the weighted empirical CDF (inverse CDF / nearest-rank method).
For a given percent p, it returns the smallest data value x such that the cumulative weight of all values <= x is at least p% of the total weight. This matches the behavior of Python's statsmodels DescrStatsW.quantile.
The data and weights slices must be the same length. Weights must be non-negative and at least one weight must be positive. The percent parameter must be between 0 and 100 (exclusive).
func PopulationKurtosis ¶ added in v0.11.0
func PopulationKurtosis(input Float64Data) (float64, error)
PopulationKurtosis computes the population excess kurtosis (Fisher definition) using the fourth central moment normalized by the squared variance, so a normal distribution has a kurtosis of zero.
func PopulationSkewness ¶ added in v0.9.0
func PopulationSkewness(input Float64Data) (float64, error)
PopulationSkewness computes the population skewness using the third central moment normalized by the cube of the standard deviation.
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
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 Product ¶ added in v0.11.0
func Product(input Float64Data) (float64, error)
Product calculates the product of a slice of floats by multiplying the values from left to right. It is the scalar counterpart of CumulativeProduct. Large inputs can overflow to Inf; use GeometricMean for an overflow-safe summary of multiplicative data.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
product, _ := stats.Product([]float64{2, 3, 4})
fmt.Println(product)
}
Output: 24
func RMS ¶ added in v0.11.0
func RMS(input Float64Data) (float64, error)
RMS calculates the root mean square of a slice of floats, defined as the square root of the mean of the squared values.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
rms, _ := stats.RMS([]float64{3, 4})
fmt.Printf("%.4f", rms)
}
Output: 3.5355
func Range ¶ added in v0.10.0
func Range(input Float64Data) (float64, error)
Range finds the difference between the highest and lowest numbers in a slice
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
d := []float64{4, 1, 9}
a, _ := stats.Range(d)
fmt.Println(a)
}
Output: 8
func Rank ¶ added in v0.10.0
func Rank(input Float64Data) ([]float64, error)
Rank assigns fractional (average) ranks to the input values. Ranks are 1-based and tied values receive the average of the ranks they would have been assigned.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
r, _ := stats.Rank([]float64{3, 1, 4, 1})
fmt.Println(r)
}
Output: [3 1.5 4 1.5]
func Rescale ¶ added in v0.11.0
func Rescale(input Float64Data) ([]float64, error)
Rescale normalizes the input values to the range of 0 to 1 by subtracting the minimum and dividing by the range, also known as min-max normalization.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
r, _ := stats.Rescale([]float64{2, 4, 6})
fmt.Println(r)
}
Output: [0 0.5 1]
func Round ¶
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 SEM ¶ added in v0.11.0
func SEM(input Float64Data) (float64, error)
SEM calculates the standard error of the mean of a slice of floats, defined as the sample standard deviation divided by the square root of the sample size. This matches the behavior of Python's scipy.stats.sem with ddof=1.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
sem, _ := stats.SEM([]float64{1, 2, 3, 4, 5})
fmt.Printf("%.4f", sem)
}
Output: 0.7071
func Sample ¶
func Sample(input Float64Data, takenum int, replacement bool) ([]float64, error)
Sample returns sample from input with replacement or without
func SampleKurtosis ¶ added in v0.11.0
func SampleKurtosis(input Float64Data) (float64, error)
SampleKurtosis computes the bias-corrected sample excess kurtosis, matching pandas .kurt() and scipy.stats.kurtosis with bias=False.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
k, _ := stats.SampleKurtosis([]float64{1, 2, 3, 4, 5})
fmt.Printf("%.1f\n", k)
}
Output: -1.2
func SampleSkewness ¶ added in v0.9.0
func SampleSkewness(input Float64Data) (float64, error)
SampleSkewness computes the adjusted Fisher-Pearson standardized moment coefficient, correcting for bias in small samples.
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 Skewness ¶ added in v0.9.0
func Skewness(input Float64Data) (float64, error)
Skewness computes the population skewness of the dataset
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 Spearman ¶ added in v0.10.0
func Spearman(data1, data2 Float64Data) (float64, error)
Spearman calculates the Spearman rank correlation coefficient between two variables. It works by ranking the data and then computing the Pearson correlation of the ranks. This method handles tied values using fractional (average) ranking.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
s1 := []float64{1, 2, 3, 4, 5}
s2 := []float64{5, 6, 7, 8, 7}
a, _ := stats.Spearman(s1, s2)
rounded, _ := stats.Round(a, 5)
fmt.Println(rounded)
}
Output: 0.82078
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 TTest ¶ added in v0.10.0
func TTest(data1, data2 Float64Data, populationMean float64) (t float64, pvalue float64, err error)
TTest performs a one-sample or two-sample (independent) Student's t-test.
For a one-sample t-test, pass the sample data as data1, nil for data2, and the expected population mean as populationMean.
For a two-sample independent t-test (assuming equal variance), pass both sample datasets. The populationMean parameter is ignored in this case.
Returns the t statistic and the two-tailed p-value.
func Trimean ¶
func Trimean(input Float64Data) (float64, error)
Trimean finds the average of the median and the midhinge
func TrimmedMean ¶ added in v0.11.0
func TrimmedMean(input Float64Data, percent float64) (float64, error)
TrimmedMean finds the mean of a slice of floats after removing a fraction of the smallest and largest values. This matches the behavior of Python's scipy.stats.trim_mean.
The percent parameter is the fraction removed from each tail and must be in the range [0, 0.5). The number of elements trimmed from each tail is floor(len(input) * percent). A percent of zero returns the same result as Mean.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
tm, _ := stats.TrimmedMean([]float64{1, 2, 3, 4, 100}, 0.2)
fmt.Println(tm)
}
Output: 3
func VarGeom ¶ added in v0.7.0
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
func WeightedMean ¶ added in v0.10.0
func WeightedMean(data, weights Float64Data) (float64, error)
WeightedMean finds the weighted mean of a slice of floats, defined as the sum of each data value multiplied by its weight divided by the sum of all the weights. This matches the behavior of Python's numpy.average with the weights argument.
The data and weights slices must be the same length. Weights must be non-negative and at least one weight must be positive.
func Winsorize ¶ added in v0.11.0
func Winsorize(input Float64Data, percent float64) ([]float64, error)
Winsorize limits the effect of outliers in a slice of floats by clamping a fraction of the smallest and largest values. This matches the behavior of Python's scipy.stats.mstats.winsorize with symmetric limits.
The percent parameter is the fraction clamped in each tail and must be in the range [0, 0.5). With k = floor(len(input) * percent), values below the k-th smallest value are set to it and values above the k-th largest value are set to it. The returned slice preserves the original element order and a percent of zero returns a copy of the input.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
w, _ := stats.Winsorize([]float64{1, 2, 3, 4, 100}, 0.2)
fmt.Println(w)
}
Output: [2 2 3 4 4]
func ZScore ¶ added in v0.10.0
func ZScore(input Float64Data) ([]float64, error)
ZScore standardizes the input values by subtracting the mean and dividing by the sample standard deviation, returning the number of standard deviations each value is from the mean.
Example ¶
package main
import (
"fmt"
"github.com/montanaflynn/stats"
)
func main() {
z, _ := stats.ZScore([]float64{2, 4, 6})
fmt.Println(z)
}
Output: [-1 0 1]
func ZTest ¶ added in v0.10.0
func ZTest(data1, data2 Float64Data, populationMean, populationStdDev float64) (z float64, pvalue float64, err error)
ZTest performs a one-sample or two-sample Z-test.
For a one-sample Z-test, pass the sample data as data1, nil for data2, the known population mean as populationMean, and the known population standard deviation as populationStdDev.
For a two-sample Z-test, pass both sample datasets and the known population standard deviations. The populationMean parameter is ignored in this case.
Returns the Z statistic and the two-tailed p-value.
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
Range 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 range 2.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) ArgMax ¶ added in v0.10.0
func (f Float64Data) ArgMax() (int, error)
ArgMax returns the index of the highest number in the data
func (Float64Data) ArgMin ¶ added in v0.10.0
func (f Float64Data) ArgMin() (int, error)
ArgMin returns the index of the lowest number in the data
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) Clip ¶ added in v0.11.0
func (f Float64Data) Clip(min, max float64) ([]float64, error)
Clip clamps each value in the input slice into the inclusive range between min and max.
func (Float64Data) CoefficientOfVariation ¶ added in v0.10.0
func (f Float64Data) CoefficientOfVariation() (float64, error)
CoefficientOfVariation finds the sample standard deviation divided by the mean
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) CumulativeMax ¶ added in v0.10.0
func (f Float64Data) CumulativeMax() ([]float64, error)
CumulativeMax calculates the cumulative maximum of the data
func (Float64Data) CumulativeMin ¶ added in v0.10.0
func (f Float64Data) CumulativeMin() ([]float64, error)
CumulativeMin calculates the cumulative minimum of the data
func (Float64Data) CumulativeProduct ¶ added in v0.10.0
func (f Float64Data) CumulativeProduct() ([]float64, error)
CumulativeProduct calculates the cumulative product of the data
func (Float64Data) CumulativeSum ¶
func (f Float64Data) CumulativeSum() ([]float64, error)
CumulativeSum returns the cumulative sum of the data
func (Float64Data) Diff ¶ added in v0.10.0
func (f Float64Data) Diff() ([]float64, error)
Diff returns the successive differences of the data
func (Float64Data) EWMA ¶ added in v0.11.0
func (f Float64Data) EWMA(alpha float64) ([]float64, error)
EWMA returns the exponentially weighted moving average of the data with smoothing factor alpha
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 geometric mean of the data
func (Float64Data) HarmonicMean ¶
func (f Float64Data) HarmonicMean() (float64, error)
HarmonicMean returns the harmonic mean of the data
func (Float64Data) Histogram ¶ added in v0.11.0
func (f Float64Data) Histogram(bins int) ([]int, []float64, error)
Histogram returns the counts and equal-width bin edges of the data
func (Float64Data) InterQuartileRange ¶
func (f Float64Data) InterQuartileRange() (float64, error)
InterQuartileRange finds the range between Q1 and Q3
func (Float64Data) KendallTau ¶ added in v0.11.0
func (f Float64Data) KendallTau(d Float64Data) (float64, error)
KendallTau calculates Kendall's tau-b rank correlation coefficient between two variables.
func (Float64Data) Kurtosis ¶ added in v0.11.0
func (f Float64Data) Kurtosis() (float64, error)
Kurtosis finds the population excess kurtosis of a slice of floats
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) MovingAverage ¶ added in v0.10.0
func (f Float64Data) MovingAverage(window int) ([]float64, error)
MovingAverage returns the rolling mean of the data over a trailing window
func (Float64Data) MovingMax ¶ added in v0.11.0
func (f Float64Data) MovingMax(window int) ([]float64, error)
MovingMax returns the rolling maximum of the data over a trailing window
func (Float64Data) MovingMedian ¶ added in v0.11.0
func (f Float64Data) MovingMedian(window int) ([]float64, error)
MovingMedian returns the rolling median of the data over a trailing window
func (Float64Data) MovingMin ¶ added in v0.11.0
func (f Float64Data) MovingMin(window int) ([]float64, error)
MovingMin returns the rolling minimum of the data over a trailing window
func (Float64Data) MovingStdDev ¶ added in v0.10.0
func (f Float64Data) MovingStdDev(window int) ([]float64, error)
MovingStdDev returns the rolling sample standard deviation of the data over a trailing window
func (Float64Data) MovingSum ¶ added in v0.11.0
func (f Float64Data) MovingSum(window int) ([]float64, error)
MovingSum returns the rolling sum of the data over a trailing window
func (Float64Data) Pearson ¶
func (f Float64Data) Pearson(d Float64Data) (float64, error)
Pearson calculates the Pearson product-moment correlation coefficient between two variables.
func (Float64Data) PercentChange ¶ added in v0.10.0
func (f Float64Data) PercentChange() ([]float64, error)
PercentChange returns the fractional change between successive elements of the data
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) PercentileOfScore ¶ added in v0.11.0
func (f Float64Data) PercentileOfScore(score float64) (float64, error)
PercentileOfScore calculates the percentile rank of a score relative to the data
func (Float64Data) PopulationKurtosis ¶ added in v0.11.0
func (f Float64Data) PopulationKurtosis() (float64, error)
PopulationKurtosis finds the population excess kurtosis of a slice of floats
func (Float64Data) PopulationVariance ¶
func (f Float64Data) PopulationVariance() (float64, error)
PopulationVariance finds the amount of variance within a population
func (Float64Data) Product ¶ added in v0.11.0
func (f Float64Data) Product() (float64, error)
Product calculates the product of the data
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) RMS ¶ added in v0.11.0
func (f Float64Data) RMS() (float64, error)
RMS calculates the root mean square of the data
func (Float64Data) Range ¶ added in v0.10.0
func (f Float64Data) Range() (float64, error)
Range returns the difference between the highest and lowest numbers in the data
func (Float64Data) Rank ¶ added in v0.10.0
func (f Float64Data) Rank() ([]float64, error)
Rank assigns fractional (average) ranks to the input values
func (Float64Data) Rescale ¶ added in v0.11.0
func (f Float64Data) Rescale() ([]float64, error)
Rescale normalizes the input values to the range of 0 to 1 by subtracting the minimum and dividing by the range
func (Float64Data) SEM ¶ added in v0.11.0
func (f Float64Data) SEM() (float64, error)
SEM calculates the standard error of the mean of the data
func (Float64Data) Sample ¶
func (f Float64Data) Sample(n int, r bool) ([]float64, error)
Sample returns sample from input with replacement or without
func (Float64Data) SampleKurtosis ¶ added in v0.11.0
func (f Float64Data) SampleKurtosis() (float64, error)
SampleKurtosis finds the bias-corrected sample excess kurtosis of a slice of floats
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) Spearman ¶ added in v0.10.0
func (f Float64Data) Spearman(d Float64Data) (float64, error)
Spearman calculates the Spearman rank correlation coefficient between two variables.
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) TrimmedMean ¶ added in v0.11.0
func (f Float64Data) TrimmedMean(percent float64) (float64, error)
TrimmedMean finds the mean of the data after removing a fraction of the smallest and largest values from each tail
func (Float64Data) Variance ¶
func (f Float64Data) Variance() (float64, error)
Variance the amount of variation in the dataset
func (Float64Data) WeightedMean ¶ added in v0.10.0
func (f Float64Data) WeightedMean(weights Float64Data) (float64, error)
WeightedMean finds the weighted mean of the data using the given weights
func (Float64Data) Winsorize ¶ added in v0.11.0
func (f Float64Data) Winsorize(percent float64) ([]float64, error)
Winsorize returns a copy of the data with a fraction of the smallest and largest values in each tail clamped
func (Float64Data) ZScore ¶ added in v0.10.0
func (f Float64Data) ZScore() ([]float64, error)
ZScore standardizes the input values by subtracting the mean and dividing by the sample standard deviation
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 ¶
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 ¶
ExponentialRegression returns an exponential regression on data series. A non-positive Y value returns ErrYCoord, and a series without at least two distinct X values returns ErrBounds.
func LinearRegression ¶
LinearRegression finds the least squares linear regression on data series. A series without at least two distinct X values returns ErrBounds.
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.4} {2 3.1} {3 3.8000000000000003}]
func LogarithmicRegression ¶
LogarithmicRegression returns a logarithmic regression on data series. A non-positive X value or a series without at least two distinct X values returns ErrBounds.
Source Files
¶
- clip.go
- coefficient_of_variation.go
- correlation.go
- cumulative.go
- cumulative_sum.go
- data.go
- describe.go
- deviation.go
- diff.go
- distances.go
- doc.go
- entropy.go
- errors.go
- ewma.go
- extremes.go
- geometric_distribution.go
- histogram.go
- interp.go
- kendall.go
- kurtosis.go
- legacy.go
- load.go
- max.go
- mean.go
- median.go
- min.go
- mode.go
- moving.go
- norm.go
- outlier.go
- percentile.go
- percentile_of_score.go
- percentile_weighted.go
- product.go
- quartile.go
- rank.go
- ranksum.go
- regression.go
- rescale.go
- rms.go
- rolling.go
- round.go
- sample.go
- sem.go
- sigmoid.go
- skewness.go
- softmax.go
- sum.go
- trimmed_mean.go
- ttest.go
- util.go
- variance.go
- weighted_mean.go
- winsorize.go
- zscore.go
- ztest.go