{"cells":[{"metadata":{},"cell_type":"markdown","source":"A quick (and dirty) look at the relationsship between overal seed differences and differences in scores between winning and losing teams, and the probability of winning a game when your seeding is $N$ points higher (or, well, lower, since lower is better :)"},{"metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true},"cell_type":"code","source":"library(tidyverse) # metapackage with lots of helpful functions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"overall_seeds <- read_csv(\"../input/march-madness-overall-seeds-men/MNCAAOverallSeeds.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results <- read_csv(\"../input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneyCompactResults.csv\")  %>% \n    inner_join(overall_seeds %>% rename(WOverallSeed = OverallSeed), \n               by = c(\"Season\", \"WTeamID\" = \"TeamID\"))  %>% \n    inner_join(overall_seeds %>% rename(LOverallSeed = OverallSeed),\n               by = c(\"Season\", \"LTeamID\" = \"TeamID\")) %>% \n    mutate(DiffOverallSeed = -(WOverallSeed - LOverallSeed),\n           DiffScore = WScore - LScore)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Score differences and overall seed differences "},{"metadata":{},"cell_type":"markdown","source":"(Add a quadratic fit just to emphasize the variation of the data. Might not have any real significance, but it looks reasonable :)"},{"metadata":{"trusted":true},"cell_type":"code","source":"results %>% \n    ggplot(aes(x = DiffOverallSeed, y = DiffScore)) + \n    geom_point() +\n    geom_smooth(method = \"lm\", formula = y ~ poly(x, 2), se = FALSE) +\n    facet_wrap(~ Season) +\n    labs(title = \"Difference in overall seed vs. difference in score\",\n         x = \"Difference in overall seed\",\n         y = \"Score difference\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Winning probabilities and overall seed differences\n\nEven though the relative strengths of the teams might not be the same from season to season (as suggested by the above score plots), let's still try and predict winning probabilities from seed differences using data for all available seasons."},{"metadata":{"trusted":true},"cell_type":"code","source":"abs_winning_probabilities <- results %>% \n    mutate(AbsDiffOverallSeed = abs(DiffOverallSeed), \n           HigherSeedWon = WOverallSeed < LOverallSeed) %>% \n    group_by(AbsDiffOverallSeed) %>% \n    summarize(WinningProbability = mean(HigherSeedWon))\n\n# Make the data symmetric by \nwinning_probabilities <-\n    bind_rows(\n        abs_winning_probabilities %>% rename(DiffOverallSeed = AbsDiffOverallSeed),\n        abs_winning_probabilities %>% \n            transmute(DiffOverallSeed = -AbsDiffOverallSeed,\n                      WinningProbability = 1 - WinningProbability))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"winning_probabilities  %>% \n    ggplot(aes(x = DiffOverallSeed, y = WinningProbability)) +\n    geom_point() +\n    geom_smooth() +\n    scale_y_continuous(breaks = c(0, 0.5, 1), labels = scales::percent_format()) +\n    ggtitle(\"Winning probability vs. overall seed difference\") +\n    xlab(\"Overall seed difference\") +\n    ylab(\"Probability of winning\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"If we only look at the positive differences and use the default smoother, we get a different picture:"},{"metadata":{"trusted":true},"cell_type":"code","source":"abs_winning_probabilities  %>% \n    ggplot(aes(x = AbsDiffOverallSeed, y = WinningProbability)) +\n    geom_point() +\n    geom_smooth() +\n    scale_y_continuous(breaks = c(0, 0.5, 1), labels = scales::percent_format()) +\n    ggtitle(\"Winning probability vs. overall seed difference\") +\n    xlab(\"Overall seed difference\") +\n    ylab(\"Probability of winning\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"logistic <- function(d, k) {\n    1 / (1 + exp(-k * d))\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model <- nls(WinningProbability ~ logistic(DiffOverallSeed, k), \n             data = winning_probabilities,\n             start = list(k = 1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"summary(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"logistic_predictions <- winning_probabilities %>% \n    mutate(WinningProbability = predict(model, winning_probabilities))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"winning_probabilities  %>% \n    ggplot(aes(x = DiffOverallSeed, y = WinningProbability)) +\n    geom_point() +\n    geom_line(data = logistic_predictions, color = \"blue\") +\n    scale_y_continuous(breaks = c(0, 0.5, 1), labels = scales::percent_format()) +\n    labs(title = \"Winning probability vs. overall seed difference\",\n         subtitle = \"(with fitted logistic curve)\",\n         x =\"Overall seed difference\",\n         y =\"Probability of winning\")","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":1}