/
ranking.jl
150 lines (130 loc) · 5.2 KB
/
ranking.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
borda_update!()
Updates a set of Borda counts, given a new set of ranking data.
Designed to be used iteratively over a set of rankings.
## Arguments
* `scores::DataFrame`: the current Borda counts for a list of candidates
* `ranking::DataFrame`: a set of rankings of candidates (which should overlap but isn't necessarily a subset of scores)
## Examples
```jldoctest
julia> scores = DataFrame( Title=["A", "B", "C"], Score=zeros(3) )
julia> ranking = DataFrame( Title=["A", "B", "D"], Ranking=[1, 2, 3] )
julia> borda_update!( scores, ranking )
julia> scores
3×2 DataFrame
│ Row │ Title │ Score │
│ │ String │ Float64 │
├─────┼────────┼─────────┤
│ 1 │ A │ 3.0 │
│ 2 │ B │ 2.0 │
│ 3 │ C │ 0.0 │
```
"""
function borda_update!( scores::DataFrame, ranking::DataFrame )
# https://en.wikipedia.org/wiki/Borda_count
n = size(scores,1)
for i=1:size(ranking,1)
t = ranking[i,:Title]
if !ismissing( ranking[i,:Ranking] )
r = ranking[i,:Ranking]
else
r = size(ranking,1)
end
s = n - r + 1
k = findfirst( scores[:,:Title] .== t )
if k != nothing
scores[k,:Score] += s
end
end
end
function power_update!( scores::DataFrame, ranking::DataFrame; alpha::Float64=1.0 )
# https://en.wikipedia.org/wiki/Borda_count
# power-based generalisation of Dowdall
n = size(scores,1)
for i=1:size(ranking,1)
t = ranking[i,:Title]
if !ismissing( ranking[i,:Ranking] )
r = ranking[i,:Ranking]
else
r = size(ranking,1)
end
s = 1/ (r^alpha)
k = findfirst( scores[:,:Title] .== t )
if k != nothing
scores[k,:Score] += s
end
end
end
dowdall_update!( scores::DataFrame, ranking::DataFrame) = power_update!( scores, ranking )
function preference_matrix_update!( preferences::Array{Int,2}, preference_list::DataFrame, ranking::DataFrame )
# calculate the preference matrix for the input ranking
m = size(preference_list,1)
pref1 = zeros(eltype(preferences), m, m)
for I=1:m
i = findfirst( ranking[:,:Title] .== preference_list[I,:Title] )
# println(" I = $I, i=$i ")
for J=I+1:m
j = findfirst( ranking[:,:Title] .== preference_list[J,:Title] )
if i==nothing && j==nothing
# nothing to do
elseif j==nothing
# println(" >I = $I ($(preference_list[I,:Title])), J = $J ($(preference_list[J,:Title])), i=$i j=nothing")
pref1[I,J] += 1
elseif i==nothing
# println(" <I = $I ($(preference_list[I,:Title])), J = $J ($(preference_list[J,:Title])), j=$j i=nothing")
pref1[J,I] += 1
elseif ismissing(ranking[i,:Ranking]) && ismissing(ranking[j,:Ranking])
# nothing to do
elseif ismissing(ranking[j,:Ranking]) || (ranking[i,:Ranking] < ranking[j,:Ranking])
# println(" >I = $I ($(preference_list[I,:Title])), J = $J ($(preference_list[J,:Title])), i=$i j=$j")
pref1[I,J] += 1
elseif ismissing(ranking[i,:Ranking]) || (ranking[i,:Ranking] > ranking[j,:Ranking])
# println(" <I = $I ($(preference_list[I,:Title])), J = $J ($(preference_list[J,:Title])), i=$i j=$j")
pref1[J,I] += 1
end
end
end
# update the old preference matrix
preferences .+= pref1
end
function copeland_score(preferences::Array{Int,2})
P = preferences .- preferences' # pairwise victories - pairwise defeats between each (i,j)
return vec( sum( sign.(P); dims=2 ) )
end
function copeland2_score(preferences::Array{Int,2})
P = preferences .- preferences' # pairwise victories - pairwise defeats between each (i,j)
return vec( sum( P; dims=2 ) )
end
function condorcet_rank(preferences::Array{Int,2} ; method::String="Copeland")
# https://en.wikipedia.org/wiki/Condorcet_method#Condorcet_ranking_methods
if lowercase(method)=="copeland"
# https://en.wikipedia.org/wiki/Copeland%27s_method
r = copeland_score(preferences)
ranking = sortperm(r; rev=true)
elseif lowercase(method)=="second-order copeland"
# https://en.wikipedia.org/wiki/Copeland%27s_method
r = copeland2_score(preferences)
ranking = sortperm(r; rev=true)
elseif lowercase(method)==""
# https://en.wikipedia.org/wiki/Kemeny%E2%80%93Young_method
elseif lowercase(method)==""
# https://en.wikipedia.org/wiki/Ranked_pairs
elseif lowercase(method)==""
# https://en.wikipedia.org/wiki/Schulze_method
else
error("method $method isn't implemented")
end
end
# Report the Top-N elements of a DataFrame
function topN(scores::DataFrame, N::Int; col=:Score, rev=true)
k = sortperm(scores, col; rev=rev)[1:N]
end
# put the scores on a rage from 0-1
function normalise( scores::Array{T,1}; rev=false ) where {T <: Number }
r = ( scores .- minimum(scores) ) ./ ( maximum(scores) - minimum(scores) )
if rev
return 1 .- r
else
return r
end
end