{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Bias?\nI found it strange that Team A won so many more games than Team B.\nI investigate evidence that Teams A and B are not symmetrical.\n\n(We use the .parquet files generously provided @Reybahl in [this notebook](https://www.kaggle.com/code/reymaster/compress-files-parquet-7x-loading-speedup). Please up-vote that notebook)\n","metadata":{}},{"cell_type":"code","source":"library(tidyverse) # metapackage of all tidyverse packages\nlibrary(arrow)\nlibrary(ggplot2)\ndata_path <- \"../input/tps-oct-2022-compressed-parquet-files/\"","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","execution":{"iopub.status.busy":"2022-10-04T03:15:28.817868Z","iopub.execute_input":"2022-10-04T03:15:28.819846Z","iopub.status.idle":"2022-10-04T03:15:31.057420Z"},"jupyter":{"outputs_hidden":true,"source_hidden":true},"collapsed":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Calculate Scores\n\nFirst we get the scores (each goal is seperated by a change in `event_id`)\nThen we calculate the winners of each game","metadata":{}},{"cell_type":"code","source":"all_games <- data.frame()\n\n# iterate through the files\nfor(file_num in 0:9){\n    traindata <- read_parquet(paste0(data_path, \"train_\", file_num, \".parquet.gzip\"))\n    \n    # extract goals from the events\n    goals <- traindata %>% select(game_num, event_id, team_scoring_next) %>% distinct()\n    \n    # tally the score lines    \n    game_scores <- goals %>%\n        filter(!is.na(team_scoring_next)) %>%\n        pivot_wider(id_cols = \"game_num\", names_from = \"team_scoring_next\", values_from = \"event_id\", values_fn = length, values_fill = 0) %>%\n        mutate(result = sign(A-B) %>% recode(`1`=\"A\", `-1`=\"B\", `0`=\"tie\"))\n    \n    # add to the master dataframe\n    all_games <- all_games %>% bind_rows(game_scores)    \n}\n\nhead(all_games)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T04:02:59.153991Z","iopub.execute_input":"2022-10-04T04:02:59.155755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Investigate bias","metadata":{}},{"cell_type":"code","source":"evaluate_A_vs_B <- function(A_or_B){\n    table(A_or_B) %>% print()\n    table(A_or_B) %>% prop.table() %>% print()\n    print(paste0(\"Chances that A won this many games by chance: \",\n             pbinom(sum(A_or_B==\"A\"), size=sum(A_or_B %in% c(\"A\",\"B\")), prob=.5, lower.tail=FALSE)))\n}\n\nevaluate_A_vs_B(all_games$result)","metadata":{"execution":{"iopub.status.busy":"2022-10-04T03:40:31.833298Z","iopub.execute_input":"2022-10-04T03:40:31.835638Z","iopub.status.idle":"2022-10-04T03:40:31.860681Z"},"trusted":true},"execution_count":null,"outputs":[]}]}