-
Notifications
You must be signed in to change notification settings - Fork 0
/
pearson.go
59 lines (47 loc) · 1.07 KB
/
pearson.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
package calc
import (
"runtime"
"sync"
)
type pWork struct {
from int
to int
}
func pearson(timeSeries [][600]float32, stats []LinStatEle, matBuffer [][13362]float32, order <-chan int, wg *sync.WaitGroup) {
for {
work, ok := <-order
if ok {
for i := work; i < 13362; i++ {
var accProd float32
for t := 0; t < 600; t++ {
accProd += timeSeries[work][t] * timeSeries[i][t]
}
cov := (accProd / 600) - (stats[work].avg * stats[i].avg)
pearson := cov / (stats[work].stddev * stats[i].stddev)
matBuffer[work][i] += pearson
if work != i {
matBuffer[i][work] += pearson
}
}
wg.Done()
} else {
break
}
}
return
}
// DoPearson does Pearson's correlation calculation
func DoPearson(timeSeries [][600]float32, stats []LinStatEle, matBuffer [][13362]float32) {
order := make(chan int, runtime.NumCPU())
var wg sync.WaitGroup
for i := 0; i < runtime.NumCPU(); i++ {
go pearson(timeSeries, stats, matBuffer, order, &wg)
}
wg.Add(13362)
for i := 0; i < 13362; i++ {
order <- i
}
wg.Wait()
close(order)
return
}