/
statisticalaccumulator.go
94 lines (48 loc) · 1.07 KB
/
statisticalaccumulator.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package statisticalaccumulator
import "math/big"
type StatisticalAccumulator struct {
n *big.Rat
sigmaXI *big.Rat
sigmaXISquared *big.Rat
one *big.Rat
}
func New() (*StatisticalAccumulator) {
n := new(big.Rat)
n.SetInt64(0)
sigmaXI := new(big.Rat)
sigmaXI.SetInt64(0)
sigmaXISquared := new(big.Rat)
sigmaXISquared.SetInt64(0)
one := new(big.Rat)
one.SetInt64(1)
me := StatisticalAccumulator{
n:n,
sigmaXI:sigmaXI,
sigmaXISquared:sigmaXISquared,
one:one,
}
return &me
}
func (me *StatisticalAccumulator) N() (*big.Rat) {
n := new(big.Rat)
n.Set(me.n)
return n
}
func (me *StatisticalAccumulator) Mean() (*big.Rat) {
mean := new(big.Rat)
mean.Inv(me.n)
mean.Mul(mean, me.sigmaXI)
return mean
}
func (me *StatisticalAccumulator) Variance() (*big.Rat) {
variance := new(big.Rat)
variance.Inv(me.n)
variance.Mul(variance, me.sigmaXISquared)
temp := new(big.Rat)
temp.Mul(me.n, me.n)
temp.Inv(temp)
temp.Mul(temp, me.sigmaXI)
temp.Mul(temp, me.sigmaXI)
variance.Sub(variance, temp)
return variance
}