-
Notifications
You must be signed in to change notification settings - Fork 7
/
basics.go
54 lines (43 loc) · 830 Bytes
/
basics.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package onlinestats
import "math"
func SumSq(a []float64) float64 {
var t float64
for _, aa := range a {
t += (aa * aa)
}
return t
}
func Sum(a []float64) float64 {
var t float64
for _, aa := range a {
t += aa
}
return t
}
func Mean(a []float64) float64 {
return Sum(a) / float64(len(a))
}
func variance(a []float64, sample bool) float64 {
var adjustN float64
if sample {
adjustN = -1
}
m := Mean(a)
var sum2 float64
var sum3 float64
for _, aa := range a {
sum2 += (aa - m) * (aa - m)
sum3 += (aa - m)
}
n := float64(len(a))
return (sum2 - (sum3*sum3)/n) / (n + adjustN)
}
func Variance(a []float64) float64 {
return variance(a, false)
}
func SampleVariance(a []float64) float64 {
return variance(a, true)
}
func SampleStddev(a []float64) float64 {
return math.Sqrt(SampleVariance(a))
}