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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pdist.rb +47 -17
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 55a92e4315831c4dbcf6d6f82234c3866c0e2e3d
4
- data.tar.gz: 4d302000e4f0b641f00ac5ca203a234aa9e4b636
3
+ metadata.gz: 6a6f5b3ffbd9c7a94d90ef885b6e3d672cfd9d76
4
+ data.tar.gz: 06829fa9169d0bfe40034928915e9a4e51b0e3ba
5
5
  SHA512:
6
- metadata.gz: aa1bd2577a5da537d5204c3d07c4c79a79403eaae915c468f0b8dc40c24026097e76829d31d8c9b1bb31cc10843dc86cc74932484cfeebf7b488e93117d85508
7
- data.tar.gz: c029ec2faedefb7f1f501a9fb2c61ca51753d91da446fc87bada34d04b26c26107e6b71c7b740fd5ca5b3cbdfea3591cc9eecd6ad79ce77721bd68b2b95da707
6
+ metadata.gz: bd6c346ba5a2c20e3c197ace9c893bdd44b00ae7658d538e52bffff47e5e8fabd92bce3eebcff3193bdd5fd9bc54b0f7db519ec031d2ba2242c21f49953dc97b
7
+ data.tar.gz: 534384b0ba7b37c391e8a73f16e72140765bfb44573bcc9682e7ab9abd4c15e2b50fad2e6fc3452d0f5540bcbf2c24a30ae1a82087caa94e564a11319bd9226c
@@ -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 = distances(original, permutation).inject(:+) # sum of each object's deviation, is the deviation distance between original and permutation
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, normalized between 0.0 and 1.0
28
- def self.square(original, permutation)
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
- s = sq_dists.inject(:+) # sum of each object's squared deviation, is the squared deviation distance between original and permutation
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, normalized between 0.0 and 1.0
37
- def self.hamming(original, permutation)
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
- total_hd = hds.inject(:+).to_f # total hamming distance is the sum of the hamming distances
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, normalized between 0.0 and 1.0
51
- def self.rdist(original, permutation)
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
- n = permutation.length
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 / (n - 1).to_f # normalized: dividing by maximum number of consecutive occurences
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 = Diff::LCS.LCS(original, permutation) # diff-lcs gem used to calculate longest common sub-sequence
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, normalized between 0.0 and 1.0
72
- def self.kendalls_tau(original, permutation) # TODO edit the if statement to work with any type of object
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
- s = kt.inject(:+) # the number of pairwise adjacent permutations required to transform original into permutation
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdist
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edward Chalstrey