-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.go
48 lines (42 loc) · 957 Bytes
/
stat.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
package libwx
import "math"
func circularMean(x, weights []float64) float64 {
if weights != nil && len(x) != len(weights) {
panic("stat: slice length mismatch")
}
var aX, aY float64
if weights != nil {
for i, v := range x {
aX += weights[i] * math.Cos(v)
aY += weights[i] * math.Sin(v)
}
} else {
for _, v := range x {
aX += math.Cos(v)
aY += math.Sin(v)
}
}
return math.Atan2(aY, aX)
}
func circularStdDev(x []float64, weights []float64) float64 {
if weights != nil && len(x) != len(weights) {
panic("stat: slice length mismatch")
}
var aX, aY float64
if weights != nil {
var sumW float64
for i, v := range x {
w := weights[i]
sumW += w
aX += w * math.Cos(v)
aY += w * math.Sin(v)
}
return math.Sqrt(-2 * math.Log(math.Hypot(aY, aX)/sumW))
} else {
for _, v := range x {
aX += math.Cos(v)
aY += math.Sin(v)
}
return math.Sqrt(-2 * math.Log(math.Hypot(aY, aX)/float64(len(x))))
}
}