aboutsummaryrefslogtreecommitdiff
path: root/stats.go
blob: bc5be7c0af52f0dfe9d058ce21bec3dc62a9a051 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Provides some basic statisitcs functionality. Specifically tailored around
// dealing with large streams of data at the moment.
package stats

import (
	"math"
)

// Stores the average, standard deviation, and the current number of entities
// processed
type Stats struct {
	Mean     float64
	Variance float64
	N        float64
}

// Returns the square root of 1/2 the variance, or Standard Deviation from the
// mean
func (s *Stats) Stdev() float64 {
	return math.Sqrt(s.Variance / 2)
}

// Adds an entry to our struct, taking care of adjusting the Mean and Variance
func (s *Stats) AddEntry(e float64) {
	m := s.Mean
	s.Mean += (e - m) / s.N
	s.Variance += (e - m) * (e - s.Mean)
	s.N++
}