diff options
author | Patrick Simianer <p@simianer.de> | 2017-07-05 07:53:33 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2017-07-05 07:53:33 +0200 |
commit | 2f04e5647ca1889cdd5da731dfc9f15138212def (patch) | |
tree | 36de5edece6e51091abe63c8036ba33b1e276193 | |
parent | 808f45d9e60c125e2a76f9f3f1a65130da72a276 (diff) |
ranking
-rwxr-xr-x | algorithms/ranking-dist.rb | 75 |
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))) + |