-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathalgebra.go
70 lines (56 loc) · 1.29 KB
/
algebra.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
package govector
import (
"fmt"
"math"
)
// Product returns a vector of element-wise products of two input vectors.
func Product(x, y Vector) (Vector, error) {
if len(x) != len(y) {
return nil, fmt.Errorf("x and y have unequal lengths: %d / %d", len(x), len(y))
}
p := make(Vector, len(x))
for i, _ := range x {
p[i] = x[i] * y[i]
}
return p, nil
}
// DotProduct returns the dot product of two vectors.
func DotProduct(x, y Vector) (float64, error) {
p, err := Product(x, y)
if err != nil {
return NA, err
}
return p.Sum(), nil
}
// Norm returns the vector norm. Use pow = 2.0 for Euclidean.
func Norm(x Vector, pow float64) float64 {
s := 0.0
for _, xval := range x {
s += math.Pow(xval, pow)
}
return math.Pow(s, 1/pow)
}
// Cosine returns the cosine similarity between two vectors.
func Cosine(x, y Vector) (float64, error) {
d, err := DotProduct(x, y)
if err != nil {
return NA, err
}
xnorm := Norm(x, 2.0)
ynorm := Norm(y, 2.0)
return d / (xnorm * ynorm), nil
}
// Cor returns the Pearson correlation between two vectors.
func Cor(x, y Vector) (float64, error) {
n := float64(len(x))
xy, err := Product(x, y)
if err != nil {
return NA, err
}
sx := x.Sd()
sy := y.Sd()
mx := x.Mean()
my := y.Mean()
r := (xy.Sum() - n*mx*my) / ((n - 1) * sx * sy)
return r, nil
}