{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Load data files\n---","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom IPython.display import display_html, HTML\n\nlbs = []\nlbs.append(pd.read_csv(\"../input/gresearch-leaderboards/g-research-crypto-forecasting-publicleaderboard_update1.csv\"))\nlbs.append(pd.read_csv(\"../input/gresearch-leaderboards/g-research-crypto-forecasting-publicleaderboard_update2.csv\"))\nlbs.append(pd.read_csv(\"../input/gresearch-leaderboards/g-research-crypto-forecasting-publicleaderboard_update3.csv\"))\nlbs.append(pd.read_csv(\"../input/gresearch-leaderboards/g-research-crypto-forecasting-publicleaderboard_update4.csv\"))\nlbs.append(pd.read_csv(\"../input/gresearch-leaderboards/g-research-crypto-forecasting-publicleaderboard_update5.csv\"))\nlbs.append(pd.read_csv(\"../input/gresearch-leaderboards/g-research-crypto-forecasting-privateleaderboard.csv\"))\n\ndef print_header(text, size=1):\n    display_html(HTML(\"<h{}>{}</h{}>\".format(size, text, size)))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-04T16:42:12.107674Z","iopub.execute_input":"2022-05-04T16:42:12.108400Z","iopub.status.idle":"2022-05-04T16:42:12.225323Z","shell.execute_reply.started":"2022-05-04T16:42:12.108280Z","shell.execute_reply":"2022-05-04T16:42:12.224601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Build table where each row contains old and new positions and scores\n---","metadata":{}},{"cell_type":"code","source":"# Concatenate lbs and remove duplicates to get TeamIDs and TeamNames\nteams = pd.concat(lbs[::-1]).drop_duplicates(subset=\"TeamId\")[['TeamId', 'TeamName']] # Go through lbs in reverse order to obtain latest team names\n\nteamNames = []\nchangeTable = []\n\nfor index, row in teams.iterrows():\n    \n    teamId = row['TeamId']\n    teamName = row['TeamName']\n\n    pos = []\n    score = []\n    \n    for i in range(len(lbs)):\n        elem = lbs[i].loc[lbs[i]['TeamId'] == teamId]\n        \n        if len(elem) > 0:\n            pos.append(elem.index[0] + 1)\n            score.append(elem['Score'].iloc[0])\n        else:\n            pos.append(np.nan)\n            score.append(np.nan)\n\n    teamNames.append(teamName)\n    changeTable.append([teamId] + pos + score)\n    \nchangeTable = np.array(changeTable)\nteamNames = np.array(teamNames)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:42:12.226894Z","iopub.execute_input":"2022-05-04T16:42:12.228284Z","iopub.status.idle":"2022-05-04T16:42:18.206150Z","shell.execute_reply.started":"2022-05-04T16:42:12.228232Z","shell.execute_reply":"2022-05-04T16:42:18.205168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lb_lens = []\n\nfor i, lb in enumerate(lbs):\n    print(\"Entries in the LB after update {}:\".format(i + 1), len(lb))\n    lb_lens.append(len(lb))\n\n# Interestingly, a few submissions only failed in earlier updates but not in the latest ones\nplt.title(\"Failed submissions for each update\")\nplt.bar([\"Update {}\".format(i + 1) for i in range(len(lbs))], len(changeTable) - np.array(lb_lens)) # Plot successful submissions with each update\nplt.show()\nlbs[-1].head()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:42:18.207347Z","iopub.execute_input":"2022-05-04T16:42:18.207571Z","iopub.status.idle":"2022-05-04T16:42:18.450007Z","shell.execute_reply.started":"2022-05-04T16:42:18.207544Z","shell.execute_reply":"2022-05-04T16:42:18.449113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Total entries:\", len(changeTable))\nremove = np.isnan(np.sum(changeTable[:, 2:].astype(np.float32), axis=1))\nteamNames = teamNames[~remove]\nchangeTable = changeTable[~remove]\nprint(\"After removing failed submissions:\", len(changeTable))\nremove = np.min(changeTable[:, 1 + len(lbs):], axis = 1) == -1\nteamNames = teamNames[~remove]\nchangeTable = changeTable[~remove]\nprint(\"After removing -1 submission:\", len(changeTable))","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:42:18.451901Z","iopub.execute_input":"2022-05-04T16:42:18.452225Z","iopub.status.idle":"2022-05-04T16:42:18.462734Z","shell.execute_reply.started":"2022-05-04T16:42:18.452191Z","shell.execute_reply":"2022-05-04T16:42:18.461710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, k in [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]:\n\n    print_header(\"Change from update {} to update {}:\".format(i + 1, k + 1))\n    \n    order = np.argsort(changeTable[:, 1 + i])\n    \n    sortedTeamNames = teamNames[order]\n    sortedChangeTable = changeTable[order]\n\n    print_header(\"Median overall rank/score change:\", 4)\n    print(np.median(np.abs(changeTable[:, 1 + i] - changeTable[:, 1 + k])), \n          round(np.median(np.abs(changeTable[:, 1 + len(lbs) + k] - changeTable[:, 1 + len(lbs) + i])), 4))\n\n    for n in [500, 200, 100, 50, 20, 10]:\n        print_header(\"Median top {} rank/score change:\".format(n), 4)\n        print(np.median(np.abs(sortedChangeTable[:n, 1 + i] - sortedChangeTable[:n, 1 + k])), \n              round(np.median(np.abs(sortedChangeTable[:n, 1 + len(lbs) + k] - sortedChangeTable[:n, 1 + len(lbs) + i])), 4))\n    \n    print_header(\"Maximum overall rank improvement:\", 4)\n    argtop = np.argmax(sortedChangeTable[:, 1 + i] - sortedChangeTable[:, 1 + k])\n\n    print(\"Team:\", sortedTeamNames[argtop])\n    print(\"Rank:\", int(sortedChangeTable[argtop, 1 + i]), \"->\", int(sortedChangeTable[argtop, 1 + k]))\n    print(\"Score:\", sortedChangeTable[argtop, 1 + len(lbs) + i], \"->\", sortedChangeTable[argtop, 1 + len(lbs) + k])  \n    \n    for n in [500, 200, 100, 50, 20, 10]:\n        print_header(\"Maximum top {} rank improvement:\".format(n), 4)\n        argtop = np.argmax(sortedChangeTable[:n, 1 + i] - sortedChangeTable[:n, 1 + k])\n\n        print(\"Team:\", sortedTeamNames[argtop])\n        print(\"Rank:\", int(sortedChangeTable[argtop, 1 + i]), \"->\", int(sortedChangeTable[argtop, 1 + k]))\n        print(\"Score:\", sortedChangeTable[argtop, 1 + len(lbs) + i], \"->\", sortedChangeTable[argtop, 1 + len(lbs) + k])  \n    \n    print_header(\"Shakeup rank change histogram\", 4)\n    plt.hist(sortedChangeTable[:, 1 + i] - sortedChangeTable[:, 1 + k], 100)\n    plt.show()\n    \n    print_header(\"Shakeup score change histogram\", 4)\n    plt.hist(sortedChangeTable[:, 1 + len(lbs) + i] - sortedChangeTable[:, 1 + len(lbs) + k], 100)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:42:18.463978Z","iopub.execute_input":"2022-05-04T16:42:18.464212Z","iopub.status.idle":"2022-05-04T16:42:22.391533Z","shell.execute_reply.started":"2022-05-04T16:42:18.464184Z","shell.execute_reply":"2022-05-04T16:42:22.390702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot the shakeup\n---","metadata":{}},{"cell_type":"code","source":"from plotly.offline import init_notebook_mode, iplot, plot\nimport plotly.graph_objs as go","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:42:22.392606Z","iopub.execute_input":"2022-05-04T16:42:22.392993Z","iopub.status.idle":"2022-05-04T16:42:22.425786Z","shell.execute_reply.started":"2022-05-04T16:42:22.392963Z","shell.execute_reply":"2022-05-04T16:42:22.424749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, k in [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]:\n    \n    print_header(\"Change from update {} to update {}:\".format(i + 1, k + 1))\n    \n    # Scatter old vs new rank\n    trace = go.Scatter(x = changeTable[:, 1 + i],\n                       y = changeTable[:, 1 + k],\n                       mode = \"markers\",\n                       name = \"Rank\",\n                       marker = dict(color = 'rgba(128, 128, 255, 0.8)'),\n                       text = np.array(teamNames))\n\n    layout = dict(title = 'Rank Shakeup',\n                  xaxis= dict(title= 'Old Rank',ticklen= 5,zeroline= False),\n                  yaxis= dict(title= 'New Rank',ticklen= 5,zeroline= False))\n\n    fig = dict(data = [trace], layout = layout)\n    iplot(fig)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:42:22.427101Z","iopub.execute_input":"2022-05-04T16:42:22.428097Z","iopub.status.idle":"2022-05-04T16:42:23.709953Z","shell.execute_reply.started":"2022-05-04T16:42:22.428044Z","shell.execute_reply":"2022-05-04T16:42:23.708916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, k in [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]:\n    \n    print_header(\"Change from update {} to update {}:\".format(i + 1, k + 1))\n    \n    # Scatter old vs new rank\n    trace = go.Scatter(x = changeTable[:, 1 + len(lbs) + i],\n                       y = changeTable[:, 1 + len(lbs) + k],\n                       mode = \"markers\",\n                       name = \"Score\",\n                       marker = dict(color = 'rgba(128, 128, 255, 0.8)'),\n                       text= np.array(teamNames))\n\n    layout = dict(title = 'Score Shakeup',\n                  xaxis= dict(title= 'Old Score',ticklen= 5,zeroline= False),\n                  yaxis= dict(title= 'New Score',ticklen= 5,zeroline= False))\n\n    fig = dict(data = [trace], layout = layout)\n    iplot(fig)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T16:42:23.711175Z","iopub.execute_input":"2022-05-04T16:42:23.711478Z","iopub.status.idle":"2022-05-04T16:42:23.905061Z","shell.execute_reply.started":"2022-05-04T16:42:23.711440Z","shell.execute_reply":"2022-05-04T16:42:23.904026Z"},"trusted":true},"execution_count":null,"outputs":[]}]}