-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalysisOnPermsWMarty.jl
151 lines (134 loc) · 4.71 KB
/
AnalysisOnPermsWMarty.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
using JSON
using LinearAlgebra
using Plots
using StatsBase
gr()
# 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
# some notes
#data :: Dict{String, Any}
#data[somekey] :: Dict{String, Any}
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 everyPerm(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
# 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(everyPerm.(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 everyPerm(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
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("->", probOnAllPermsBinary)
end