forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variance.go
105 lines (78 loc) · 2.25 KB
/
variance.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
95
96
97
98
99
100
101
102
103
104
105
package stats
import "math"
// _variance finds the variance for both population and sample data
func _variance(input Float64Data, sample int) (variance float64, err error) {
if input.Len() == 0 {
return math.NaN(), EmptyInput
}
// Sum the square of the mean subtracted from each number
m, _ := Mean(input)
for _, n := range input {
variance += (float64(n) - m) * (float64(n) - m)
}
// When getting the mean of the squared differences
// "sample" will allow us to know if it's a sample
// or population and wether to subtract by one or not
return variance / float64((input.Len() - (1 * sample))), nil
}
// Variance the amount of variation in the dataset
func Variance(input Float64Data) (sdev float64, err error) {
return PopulationVariance(input)
}
// PopulationVariance finds the amount of variance within a population
func PopulationVariance(input Float64Data) (pvar float64, err error) {
v, err := _variance(input, 0)
if err != nil {
return math.NaN(), err
}
return v, nil
}
// SampleVariance finds the amount of variance within a sample
func SampleVariance(input Float64Data) (svar float64, err error) {
v, err := _variance(input, 1)
if err != nil {
return math.NaN(), err
}
return v, nil
}
// Covariance is a measure of how much two sets of data change
func Covariance(data1, data2 Float64Data) (float64, error) {
l1 := data1.Len()
l2 := data2.Len()
if l1 == 0 || l2 == 0 {
return math.NaN(), EmptyInput
}
if l1 != l2 {
return math.NaN(), SizeErr
}
m1, _ := Mean(data1)
m2, _ := Mean(data2)
// Calculate sum of squares
var ss float64
for i := 0; i < l1; i++ {
delta1 := (data1.Get(i) - m1)
delta2 := (data2.Get(i) - m2)
ss += (delta1*delta2 - ss) / float64(i+1)
}
return ss * float64(l1) / float64(l1-1), nil
}
// CovariancePopulation computes covariance for entire population between two variables.
func CovariancePopulation(data1, data2 Float64Data) (float64, error) {
l1 := data1.Len()
l2 := data2.Len()
if l1 == 0 || l2 == 0 {
return math.NaN(), EmptyInput
}
if l1 != l2 {
return math.NaN(), SizeErr
}
m1, _ := Mean(data1)
m2, _ := Mean(data2)
var s float64
for i := 0; i < l1; i++ {
delta1 := (data1.Get(i) - m1)
delta2 := (data2.Get(i) - m2)
s += delta1 * delta2
}
return s / float64(l1), nil
}