summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xalgorithms/ranking-dist.rb75
1 files changed, 75 insertions, 0 deletions
diff --git a/algorithms/ranking-dist.rb b/algorithms/ranking-dist.rb
new file mode 100755
index 0000000..c621bdd
--- /dev/null
+++ b/algorithms/ranking-dist.rb
@@ -0,0 +1,75 @@
+#!/usr/bin/env ruby
+
+items = [0, 1, 2]
+ranks = [1, 2, 3]
+#permutations = [
+# [1,2,3],
+# [2,3,1],
+# [3,1,2],
+# [3,2,1],
+# [1,3,2]
+#]
+scores = [3, 2, 1]
+
+def f items, ranks, scores,topone=false
+ prod = 1
+ items.each_with_index { |i,j|
+ x = []
+ d = Math.exp(scores[ranks[i]-1])
+ x << ranks[i]
+ sum = 0
+ if j+1 <= items.size
+ (j).upto(items.size-1) { |k|
+ sum = sum+Math.exp(scores[ranks[k]-1])
+ x << ranks[k]
+ }
+ prod = prod*(d/sum)
+ if topone
+ return prod
+ end
+ puts "#{ranks[i]} / #{x[1..-1].map { |z| "#{z}" }.join(" ")}"
+ end
+ }
+ return prod
+end
+
+#best = f items,ranks,scores
+
+permutations = {}
+sz = 0
+stop = 0
+while true
+ break if stop == 1000
+ r = ranks.shuffle
+ permutations[r] = 1
+ if sz == permutations.size
+ stop = stop+1
+ else
+ sz = permutations.size
+ stop = 0
+ end
+end
+
+puts permutations.to_s
+
+sumtoone = 0
+permutations.each_key { |r|
+ p = f items,r,scores,false
+ puts "#{r.to_s} #{p}"
+ puts
+ sumtoone = sumtoone+p
+ #if p >= best
+ # puts r.to_s
+ # puts p
+ #end
+}
+
+puts "==="
+puts sumtoone
+
+puts "+++ NLL"
+
+scores = [0.1,0.1,100]
+true_ranking = [2,1,3]
+puts(-Math.log(f(items,true_ranking,scores)))
+