-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_tools.rb
142 lines (124 loc) · 3.02 KB
/
math_tools.rb
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
def m_absolute_deviation(array, refpoint, type)
#median = median(array)
devs = []
array.each do |v|
devs << (v - refpoint).abs
end
if type == "median"
mad = median(devs)
elsif type == "mean"
mad = mean(devs)
end
return mad
end
def median(array)
return nil if array.empty?
sorted = array.sort
len = sorted.length
(sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0
end
def smooth(array,window,totalcontrol=false,ndatapoints=[],total_threshold=10000)
if window % 2 == 0
abort("Cassandra says: Smoothing window must be an odd number")
end
smoother = (window - 1) / 2
#hash = Hash.new{|hash, key| hash[key] = Array.new}
array2 = []
array3 = []
array.each.with_index do |element, index|
array2[index] = 0.0
array3[index] = 0.0
#if (index + 1 - smoother) > = 0 and (index + 1 + smoother) <= array.length
ntotal = 0.0
for i in correct((index - smoother), 0, "lower")..correct((index+smoother), array.length-1, "higher")
#if array[i] != "NA"
array2[index] += array[i]
ntotal += 1
#end
if totalcontrol
array3[index] += ndatapoints[i]
end
end
array2[index] = array2[index]/ntotal
if totalcontrol
array3[index] = array3[index]/ntotal
if array3[index] < total_threshold
array2[index] = "NA"
end
end
#end
end
return array2
end
def correct(number, limit, type)
if (type == "lower" and number < limit) or (type == "higher" and number > limit)
number = limit
end
return number
end
def mean(array)
sum = sumarray(array)
mean = sum / array.length
return mean
end
def sumarray(array)
sum = 0.0
array.each do |x|
sum += x
end
return sum
end
def stdev(array) #calculate standard deviation
n = array.length.to_f
squaresum = 0.0
mean = mean(array)
array.each do |x|
squaresum += (x - mean) ** 2
end
stdev = Math.sqrt(squaresum/(n-1))
return stdev
end
def zscore(array)
zscores = []
mean = mean(array)
stdev = stdev(array)
array.each do |x|
zscores << (x - mean) / stdev
end
return zscores
end
def cosine_delta(array1,array2)
z1 = zscore(array1)
z2 = zscore(array2)
dist = 1 - (dot_product(z1,z2) / (euclidean_norm(z1) * euclidean_norm(z2)))
return dist
end
def dot_product(array1,array2)
dp = 0.0
array1.each_index do |index|
dp += array1[index] * array2[index]
end
return dp
end
def euclidean_norm(array)
norm = Math.sqrt(dot_product(array,array))
return norm
end
def div_by_zero(a,b)
if b != 0
c = a.to_f/b
else
c = 0.0
end
return c
end
def entropy(array)
e = 0.0
array.each do |p|
if p != 0
e += p * Math.log(p, 2)
end
end
e = -e
return e
end