-
Notifications
You must be signed in to change notification settings - Fork 3
/
golang_math.go
153 lines (141 loc) · 4.11 KB
/
golang_math.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Compiled with go version go1.9 linux/amd64
*/
package main
import (
"fmt"
"math"
"os"
)
/* Following function computes
* means of a given data set.
*/
func compute_mean(data_points []float32) float32 {
var (
i int
sum float32
number_of_elements int
)
number_of_elements = len(data_points)
for i = 0; i < number_of_elements; i++ {
sum = sum + data_points[i]
}
return (sum / float32(number_of_elements))
}
/* Following function computes standard
* deviation of a data point
*/
func compute_std_deviation(data_points []float32) float32 {
var (
number_of_elements int
i int
mean float32
data_point_dev []float32
std_div, temp float32
)
number_of_elements = len(data_points)
data_point_dev = make([]float32, number_of_elements)
/*Calculate mean of the data points*/
mean = compute_mean(data_points)
/*Calculate standard deviation of individual data points*/
for i = 0; i < number_of_elements; i++ {
temp = data_points[i] - mean
data_point_dev[i] = (temp * temp)
}
/* Finally, Compute standard Deviation of data points
* by taking mean of individual std. Deviation.
*/
std_div = compute_mean(data_point_dev)
return float32(math.Sqrt(float64(std_div)))
}
/* Following function computes covariance
* of two data sets.
*/
func covariance(data_point_x []float32, data_point_y []float32) float32 {
var (
i int
len_x int
mean_x float32
mean_y float32
sum float32
)
/*Check number of data points*/
len_x = len(data_point_x)
if len_x != len(data_point_y) {
fmt.Println("Both data set must of equal size!")
os.Exit(1)
}
/*Calculate mean of data points*/
mean_x = compute_mean(data_point_x)
mean_y = compute_mean(data_point_y)
/*Finally, covariance*/
for i = 0; i < len_x; i++ {
sum = sum + ((data_point_x[i] - mean_x) * (data_point_y[i] - mean_y))
}
return (sum / float32(len_x))
}
/* Following function calculates the correlation
* coefficient of given data sets.
*/
func compute_corelation_coeffecient(data_point_x []float32, data_point_y []float32) float32 {
var (
covariance_x_y, std_div_x, std_div_y float32
)
covariance_x_y = covariance(data_point_x, data_point_y)
std_div_x = compute_std_deviation(data_point_x)
std_div_y = compute_std_deviation(data_point_y)
return (covariance_x_y / (std_div_x * std_div_y))
}
/* Function computes Manhattan distance between two data sets */
func compute_manhattan_distance(data_point_x, data_point_y []float32) float32 {
var i int
var sum float32
if len(data_point_x) != len(data_point_y) {
fmt.Println("Both data sets must be equal in numbers!")
os.Exit(1)
}
for i = 0; i < len(data_point_x); i++ {
sum = sum + float32(math.Abs(float64(data_point_x[i])-float64(data_point_y[i])))
}
return sum
}
/* Function Calculates Euclidean distance
* between two data sets
*/
func compute_Euclidean_distance(data_point_x, data_point_y []float32) float32 {
var i int
var sum float32
if len(data_point_x) != len(data_point_y) {
fmt.Println("Both data sets must be equal in numbers!")
os.Exit(1)
}
for i = 0; i < len(data_point_x); i++ {
sum = sum + ((data_point_x[i] - data_point_y[i]) * (data_point_x[i] - data_point_y[i]))
}
return float32(math.Sqrt(float64(sum)))
}
/* Compute Chebyshev distance between two data points */
func compute_Chebyshev_distance(data_point_x, data_point_y []float32) float32 {
var (
distance float32
i int
)
if len(data_point_x) != len(data_point_y) {
fmt.Println("Data points must be equal!")
os.Exit(1)
}
distance = 0
for i = 0; i < len(data_point_y); i++ {
if distance < float32(math.Abs(float64(data_point_x[i]-data_point_y[i]))) {
distance = float32(math.Abs(float64(data_point_x[i] - data_point_y[i])))
}
}
return distance
}
func main() {
data_point_a := []float32{2, 4, 4, 4, 5, 5, 7, 9}
data_point_b := []float32{2, 4, 4, 4, 5, 5, 7, 1}
fmt.Println("Standard Deviation:", compute_std_deviation(data_point_a))
fmt.Println("Chebyshev Distance:", compute_Chebyshev_distance(data_point_a, data_point_b))
fmt.Println("Correlation Coeffiecent:", compute_corelation_coeffecient(data_point_a, data_point_b))
}