-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelsForRankingsToyExs.jl
106 lines (94 loc) · 2.59 KB
/
ModelsForRankingsToyExs.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
function permGroup(A::Union{Set,Array})
unique!(A)
P = Array{eltype(A),1}[]
function continuePerm(head,tail)
if length(tail) > 0
for t in tail
newHead = union(head, [t])
newTail = setdiff(tail, [t])
continuePerm(newHead, newTail)
end
else
push!(P, head)
end
end
continuePerm(eltype(A)[], A)
return P
end
S₃ = permGroup([1,2,3])
function I(x,y, list)
looking = true
for elem in list
if elem == x return 1
end
if elem == y return 0
end
end
end
function C(m::Array{T,2} where T <: Real)
k = size(m)[1]
Sk = permGroup(collect(1:k))
sum = 0
for perm in Sk
prod = 1
for i in 1:k
for j in (i+1):k
prod *= (m[i,j] ^ I(i,j, perm) )
end
end
sum += prod
end
return sum
end
# Function implements (2) in On the Babington Smith Class of Models for Rankings
# P(π) = ∏_{i<j} ( Θ_{ij} )^{I}
function P(perm::Array, m::Array{T,2} where T <: Real)
k = length(perm)
if k != size(m)[1] && k != size(m)[2] error("The following is false: k != size(m)[1] && k != size(m)[2]") end
prod = 1
for i in 1:k
for j in (i+1):k
prod *= (m[i,j] ^ I(i,j, perm))
end
end
return prod / C(m)
end
Θ = [ 0 1 1 0;
0 0 1 1;
0 0 0 1;
1 0 0 0 ]
for i in 0:4
println("-------- Shift by $i ---------------")
A = circshift(Θ, (i,i))
notA = (A .+ 1) .% 2
println("C(Θ) = $C(Θ) and C(notA)= $(C(notA))")
for p in permGroup(collect(1:4))
println("π=$p and P(π|A) = $(P(p, A)) and P(π|notA) = $(P(p, notA)) ")
#println("P($p |notΘ) = $(P(p, notΘ))")
#println("$(P(p, notΘ)) ")
end
end
function Propto(perm::Array, m::Array{T,2} where T <: Real)
k = length(perm)
if k != size(m)[1] && k != size(m)[2] error("The following is false: k != size(m)[1] && k != size(m)[2]") end
prod = 1
for i in 1:k
for j in (i+1):k
prod *= ( m[i,j]^I(i,j, perm) * (1 - m[i,j])^(1-I(i,j, perm)) )
end
end
return prod
end
Θ = [ 0 1 1 0;
0 0 1 1;
0 0 0 1;
1 0 0 0 ]
for i in 0:4
println("-------- Shift by $i ---------------")
A = circshift(Θ, (i,i))
notA = (A .+ 1) .% 2
for p in permGroup(collect(1:4))
println("π=$p and Propto(π|A) = $(Propto(p, A)) and Propto(π|notA) = $(Propto(p, notA)) ")
end
println("-------------------")
end