{"metadata":{"kaggle":{"accelerator":"none","dataSources":[{"sourceId":50160,"databundleVersionId":7602123,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"papermill":{"default_parameters":{},"duration":7.602135,"end_time":"2024-02-23T08:47:19.591432","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-02-23T08:47:11.989297","version":"2.5.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\n\n\ndef gini_at7459(gini_in_time, w_fallingrate=88.0, w_resstd=-0.5, f=8):\n    w_fallingrate /= f + 1\n\n    x = np.arange(len(gini_in_time))\n    y = gini_in_time\n    a, b = np.polyfit(x, y, 1)\n    y_hat = a*x + b\n    residuals = y - y_hat\n    res_std = np.std(residuals)\n    avg_gini = np.mean(gini_in_time)\n    far = a\n    for s in range(1,f):\n        start_index = len(x) // f * (s)\n        end_index = len(x) // f * (s+1)\n        x_second_fifth = x[start_index:end_index]\n        y_second_fifth = gini_in_time[start_index:end_index]\n        x1 = x[start_index:end_index]\n        y1 = gini_in_time[start_index:end_index]\n        a1, b1 = np.polyfit(x1, y1, 1)\n        far += min(0,a1)\n\n    return avg_gini + w_fallingrate * (far) + w_resstd * res_std \n\n\ndef gini_at7459_v2(gini_in_time, w_fallingrate=88.0, w_resstd=-0.5, win=6):\n    \n    x = np.arange(len(gini_in_time))\n    y = gini_in_time\n    a, b = np.polyfit(x, y, 1)\n    y_hat = a*x + b\n    residuals = y - y_hat\n    res_std = np.std(residuals)\n    avg_gini = np.mean(gini_in_time)\n    \n    a1_lst = []\n    for start_index in range(0, len(x)-win):\n        end_index = start_index + win\n        x1 = x[start_index:end_index]\n        y1 = gini_in_time[start_index:end_index]\n        a1, b1 = np.polyfit(x1, y1, 1)\n        a1_lst.append(min(0,a1))\n    a = np.mean(a1_lst)\n    \n    return avg_gini + w_fallingrate * a + w_resstd * res_std ","metadata":{"papermill":{"duration":1.672264,"end_time":"2024-02-23T08:47:16.880964","exception":false,"start_time":"2024-02-23T08:47:15.208700","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-23T13:35:52.237740Z","iopub.execute_input":"2024-02-23T13:35:52.238057Z","iopub.status.idle":"2024-02-23T13:35:52.278264Z","shell.execute_reply.started":"2024-02-23T13:35:52.238032Z","shell.execute_reply":"2024-02-23T13:35:52.277094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data for experiments\nn = 50\nweek_nums = np.arange(n)\n\nnp.random.seed(0)\nginis = (0.8 - 0.0005*np.arange(n)) + 0.01*np.random.randn(n) # original gini scores\n\nf = 8\nginis_hacked = ginis.copy()\nginis_hacked[np.arange(0, n, n//f)] -= 0.05 # hacked gini scores","metadata":{"papermill":{"duration":0.015718,"end_time":"2024-02-23T08:47:16.923082","exception":false,"start_time":"2024-02-23T08:47:16.907364","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-23T13:35:52.280373Z","iopub.execute_input":"2024-02-23T13:35:52.280766Z","iopub.status.idle":"2024-02-23T13:35:52.286235Z","shell.execute_reply.started":"2024-02-23T13:35:52.280734Z","shell.execute_reply":"2024-02-23T13:35:52.285159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualize original metric\nfig, ax = plt.subplots()\nax.plot(ginis, \n        label='ginis: metric={:.3f}'.format(gini_at7459(ginis)))\nax.plot(ginis_hacked, linestyle='--', \n        label='ginis_hacked: metric={:.3f}'.format(gini_at7459(ginis_hacked)))\n\nax.set_title('gini_at7459')\nax.legend()\nplt.show()","metadata":{"papermill":{"duration":1.770176,"end_time":"2024-02-23T08:47:19.038450","exception":false,"start_time":"2024-02-23T08:47:17.268274","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-23T13:35:52.288161Z","iopub.execute_input":"2024-02-23T13:35:52.288657Z","iopub.status.idle":"2024-02-23T13:35:52.557478Z","shell.execute_reply.started":"2024-02-23T13:35:52.288610Z","shell.execute_reply":"2024-02-23T13:35:52.556319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualize original metric\nfig, ax = plt.subplots()\nax.plot(ginis, \n        label='ginis: metric={:.3f}'.format(gini_at7459_v2(ginis)))\nax.plot(ginis_hacked, linestyle='--', \n        label='ginis_hacked: metric={:.3f}'.format(gini_at7459_v2(ginis_hacked)))\n\nax.set_title('gini_at7459_v2')\nax.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-23T13:35:52.559361Z","iopub.execute_input":"2024-02-23T13:35:52.559595Z","iopub.status.idle":"2024-02-23T13:35:52.736951Z","shell.execute_reply.started":"2024-02-23T13:35:52.559574Z","shell.execute_reply":"2024-02-23T13:35:52.736033Z"},"trusted":true},"execution_count":null,"outputs":[]}]}