-
Notifications
You must be signed in to change notification settings - Fork 1
/
distances.go
93 lines (73 loc) · 2.18 KB
/
distances.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
package vectormath
import (
"fmt"
"github.com/dgryski/go-onlinestats"
"math"
)
type Distance int
const (
Euclidean Distance = 0
Cosine Distance = 1
Manhattan Distance = 2
Pearson Distance = 3
)
// TODO(Juan): Change the x and y variables to a and b
func EuclideanDistance(x []float64, y []float64) (float64, error) {
var sum float64
if len(x) != len(y) {
return sum, fmt.Errorf("different slice sizes len(x): %v, len(y): %v", len(x), len(y))
}
for index, element := range x {
sum += math.Pow(element-y[index], 2)
}
return math.Sqrt(sum), nil
}
func ManhattanDistance(x []float64, y []float64) (float64, error) {
var sum float64
if len(x) != len(y) {
return sum, fmt.Errorf("different slice sizes len(x): %v, len(y): %v", len(x), len(y))
}
for index, element := range x {
sum += math.Abs(element - y[index])
}
return sum, nil
}
// CosineSimilarity calculates the cosine similarity between two vectors.
// cos(d_1, d_2) = (d_1 . d_2) / (||d_1|| * ||d_2||)
func CosineSimilarity(x []float64, y []float64) (float64, error) {
dot, err := dotProduct(x, y)
if err != nil {
return 0, fmt.Errorf("Could not calculate the cosine similarity: %v", err)
}
fmt.Printf("cosine: %v\n", float64((dot))/(vectorEuclideanNorm(x)*vectorEuclideanNorm(y)))
return float64((dot)) / (vectorEuclideanNorm(x) * vectorEuclideanNorm(y)), nil
}
func PearsonCorrelation(a, b []float64) (float64, error) {
if len(a) != len(b) {
return 0, fmt.Errorf("different slice sizes len(a): %v, len(b): %v", len(a), len(b))
}
return onlinestats.Pearson(a, b), nil
}
// VectorEuclideanNorm calculates the euclidean norm (also known as magnitude or length)
// x = sqrt(x^2_1 + x^2_2 + ... + x^2_n)
func vectorEuclideanNorm(vec []float64) float64 {
if len(vec) == 0 {
return 0.0
}
var sum float64
for _, val := range vec {
sum += math.Pow(float64(val), 2)
}
return math.Sqrt(sum)
}
// DotProduct computes the dot product between 2 vectors, d_1 . d_2
func dotProduct(x []float64, y []float64) (float64, error) {
var sum float64
if len(x) != len(y) {
return sum, fmt.Errorf("different slice sizes %v, %v", len(x), len(y))
}
for i, element := range x {
sum += element * y[i]
}
return sum, nil
}