diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2019-03-09 10:38:17 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2019-03-09 10:38:17 -0500 |
| commit | 46f933fbac76630016e96aeb1f434cdd09ec3aa8 (patch) | |
| tree | 9fd8535bcc09ab7f462ae120fbdd52fcfe7d9d12 | |
| parent | 9904322b0cf7e1b98a5d7cb9ffc8453edcca83b1 (diff) | |
| download | stats-46f933fbac76630016e96aeb1f434cdd09ec3aa8.tar.gz stats-46f933fbac76630016e96aeb1f434cdd09ec3aa8.tar.xz | |
Merge working development into master
| -rw-r--r-- | print.go | 21 | ||||
| -rw-r--r-- | stats.go | 90 |
2 files changed, 111 insertions, 0 deletions
diff --git a/print.go b/print.go new file mode 100644 index 0000000..2a39c71 --- /dev/null +++ b/print.go @@ -0,0 +1,21 @@ +package stats + +import ( + "fmt" +) + +// Prints each calculated statistic on a line with one tab preceding it +func (s *Stats) PrettyStats() string { + return fmt.Sprintf(` + Mean: %.2f + Variance: %.2f + Standard Deviation: %.2f + Skewness: %.2f + Kurtosis: %.2f +`, s.Mean(), s.Variance(), s.StandardDeviation(), s.Skewness(), s.Kurtosis()) +} + +// Returns a string containing all of the data within the struct +func (s *Stats) Internals() string { + return fmt.Sprintf("%#v", s) +} diff --git a/stats.go b/stats.go new file mode 100644 index 0000000..ffe94eb --- /dev/null +++ b/stats.go @@ -0,0 +1,90 @@ +// Provides some basic statisitcs functionality. Specifically tailored around +// dealing with large streams of data +// It's a Go implimentation of this: +// +// https://www.johndcook.com/blog/skewness_kurtosis/ +// +// Example program: +// package main +// +// import ( +// "git.riedstra.us/go/stats" +// +// "fmt" +// ) +// +// func main() { +// myFakeStreamOfData := []float64{01, 04, 60, 55, 80, 06, 75, 51, 63, 10} +// myStats := &stats.Stats{} +// for _, entry := range myFakeStreamOfData { +// myStats.Push(entry) +// } +// fmt.Println(myStats.PrettyStats()) +// } +// Example output: +// +// Mean: 40.50 +// Variance: 996.72 +// Standard Deviation: 31.57 +// Skewness: -0.20 +// Kurtosis: -1.66 +// +// +package stats + +import ( + "math" +) + +// Structure used to hold the information about the dataset. +// several methods are exposed in order to make your life easy +type Stats struct { + n, m1, m2, m3, m4 float64 +} + +// Zeros out the struct for re-use +func (s *Stats) Clear() { + s.n = 0 + s.m1, s.m2, s.m3, s.m4 = 0, 0, 0, 0 +} + +// This function is used to push numbers onto the struct and calculate on the +// fly the necessary information to output relevant statistics +func (s *Stats) Push(x float64) { + var delta, delta_n, delta_n2, term1, n1 float64 + + n1 = s.n + s.n++ + delta = x - s.m1 + delta_n = delta / s.n + delta_n2 = delta_n * delta_n + term1 = delta * delta_n * n1 + s.m1 += delta_n + s.m4 += term1*delta_n2*(s.n*s.n-3*s.n+3) + 6*delta_n2*s.m2 - 4*delta_n*s.m3 + s.m3 += term1*delta_n*(s.n-2) - 3*delta_n*s.m2 + s.m2 += term1 +} + +func (s *Stats) NumValues() float64 { + return s.n +} + +func (s *Stats) Mean() float64 { + return s.m1 +} + +func (s *Stats) Variance() float64 { + return s.m2 / (s.n - 1) +} + +func (s *Stats) StandardDeviation() float64 { + return math.Sqrt(s.Variance()) +} + +func (s *Stats) Skewness() float64 { + return math.Sqrt(s.n) * s.m3 / math.Pow(s.m2, 1.5) +} + +func (s *Stats) Kurtosis() float64 { + return s.n*s.m4/(s.m2*s.m2) - 3 +} |
