-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalysisOnPerm3.jl
213 lines (184 loc) · 5.86 KB
/
AnalysisOnPerm3.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using JSON
using LinearAlgebra
using Plots
using StatsBase
gr()
#### Note the following
function (P::Dict)(D0::Dict)
map!(x->P[x], values(D0))
println(keys(D0))
println(values(D0))
end
id = Dict( [1=>1, 2=>2, 3=>3, 4=>4, 5=>5] )
E = Dict( [1=>3, 2=>4, 3=>5, 4=>2, 5=>1] )
# then run this 6 times
E(id)
(d::Dict)() = d( Dict([i=>i for i in 1:5]) )
d = Dict([1=>2,2=>3,3=>4,4=>5,5=>1])
Dict{Int64,Int64} with 2 entries:
2 => 1
1 => 2
julia> d([2,1])
2-element Array{Int64,1}:
1
2
julia> d()
2-element Array{Int64,1}:
2
1
# I love this helper: https://github.com/JuliaLang/julia/issues/24741
function subtypetree(t, level=1, indent=4)
level == 1 && println(t)
for s in subtypes(t)
println(join(fill(" ", level * indent)) * string(s))
subtypetree(s, level+1, indent)
end
end
# CC countries
isoDict = Dict([ "Australia" => :AUS,
"Basque Country" => :ESP,
"Brazil" => :BRA,
"Fiji" => :FJI,
"France" => :FRA,
"Hawaii" => :USA,
"Indonesia" => :IDN,
"Italy" => :ITA,
"Japan" => :JPN,
"New Zealand" => :NZL,
"Portugal" => :PRT,
"South Africa" => :ZAF,
"Spain" => :ESP,
"United States" => :USA ])
data = Dict()
# File Path for Data
open("Data/CombinedCountries/CleanAllDataCC.txt", "r") do f
global data
data = JSON.parse(f) # parse and transform data
end
waves = []
for wid in keys(data)
if data[wid]["nJudOrigs"] == 5 & data[wid]["nSubScos"] == 5
origs = unique(data[wid]["subScoOrig"])
matchIndicator = (data[wid]["athOrig"] in origs)
labeledScos = Dict([isoDict[origin] => Float16[] for origin in origs])
origScoPairs = collect(zip(data[wid]["subScoOrig"],data[wid]["subSco"]))
labeledScosBinary = Dict([:Match => Float16[], :NoMatch => Float16[] ])
for p in origScoPairs
# push!( array of judge scores from country p[1], score=p[2] )
push!(labeledScos[ isoDict[p[1]] ], p[2])
if p[1] == data[wid]["athOrig"]
push!(labeledScosBinary[:Match], p[2])
else
push!(labeledScosBinary[:NoMatch], p[2])
end
end
x = ( id=wid,
evtYear=data[wid]["evtYear"],
evtOrig=isoDict[data[wid]["evtOrig"]],
evtName=data[wid]["evtName"],
evtId=data[wid]["evtId"],
rnd=data[wid]["rnd"],
rndId=data[wid]["rndId"],
heat=data[wid]["heat"],
heatId=data[wid]["heatId"],
athName=data[wid]["athName"],
athId=data[wid]["athId"],
athOrig=isoDict[data[wid]["athOrig"]],
currentPoints=data[wid]["currentPoints"],
endingPoints=data[wid]["endingPoints"],
panel=labeledScos,
panelBinary=labeledScosBinary,
subScos=data[wid]["subSco"],
subScoOrigs=map(x->isoDict[x], data[wid]["subScoOrig"]),
panelOrigs=Set(map(x->isoDict[x], data[wid]["subScoOrig"])),
match=matchIndicator )
push!(waves, x)
end
end
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
sort(collect(values(isoDict)))
function permInv(A::Union{Set,Array})
# want to implement with arbitrary symbol
function strictOrder(S)
for i in 1:(length(S)-1)
if !(S[i] < S[i+1])
return false
end
end
return true
end
#=
ord = [:USA,:BRA,:AUS]
w = first(waves)
S = Iterators.product([w.panel[c] for c in ord]...)
=#
allPanelCompositions = unique( map(x->x.panelOrigs, waves))
allPerms = union(permGroup.(collect.(allPanelCompositions))...)
# Check sum()
sum( factorial.(length.(allPanelCompositions))) == length(allPerms)
probOnAllPerms = Dict([p=>0.0 for p in allPerms])
for panelComp in allPanelCompositions
for w in filter(x -> x.panelOrigs == panelComp, waves)
for ord in permGroup(collect(panelComp))
S = Iterators.product([w.panel[c] for c in ord]...)
v = mapreduce(s-> strictOrder(s) ? 1 : 0, +, S) / length(S)
probOnAllPerms[ord] += v
end
end
end
function getPairs(A::Array)
unique!(A)
p = Array[]
for i in 1:length(A)
for j in (i+1):length(A)
push!(p, [A[i],A[j]])
end
end
return p
end
function getPairs(A::Set)
A = sort(collect(A))
p = Array[]
for i in 1:length(A)
for j in (i+1):length(A)
push!(p, [A[i],A[j]])
end
end
return p
end
probOnAllPairs = Dict([p=>0.0 for p in getPairs( sort(collect(values(isoDict))) )])
for w in waves
for p in getPairs( w.panelOrigs )
S = Iterators.product([ w.panel[ p[1] ], w.panel[ p[2] ] ])
v = mapreduce(s-> strictOrder(s) ? 1 : 0, +, S) / length(S)
probOnAllPairs[p] += v
end
end
for Country in values(isoDict)
probOnAllPermsBinary = Dict([p=>0.0 for p in everyPerm([:Match,:NoMatch])])
for w in filter(x -> x.match & (x.athOrig == Country), waves)
for ord in everyPerm([:Match,:NoMatch])
S = Iterators.product([w.panelBinary[c] for c in ord]...)
v = mapreduce(s-> strictOrder(s) ? 1 : 0, +, S) / length(S)
#v /= min(length(w.panelBinary[:Match]), length(w.panelBinary[:NoMatch]))
probOnAllPermsBinary[ord] += v
end
end
println(Country)
println("->", probOnAllPermsBiånary)
end