Skip to content

Instantly share code, notes, and snippets.

@gwwfps
Last active August 29, 2015 14:04
Show Gist options
  • Save gwwfps/5ab6f2032deae0641dba to your computer and use it in GitHub Desktop.
Save gwwfps/5ab6f2032deae0641dba to your computer and use it in GitHub Desktop.
LCS result simulation
rounds = 4
teams = ["C9" "COL" "CLG" "EG" "LMQ" "DIG" "CRS" "TSM"]
wins =
Uint8[ 0 1 2 2 2 2 2 1 ; # C9
2 0 0 1 0 2 2 0 ; # COL
1 3 0 2 1 3 1 2 ; # CLG
1 2 1 0 0 0 2 0 ; # EG
1 3 2 3 0 1 1 3 ; # LMQ
1 1 0 3 2 0 1 3 ; # DIG
1 1 2 1 2 2 0 0 ; # CRS
2 3 1 3 0 0 3 0 ; # TSM
]
weighted = true
# test case
# teams = ["LD" "FNC" "ATN" "NIP" "EG" "GG" "SK" "MYM"]
# wins =
# Uint8[ 0 2 2 2 2 2 3 1 ; # LD
# 1 0 3 2 1 3 1 2 ; # FNC
# 1 1 0 2 3 0 2 4 ; # ATN
# 1 1 1 0 3 3 1 2 ; # NIP
# 1 3 0 0 0 3 2 2 ; # EG
# 2 0 3 1 0 0 2 3 ; # GG
# 1 2 1 2 2 1 0 1 ; # SK
# 2 1 0 2 1 0 2 0 ; # MYM
# ]
function simulatesplit()
numteams = length(teams)
simwins = zeros(Uint8, numteams, numteams)
for team in 1:numteams
for vsteam in team+1:numteams
w = wins[team, vsteam]
l = wins[vsteam, team]
gamesleft = rounds - w - l
if gamesleft > 0
winrate = 0.5
if weighted
winrate = 0.25 + (w / (w + l)) * 0.5
end
for i in 1:gamesleft
if rand() < winrate
w += 1
end
end
l = rounds - w
end
simwins[team, vsteam] = w
simwins[vsteam, team] = l
end
end
simwins
end
function ranklt(s)
function lt(t1, t2)
if t1[2] == t2[2]
w = s[t1[1], t2[1]]
l = s[t2[1], t1[1]]
if w == l
return randbool()
else
return w > l
end
end
t1[2] > t2[2]
end
end
function simulatemulti(n)
numteams = length(teams)
rankcounts = zeros(typeof(n), numteams, numteams)
for i in 1:n
s = simulatesplit()
teamranks = sortperm([(j, sum(s[j, 1:end])) for j in 1:length(teams)], lt=ranklt(s))
for (rank, team) in enumerate(teamranks)
rankcounts[team, rank] = rankcounts[team, rank] + 1
end
end
percentages = map(x->x/n, rankcounts)
println(string("Team | ", join([i for i in 1:numteams], " | ")))
println(string(":---:|", join(["---" for i in 1:numteams], "|")))
for team in 1:numteams
println(string(teams[team], " | ", join([@sprintf("%.2f%%", n*100)
for n in percentages[team, 1:end]], " | ")))
end
end
simulatemulti(100000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment