<jupyter_start><jupyter_text>MCELOM ***Monte-Carlo Elo Madness*** Monte-Carlo Simulation - Repeatedly repeating a process with a bit of randomness*aka simulating a tournament* Elo - A simple rating system where win probability can be calculated from rankings*aka a way to predict the winner for each game* Madness - It's March*I'm not explaining this one further*  Load Libraries<jupyter_code>import pandas as pd
import openpyxl
from random import randint<jupyter_output><empty_output><jupyter_text>Load DataFrames<jupyter_code>df320 = pd.read_excel("data/538Data.xlsx", sheet_name="320")
df319 = pd.read_excel("data/538Data.xlsx", sheet_name="319")
df317 = pd.read_excel("data/538Data.xlsx", sheet_name="317")

teamgame_df = pd.read_excel("data/BracketStructure.xlsx", sheet_name="TeamGame")
game_df = pd.read_excel("data/BracketStructure.xlsx", sheet_name="Game")<jupyter_output><empty_output><jupyter_text>Define Functions<jupyter_code>def calc_win_chance(df, team1_id, team2_id):
    team1_rating = df[df.team_slot == team1_id].team_rating.values[0]
    team2_rating = df[df.team_slot == team2_id].team_rating.values[0]

    chance = 1.0/(1.0 + 10.0**(-(team1_rating - team2_rating) * 30.464 / 400.0))

    return chance

def pick_winner(teamdf, gamedf, gameid):
    team1 = gamedf[gamedf.game_id == gameid].team1.values[0]
    team2 = gamedf[gamedf.game_id == gameid].team2.values[0]

    threshold = calc_win_chance(teamdf, team1, team2)

    rand_outcome = randint(0,1001)/1000.0

    return team1 if rand_outcome <= threshold else team2

def advance_winner(gamedf, gameid, winner):
    if gamedf[gamedf.game_id == gameid].team1.values[0] != winner and gamedf[gamedf.game_id == gameid].team2.values[0] != winner:
        raise

    i = game_df.index[game_df['game_id'] == gameid].tolist()[0]
    nextgame = game_df['goto_id'][i]
    game_df['winner'][i] = winner

    ni = game_df.index[game_df['game_id'] == nextgame].tolist()[0]

    if gameid == game_df['team1_fromgame'][ni]:
        game_df['team1'][ni] = winner
    elif gameid == game_df['team2_fromgame'][ni]:
        game_df['team2'][ni] = winner
    else:
        raise
    print(gamedf[gamedf.game_id == gameid])

def simulate_games(gamesdf, teamsdf):
    rounds = gamesdf.round.unique()

#calc_win_chance(df317, 120, 122)
#for i in range(10):
#    print(pick_winner(df317, game_df, "b31"))

game = "b31"
winning_team = pick_winner(df317, game_df, game)
advance_winner(game_df, game, winning_team)
game_df.loc[32]
game_df.loc[15]
x = game_df.loc[game_df.game_id == "b31"].index
roundsnum = game_df["round"].unique()

roundsnum = roundsnum

roundsnum
game_df.round<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1371664787673807, 'fraction_numerical': 0.04133784291619692, 'mean_word_length': 5.263529411764706, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '279581', 'license': 'Unlicense', 'n_tokens_mistral': 1075, 'n_tokens_neox': 1005, 'n_words': 187, 'path': 'Test.ipynb', 'repo_name': 'bojanderson/MCELOM', 'size': '2661'}