pdist 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pdist.rb +47 -17
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a6f5b3ffbd9c7a94d90ef885b6e3d672cfd9d76
|
4
|
+
data.tar.gz: 06829fa9169d0bfe40034928915e9a4e51b0e3ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd6c346ba5a2c20e3c197ace9c893bdd44b00ae7658d538e52bffff47e5e8fabd92bce3eebcff3193bdd5fd9bc54b0f7db519ec031d2ba2242c21f49953dc97b
|
7
|
+
data.tar.gz: 534384b0ba7b37c391e8a73f16e72140765bfb44573bcc9682e7ab9abd4c15e2b50fad2e6fc3452d0f5540bcbf2c24a30ae1a82087caa94e564a11319bd9226c
|
data/lib/pdist.rb
CHANGED
@@ -12,9 +12,14 @@ class PDist
|
|
12
12
|
return difference.map! {|i| i.abs } # the deviation's, as absolute values (direction does not matter)
|
13
13
|
end
|
14
14
|
|
15
|
+
# Returns float of the deviation distance between original and permutation
|
16
|
+
def self.deviation_raw(original, permutation)
|
17
|
+
return distances(original, permutation).inject(:+) # sum of each object's deviation, is the deviation distance between original and permutation
|
18
|
+
end
|
19
|
+
|
15
20
|
# Returns float of the deviation distance between original and permutation, normalized between 0.0 and 1.0
|
16
21
|
def self.deviation(original, permutation)
|
17
|
-
s =
|
22
|
+
s = deviation_raw(original, permutation)
|
18
23
|
n = permutation.length
|
19
24
|
if n % 2 == 0
|
20
25
|
distance = (2.0 / (n ** 2).to_f) * s
|
@@ -24,17 +29,22 @@ class PDist
|
|
24
29
|
return distance # if/else block normalizes the deviation distance
|
25
30
|
end
|
26
31
|
|
27
|
-
# Returns float of the squared deviation distance between original and permutation
|
28
|
-
def self.
|
32
|
+
# Returns float of the squared deviation distance between original and permutation
|
33
|
+
def self.square_raw(original, permutation)
|
29
34
|
sq_dists = []
|
30
35
|
distances(original, permutation).each{|d| sq_dists << d**2} # each object's deviation is squared
|
31
|
-
|
36
|
+
return sq_dists.inject(:+) # sum of each object's squared deviation, is the squared deviation distance between original and permutation
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns float of the squared deviation distance between original and permutation, normalized between 0.0 and 1.0
|
40
|
+
def self.square(original, permutation)
|
41
|
+
s = square_raw(original, permutation)
|
32
42
|
n = permutation.length
|
33
43
|
return (3.0 / (n**3 - n).to_f) * s # normalizes the squared deviation distance
|
34
44
|
end
|
35
45
|
|
36
|
-
# Returns float of the generalized hamming distance between original and permutation
|
37
|
-
def self.
|
46
|
+
# Returns float of the generalized hamming distance between original and permutation
|
47
|
+
def self.hamming_raw(original, permutation)
|
38
48
|
x = 0
|
39
49
|
hds = [] # hamming distances
|
40
50
|
permutation.each do |object| # hamming distances are 0 when object's have same index in original and permutation, and 1 when not
|
@@ -43,34 +53,48 @@ class PDist
|
|
43
53
|
end
|
44
54
|
x+=1
|
45
55
|
end
|
46
|
-
|
56
|
+
return hds.inject(:+).to_f # total hamming distance is the sum of the hamming distances
|
57
|
+
end
|
58
|
+
|
59
|
+
# Returns float of the generalized hamming distance between original and permutation, normalized between 0.0 and 1.0
|
60
|
+
def self.hamming(original, permutation)
|
61
|
+
total_hd = hamming_raw(original, permutation)
|
47
62
|
return total_hd / permutation.length.to_f # normalizes by dividing the total hamming distance by the maximum possible total, which == number of objects
|
48
63
|
end
|
49
64
|
|
50
|
-
# Returns float of the compliment R distance between original and permutation
|
51
|
-
def self.
|
65
|
+
# Returns float of the compliment R distance between original and permutation
|
66
|
+
def self.rdist_raw(original, permutation)
|
52
67
|
x = 0
|
53
68
|
r = [] # compliment of R distance == number of times two objects consecutive in original, are consectutive in permutation
|
54
|
-
|
55
|
-
(n - 1).times do
|
69
|
+
(permutation.length - 1).times do
|
56
70
|
y = permutation.index(original[x])
|
57
71
|
if original[x+1] != permutation[y+1]
|
58
72
|
r << 1
|
59
73
|
end
|
60
74
|
x+=1
|
61
75
|
end
|
62
|
-
return r.inject(:+).to_f
|
76
|
+
return r.inject(:+).to_f
|
77
|
+
end
|
78
|
+
|
79
|
+
# Returns float of the compliment R distance between original and permutation, normalized between 0.0 and 1.0
|
80
|
+
def self.rdist(original, permutation)
|
81
|
+
dist = rdist_raw(original, permutation)
|
82
|
+
return dist / (permutation.length - 1).to_f # normalized: dividing by maximum number of consecutive occurences
|
83
|
+
end
|
84
|
+
|
85
|
+
# Returns float of the longest common sub-sequence between original and permutation
|
86
|
+
def self.lcs_raw(original, permutation)
|
87
|
+
return Diff::LCS.LCS(original, permutation) # diff-lcs gem used to calculate longest common sub-sequence
|
63
88
|
end
|
64
89
|
|
65
90
|
# Returns float of the longest common sub-sequence between original and permutation, normalized between 0.0 and 1.0
|
66
91
|
def self.lcs(original, permutation)
|
67
|
-
lcs =
|
92
|
+
lcs = lcs_raw(original, permutation)
|
68
93
|
return (permutation.length - lcs.length).to_f / (permutation.length - 1).to_f # normalized: dividing by longest possible common sub-sequence
|
69
94
|
end
|
70
95
|
|
71
|
-
# Returns float of the kendall's tau distance between original and permutation
|
72
|
-
def self.
|
73
|
-
n = permutation.length
|
96
|
+
# Returns float of the kendall's tau distance between original and permutation
|
97
|
+
def self.kendalls_tau_raw(original, permutation)
|
74
98
|
kt = []
|
75
99
|
original.each do |x| # for each of the objects in original...
|
76
100
|
permutation.each do |y| # ... iterate over the objects in permutation
|
@@ -80,7 +104,13 @@ class PDist
|
|
80
104
|
end
|
81
105
|
end
|
82
106
|
end
|
83
|
-
|
107
|
+
return kt.inject(:+) # the number of pairwise adjacent permutations required to transform original into permutation
|
108
|
+
end
|
109
|
+
|
110
|
+
# Returns float of the kendall's tau distance between original and permutation, normalized between 0.0 and 1.0
|
111
|
+
def self.kendalls_tau(original, permutation) # TODO edit the if statement to work with any type of object
|
112
|
+
n = permutation.length
|
113
|
+
s = kendalls_tau_raw(original, permutation)
|
84
114
|
return 2 * (s.to_f / (n**2 - n).to_f) # normalized kendall's tau distance
|
85
115
|
end
|
86
116
|
end
|