File: avgvar.go

package info (click to toggle)
golang-github-petar-gollrb 0.0~git20130427.0.53be0d3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 128 kB
  • sloc: java: 384; makefile: 3
file content (39 lines) | stat: -rw-r--r-- 886 bytes parent folder | download | duplicates (4)
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
30
31
32
33
34
35
36
37
38
39
// Copyright 2010 Petar Maymounkov. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package llrb

import "math"

// avgVar maintains the average and variance of a stream of numbers
// in a space-efficient manner.
type avgVar struct {
	count      int64
	sum, sumsq float64
}

func (av *avgVar) Init() {
	av.count = 0
	av.sum = 0.0
	av.sumsq = 0.0
}

func (av *avgVar) Add(sample float64) {
	av.count++
	av.sum += sample
	av.sumsq += sample * sample
}

func (av *avgVar) GetCount() int64 { return av.count }

func (av *avgVar) GetAvg() float64 { return av.sum / float64(av.count) }

func (av *avgVar) GetTotal() float64 { return av.sum }

func (av *avgVar) GetVar() float64 {
	a := av.GetAvg()
	return av.sumsq/float64(av.count) - a*a
}

func (av *avgVar) GetStdDev() float64 { return math.Sqrt(av.GetVar()) }