Last active
January 17, 2021 23:29
-
-
Save xiaodaigh/0326a03d97a0b7ded0aa88d4aeeef812 to your computer and use it in GitHub Desktop.
2021 Chunlan Cup simulation from quarters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Player | |
name::String | |
rating::Int | |
end | |
struct Match | |
best_of::Int | |
end | |
function player1_predicted_win_pct(player1, player2) | |
power = (player2.rating-player1.rating)/400 | |
denom = 1 + 10^power | |
1/denom | |
end | |
function simulate_match(player1, player2, match) | |
p1_win_pct = player1_predicted_win_pct(player1, player2) | |
sum(rand(match.best_of) .< p1_win_pct) > match.best_of/2 ? player1 : player2 | |
end | |
players = [ | |
Player("Ke Jie", 3725), | |
Player("Xu Haohong", 3490), | |
Player("Tang Weixing", 3479), | |
Player("Park Yeonghun", 3455), | |
Player("Lian Xiao", 3576), | |
Player("Byun Sangil", 3569), | |
Player("Fan Tingyu", 3591), | |
Player("Shin Jinseo", 3824) | |
] | |
function simulate_knockout_one_round(players, match) | |
winners = Vector{Player}(undef, length(players)÷2) | |
for i in 1:length(players)÷2 | |
player1_id = 2i-1 | |
player2_id = 2i | |
winners[i] = simulate_match(players[player1_id], players[player2_id], match) | |
end | |
winners | |
end | |
simulate_knockout_one_round(players, Match(1)) | |
function chun_lan_tournament(players) | |
# quarter | |
winners = simulate_knockout_one_round(players, Match(1)) | |
# semi | |
winners = simulate_knockout_one_round(winners, Match(1)) | |
# final | |
simulate_knockout_one_round(winners, Match(3)) | |
end | |
winner = [chun_lan_tournament(players) for i in 1:1_000_000] | |
using DataFrames | |
data = DataFrame( | |
winner = winner | |
) | |
data1=sort!(combine(groupby(data, :winner), nrow), :nrow, rev=true) | |
data1.winpct = data1.nrow ./ 1_000_000 | |
data1 | |
# 8×3 DataFrame | |
# Row │ winner nrow winpct | |
# │ Array… Int64 Float64 | |
# ─────┼───────────────────────────────────────────────────── | |
# 1 │ Player[Player("Shin Jinseo", 382… 509946 0.509946 | |
# 2 │ Player[Player("Ke Jie", 3725)] 304863 0.304863 | |
# 3 │ Player[Player("Lian Xiao", 3576)] 49945 0.049945 | |
# 4 │ Player[Player("Byun Sangil", 356… 45178 0.045178 | |
# 5 │ Player[Player("Fan Tingyu", 3591… 44869 0.044869 | |
# 6 │ Player[Player("Tang Weixing", 34… 17826 0.017826 | |
# 7 │ Player[Player("Xu Haohong", 3490… 15417 0.015417 | |
# 8 │ Player[Player("Park Yeonghun", 3… 11956 0.011956 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment