summaryrefslogtreecommitdiff
path: root/kendalls-tau
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2016-07-05 11:01:46 +0200
committerPatrick Simianer <p@simianer.de>2016-07-05 11:01:46 +0200
commit2b1d7f881c19c4d4b5afae194e02d3300c7675d0 (patch)
tree5a06ee7de98640a39244b57bb369697176b44ebf /kendalls-tau
parent69949dda35c3ea21d8e926e5f0a596a0a0f61c6a (diff)
mv
Diffstat (limited to 'kendalls-tau')
-rwxr-xr-xkendalls-tau75
1 files changed, 75 insertions, 0 deletions
diff --git a/kendalls-tau b/kendalls-tau
new file mode 100755
index 0000000..c0c20be
--- /dev/null
+++ b/kendalls-tau
@@ -0,0 +1,75 @@
+#!/usr/bin/env ruby
+
+#################################################
+# reads space delimted pairs of scores as input,
+# outputs Kendall's τ
+#################################################
+
+def kendall_with_ties l
+ concordant = 0
+ disconcordant = 0
+ tie_a = 0
+ tie_b = 0
+ l.each_with_index { |k,i|
+ l[i+1,l.size].each_with_index { |m,j|
+ if (k.first < m.first && k[1] < m[1]) ||
+ (k.first > m.first && k[1] > m[1])
+ concordant += 1
+ elsif (k.first == m.first && k[1] != m[1])
+ tie_a += 1
+ elsif (k.first != m.first && k[1] == m[1])
+ tie_b += 1
+ else
+ disconcordant += 1
+ end
+ }
+ }
+
+ return (concordant-disconcordant)/(Math.sqrt((concordant+disconcordant+tie_a)*(concordant+disconcordant+tie_b)))
+end
+
+def kendall l
+ concordant = 0
+ disconcordant = 0
+ l.each_with_index { |k,i|
+ l[i+1,l.size].each_with_index { |m,j|
+ if (k.first <= m.first && k[1] <= m[1]) ||
+ (k.first >= m.first && k[1] >= m[1])
+ concordant += 1
+ else
+ disconcordant += 1
+ end
+ }
+ }
+
+ return (concordant-disconcordant)/(0.5 * l.size * (l.size-1))
+end
+
+def has_ties? l
+ if l.map{ |p| p[1] }.uniq.size != l.size ||
+ l.map{ |p| p[2] }.uniq.size != l.size
+ return true
+ end
+
+ return false
+end
+
+def main
+ l = []
+ while line = STDIN.gets
+ a,b = line.split
+ l << [a.to_f, b.to_f]
+ end
+
+ v = -1
+ if has_ties? l
+ v = kendall_with_ties l
+ else
+ v = kendall l
+ end
+
+ puts v
+end
+
+main
+